blob: bac6207191d513d710db7a34681038fbe01d1c37 [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 *
J. Bruce Fields2b634822015-10-15 15:33:23 -04002265 * Also note in 4.0 case should also be checking for openowners
2266 * kept around just for close handling.
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002267 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002268 return !list_empty(&clp->cl_openowners)
Kinglong Mee47e970b2015-07-21 13:09:17 +08002269#ifdef CONFIG_NFSD_PNFS
2270 || !list_empty(&clp->cl_lo_states)
2271#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002272 || !list_empty(&clp->cl_delegations)
2273 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002274}
2275
Al Virob37ad282006-10-19 23:28:59 -07002276__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002277nfsd4_exchange_id(struct svc_rqst *rqstp,
2278 struct nfsd4_compound_state *cstate,
2279 struct nfsd4_exchange_id *exid)
2280{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002281 struct nfs4_client *conf, *new;
2282 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002283 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002284 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002285 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002286 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002287 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002288 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002289
Jeff Layton363168b2009-08-14 12:57:56 -04002290 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002291 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002292 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002293 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002294 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002295
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002296 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002297 return nfserr_inval;
2298
Andy Adamson0733d212009-04-03 08:28:01 +03002299 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002300 case SP4_MACH_CRED:
2301 if (!svc_rqst_integrity_protected(rqstp))
2302 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002303 case SP4_NONE:
2304 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002305 default: /* checked by xdr code */
2306 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002307 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002308 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002309 }
2310
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002311 new = create_client(exid->clname, rqstp, &verf);
2312 if (new == NULL)
2313 return nfserr_jukebox;
2314
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002315 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002316 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002317 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002318 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002319 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2320 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2321
J. Bruce Fields136e6582012-05-12 20:37:23 -04002322 if (update) {
2323 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002324 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002325 goto out;
2326 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002327 if (!mach_creds_match(conf, rqstp)) {
2328 status = nfserr_wrong_cred;
2329 goto out;
2330 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002331 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002332 status = nfserr_perm;
2333 goto out;
2334 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002335 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002336 status = nfserr_not_same;
2337 goto out;
2338 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002339 /* case 6 */
2340 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002341 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002342 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002343 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002344 if (client_has_state(conf)) {
2345 status = nfserr_clid_inuse;
2346 goto out;
2347 }
Andy Adamson0733d212009-04-03 08:28:01 +03002348 goto out_new;
2349 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002350 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002351 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002352 goto out_copy;
2353 }
2354 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002355 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002356 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002357 }
2358
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002359 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002360 status = nfserr_noent;
2361 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002362 }
2363
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002364 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002365 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002366 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002367
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002368 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002369out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002370 if (conf) {
2371 status = mark_client_expired_locked(conf);
2372 if (status)
2373 goto out;
2374 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002375 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002376 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002377
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002378 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002379 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002380 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002381out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002382 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2383 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002384
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002385 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2386 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002387
2388 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002389 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002390 status = nfs_ok;
2391
2392out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002393 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002394 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002395 expire_client(new);
2396 if (unconf)
2397 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002398 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002399}
2400
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002401static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002402check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002403{
Andy Adamson88e588d2009-07-23 19:02:15 -04002404 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2405 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002406
2407 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002408 if (slot_inuse) {
2409 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002410 return nfserr_jukebox;
2411 else
2412 return nfserr_seq_misordered;
2413 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002414 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002415 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002416 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002417 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002418 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002419 return nfserr_seq_misordered;
2420}
2421
Andy Adamson49557cc2009-07-23 19:02:16 -04002422/*
2423 * Cache the create session result into the create session single DRC
2424 * slot cache by saving the xdr structure. sl_seqid has been set.
2425 * Do this for solo or embedded create session operations.
2426 */
2427static void
2428nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002429 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002430{
2431 slot->sl_status = nfserr;
2432 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2433}
2434
2435static __be32
2436nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2437 struct nfsd4_clid_slot *slot)
2438{
2439 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2440 return slot->sl_status;
2441}
2442
Mi Jinlong1b74c252011-07-14 14:50:17 +08002443#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2444 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2445 1 + /* MIN tag is length with zero, only length */ \
2446 3 + /* version, opcount, opcode */ \
2447 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2448 /* seqid, slotID, slotID, cache */ \
2449 4 ) * sizeof(__be32))
2450
2451#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2452 2 + /* verifier: AUTH_NULL, length 0 */\
2453 1 + /* status */ \
2454 1 + /* MIN tag is length with zero, only length */ \
2455 3 + /* opcount, opcode, opstatus*/ \
2456 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2457 /* seqid, slotID, slotID, slotID, status */ \
2458 5 ) * sizeof(__be32))
2459
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002460static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002461{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002462 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2463
J. Bruce Fields373cd402013-04-08 15:42:12 -04002464 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2465 return nfserr_toosmall;
2466 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2467 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002468 ca->headerpadsz = 0;
2469 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2470 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2471 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2472 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2473 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2474 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2475 /*
2476 * Note decreasing slot size below client's request may make it
2477 * difficult for client to function correctly, whereas
2478 * decreasing the number of slots will (just?) affect
2479 * performance. When short on memory we therefore prefer to
2480 * decrease number of slots instead of their size. Clients that
2481 * request larger slots than they need will get poor results:
2482 */
2483 ca->maxreqs = nfsd4_get_drc_mem(ca);
2484 if (!ca->maxreqs)
2485 return nfserr_jukebox;
2486
J. Bruce Fields373cd402013-04-08 15:42:12 -04002487 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002488}
2489
Kinglong Mee8a891632013-12-23 18:11:02 +08002490#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2491 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2492#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2493 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2494
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002495static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2496{
2497 ca->headerpadsz = 0;
2498
2499 /*
2500 * These RPC_MAX_HEADER macros are overkill, especially since we
2501 * don't even do gss on the backchannel yet. But this is still
2502 * less than 1k. Tighten up this estimate in the unlikely event
2503 * it turns out to be a problem for some client:
2504 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002505 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002506 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002507 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002508 return nfserr_toosmall;
2509 ca->maxresp_cached = 0;
2510 if (ca->maxops < 2)
2511 return nfserr_toosmall;
2512
2513 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002514}
2515
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002516static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2517{
2518 switch (cbs->flavor) {
2519 case RPC_AUTH_NULL:
2520 case RPC_AUTH_UNIX:
2521 return nfs_ok;
2522 default:
2523 /*
2524 * GSS case: the spec doesn't allow us to return this
2525 * error. But it also doesn't allow us not to support
2526 * GSS.
2527 * I'd rather this fail hard than return some error the
2528 * client might think it can already handle:
2529 */
2530 return nfserr_encr_alg_unsupp;
2531 }
2532}
2533
Andy Adamson069b6ad2009-04-03 08:27:58 +03002534__be32
2535nfsd4_create_session(struct svc_rqst *rqstp,
2536 struct nfsd4_compound_state *cstate,
2537 struct nfsd4_create_session *cr_ses)
2538{
Jeff Layton363168b2009-08-14 12:57:56 -04002539 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002540 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002541 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002542 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002543 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002544 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002545 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002546 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002547
Mi Jinlonga62573d2011-03-23 17:57:07 +08002548 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2549 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002550 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2551 if (status)
2552 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002553 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002554 if (status)
2555 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002556 status = check_backchannel_attrs(&cr_ses->back_channel);
2557 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002558 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002559 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002560 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002561 if (!new)
2562 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002563 conn = alloc_conn_from_crses(rqstp, cr_ses);
2564 if (!conn)
2565 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002566
Jeff Laytond20c11d2014-07-30 08:27:07 -04002567 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002568 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002569 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002570 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002571
2572 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002573 status = nfserr_wrong_cred;
2574 if (!mach_creds_match(conf, rqstp))
2575 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002576 cs_slot = &conf->cl_cs_slot;
2577 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08002578 if (status) {
2579 if (status == nfserr_replay_cache)
2580 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002581 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002582 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002583 } else if (unconf) {
2584 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002585 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002586 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002587 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002588 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002589 status = nfserr_wrong_cred;
2590 if (!mach_creds_match(unconf, rqstp))
2591 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002592 cs_slot = &unconf->cl_cs_slot;
2593 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002594 if (status) {
2595 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002596 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002597 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002598 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002599 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002600 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002601 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002602 if (status) {
2603 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002604 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002605 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002606 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002607 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002608 conf = unconf;
2609 } else {
2610 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002611 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002612 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002613 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002614 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002615 * We do not support RDMA or persistent sessions
2616 */
2617 cr_ses->flags &= ~SESSION4_PERSIST;
2618 cr_ses->flags &= ~SESSION4_RDMA;
2619
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002620 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002621 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002622
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002623 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002624 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002625 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002626 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002627
Jeff Laytond20c11d2014-07-30 08:27:07 -04002628 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002629 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002630 spin_unlock(&nn->client_lock);
2631 /* init connection and backchannel */
2632 nfsd4_init_conn(rqstp, conn, new);
2633 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002634 if (old)
2635 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002636 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002637out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002638 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002639 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002640 if (old)
2641 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002642out_free_session:
2643 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002644out_release_drc_mem:
2645 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002646 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002647}
2648
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002649static __be32 nfsd4_map_bcts_dir(u32 *dir)
2650{
2651 switch (*dir) {
2652 case NFS4_CDFC4_FORE:
2653 case NFS4_CDFC4_BACK:
2654 return nfs_ok;
2655 case NFS4_CDFC4_FORE_OR_BOTH:
2656 case NFS4_CDFC4_BACK_OR_BOTH:
2657 *dir = NFS4_CDFC4_BOTH;
2658 return nfs_ok;
2659 };
2660 return nfserr_inval;
2661}
2662
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002663__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2664{
2665 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002666 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002667 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002668
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002669 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2670 if (status)
2671 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002672 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002673 session->se_cb_prog = bc->bc_cb_program;
2674 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002675 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002676
2677 nfsd4_probe_callback(session->se_client);
2678
2679 return nfs_ok;
2680}
2681
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002682__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2683 struct nfsd4_compound_state *cstate,
2684 struct nfsd4_bind_conn_to_session *bcts)
2685{
2686 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002687 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002688 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002689 struct net *net = SVC_NET(rqstp);
2690 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002691
2692 if (!nfsd4_last_compound_op(rqstp))
2693 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002694 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002695 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002696 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002697 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002698 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002699 status = nfserr_wrong_cred;
2700 if (!mach_creds_match(session->se_client, rqstp))
2701 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002702 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002703 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002704 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002705 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002706 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002707 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002708 goto out;
2709 nfsd4_init_conn(rqstp, conn, session);
2710 status = nfs_ok;
2711out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002712 nfsd4_put_session(session);
2713out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002714 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002715}
2716
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002717static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2718{
2719 if (!session)
2720 return 0;
2721 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2722}
2723
Andy Adamson069b6ad2009-04-03 08:27:58 +03002724__be32
2725nfsd4_destroy_session(struct svc_rqst *r,
2726 struct nfsd4_compound_state *cstate,
2727 struct nfsd4_destroy_session *sessionid)
2728{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002729 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002730 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002731 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002732 struct net *net = SVC_NET(r);
2733 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002734
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002735 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002736 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002737 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002738 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002739 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002740 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002741 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002742 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002743 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002744 if (!ses)
2745 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002746 status = nfserr_wrong_cred;
2747 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002748 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002749 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002750 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002751 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002752 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002753 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002754
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002755 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002756
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002757 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002758 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002759out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002760 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002761out_client_lock:
2762 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002763out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03002764 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002765}
2766
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002767static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002768{
2769 struct nfsd4_conn *c;
2770
2771 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002772 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002773 return c;
2774 }
2775 }
2776 return NULL;
2777}
2778
J. Bruce Fields57266a62013-04-13 14:27:29 -04002779static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002780{
2781 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002782 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002783 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002784 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002785
2786 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002787 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002788 if (c)
2789 goto out_free;
2790 status = nfserr_conn_not_bound_to_session;
2791 if (clp->cl_mach_cred)
2792 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002793 __nfsd4_hash_conn(new, ses);
2794 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002795 ret = nfsd4_register_conn(new);
2796 if (ret)
2797 /* oops; xprt is already down: */
2798 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002799 return nfs_ok;
2800out_free:
2801 spin_unlock(&clp->cl_lock);
2802 free_conn(new);
2803 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002804}
2805
Mi Jinlong868b89c2011-04-27 09:09:58 +08002806static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2807{
2808 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2809
2810 return args->opcnt > session->se_fchannel.maxops;
2811}
2812
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002813static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2814 struct nfsd4_session *session)
2815{
2816 struct xdr_buf *xb = &rqstp->rq_arg;
2817
2818 return xb->len > session->se_fchannel.maxreq_sz;
2819}
2820
Andy Adamson069b6ad2009-04-03 08:27:58 +03002821__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002822nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002823 struct nfsd4_compound_state *cstate,
2824 struct nfsd4_sequence *seq)
2825{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002826 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002827 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002828 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002829 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002830 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002831 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002832 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002833 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002834 struct net *net = SVC_NET(rqstp);
2835 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002836
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002837 if (resp->opcnt != 1)
2838 return nfserr_sequence_pos;
2839
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002840 /*
2841 * Will be either used or freed by nfsd4_sequence_check_conn
2842 * below.
2843 */
2844 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2845 if (!conn)
2846 return nfserr_jukebox;
2847
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002848 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002849 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002850 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002851 goto out_no_session;
2852 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002853
Mi Jinlong868b89c2011-04-27 09:09:58 +08002854 status = nfserr_too_many_ops;
2855 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002856 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002857
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002858 status = nfserr_req_too_big;
2859 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002860 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002861
Benny Halevyb85d4c02009-04-03 08:28:08 +03002862 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002863 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002864 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002865
Andy Adamson557ce262009-08-28 08:45:04 -04002866 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002867 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2868
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002869 /* We do not negotiate the number of slots yet, so set the
2870 * maxslots to the session maxreqs which is used to encode
2871 * sr_highest_slotid and the sr_target_slot id to maxslots */
2872 seq->maxslots = session->se_fchannel.maxreqs;
2873
J. Bruce Fields73e79482012-02-13 16:39:00 -05002874 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2875 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002876 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002877 status = nfserr_seq_misordered;
2878 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002879 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002880 cstate->slot = slot;
2881 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002882 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002883 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002884 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002885 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002886 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002887 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002888 }
2889 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002890 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002891
J. Bruce Fields57266a62013-04-13 14:27:29 -04002892 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002893 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002894 if (status)
2895 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002896
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002897 buflen = (seq->cachethis) ?
2898 session->se_fchannel.maxresp_cached :
2899 session->se_fchannel.maxresp_sz;
2900 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2901 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002902 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002903 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002904 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002905
2906 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002907 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002908 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002909 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002910 if (seq->cachethis)
2911 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002912 else
2913 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002914
2915 cstate->slot = slot;
2916 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002917 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002918
Benny Halevyb85d4c02009-04-03 08:28:08 +03002919out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002920 switch (clp->cl_cb_state) {
2921 case NFSD4_CB_DOWN:
2922 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2923 break;
2924 case NFSD4_CB_FAULT:
2925 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2926 break;
2927 default:
2928 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002929 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002930 if (!list_empty(&clp->cl_revoked))
2931 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002932out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002933 if (conn)
2934 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002935 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002936 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002937out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002938 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002939 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002940}
2941
Trond Myklebustb6076642014-06-30 11:48:35 -04002942void
2943nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2944{
2945 struct nfsd4_compound_state *cs = &resp->cstate;
2946
2947 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002948 if (cs->status != nfserr_replay_cache) {
2949 nfsd4_store_cache_entry(resp);
2950 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2951 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002952 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002953 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002954 } else if (cs->clp)
2955 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002956}
2957
Mi Jinlong345c2842011-10-20 17:51:39 +08002958__be32
2959nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2960{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002961 struct nfs4_client *conf, *unconf;
2962 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002963 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002964 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002965
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002966 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002967 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002968 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002969 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002970
2971 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002972 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002973 status = nfserr_clientid_busy;
2974 goto out;
2975 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04002976 status = mark_client_expired_locked(conf);
2977 if (status)
2978 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002979 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08002980 } else if (unconf)
2981 clp = unconf;
2982 else {
2983 status = nfserr_stale_clientid;
2984 goto out;
2985 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002986 if (!mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002987 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002988 status = nfserr_wrong_cred;
2989 goto out;
2990 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002991 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002992out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002993 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002994 if (clp)
2995 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002996 return status;
2997}
2998
Andy Adamson069b6ad2009-04-03 08:27:58 +03002999__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003000nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
3001{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003002 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003003
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003004 if (rc->rca_one_fs) {
3005 if (!cstate->current_fh.fh_dentry)
3006 return nfserr_nofilehandle;
3007 /*
3008 * We don't take advantage of the rca_one_fs case.
3009 * That's OK, it's optional, we can safely ignore it.
3010 */
3011 return nfs_ok;
3012 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003013
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003014 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04003015 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3016 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003017 goto out;
3018
3019 status = nfserr_stale_clientid;
3020 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003021 /*
3022 * The following error isn't really legal.
3023 * But we only get here if the client just explicitly
3024 * destroyed the client. Surely it no longer cares what
3025 * error it gets back on an operation for the dead
3026 * client.
3027 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003028 goto out;
3029
3030 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04003031 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003032out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003033 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003034}
3035
3036__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003037nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3038 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003040 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003042 struct nfs4_client *conf, *new;
3043 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003044 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003045 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3046
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003047 new = create_client(clname, rqstp, &clverifier);
3048 if (new == NULL)
3049 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003050 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003051 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003052 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04003053 if (conf && client_has_state(conf)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003054 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003056 if (clp_used_exchangeid(conf))
3057 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003058 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04003059 char addr_str[INET6_ADDRSTRLEN];
3060 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3061 sizeof(addr_str));
3062 dprintk("NFSD: setclientid: string in use by client "
3063 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 goto out;
3065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003067 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003068 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003069 unhash_client_locked(unconf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003070 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003071 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003073 gen_confirm(new, nn);
3074 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003075 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003076 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003077 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003078 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3080 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3081 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003082 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 status = nfs_ok;
3084out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003085 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003086 if (new)
3087 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003088 if (unconf)
3089 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 return status;
3091}
3092
3093
Al Virob37ad282006-10-19 23:28:59 -07003094__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003095nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3096 struct nfsd4_compound_state *cstate,
3097 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098{
NeilBrown21ab45a2005-06-23 22:04:14 -07003099 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003100 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3102 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003103 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003104 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003106 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003108
Jeff Laytond20c11d2014-07-30 08:27:07 -04003109 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003110 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003111 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003112 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003113 * We try hard to give out unique clientid's, so if we get an
3114 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003115 * the client may be buggy; this should never happen.
3116 *
3117 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003118 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003119 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04003120 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3121 goto out;
3122 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3123 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003124 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003125 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3126 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003128 else /* case 4: client hasn't noticed we rebooted yet? */
3129 status = nfserr_stale_clientid;
3130 goto out;
3131 }
3132 status = nfs_ok;
3133 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003134 old = unconf;
3135 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003136 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003137 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003138 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3139 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04003140 status = nfserr_clid_inuse;
3141 if (client_has_state(old)
3142 && !same_creds(&unconf->cl_cred,
3143 &old->cl_cred))
3144 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003145 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003146 if (status) {
3147 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003148 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003149 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003150 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003151 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003152 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003153 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003154 get_client_locked(conf);
3155 spin_unlock(&nn->client_lock);
3156 nfsd4_probe_callback(conf);
3157 spin_lock(&nn->client_lock);
3158 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003160 spin_unlock(&nn->client_lock);
3161 if (old)
3162 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 return status;
3164}
3165
J. Bruce Fields32513b42011-10-13 16:00:16 -04003166static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003168 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3169}
3170
3171/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003172static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3173 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003174{
Trond Myklebust950e0112014-06-30 11:48:31 -04003175 lockdep_assert_held(&state_lock);
3176
J. Bruce Fields32513b42011-10-13 16:00:16 -04003177 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003178 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003179 INIT_LIST_HEAD(&fp->fi_stateids);
3180 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02003181 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003182 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003183 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003184 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003185 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003186 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3187 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003188#ifdef CONFIG_NFSD_PNFS
3189 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02003190 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003191#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04003192 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193}
3194
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003195void
NeilBrowne60d4392005-06-23 22:03:01 -07003196nfsd4_free_slabs(void)
3197{
Sachin Bhamare8287f002015-04-27 14:50:14 +02003198 kmem_cache_destroy(odstate_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003199 kmem_cache_destroy(openowner_slab);
3200 kmem_cache_destroy(lockowner_slab);
3201 kmem_cache_destroy(file_slab);
3202 kmem_cache_destroy(stateid_slab);
3203 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003204}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Bryan Schumaker72083392011-11-01 15:24:59 -04003206int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207nfsd4_init_slabs(void)
3208{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003209 openowner_slab = kmem_cache_create("nfsd4_openowners",
3210 sizeof(struct nfs4_openowner), 0, 0, NULL);
3211 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003212 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003213 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003214 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003215 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003216 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003217 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003218 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003219 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003220 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003221 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003222 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003223 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003224 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003225 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003226 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003227 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003228 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003229 odstate_slab = kmem_cache_create("nfsd4_odstate",
3230 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3231 if (odstate_slab == NULL)
3232 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003234
Sachin Bhamare8287f002015-04-27 14:50:14 +02003235out_free_deleg_slab:
3236 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003237out_free_stateid_slab:
3238 kmem_cache_destroy(stateid_slab);
3239out_free_file_slab:
3240 kmem_cache_destroy(file_slab);
3241out_free_lockowner_slab:
3242 kmem_cache_destroy(lockowner_slab);
3243out_free_openowner_slab:
3244 kmem_cache_destroy(openowner_slab);
3245out:
NeilBrowne60d4392005-06-23 22:03:01 -07003246 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3247 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248}
3249
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003250static void init_nfs4_replay(struct nfs4_replay *rp)
3251{
3252 rp->rp_status = nfserr_serverfault;
3253 rp->rp_buflen = 0;
3254 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003255 mutex_init(&rp->rp_mutex);
3256}
3257
3258static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3259 struct nfs4_stateowner *so)
3260{
3261 if (!nfsd4_has_session(cstate)) {
3262 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003263 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04003264 }
3265}
3266
3267void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3268{
3269 struct nfs4_stateowner *so = cstate->replay_owner;
3270
3271 if (so != NULL) {
3272 cstate->replay_owner = NULL;
3273 mutex_unlock(&so->so_replay.rp_mutex);
3274 nfs4_put_stateowner(so);
3275 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003276}
3277
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003278static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279{
3280 struct nfs4_stateowner *sop;
3281
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003282 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003283 if (!sop)
3284 return NULL;
3285
3286 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3287 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003288 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003289 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003291 sop->so_owner.len = owner->len;
3292
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003293 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003294 sop->so_client = clp;
3295 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003296 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003297 return sop;
3298}
3299
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003300static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003301{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003302 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003303
Trond Myklebustd4f04892014-07-29 21:34:36 -04003304 list_add(&oo->oo_owner.so_strhash,
3305 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003306 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307}
3308
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003309static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3310{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003311 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003312}
3313
Jeff Layton6b180f02014-07-29 21:34:26 -04003314static void nfs4_free_openowner(struct nfs4_stateowner *so)
3315{
3316 struct nfs4_openowner *oo = openowner(so);
3317
3318 kmem_cache_free(openowner_slab, oo);
3319}
3320
3321static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003322 .so_unhash = nfs4_unhash_openowner,
3323 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003324};
3325
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003326static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003327alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003328 struct nfsd4_compound_state *cstate)
3329{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003330 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003331 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003333 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3334 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003336 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003337 oo->oo_owner.so_is_open_owner = 1;
3338 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003339 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003340 if (nfsd4_has_session(cstate))
3341 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003342 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003343 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003344 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003345 spin_lock(&clp->cl_lock);
3346 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003347 if (ret == NULL) {
3348 hash_openowner(oo, clp, strhashval);
3349 ret = oo;
3350 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08003351 nfs4_free_stateowner(&oo->oo_owner);
3352
Trond Myklebustd4f04892014-07-29 21:34:36 -04003353 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04003354 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355}
3356
J. Bruce Fields996e0932011-10-17 11:14:48 -04003357static 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 -04003358 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003360 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003361 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003362 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003363 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07003364 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003365 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 stp->st_access_bmap = 0;
3367 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003368 stp->st_openstp = NULL;
Jeff Layton35a92fe2015-09-17 07:47:08 -04003369 init_rwsem(&stp->st_rwsem);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003370 spin_lock(&oo->oo_owner.so_client->cl_lock);
3371 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003372 spin_lock(&fp->fi_lock);
3373 list_add(&stp->st_perfile, &fp->fi_stateids);
3374 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003375 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376}
3377
Jeff Laytond3134b12014-07-29 21:34:32 -04003378/*
3379 * In the 4.0 case we need to keep the owners around a little while to handle
3380 * CLOSE replay. We still do need to release any file access that is held by
3381 * them before returning however.
3382 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003384move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385{
Jeff Layton217526e2014-07-30 08:27:11 -04003386 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003387 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3388 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3389 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003390
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003391 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
Jeff Laytonb401be222014-07-29 21:34:33 -04003393 /*
3394 * We know that we hold one reference via nfsd4_close, and another
3395 * "persistent" reference for the client. If the refcount is higher
3396 * than 2, then there are still calls in progress that are using this
3397 * stateid. We can't put the sc_file reference until they are finished.
3398 * Wait for the refcount to drop to 2. Since it has been unhashed,
3399 * there should be no danger of the refcount going back up again at
3400 * this point.
3401 */
3402 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3403
Jeff Laytond3134b12014-07-29 21:34:32 -04003404 release_all_access(s);
3405 if (s->st_stid.sc_file) {
3406 put_nfs4_file(s->st_stid.sc_file);
3407 s->st_stid.sc_file = NULL;
3408 }
Jeff Layton217526e2014-07-30 08:27:11 -04003409
3410 spin_lock(&nn->client_lock);
3411 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003412 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003413 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003414 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003415 spin_unlock(&nn->client_lock);
3416 if (last)
3417 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418}
3419
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420/* search file_hashtbl[] for file */
3421static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04003422find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 struct nfs4_file *fp;
3425
Jeff Layton5b095e92014-10-23 08:01:02 -04003426 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02003427 if (fh_match(&fp->fi_fhandle, fh)) {
Jeff Layton5b095e92014-10-23 08:01:02 -04003428 if (atomic_inc_not_zero(&fp->fi_ref))
3429 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 }
3432 return NULL;
3433}
3434
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02003435struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003436find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003437{
3438 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003439 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003440
Jeff Layton5b095e92014-10-23 08:01:02 -04003441 rcu_read_lock();
3442 fp = find_file_locked(fh, hashval);
3443 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04003444 return fp;
3445}
3446
3447static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003448find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003449{
3450 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003451 unsigned int hashval = file_hashval(fh);
3452
3453 rcu_read_lock();
3454 fp = find_file_locked(fh, hashval);
3455 rcu_read_unlock();
3456 if (fp)
3457 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04003458
3459 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04003460 fp = find_file_locked(fh, hashval);
3461 if (likely(fp == NULL)) {
3462 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04003463 fp = new;
3464 }
3465 spin_unlock(&state_lock);
3466
3467 return fp;
3468}
3469
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003470/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 * Called to check deny when READ with all zero stateid or
3472 * WRITE with all zero or all one stateid
3473 */
Al Virob37ad282006-10-19 23:28:59 -07003474static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3476{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003478 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479
Trond Myklebustca943212014-07-23 16:17:39 -04003480 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003481 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003482 return ret;
3483 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003484 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003485 if (fp->fi_share_deny & deny_type)
3486 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003487 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003488 put_nfs4_file(fp);
3489 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490}
3491
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003492static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003494 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04003495 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3496 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003497
Trond Myklebust11b91642014-07-29 21:34:08 -04003498 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003499
Jeff Layton02e12152014-07-16 10:31:57 -04003500 /*
3501 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003502 * already holding inode->i_lock.
3503 *
Jeff Laytondff13992014-07-08 14:02:49 -04003504 * If the dl_time != 0, then we know that it has already been
3505 * queued for a lease break. Don't queue it again.
3506 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003507 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003508 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003509 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003510 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003511 }
Jeff Layton02e12152014-07-16 10:31:57 -04003512 spin_unlock(&state_lock);
3513}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003515static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3516 struct rpc_task *task)
3517{
3518 struct nfs4_delegation *dp = cb_to_delegation(cb);
3519
Andrew Elblea4579742015-08-31 12:06:41 -04003520 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
3521 return 1;
3522
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003523 switch (task->tk_status) {
3524 case 0:
3525 return 1;
3526 case -EBADHANDLE:
3527 case -NFS4ERR_BAD_STATEID:
3528 /*
3529 * Race: client probably got cb_recall before open reply
3530 * granting delegation.
3531 */
3532 if (dp->dl_retries--) {
3533 rpc_delay(task, 2 * HZ);
3534 return 0;
3535 }
3536 /*FALLTHRU*/
3537 default:
3538 return -1;
3539 }
3540}
3541
3542static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3543{
3544 struct nfs4_delegation *dp = cb_to_delegation(cb);
3545
3546 nfs4_put_stid(&dp->dl_stid);
3547}
3548
3549static struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3550 .prepare = nfsd4_cb_recall_prepare,
3551 .done = nfsd4_cb_recall_done,
3552 .release = nfsd4_cb_recall_release,
3553};
3554
Jeff Layton02e12152014-07-16 10:31:57 -04003555static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3556{
3557 /*
3558 * We're assuming the state code never drops its reference
3559 * without first removing the lease. Since we're in this lease
3560 * callback (and since the lease code is serialized by the kernel
3561 * lock) we know the server hasn't removed the lease yet, we know
3562 * it's safe to take a reference.
3563 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003564 atomic_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02003565 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003566}
3567
Jeff Layton1c8c6012013-06-21 08:58:15 -04003568/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003569static bool
3570nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003571{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003572 bool ret = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003573 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3574 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003575
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003576 if (!fp) {
3577 WARN(1, "(%p)->fl_owner NULL\n", fl);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003578 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003579 }
3580 if (fp->fi_had_conflict) {
3581 WARN(1, "duplicate break on %p\n", fp);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003582 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003583 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003584 /*
3585 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003586 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003587 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003588 */
3589 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
Jeff Layton02e12152014-07-16 10:31:57 -04003591 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003592 fp->fi_had_conflict = true;
3593 /*
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003594 * If there are no delegations on the list, then return true
3595 * so that the lease code will go ahead and delete it.
Jeff Layton417c6622014-07-21 09:34:57 -04003596 */
3597 if (list_empty(&fp->fi_delegations))
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003598 ret = true;
Jeff Layton417c6622014-07-21 09:34:57 -04003599 else
3600 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3601 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003602 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003603 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604}
3605
Jeff Laytonc45198e2014-09-01 07:12:07 -04003606static int
Jeff Layton7448cc32015-01-16 15:05:57 -05003607nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3608 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609{
3610 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04003611 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 else
3613 return -EAGAIN;
3614}
3615
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003616static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003617 .lm_break = nfsd_break_deleg_cb,
3618 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619};
3620
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003621static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3622{
3623 if (nfsd4_has_session(cstate))
3624 return nfs_ok;
3625 if (seqid == so->so_seqid - 1)
3626 return nfserr_replay_me;
3627 if (seqid == so->so_seqid)
3628 return nfs_ok;
3629 return nfserr_bad_seqid;
3630}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
Jeff Layton4b24ca72014-06-30 11:48:44 -04003632static __be32 lookup_clientid(clientid_t *clid,
3633 struct nfsd4_compound_state *cstate,
3634 struct nfsd_net *nn)
3635{
3636 struct nfs4_client *found;
3637
3638 if (cstate->clp) {
3639 found = cstate->clp;
3640 if (!same_clid(&found->cl_clientid, clid))
3641 return nfserr_stale_clientid;
3642 return nfs_ok;
3643 }
3644
3645 if (STALE_CLIENTID(clid, nn))
3646 return nfserr_stale_clientid;
3647
3648 /*
3649 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3650 * cached already then we know this is for is for v4.0 and "sessions"
3651 * will be false.
3652 */
3653 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003654 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003655 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003656 if (!found) {
3657 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003658 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003659 }
3660 atomic_inc(&found->cl_refcount);
3661 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003662
3663 /* Cache the nfs4_client in cstate! */
3664 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003665 return nfs_ok;
3666}
3667
Al Virob37ad282006-10-19 23:28:59 -07003668__be32
Andy Adamson66689582009-04-03 08:28:45 +03003669nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003670 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 clientid_t *clientid = &open->op_clientid;
3673 struct nfs4_client *clp = NULL;
3674 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003675 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003676 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003678 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003680 /*
3681 * In case we need it later, after we've already created the
3682 * file and don't want to risk a further failure:
3683 */
3684 open->op_file = nfsd4_alloc_file();
3685 if (open->op_file == NULL)
3686 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687
Trond Myklebust2d91e892014-06-30 11:48:46 -04003688 status = lookup_clientid(clientid, cstate, nn);
3689 if (status)
3690 return status;
3691 clp = cstate->clp;
3692
Trond Myklebustd4f04892014-07-29 21:34:36 -04003693 strhashval = ownerstr_hashval(&open->op_owner);
3694 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003695 open->op_openowner = oo;
3696 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003697 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003699 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003700 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003701 release_openowner(oo);
3702 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003703 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003704 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003705 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3706 if (status)
3707 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003708 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003709new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003710 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003711 if (oo == NULL)
3712 return nfserr_jukebox;
3713 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003714alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003715 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003716 if (!open->op_stp)
3717 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003718
3719 if (nfsd4_has_session(cstate) &&
3720 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
3721 open->op_odstate = alloc_clnt_odstate(clp);
3722 if (!open->op_odstate)
3723 return nfserr_jukebox;
3724 }
3725
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003726 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727}
3728
Al Virob37ad282006-10-19 23:28:59 -07003729static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003730nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3731{
3732 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3733 return nfserr_openmode;
3734 else
3735 return nfs_ok;
3736}
3737
Daniel Mackc47d8322011-05-16 16:38:14 +02003738static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003739{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003740 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3741}
3742
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003743static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003744{
3745 struct nfs4_stid *ret;
3746
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003747 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003748 if (!ret)
3749 return NULL;
3750 return delegstateid(ret);
3751}
3752
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003753static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3754{
3755 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3756 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3757}
3758
Al Virob37ad282006-10-19 23:28:59 -07003759static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003760nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003761 struct nfs4_delegation **dp)
3762{
3763 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003764 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003765 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003766
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003767 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3768 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003769 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003770 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003771 status = nfs4_check_delegmode(deleg, flags);
3772 if (status) {
3773 nfs4_put_stid(&deleg->dl_stid);
3774 goto out;
3775 }
3776 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003777out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003778 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003779 return nfs_ok;
3780 if (status)
3781 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003782 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003783 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003784}
3785
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003786static struct nfs4_ol_stateid *
3787nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003789 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003790 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791
Trond Myklebust1d31a252014-07-10 14:07:25 -04003792 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003793 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 /* ignore lock owners */
3795 if (local->st_stateowner->so_is_open_owner == 0)
3796 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003797 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003798 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003799 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003800 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003803 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003804 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805}
3806
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003807static inline int nfs4_access_to_access(u32 nfs4_access)
3808{
3809 int flags = 0;
3810
3811 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3812 flags |= NFSD_MAY_READ;
3813 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3814 flags |= NFSD_MAY_WRITE;
3815 return flags;
3816}
3817
Al Virob37ad282006-10-19 23:28:59 -07003818static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3820 struct nfsd4_open *open)
3821{
3822 struct iattr iattr = {
3823 .ia_valid = ATTR_SIZE,
3824 .ia_size = 0,
3825 };
3826 if (!open->op_truncate)
3827 return 0;
3828 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003829 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3831}
3832
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003833static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003834 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3835 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003836{
Trond Myklebustde186432014-07-10 14:07:26 -04003837 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003838 __be32 status;
3839 int oflag = nfs4_access_to_omode(open->op_share_access);
3840 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003841 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003842
Trond Myklebustde186432014-07-10 14:07:26 -04003843 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003844
3845 /*
3846 * Are we trying to set a deny mode that would conflict with
3847 * current access?
3848 */
3849 status = nfs4_file_check_deny(fp, open->op_share_deny);
3850 if (status != nfs_ok) {
3851 spin_unlock(&fp->fi_lock);
3852 goto out;
3853 }
3854
3855 /* set access to the file */
3856 status = nfs4_file_get_access(fp, open->op_share_access);
3857 if (status != nfs_ok) {
3858 spin_unlock(&fp->fi_lock);
3859 goto out;
3860 }
3861
3862 /* Set access bits in stateid */
3863 old_access_bmap = stp->st_access_bmap;
3864 set_access(open->op_share_access, stp);
3865
3866 /* Set new deny mask */
3867 old_deny_bmap = stp->st_deny_bmap;
3868 set_deny(open->op_share_deny, stp);
3869 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3870
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003871 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003872 spin_unlock(&fp->fi_lock);
3873 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003874 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003875 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003876 spin_lock(&fp->fi_lock);
3877 if (!fp->fi_fds[oflag]) {
3878 fp->fi_fds[oflag] = filp;
3879 filp = NULL;
3880 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003881 }
Trond Myklebustde186432014-07-10 14:07:26 -04003882 spin_unlock(&fp->fi_lock);
3883 if (filp)
3884 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003885
3886 status = nfsd4_truncate(rqstp, cur_fh, open);
3887 if (status)
3888 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003889out:
3890 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003891out_put_access:
3892 stp->st_access_bmap = old_access_bmap;
3893 nfs4_file_put_access(fp, open->op_share_access);
3894 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3895 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003896}
3897
Al Virob37ad282006-10-19 23:28:59 -07003898static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003899nfs4_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 -07003900{
Al Virob37ad282006-10-19 23:28:59 -07003901 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02003902 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003904 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003905 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003906
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003907 /* test and set deny mode */
3908 spin_lock(&fp->fi_lock);
3909 status = nfs4_file_check_deny(fp, open->op_share_deny);
3910 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003911 set_deny(open->op_share_deny, stp);
3912 fp->fi_share_deny |=
3913 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3914 }
3915 spin_unlock(&fp->fi_lock);
3916
3917 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003920 status = nfsd4_truncate(rqstp, cur_fh, open);
3921 if (status != nfs_ok)
3922 reset_union_bmap_deny(old_deny_bmap, stp);
3923 return status;
3924}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003926/* Should we give out recallable state?: */
3927static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3928{
3929 if (clp->cl_cb_state == NFSD4_CB_UP)
3930 return true;
3931 /*
3932 * In the sessions case, since we don't have to establish a
3933 * separate connection for callbacks, we assume it's OK
3934 * until we hear otherwise:
3935 */
3936 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3937}
3938
Jeff Laytond564fbe2014-07-16 10:31:58 -04003939static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003940{
3941 struct file_lock *fl;
3942
3943 fl = locks_alloc_lock();
3944 if (!fl)
3945 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003946 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003947 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003948 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3949 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003950 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003951 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003952 return fl;
3953}
3954
J. Bruce Fields99c41512013-05-21 16:21:25 -04003955static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003956{
Trond Myklebust11b91642014-07-29 21:34:08 -04003957 struct nfs4_file *fp = dp->dl_stid.sc_file;
Kinglong Meeefde6b42015-07-13 17:33:24 +08003958 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003959 struct file *filp;
3960 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003961
Jeff Laytond564fbe2014-07-16 10:31:58 -04003962 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003963 if (!fl)
3964 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003965 filp = find_readable_file(fp);
3966 if (!filp) {
3967 /* We should always have a readable file here */
3968 WARN_ON_ONCE(1);
Kinglong Meeaf9dbaf2015-07-13 17:30:51 +08003969 locks_free_lock(fl);
Jeff Layton417c6622014-07-21 09:34:57 -04003970 return -EBADF;
3971 }
3972 fl->fl_file = filp;
Jeff Laytone6f5c782014-08-22 10:40:25 -04003973 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04003974 if (fl)
Jeff Layton417c6622014-07-21 09:34:57 -04003975 locks_free_lock(fl);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04003976 if (status)
Jeff Layton417c6622014-07-21 09:34:57 -04003977 goto out_fput;
Benny Halevycdc97502014-05-30 09:09:30 -04003978 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003979 spin_lock(&fp->fi_lock);
3980 /* Did the lease get broken before we took the lock? */
3981 status = -EAGAIN;
3982 if (fp->fi_had_conflict)
3983 goto out_unlock;
3984 /* Race breaker */
Jeff Layton0c637be2014-08-22 12:05:43 -04003985 if (fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04003986 status = 0;
Jeff Layton67db1032014-12-13 09:11:40 -05003987 ++fp->fi_delegees;
Jeff Layton417c6622014-07-21 09:34:57 -04003988 hash_delegation_locked(dp, fp);
3989 goto out_unlock;
3990 }
Jeff Layton417c6622014-07-21 09:34:57 -04003991 fp->fi_deleg_file = filp;
Jeff Layton67db1032014-12-13 09:11:40 -05003992 fp->fi_delegees = 1;
Benny Halevy931ee562014-05-30 09:09:27 -04003993 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003994 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003995 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003996 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003997out_unlock:
3998 spin_unlock(&fp->fi_lock);
3999 spin_unlock(&state_lock);
4000out_fput:
4001 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05004002 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004003}
4004
Jeff Layton0b266932014-07-25 07:34:25 -04004005static struct nfs4_delegation *
4006nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02004007 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004008{
Jeff Layton0b266932014-07-25 07:34:25 -04004009 int status;
4010 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04004011
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004012 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004013 return ERR_PTR(-EAGAIN);
4014
Sachin Bhamare8287f002015-04-27 14:50:14 +02004015 dp = alloc_init_deleg(clp, fh, odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004016 if (!dp)
4017 return ERR_PTR(-ENOMEM);
4018
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004019 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04004020 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004021 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04004022 dp->dl_stid.sc_file = fp;
Jeff Layton0c637be2014-08-22 12:05:43 -04004023 if (!fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04004024 spin_unlock(&fp->fi_lock);
4025 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04004026 status = nfs4_setlease(dp);
4027 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04004028 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004029 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04004030 status = -EAGAIN;
4031 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004032 }
Jeff Layton67db1032014-12-13 09:11:40 -05004033 ++fp->fi_delegees;
Benny Halevy931ee562014-05-30 09:09:27 -04004034 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04004035 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04004036out_unlock:
4037 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04004038 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04004039out:
4040 if (status) {
Sachin Bhamare8287f002015-04-27 14:50:14 +02004041 put_clnt_odstate(dp->dl_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04004042 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04004043 return ERR_PTR(status);
4044 }
4045 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004046}
4047
Benny Halevy4aa89132012-02-21 14:16:44 -08004048static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4049{
4050 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4051 if (status == -EAGAIN)
4052 open->op_why_no_deleg = WND4_CONTENTION;
4053 else {
4054 open->op_why_no_deleg = WND4_RESOURCE;
4055 switch (open->op_deleg_want) {
4056 case NFS4_SHARE_WANT_READ_DELEG:
4057 case NFS4_SHARE_WANT_WRITE_DELEG:
4058 case NFS4_SHARE_WANT_ANY_DELEG:
4059 break;
4060 case NFS4_SHARE_WANT_CANCEL:
4061 open->op_why_no_deleg = WND4_CANCELLED;
4062 break;
4063 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004064 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08004065 }
4066 }
4067}
4068
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069/*
4070 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04004071 *
4072 * Note we don't support write delegations, and won't until the vfs has
4073 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 */
4075static void
Jeff Layton4cf59222014-07-25 07:34:24 -04004076nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4077 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078{
4079 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04004080 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4081 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004082 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004083 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004085 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07004086 open->op_recall = 0;
4087 switch (open->op_claim_type) {
4088 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05004089 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07004090 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004091 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4092 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004093 break;
4094 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00004095 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004096 /*
4097 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004098 * had the chance to reclaim theirs, *and* until
4099 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004100 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004101 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004102 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004103 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004104 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04004105 /*
4106 * Also, if the file was opened for write or
4107 * create, there's a good chance the client's
4108 * about to write to it, resulting in an
4109 * immediate recall (since we don't support
4110 * write delegations):
4111 */
NeilBrown7b190fe2005-06-23 22:03:23 -07004112 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04004113 goto out_no_deleg;
4114 if (open->op_create == NFS4_OPEN_CREATE)
4115 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004116 break;
4117 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004118 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004119 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004120 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004121 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004122 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004124 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004126 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004127 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04004128 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04004129 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004130 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004131out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004132 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4133 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004134 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04004135 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004136 open->op_recall = 1;
4137 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04004138
4139 /* 4.1 client asking for a delegation? */
4140 if (open->op_deleg_want)
4141 nfsd4_open_deleg_none_ext(open, status);
4142 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143}
4144
Benny Halevye27f49c2012-02-21 14:16:54 -08004145static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4146 struct nfs4_delegation *dp)
4147{
4148 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4149 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4150 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4151 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4152 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4153 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4154 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4155 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4156 }
4157 /* Otherwise the client must be confused wanting a delegation
4158 * it already has, therefore we don't return
4159 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4160 */
4161}
4162
Al Virob37ad282006-10-19 23:28:59 -07004163__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4165{
Andy Adamson66689582009-04-03 08:28:45 +03004166 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004167 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004169 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004170 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004171 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 /*
4174 * Lookup file; if found, lookup stateid and check open request,
4175 * and check for delegations in the process of being recalled.
4176 * If not found, create the nfs4_file struct
4177 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004178 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004179 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004180 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004181 if (status)
4182 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04004183 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004185 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004186 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004187 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004188 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 }
4190
4191 /*
4192 * OPEN the file, or upgrade an existing OPEN.
4193 * If truncate fails, the OPEN fails.
4194 */
4195 if (stp) {
4196 /* Stateid was found, this is an OPEN upgrade */
Jeff Layton35a92fe2015-09-17 07:47:08 -04004197 down_read(&stp->st_rwsem);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004198 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04004199 if (status) {
4200 up_read(&stp->st_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04004202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004204 stp = open->op_stp;
4205 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04004206 init_open_stateid(stp, fp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04004207 down_read(&stp->st_rwsem);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004208 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4209 if (status) {
Jeff Layton35a92fe2015-09-17 07:47:08 -04004210 up_read(&stp->st_rwsem);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004211 release_open_stateid(stp);
4212 goto out;
4213 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004214
4215 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4216 open->op_odstate);
4217 if (stp->st_clnt_odstate == open->op_odstate)
4218 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004220 update_stateid(&stp->st_stid.sc_stateid);
4221 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Jeff Layton35a92fe2015-09-17 07:47:08 -04004222 up_read(&stp->st_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223
Benny Halevyd24433c2012-02-16 20:57:17 +02004224 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004225 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4226 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4227 open->op_why_no_deleg = WND4_NOT_WANTED;
4228 goto nodeleg;
4229 }
4230 }
4231
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 /*
4233 * Attempt to hand out a delegation. No error return, because the
4234 * OPEN succeeds even if we fail.
4235 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004236 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004237nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 status = nfs_ok;
4239
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004240 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004241 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004243 /* 4.1 client trying to upgrade/downgrade delegation? */
4244 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004245 open->op_deleg_want)
4246 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004247
NeilBrown13cd2182005-06-23 22:03:10 -07004248 if (fp)
4249 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004250 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08004251 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 /*
4253 * To finish the open response, we just need to set the rflags.
4254 */
4255 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004256 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004257 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004259 if (dp)
4260 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004261 if (stp)
4262 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263
4264 return status;
4265}
4266
Jeff Layton58fb12e2014-07-29 21:34:27 -04004267void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04004268 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004269{
4270 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004271 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004272
Jeff Laytond3134b12014-07-29 21:34:32 -04004273 nfsd4_cstate_assign_replay(cstate, so);
4274 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004275 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004276 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04004277 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004278 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004279 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004280 if (open->op_odstate)
4281 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004282}
4283
Al Virob37ad282006-10-19 23:28:59 -07004284__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004285nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4286 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287{
4288 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004289 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004290 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 dprintk("process_renew(%08x/%08x): starting\n",
4293 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004294 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004295 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004297 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004299 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004300 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 goto out;
4302 status = nfs_ok;
4303out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 return status;
4305}
4306
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04004307void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004308nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004309{
Jeff Layton33dcc482012-04-10 11:08:48 -04004310 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004311 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004312 return;
4313
NeilBrowna76b4312005-06-23 22:04:01 -07004314 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004315 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04004316 /*
4317 * If the server goes down again right now, an NFSv4
4318 * client will still be allowed to reclaim after it comes back up,
4319 * even if it hasn't yet had a chance to reclaim state this time.
4320 *
4321 */
Jeff Layton919b8042014-09-12 16:40:20 -04004322 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004323 /*
4324 * At this point, NFSv4 clients can still reclaim. But if the
4325 * server crashes, any that have not yet reclaimed will be out
4326 * of luck on the next boot.
4327 *
4328 * (NFSv4.1+ clients are considered to have reclaimed once they
4329 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4330 * have reclaimed after their first OPEN.)
4331 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004332 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004333 /*
4334 * At this point, and once lockd and/or any other containers
4335 * exit their grace period, further reclaims will fail and
4336 * regular locking can resume.
4337 */
NeilBrowna76b4312005-06-23 22:04:01 -07004338}
4339
NeilBrownfd39ca92005-06-23 22:04:03 -07004340static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004341nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342{
4343 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004344 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004346 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004348 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004349 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004352 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004353 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004354 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004355 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 clp = list_entry(pos, struct nfs4_client, cl_lru);
4357 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4358 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004359 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 break;
4361 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004362 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004363 dprintk("NFSD: client in use (clientid %08x)\n",
4364 clp->cl_clientid.cl_id);
4365 continue;
4366 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004367 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004368 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004369 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004370 list_for_each_safe(pos, next, &reaplist) {
4371 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 dprintk("NFSD: purging unused client (clientid %08x)\n",
4373 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004374 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 expire_client(clp);
4376 }
Benny Halevycdc97502014-05-30 09:09:30 -04004377 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004378 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4380 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004381 t = dp->dl_time - cutoff;
4382 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 break;
4384 }
Jeff Layton3fcbbd22015-08-24 12:41:48 -04004385 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04004386 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 }
Benny Halevycdc97502014-05-30 09:09:30 -04004388 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004389 while (!list_empty(&reaplist)) {
4390 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4391 dl_recall_lru);
4392 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004393 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 }
Jeff Layton217526e2014-07-30 08:27:11 -04004395
4396 spin_lock(&nn->client_lock);
4397 while (!list_empty(&nn->close_lru)) {
4398 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4399 oo_close_lru);
4400 if (time_after((unsigned long)oo->oo_time,
4401 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004402 t = oo->oo_time - cutoff;
4403 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 break;
4405 }
Jeff Layton217526e2014-07-30 08:27:11 -04004406 list_del_init(&oo->oo_close_lru);
4407 stp = oo->oo_last_closed_stid;
4408 oo->oo_last_closed_stid = NULL;
4409 spin_unlock(&nn->client_lock);
4410 nfs4_put_stid(&stp->st_stid);
4411 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 }
Jeff Layton217526e2014-07-30 08:27:11 -04004413 spin_unlock(&nn->client_lock);
4414
Jeff Laytona832e7a2014-05-30 09:09:26 -04004415 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004416 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417}
4418
Harvey Harrisona254b242008-02-20 12:49:00 -08004419static struct workqueue_struct *laundry_wq;
4420static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004421
4422static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004423laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424{
4425 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004426 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4427 work);
4428 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4429 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004431 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004433 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434}
4435
Jeff Layton8fcd4612015-07-30 06:57:46 -04004436static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004437{
Jeff Layton8fcd4612015-07-30 06:57:46 -04004438 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004439 return nfserr_bad_stateid;
4440 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441}
4442
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004444access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004446 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4447 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4448 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449}
4450
4451static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004452access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004454 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4455 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456}
4457
4458static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004459__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460{
Al Virob37ad282006-10-19 23:28:59 -07004461 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
J. Bruce Fields02921912010-07-29 15:16:59 -04004463 /* For lock stateid's, we test the parent open, not the lock: */
4464 if (stp->st_openstp)
4465 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004466 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004468 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 goto out;
4470 status = nfs_ok;
4471out:
4472 return status;
4473}
4474
Al Virob37ad282006-10-19 23:28:59 -07004475static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004476check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004478 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004480 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004481 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 * conflicting state; so we must wait out the grace period. */
4483 return nfserr_grace;
4484 } else if (flags & WR_STATE)
4485 return nfs4_share_conflict(current_fh,
4486 NFS4_SHARE_DENY_WRITE);
4487 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4488 return nfs4_share_conflict(current_fh,
4489 NFS4_SHARE_DENY_READ);
4490}
4491
4492/*
4493 * Allow READ/WRITE during grace period on recovered state only for files
4494 * that are not able to provide mandatory locking.
4495 */
4496static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004497grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004499 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500}
4501
J. Bruce Fields81b82962011-08-23 11:03:29 -04004502/* Returns true iff a is later than b: */
4503static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4504{
Jim Rees1a9357f2013-05-17 17:33:00 -04004505 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004506}
4507
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004508static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004509{
Andy Adamson66689582009-04-03 08:28:45 +03004510 /*
4511 * When sessions are used the stateid generation number is ignored
4512 * when it is zero.
4513 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004514 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004515 return nfs_ok;
4516
4517 if (in->si_generation == ref->si_generation)
4518 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004519
J. Bruce Fields0836f582008-01-26 19:08:12 -05004520 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004521 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004522 return nfserr_bad_stateid;
4523 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004524 * However, we could see a stateid from the past, even from a
4525 * non-buggy client. For example, if the client sends a lock
4526 * while some IO is outstanding, the lock may bump si_generation
4527 * while the IO is still in flight. The client could avoid that
4528 * situation by waiting for responses on all the IO requests,
4529 * but better performance may result in retrying IO that
4530 * receives an old_stateid error if requests are rarely
4531 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004532 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004533 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004534}
4535
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004536static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4537{
4538 if (ols->st_stateowner->so_is_open_owner &&
4539 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4540 return nfserr_bad_stateid;
4541 return nfs_ok;
4542}
4543
Chuck Lever7df302f2012-05-29 13:56:37 -04004544static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004545{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004546 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004547 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004548
Chuck Lever7df302f2012-05-29 13:56:37 -04004549 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004550 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004551 /* Client debugging aid. */
4552 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4553 char addr_str[INET6_ADDRSTRLEN];
4554 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4555 sizeof(addr_str));
4556 pr_warn_ratelimited("NFSD: client %s testing state ID "
4557 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004558 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004559 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004560 spin_lock(&cl->cl_lock);
4561 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004562 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004563 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004564 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004565 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004566 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004567 switch (s->sc_type) {
4568 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004569 status = nfs_ok;
4570 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004571 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004572 status = nfserr_deleg_revoked;
4573 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004574 case NFS4_OPEN_STID:
4575 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004576 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04004577 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004578 default:
4579 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004580 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004581 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004582 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004583 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004584 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004585out_unlock:
4586 spin_unlock(&cl->cl_lock);
4587 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004588}
4589
Christoph Hellwigcd61c522014-08-14 08:44:57 +02004590__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004591nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4592 stateid_t *stateid, unsigned char typemask,
4593 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004594{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004595 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004596
4597 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4598 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004599 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004600 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004601 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004602 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004603 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004604 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004605 if (status)
4606 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004607 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004608 if (!*s)
4609 return nfserr_bad_stateid;
4610 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004611}
4612
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004613static struct file *
4614nfs4_find_file(struct nfs4_stid *s, int flags)
4615{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004616 if (!s)
4617 return NULL;
4618
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004619 switch (s->sc_type) {
4620 case NFS4_DELEG_STID:
4621 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
4622 return NULL;
4623 return get_file(s->sc_file->fi_deleg_file);
4624 case NFS4_OPEN_STID:
4625 case NFS4_LOCK_STID:
4626 if (flags & RD_STATE)
4627 return find_readable_file(s->sc_file);
4628 else
4629 return find_writeable_file(s->sc_file);
4630 break;
4631 }
4632
4633 return NULL;
4634}
4635
4636static __be32
4637nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
4638{
4639 __be32 status;
4640
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004641 status = nfsd4_check_openowner_confirmed(ols);
4642 if (status)
4643 return status;
4644 return nfs4_check_openmode(ols, flags);
4645}
4646
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004647static __be32
4648nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
4649 struct file **filpp, bool *tmp_file, int flags)
4650{
4651 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
4652 struct file *file;
4653 __be32 status;
4654
4655 file = nfs4_find_file(s, flags);
4656 if (file) {
4657 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
4658 acc | NFSD_MAY_OWNER_OVERRIDE);
4659 if (status) {
4660 fput(file);
4661 return status;
4662 }
4663
4664 *filpp = file;
4665 } else {
4666 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
4667 if (status)
4668 return status;
4669
4670 if (tmp_file)
4671 *tmp_file = true;
4672 }
4673
4674 return 0;
4675}
4676
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004678 * Checks for stateid operations
4679 */
Al Virob37ad282006-10-19 23:28:59 -07004680__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004681nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
4682 struct nfsd4_compound_state *cstate, stateid_t *stateid,
4683 int flags, struct file **filpp, bool *tmp_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004685 struct svc_fh *fhp = &cstate->current_fh;
4686 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004687 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004688 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004689 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004690 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 if (filpp)
4693 *filpp = NULL;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004694 if (tmp_file)
4695 *tmp_file = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004697 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 return nfserr_grace;
4699
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004700 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
4701 status = check_special_stateids(net, fhp, stateid, flags);
4702 goto done;
4703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004705 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004706 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004707 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004708 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04004709 return status;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004710 status = check_stateid_generation(stateid, &s->sc_stateid,
4711 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04004712 if (status)
4713 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004714
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004715 switch (s->sc_type) {
4716 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004717 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004718 break;
4719 case NFS4_OPEN_STID:
4720 case NFS4_LOCK_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004721 status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004722 break;
4723 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004724 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004725 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04004727 if (status)
4728 goto out;
4729 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004730
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004731done:
4732 if (!status && filpp)
4733 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734out:
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004735 if (s)
4736 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 return status;
4738}
4739
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004740/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004741 * Test if the stateid is valid
4742 */
4743__be32
4744nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4745 struct nfsd4_test_stateid *test_stateid)
4746{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004747 struct nfsd4_test_stateid_id *stateid;
4748 struct nfs4_client *cl = cstate->session->se_client;
4749
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004750 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004751 stateid->ts_id_status =
4752 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004753
Bryan Schumaker17456802011-07-13 10:50:48 -04004754 return nfs_ok;
4755}
4756
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004757__be32
4758nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4759 struct nfsd4_free_stateid *free_stateid)
4760{
4761 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004762 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004763 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004764 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004765 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004766 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004767
Jeff Layton1af71cc2014-07-29 21:34:14 -04004768 spin_lock(&cl->cl_lock);
4769 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004770 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004771 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004772 switch (s->sc_type) {
4773 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004774 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004775 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004776 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004777 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4778 if (ret)
4779 break;
4780 ret = nfserr_locks_held;
4781 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004782 case NFS4_LOCK_STID:
4783 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4784 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004785 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004786 stp = openlockstateid(s);
4787 ret = nfserr_locks_held;
4788 if (check_for_locks(stp->st_stid.sc_file,
4789 lockowner(stp->st_stateowner)))
4790 break;
Jeff Laytone8568732015-08-24 12:41:47 -04004791 WARN_ON(!unhash_lock_stateid(stp));
Jeff Layton1af71cc2014-07-29 21:34:14 -04004792 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004793 nfs4_put_stid(s);
4794 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004795 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004796 case NFS4_REVOKED_DELEG_STID:
4797 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004798 list_del_init(&dp->dl_recall_lru);
4799 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004800 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004801 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004802 goto out;
4803 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004804 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004805out_unlock:
4806 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004807out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004808 return ret;
4809}
4810
NeilBrown4c4cd222005-07-07 17:59:27 -07004811static inline int
4812setlkflg (int type)
4813{
4814 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4815 RD_STATE : WR_STATE;
4816}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004818static __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 -04004819{
4820 struct svc_fh *current_fh = &cstate->current_fh;
4821 struct nfs4_stateowner *sop = stp->st_stateowner;
4822 __be32 status;
4823
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004824 status = nfsd4_check_seqid(cstate, sop, seqid);
4825 if (status)
4826 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004827 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4828 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004829 /*
4830 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004831 * nfserr_replay_me from the previous step, and
4832 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004833 */
4834 return nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04004835 down_write(&stp->st_rwsem);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004836 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04004837 if (status == nfs_ok)
4838 status = nfs4_check_fh(current_fh, &stp->st_stid);
4839 if (status != nfs_ok)
4840 up_write(&stp->st_rwsem);
4841 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004842}
4843
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844/*
4845 * Checks for sequence id mutating operations.
4846 */
Al Virob37ad282006-10-19 23:28:59 -07004847static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004848nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004849 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004850 struct nfs4_ol_stateid **stpp,
4851 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004853 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004854 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004855 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004857 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4858 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004859
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004861 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004862 if (status)
4863 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004864 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004865 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004867 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004868 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004869 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004870 else
4871 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004872 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004873}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004874
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004875static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4876 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004877{
4878 __be32 status;
4879 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004880 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004882 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004883 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004884 if (status)
4885 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004886 oo = openowner(stp->st_stateowner);
4887 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Jeff Layton35a92fe2015-09-17 07:47:08 -04004888 up_write(&stp->st_rwsem);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004889 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004890 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004891 }
4892 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004893 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894}
4895
Al Virob37ad282006-10-19 23:28:59 -07004896__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004897nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004898 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899{
Al Virob37ad282006-10-19 23:28:59 -07004900 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004901 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004902 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004903 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904
Al Viroa6a9f182013-09-16 10:57:01 -04004905 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4906 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004908 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004909 if (status)
4910 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004912 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004913 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004914 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004915 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004916 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004917 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004918 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04004919 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
4920 up_write(&stp->st_rwsem);
Trond Myklebust2585fc72014-07-29 21:34:21 -04004921 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04004922 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004923 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004924 update_stateid(&stp->st_stid.sc_stateid);
4925 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Jeff Layton35a92fe2015-09-17 07:47:08 -04004926 up_write(&stp->st_rwsem);
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004927 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004928 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004929
Jeff Layton2a4317c2012-03-21 16:42:43 -04004930 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004931 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004932put_stateid:
4933 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004935 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 return status;
4937}
4938
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004939static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004941 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004942 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004943 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004944 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004945}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004946
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004947static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4948{
4949 switch (to_access) {
4950 case NFS4_SHARE_ACCESS_READ:
4951 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4952 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4953 break;
4954 case NFS4_SHARE_ACCESS_WRITE:
4955 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4956 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4957 break;
4958 case NFS4_SHARE_ACCESS_BOTH:
4959 break;
4960 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004961 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 }
4963}
4964
Al Virob37ad282006-10-19 23:28:59 -07004965__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004966nfsd4_open_downgrade(struct svc_rqst *rqstp,
4967 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004968 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969{
Al Virob37ad282006-10-19 23:28:59 -07004970 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004971 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004972 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973
Al Viroa6a9f182013-09-16 10:57:01 -04004974 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4975 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004977 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004978 if (od->od_deleg_want)
4979 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4980 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004982 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004983 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004984 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004987 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004988 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004990 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004992 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004993 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004995 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004997 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998
Jeff Laytonce0fc432012-05-11 09:45:14 -04004999 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005001 update_stateid(&stp->st_stid.sc_stateid);
5002 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005004put_stateid:
Jeff Layton35a92fe2015-09-17 07:47:08 -04005005 up_write(&stp->st_rwsem);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005006 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005008 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 return status;
5010}
5011
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005012static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5013{
Trond Myklebustacf92952014-06-30 11:48:37 -04005014 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04005015 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04005016 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005017
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005018 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04005019 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04005020 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005021
Jeff Laytond83017f2014-07-29 21:34:42 -04005022 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04005023 if (unhashed)
5024 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04005025 spin_unlock(&clp->cl_lock);
5026 free_ol_stateid_reaplist(&reaplist);
5027 } else {
5028 spin_unlock(&clp->cl_lock);
5029 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04005030 if (unhashed)
5031 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04005032 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04005033}
5034
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035/*
5036 * nfs4_unlock_state() called after encode
5037 */
Al Virob37ad282006-10-19 23:28:59 -07005038__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005039nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005040 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041{
Al Virob37ad282006-10-19 23:28:59 -07005042 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005043 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005044 struct net *net = SVC_NET(rqstp);
5045 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046
Al Viroa6a9f182013-09-16 10:57:01 -04005047 dprintk("NFSD: nfsd4_close on file %pd\n",
5048 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005050 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5051 &close->cl_stateid,
5052 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005053 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005054 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005055 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005057 update_stateid(&stp->st_stid.sc_stateid);
5058 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Jeff Layton35a92fe2015-09-17 07:47:08 -04005059 up_write(&stp->st_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005061 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005062
5063 /* put reference from nfs4_preprocess_seqid_op */
5064 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 return status;
5067}
5068
Al Virob37ad282006-10-19 23:28:59 -07005069__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005070nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5071 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005073 struct nfs4_delegation *dp;
5074 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005075 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07005076 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005077 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005079 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005080 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005082 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005083 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005084 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005085 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04005086 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005087 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04005088 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005089
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005090 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005091put_stateid:
5092 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093out:
5094 return status;
5095}
5096
Benny Halevy87df4de2008-12-15 19:42:03 +02005097static inline u64
5098end_offset(u64 start, u64 len)
5099{
5100 u64 end;
5101
5102 end = start + len;
5103 return end >= start ? end: NFS4_MAX_UINT64;
5104}
5105
5106/* last octet in a range */
5107static inline u64
5108last_byte_offset(u64 start, u64 len)
5109{
5110 u64 end;
5111
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005112 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02005113 end = start + len;
5114 return end > start ? end - 1: NFS4_MAX_UINT64;
5115}
5116
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117/*
5118 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5119 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5120 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5121 * locking, this prevents us from being completely protocol-compliant. The
5122 * real solution to this problem is to start using unsigned file offsets in
5123 * the VFS, but this is a very deep change!
5124 */
5125static inline void
5126nfs4_transform_lock_offset(struct file_lock *lock)
5127{
5128 if (lock->fl_start < 0)
5129 lock->fl_start = OFFSET_MAX;
5130 if (lock->fl_end < 0)
5131 lock->fl_end = OFFSET_MAX;
5132}
5133
Jeff Laytoncae80b32015-04-03 09:04:04 -04005134static fl_owner_t
5135nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005136{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005137 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5138
5139 nfs4_get_stateowner(&lo->lo_owner);
5140 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005141}
5142
Jeff Laytoncae80b32015-04-03 09:04:04 -04005143static void
5144nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005145{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005146 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005147
Jeff Laytoncae80b32015-04-03 09:04:04 -04005148 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04005149 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04005150}
5151
Alexey Dobriyan7b021962009-09-21 17:01:12 -07005152static const struct lock_manager_operations nfsd_posix_mng_ops = {
Kinglong Meeaef95832014-08-22 10:18:44 -04005153 .lm_get_owner = nfsd4_fl_get_owner,
5154 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07005155};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156
5157static inline void
5158nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5159{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005160 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161
NeilBrownd5b90262006-04-10 22:55:22 -07005162 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005163 lo = (struct nfs4_lockowner *) fl->fl_owner;
5164 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5165 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005166 if (!deny->ld_owner.data)
5167 /* We just don't care that much */
5168 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005169 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5170 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07005171 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005172nevermind:
5173 deny->ld_owner.len = 0;
5174 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07005175 deny->ld_clientid.cl_boot = 0;
5176 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 }
5178 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02005179 deny->ld_length = NFS4_MAX_UINT64;
5180 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5182 deny->ld_type = NFS4_READ_LT;
5183 if (fl->fl_type != F_RDLCK)
5184 deny->ld_type = NFS4_WRITE_LT;
5185}
5186
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005187static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005188find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189{
Trond Myklebustd4f04892014-07-29 21:34:36 -04005190 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005191 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192
Trond Myklebust0a880a22014-07-30 08:27:10 -04005193 lockdep_assert_held(&clp->cl_lock);
5194
Trond Myklebustd4f04892014-07-29 21:34:36 -04005195 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5196 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005197 if (so->so_is_open_owner)
5198 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005199 if (same_owner_str(so, owner))
5200 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 }
5202 return NULL;
5203}
5204
Trond Myklebustc58c6612014-07-29 21:34:35 -04005205static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005206find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04005207{
5208 struct nfs4_lockowner *lo;
5209
Trond Myklebustd4f04892014-07-29 21:34:36 -04005210 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005211 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005212 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005213 return lo;
5214}
5215
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005216static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5217{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005218 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005219}
5220
Jeff Layton6b180f02014-07-29 21:34:26 -04005221static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5222{
5223 struct nfs4_lockowner *lo = lockowner(sop);
5224
5225 kmem_cache_free(lockowner_slab, lo);
5226}
5227
5228static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005229 .so_unhash = nfs4_unhash_lockowner,
5230 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005231};
5232
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233/*
5234 * Alloc a lock owner structure.
5235 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005236 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005238 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005240static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005241alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5242 struct nfs4_ol_stateid *open_stp,
5243 struct nfsd4_lock *lock)
5244{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005245 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005247 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5248 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005250 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5251 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005252 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005253 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005254 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005255 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005256 if (ret == NULL) {
5257 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005258 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005259 ret = lo;
5260 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08005261 nfs4_free_stateowner(&lo->lo_owner);
5262
Trond Myklebustd4f04892014-07-29 21:34:36 -04005263 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04005264 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265}
5266
Jeff Layton356a95e2014-07-29 21:34:13 -04005267static void
5268init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5269 struct nfs4_file *fp, struct inode *inode,
5270 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005272 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273
Jeff Layton356a95e2014-07-29 21:34:13 -04005274 lockdep_assert_held(&clp->cl_lock);
5275
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005276 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005277 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005278 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07005279 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005280 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04005281 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005282 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005284 stp->st_openstp = open_stp;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005285 init_rwsem(&stp->st_rwsem);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005286 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005287 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005288 spin_lock(&fp->fi_lock);
5289 list_add(&stp->st_perfile, &fp->fi_stateids);
5290 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291}
5292
Jeff Laytonc53530d2014-06-30 11:48:39 -04005293static struct nfs4_ol_stateid *
5294find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5295{
5296 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005297 struct nfs4_client *clp = lo->lo_owner.so_client;
5298
5299 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005300
5301 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005302 if (lst->st_stid.sc_file == fp) {
5303 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005304 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005305 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005306 }
5307 return NULL;
5308}
5309
Jeff Layton356a95e2014-07-29 21:34:13 -04005310static struct nfs4_ol_stateid *
5311find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5312 struct inode *inode, struct nfs4_ol_stateid *ost,
5313 bool *new)
5314{
5315 struct nfs4_stid *ns = NULL;
5316 struct nfs4_ol_stateid *lst;
5317 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5318 struct nfs4_client *clp = oo->oo_owner.so_client;
5319
5320 spin_lock(&clp->cl_lock);
5321 lst = find_lock_stateid(lo, fi);
5322 if (lst == NULL) {
5323 spin_unlock(&clp->cl_lock);
5324 ns = nfs4_alloc_stid(clp, stateid_slab);
5325 if (ns == NULL)
5326 return NULL;
5327
5328 spin_lock(&clp->cl_lock);
5329 lst = find_lock_stateid(lo, fi);
5330 if (likely(!lst)) {
5331 lst = openlockstateid(ns);
5332 init_lock_stateid(lst, lo, fi, inode, ost);
5333 ns = NULL;
5334 *new = true;
5335 }
5336 }
5337 spin_unlock(&clp->cl_lock);
5338 if (ns)
5339 nfs4_put_stid(ns);
5340 return lst;
5341}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005342
NeilBrownfd39ca92005-06-23 22:04:03 -07005343static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344check_lock_length(u64 offset, u64 length)
5345{
Kinglong Meee7969312015-07-13 17:34:19 +08005346 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5347 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348}
5349
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005350static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005351{
Trond Myklebust11b91642014-07-29 21:34:08 -04005352 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005353
Jeff Layton7214e862014-07-10 14:07:33 -04005354 lockdep_assert_held(&fp->fi_lock);
5355
Jeff Layton82c5ff12012-05-11 09:45:13 -04005356 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005357 return;
Jeff Layton12659652014-07-10 14:07:28 -04005358 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005359 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005360}
5361
Jeff Layton356a95e2014-07-29 21:34:13 -04005362static __be32
5363lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5364 struct nfs4_ol_stateid *ost,
5365 struct nfsd4_lock *lock,
5366 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005367{
Jeff Layton5db1c032014-07-29 21:34:28 -04005368 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005369 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005370 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5371 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00005372 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005373 struct nfs4_lockowner *lo;
5374 unsigned int strhashval;
5375
Kinglong Meec8623992015-07-13 17:35:37 +08005376 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005377 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005378 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005379 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5380 if (lo == NULL)
5381 return nfserr_jukebox;
5382 } else {
5383 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005384 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005385 if (!cstate->minorversion &&
5386 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005387 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005388 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005389
Jeff Layton356a95e2014-07-29 21:34:13 -04005390 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005391 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005392 status = nfserr_jukebox;
5393 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005394 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005395 status = nfs_ok;
5396out:
5397 nfs4_put_stateowner(&lo->lo_owner);
5398 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005399}
5400
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401/*
5402 * LOCK operation
5403 */
Al Virob37ad282006-10-19 23:28:59 -07005404__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005405nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005406 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005408 struct nfs4_openowner *open_sop = NULL;
5409 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005410 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005411 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005412 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005413 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005414 struct file_lock *file_lock = NULL;
5415 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005416 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005417 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005418 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005419 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005420 struct net *net = SVC_NET(rqstp);
5421 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422
5423 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5424 (long long) lock->lk_offset,
5425 (long long) lock->lk_length);
5426
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 if (check_lock_length(lock->lk_offset, lock->lk_length))
5428 return nfserr_inval;
5429
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005430 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005431 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005432 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5433 return status;
5434 }
5435
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005437 if (nfsd4_has_session(cstate))
5438 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005439 memcpy(&lock->lk_new_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04005440 &cstate->session->se_client->cl_clientid,
5441 sizeof(clientid_t));
5442
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005444 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005448 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 lock->lk_new_open_seqid,
5450 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005451 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005452 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005454 up_write(&open_stp->st_rwsem);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005455 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005456 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005457 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005458 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005459 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005460 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005461 &lock_stp, &new);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005462 if (status == nfs_ok)
5463 down_write(&lock_stp->st_rwsem);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005464 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005465 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005466 lock->lk_old_lock_seqid,
5467 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005468 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005469 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005470 if (status)
5471 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005472 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005474 lkflg = setlkflg(lock->lk_type);
5475 status = nfs4_check_openmode(lock_stp, lkflg);
5476 if (status)
5477 goto out;
5478
NeilBrown0dd395d2005-07-07 17:59:15 -07005479 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005480 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005481 goto out;
5482 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005483 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005484 goto out;
5485
Jeff Layton21179d82012-08-21 08:03:32 -04005486 file_lock = locks_alloc_lock();
5487 if (!file_lock) {
5488 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5489 status = nfserr_jukebox;
5490 goto out;
5491 }
5492
Trond Myklebust11b91642014-07-29 21:34:08 -04005493 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 switch (lock->lk_type) {
5495 case NFS4_READ_LT:
5496 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005497 spin_lock(&fp->fi_lock);
5498 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005499 if (filp)
5500 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005501 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005502 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005503 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 case NFS4_WRITE_LT:
5505 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005506 spin_lock(&fp->fi_lock);
5507 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005508 if (filp)
5509 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005510 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005511 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005512 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 default:
5514 status = nfserr_inval;
5515 goto out;
5516 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005517 if (!filp) {
5518 status = nfserr_openmode;
5519 goto out;
5520 }
Kinglong Meeaef95832014-08-22 10:18:44 -04005521
5522 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04005523 file_lock->fl_pid = current->tgid;
5524 file_lock->fl_file = filp;
5525 file_lock->fl_flags = FL_POSIX;
5526 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5527 file_lock->fl_start = lock->lk_offset;
5528 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5529 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530
Jeff Layton21179d82012-08-21 08:03:32 -04005531 conflock = locks_alloc_lock();
5532 if (!conflock) {
5533 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5534 status = nfserr_jukebox;
5535 goto out;
5536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537
Jeff Layton21179d82012-08-21 08:03:32 -04005538 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005539 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005541 update_stateid(&lock_stp->st_stid.sc_stateid);
5542 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005544 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005545 break;
5546 case (EAGAIN): /* conflock holds conflicting lock */
5547 status = nfserr_denied;
5548 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005549 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005550 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 case (EDEADLK):
5552 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005553 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005554 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005555 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005556 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005557 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559out:
Trond Myklebustde186432014-07-10 14:07:26 -04005560 if (filp)
5561 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005562 if (lock_stp) {
5563 /* Bump seqid manually if the 4.0 replay owner is openowner */
5564 if (cstate->replay_owner &&
5565 cstate->replay_owner != &lock_sop->lo_owner &&
5566 seqid_mutating_err(ntohl(status)))
5567 lock_sop->lo_owner.so_seqid++;
5568
Jeff Layton35a92fe2015-09-17 07:47:08 -04005569 up_write(&lock_stp->st_rwsem);
5570
Jeff Layton5db1c032014-07-29 21:34:28 -04005571 /*
5572 * If this is a new, never-before-used stateid, and we are
5573 * returning an error, then just go ahead and release it.
5574 */
5575 if (status && new)
5576 release_lock_stateid(lock_stp);
5577
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005578 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005579 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005580 if (open_stp)
5581 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005582 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005583 if (file_lock)
5584 locks_free_lock(file_lock);
5585 if (conflock)
5586 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 return status;
5588}
5589
5590/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005591 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5592 * so we do a temporary open here just to get an open file to pass to
5593 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5594 * inode operation.)
5595 */
Al Viro04da6e92012-04-13 00:00:04 -04005596static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005597{
5598 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005599 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5600 if (!err) {
5601 err = nfserrno(vfs_test_lock(file, lock));
Christoph Hellwigfd891452015-04-28 15:41:16 +02005602 fput(file);
Al Viro04da6e92012-04-13 00:00:04 -04005603 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005604 return err;
5605}
5606
5607/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 * LOCKT operation
5609 */
Al Virob37ad282006-10-19 23:28:59 -07005610__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005611nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5612 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613{
Jeff Layton21179d82012-08-21 08:03:32 -04005614 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005615 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005616 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005617 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005619 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 return nfserr_grace;
5621
5622 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5623 return nfserr_inval;
5624
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005625 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005626 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005627 if (status)
5628 goto out;
5629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005631 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633
Jeff Layton21179d82012-08-21 08:03:32 -04005634 file_lock = locks_alloc_lock();
5635 if (!file_lock) {
5636 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5637 status = nfserr_jukebox;
5638 goto out;
5639 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005640
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 switch (lockt->lt_type) {
5642 case NFS4_READ_LT:
5643 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005644 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 break;
5646 case NFS4_WRITE_LT:
5647 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005648 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 break;
5650 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005651 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 status = nfserr_inval;
5653 goto out;
5654 }
5655
Kinglong Meec8623992015-07-13 17:35:37 +08005656 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005657 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005658 file_lock->fl_owner = (fl_owner_t)lo;
5659 file_lock->fl_pid = current->tgid;
5660 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661
Jeff Layton21179d82012-08-21 08:03:32 -04005662 file_lock->fl_start = lockt->lt_offset;
5663 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664
Jeff Layton21179d82012-08-21 08:03:32 -04005665 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666
Jeff Layton21179d82012-08-21 08:03:32 -04005667 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005668 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005669 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005670
Jeff Layton21179d82012-08-21 08:03:32 -04005671 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005673 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 }
5675out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005676 if (lo)
5677 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04005678 if (file_lock)
5679 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 return status;
5681}
5682
Al Virob37ad282006-10-19 23:28:59 -07005683__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005684nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005685 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005687 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005689 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005690 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005691 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005692 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5693
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5695 (long long) locku->lu_offset,
5696 (long long) locku->lu_length);
5697
5698 if (check_lock_length(locku->lu_offset, locku->lu_length))
5699 return nfserr_inval;
5700
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005701 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005702 &locku->lu_stateid, NFS4_LOCK_STID,
5703 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005704 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005706 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005707 if (!filp) {
5708 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005709 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005710 }
Jeff Layton21179d82012-08-21 08:03:32 -04005711 file_lock = locks_alloc_lock();
5712 if (!file_lock) {
5713 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5714 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005715 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005716 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005717
Jeff Layton21179d82012-08-21 08:03:32 -04005718 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04005719 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04005720 file_lock->fl_pid = current->tgid;
5721 file_lock->fl_file = filp;
5722 file_lock->fl_flags = FL_POSIX;
5723 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5724 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725
Jeff Layton21179d82012-08-21 08:03:32 -04005726 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5727 locku->lu_length);
5728 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729
Jeff Layton21179d82012-08-21 08:03:32 -04005730 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005731 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005732 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 goto out_nfserr;
5734 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005735 update_stateid(&stp->st_stid.sc_stateid);
5736 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005737fput:
5738 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005739put_stateid:
Jeff Layton35a92fe2015-09-17 07:47:08 -04005740 up_write(&stp->st_rwsem);
Trond Myklebust858cc572014-07-29 21:34:16 -04005741 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005743 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005744 if (file_lock)
5745 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 return status;
5747
5748out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005749 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005750 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751}
5752
5753/*
5754 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005755 * true: locks held by lockowner
5756 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005758static bool
5759check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005761 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005762 int status = false;
5763 struct file *filp = find_any_file(fp);
5764 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005765 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005766
5767 if (!filp) {
5768 /* Any valid lock stateid should have some sort of access */
5769 WARN_ON_ONCE(1);
5770 return status;
5771 }
5772
5773 inode = file_inode(filp);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005774 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005776 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05005777 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005778 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
5779 if (fl->fl_owner == (fl_owner_t)lowner) {
5780 status = true;
5781 break;
5782 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005783 }
Jeff Layton6109c852015-01-16 15:05:57 -05005784 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 }
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005786 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 return status;
5788}
5789
Al Virob37ad282006-10-19 23:28:59 -07005790__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005791nfsd4_release_lockowner(struct svc_rqst *rqstp,
5792 struct nfsd4_compound_state *cstate,
5793 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794{
5795 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005796 struct nfs4_stateowner *sop;
5797 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005798 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005800 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005801 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005802 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005803 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804
5805 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5806 clid->cl_boot, clid->cl_id);
5807
Jeff Layton4b24ca72014-06-30 11:48:44 -04005808 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005809 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04005810 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005811
Trond Myklebustd4f04892014-07-29 21:34:36 -04005812 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005813 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005814 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005815 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005816 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005817
5818 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005819 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005820
Jeff Layton882e9d22014-07-29 21:34:37 -04005821 /* see if there are still any locks associated with it */
5822 lo = lockowner(sop);
5823 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5824 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5825 status = nfserr_locks_held;
5826 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04005827 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04005828 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005829 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005830
Kinglong Meeb5971af2014-08-22 10:18:43 -04005831 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04005832 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005833 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005834 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005835 if (lo)
5836 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 return status;
5838}
5839
5840static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005841alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842{
NeilBrowna55370a2005-06-23 22:03:52 -07005843 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844}
5845
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005846bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005847nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005848{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005849 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005850
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005851 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005852 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005853}
5854
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855/*
5856 * failure => all reset bets are off, nfserr_no_grace...
5857 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005858struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005859nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860{
5861 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005862 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863
NeilBrowna55370a2005-06-23 22:03:52 -07005864 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5865 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005866 if (crp) {
5867 strhashval = clientstr_hashval(name);
5868 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005869 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005870 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005871 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005872 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005873 }
5874 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875}
5876
Jeff Layton2a4317c2012-03-21 16:42:43 -04005877void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005878nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005879{
5880 list_del(&crp->cr_strhash);
5881 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005882 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005883}
5884
5885void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005886nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887{
5888 struct nfs4_client_reclaim *crp = NULL;
5889 int i;
5890
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005892 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5893 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005895 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896 }
5897 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005898 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899}
5900
5901/*
5902 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005903struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005904nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905{
5906 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907 struct nfs4_client_reclaim *crp = NULL;
5908
Jeff Layton278c9312012-11-12 15:00:52 -05005909 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910
Jeff Layton278c9312012-11-12 15:00:52 -05005911 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005912 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005913 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 return crp;
5915 }
5916 }
5917 return NULL;
5918}
5919
5920/*
5921* Called from OPEN. Look for clientid in reclaim list.
5922*/
Al Virob37ad282006-10-19 23:28:59 -07005923__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005924nfs4_check_open_reclaim(clientid_t *clid,
5925 struct nfsd4_compound_state *cstate,
5926 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005928 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005929
5930 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005931 status = lookup_clientid(clid, cstate, nn);
5932 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005933 return nfserr_reclaim_bad;
5934
Jeff Layton3b3e7b72014-09-12 16:40:22 -04005935 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
5936 return nfserr_no_grace;
5937
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005938 if (nfsd4_client_record_check(cstate->clp))
5939 return nfserr_reclaim_bad;
5940
5941 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942}
5943
Bryan Schumaker65178db2011-11-01 13:35:21 -04005944#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04005945static inline void
5946put_client(struct nfs4_client *clp)
5947{
5948 atomic_dec(&clp->cl_refcount);
5949}
5950
Jeff Layton285abde2014-07-30 08:27:24 -04005951static struct nfs4_client *
5952nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5953{
5954 struct nfs4_client *clp;
5955 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5956 nfsd_net_id);
5957
5958 if (!nfsd_netns_ready(nn))
5959 return NULL;
5960
5961 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5962 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5963 return clp;
5964 }
5965 return NULL;
5966}
5967
Jeff Layton7ec0e362014-07-30 08:27:17 -04005968u64
Jeff Layton285abde2014-07-30 08:27:24 -04005969nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04005970{
5971 struct nfs4_client *clp;
5972 u64 count = 0;
5973 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5974 nfsd_net_id);
5975 char buf[INET6_ADDRSTRLEN];
5976
5977 if (!nfsd_netns_ready(nn))
5978 return 0;
5979
5980 spin_lock(&nn->client_lock);
5981 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5982 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5983 pr_info("NFS Client: %s\n", buf);
5984 ++count;
5985 }
5986 spin_unlock(&nn->client_lock);
5987
5988 return count;
5989}
Bryan Schumaker65178db2011-11-01 13:35:21 -04005990
Jeff Laytona0926d12014-07-30 08:27:18 -04005991u64
Jeff Layton285abde2014-07-30 08:27:24 -04005992nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04005993{
5994 u64 count = 0;
5995 struct nfs4_client *clp;
5996 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5997 nfsd_net_id);
5998
5999 if (!nfsd_netns_ready(nn))
6000 return count;
6001
6002 spin_lock(&nn->client_lock);
6003 clp = nfsd_find_client(addr, addr_size);
6004 if (clp) {
6005 if (mark_client_expired_locked(clp) == nfs_ok)
6006 ++count;
6007 else
6008 clp = NULL;
6009 }
6010 spin_unlock(&nn->client_lock);
6011
6012 if (clp)
6013 expire_client(clp);
6014
6015 return count;
6016}
6017
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006018u64
Jeff Layton285abde2014-07-30 08:27:24 -04006019nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006020{
6021 u64 count = 0;
6022 struct nfs4_client *clp, *next;
6023 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6024 nfsd_net_id);
6025 LIST_HEAD(reaplist);
6026
6027 if (!nfsd_netns_ready(nn))
6028 return count;
6029
6030 spin_lock(&nn->client_lock);
6031 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6032 if (mark_client_expired_locked(clp) == nfs_ok) {
6033 list_add(&clp->cl_lru, &reaplist);
6034 if (max != 0 && ++count >= max)
6035 break;
6036 }
6037 }
6038 spin_unlock(&nn->client_lock);
6039
6040 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6041 expire_client(clp);
6042
6043 return count;
6044}
6045
Bryan Schumaker184c1842012-11-29 11:40:44 -05006046static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6047 const char *type)
6048{
6049 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05006050 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05006051 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6052}
6053
Jeff Layton016200c2014-07-30 08:27:21 -04006054static void
6055nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6056 struct list_head *collect)
6057{
6058 struct nfs4_client *clp = lst->st_stid.sc_client;
6059 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6060 nfsd_net_id);
6061
6062 if (!collect)
6063 return;
6064
6065 lockdep_assert_held(&nn->client_lock);
6066 atomic_inc(&clp->cl_refcount);
6067 list_add(&lst->st_locks, collect);
6068}
6069
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006070static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04006071 struct list_head *collect,
Jeff Laytone8568732015-08-24 12:41:47 -04006072 bool (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05006073{
6074 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006075 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006076 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006077 u64 count = 0;
6078
Jeff Layton016200c2014-07-30 08:27:21 -04006079 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006080 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006081 list_for_each_entry_safe(stp, st_next,
6082 &oop->oo_owner.so_stateids, st_perstateowner) {
6083 list_for_each_entry_safe(lst, lst_next,
6084 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04006085 if (func) {
Jeff Laytone8568732015-08-24 12:41:47 -04006086 if (func(lst))
6087 nfsd_inject_add_lock_to_list(lst,
6088 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04006089 }
Jeff Layton016200c2014-07-30 08:27:21 -04006090 ++count;
6091 /*
6092 * Despite the fact that these functions deal
6093 * with 64-bit integers for "count", we must
6094 * ensure that it doesn't blow up the
6095 * clp->cl_refcount. Throw a warning if we
6096 * start to approach INT_MAX here.
6097 */
6098 WARN_ON_ONCE(count == (INT_MAX / 2));
6099 if (count == max)
6100 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006101 }
6102 }
6103 }
Jeff Layton016200c2014-07-30 08:27:21 -04006104out:
6105 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006106
6107 return count;
6108}
6109
Jeff Layton016200c2014-07-30 08:27:21 -04006110static u64
6111nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6112 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05006113{
Jeff Layton016200c2014-07-30 08:27:21 -04006114 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006115}
6116
Jeff Layton016200c2014-07-30 08:27:21 -04006117static u64
6118nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006119{
Jeff Layton016200c2014-07-30 08:27:21 -04006120 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006121 nfsd_print_count(clp, count, "locked files");
6122 return count;
6123}
6124
Jeff Layton016200c2014-07-30 08:27:21 -04006125u64
Jeff Layton285abde2014-07-30 08:27:24 -04006126nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04006127{
6128 struct nfs4_client *clp;
6129 u64 count = 0;
6130 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6131 nfsd_net_id);
6132
6133 if (!nfsd_netns_ready(nn))
6134 return 0;
6135
6136 spin_lock(&nn->client_lock);
6137 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6138 count += nfsd_print_client_locks(clp);
6139 spin_unlock(&nn->client_lock);
6140
6141 return count;
6142}
6143
6144static void
6145nfsd_reap_locks(struct list_head *reaplist)
6146{
6147 struct nfs4_client *clp;
6148 struct nfs4_ol_stateid *stp, *next;
6149
6150 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6151 list_del_init(&stp->st_locks);
6152 clp = stp->st_stid.sc_client;
6153 nfs4_put_stid(&stp->st_stid);
6154 put_client(clp);
6155 }
6156}
6157
6158u64
Jeff Layton285abde2014-07-30 08:27:24 -04006159nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04006160{
6161 unsigned int count = 0;
6162 struct nfs4_client *clp;
6163 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6164 nfsd_net_id);
6165 LIST_HEAD(reaplist);
6166
6167 if (!nfsd_netns_ready(nn))
6168 return count;
6169
6170 spin_lock(&nn->client_lock);
6171 clp = nfsd_find_client(addr, addr_size);
6172 if (clp)
6173 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6174 spin_unlock(&nn->client_lock);
6175 nfsd_reap_locks(&reaplist);
6176 return count;
6177}
6178
6179u64
Jeff Layton285abde2014-07-30 08:27:24 -04006180nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04006181{
6182 u64 count = 0;
6183 struct nfs4_client *clp;
6184 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6185 nfsd_net_id);
6186 LIST_HEAD(reaplist);
6187
6188 if (!nfsd_netns_ready(nn))
6189 return count;
6190
6191 spin_lock(&nn->client_lock);
6192 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6193 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6194 if (max != 0 && count >= max)
6195 break;
6196 }
6197 spin_unlock(&nn->client_lock);
6198 nfsd_reap_locks(&reaplist);
6199 return count;
6200}
6201
Jeff Layton82e05ef2014-07-30 08:27:22 -04006202static u64
6203nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6204 struct list_head *collect,
6205 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006206{
6207 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04006208 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6209 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006210 u64 count = 0;
6211
Jeff Layton82e05ef2014-07-30 08:27:22 -04006212 lockdep_assert_held(&nn->client_lock);
6213
6214 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006215 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04006216 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006217 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04006218 if (collect) {
6219 atomic_inc(&clp->cl_refcount);
6220 list_add(&oop->oo_perclient, collect);
6221 }
6222 }
6223 ++count;
6224 /*
6225 * Despite the fact that these functions deal with
6226 * 64-bit integers for "count", we must ensure that
6227 * it doesn't blow up the clp->cl_refcount. Throw a
6228 * warning if we start to approach INT_MAX here.
6229 */
6230 WARN_ON_ONCE(count == (INT_MAX / 2));
6231 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006232 break;
6233 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04006234 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006235
6236 return count;
6237}
6238
Jeff Layton82e05ef2014-07-30 08:27:22 -04006239static u64
6240nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006241{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006242 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6243
6244 nfsd_print_count(clp, count, "openowners");
6245 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006246}
6247
Jeff Layton82e05ef2014-07-30 08:27:22 -04006248static u64
6249nfsd_collect_client_openowners(struct nfs4_client *clp,
6250 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006251{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006252 return nfsd_foreach_client_openowner(clp, max, collect,
6253 unhash_openowner_locked);
6254}
6255
6256u64
Jeff Layton285abde2014-07-30 08:27:24 -04006257nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006258{
6259 struct nfs4_client *clp;
6260 u64 count = 0;
6261 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6262 nfsd_net_id);
6263
6264 if (!nfsd_netns_ready(nn))
6265 return 0;
6266
6267 spin_lock(&nn->client_lock);
6268 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6269 count += nfsd_print_client_openowners(clp);
6270 spin_unlock(&nn->client_lock);
6271
6272 return count;
6273}
6274
6275static void
6276nfsd_reap_openowners(struct list_head *reaplist)
6277{
6278 struct nfs4_client *clp;
6279 struct nfs4_openowner *oop, *next;
6280
6281 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6282 list_del_init(&oop->oo_perclient);
6283 clp = oop->oo_owner.so_client;
6284 release_openowner(oop);
6285 put_client(clp);
6286 }
6287}
6288
6289u64
Jeff Layton285abde2014-07-30 08:27:24 -04006290nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6291 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006292{
6293 unsigned int count = 0;
6294 struct nfs4_client *clp;
6295 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6296 nfsd_net_id);
6297 LIST_HEAD(reaplist);
6298
6299 if (!nfsd_netns_ready(nn))
6300 return count;
6301
6302 spin_lock(&nn->client_lock);
6303 clp = nfsd_find_client(addr, addr_size);
6304 if (clp)
6305 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6306 spin_unlock(&nn->client_lock);
6307 nfsd_reap_openowners(&reaplist);
6308 return count;
6309}
6310
6311u64
Jeff Layton285abde2014-07-30 08:27:24 -04006312nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006313{
6314 u64 count = 0;
6315 struct nfs4_client *clp;
6316 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6317 nfsd_net_id);
6318 LIST_HEAD(reaplist);
6319
6320 if (!nfsd_netns_ready(nn))
6321 return count;
6322
6323 spin_lock(&nn->client_lock);
6324 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6325 count += nfsd_collect_client_openowners(clp, &reaplist,
6326 max - count);
6327 if (max != 0 && count >= max)
6328 break;
6329 }
6330 spin_unlock(&nn->client_lock);
6331 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006332 return count;
6333}
6334
Bryan Schumaker269de302012-11-29 11:40:42 -05006335static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6336 struct list_head *victims)
6337{
6338 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006339 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6340 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006341 u64 count = 0;
6342
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006343 lockdep_assert_held(&nn->client_lock);
6344
6345 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006346 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006347 if (victims) {
6348 /*
6349 * It's not safe to mess with delegations that have a
6350 * non-zero dl_time. They might have already been broken
6351 * and could be processed by the laundromat outside of
6352 * the state_lock. Just leave them be.
6353 */
6354 if (dp->dl_time != 0)
6355 continue;
6356
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006357 atomic_inc(&clp->cl_refcount);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04006358 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04006359 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006360 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006361 ++count;
6362 /*
6363 * Despite the fact that these functions deal with
6364 * 64-bit integers for "count", we must ensure that
6365 * it doesn't blow up the clp->cl_refcount. Throw a
6366 * warning if we start to approach INT_MAX here.
6367 */
6368 WARN_ON_ONCE(count == (INT_MAX / 2));
6369 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006370 break;
6371 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006372 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006373 return count;
6374}
6375
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006376static u64
6377nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006378{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006379 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006380
6381 nfsd_print_count(clp, count, "delegations");
6382 return count;
6383}
6384
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006385u64
Jeff Layton285abde2014-07-30 08:27:24 -04006386nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006387{
6388 struct nfs4_client *clp;
6389 u64 count = 0;
6390 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6391 nfsd_net_id);
6392
6393 if (!nfsd_netns_ready(nn))
6394 return 0;
6395
6396 spin_lock(&nn->client_lock);
6397 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6398 count += nfsd_print_client_delegations(clp);
6399 spin_unlock(&nn->client_lock);
6400
6401 return count;
6402}
6403
6404static void
6405nfsd_forget_delegations(struct list_head *reaplist)
6406{
6407 struct nfs4_client *clp;
6408 struct nfs4_delegation *dp, *next;
6409
6410 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6411 list_del_init(&dp->dl_recall_lru);
6412 clp = dp->dl_stid.sc_client;
6413 revoke_delegation(dp);
6414 put_client(clp);
6415 }
6416}
6417
6418u64
Jeff Layton285abde2014-07-30 08:27:24 -04006419nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6420 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006421{
6422 u64 count = 0;
6423 struct nfs4_client *clp;
6424 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6425 nfsd_net_id);
6426 LIST_HEAD(reaplist);
6427
6428 if (!nfsd_netns_ready(nn))
6429 return count;
6430
6431 spin_lock(&nn->client_lock);
6432 clp = nfsd_find_client(addr, addr_size);
6433 if (clp)
6434 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6435 spin_unlock(&nn->client_lock);
6436
6437 nfsd_forget_delegations(&reaplist);
6438 return count;
6439}
6440
6441u64
Jeff Layton285abde2014-07-30 08:27:24 -04006442nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006443{
6444 u64 count = 0;
6445 struct nfs4_client *clp;
6446 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6447 nfsd_net_id);
6448 LIST_HEAD(reaplist);
6449
6450 if (!nfsd_netns_ready(nn))
6451 return count;
6452
6453 spin_lock(&nn->client_lock);
6454 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6455 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6456 if (max != 0 && count >= max)
6457 break;
6458 }
6459 spin_unlock(&nn->client_lock);
6460 nfsd_forget_delegations(&reaplist);
6461 return count;
6462}
6463
6464static void
6465nfsd_recall_delegations(struct list_head *reaplist)
6466{
6467 struct nfs4_client *clp;
6468 struct nfs4_delegation *dp, *next;
6469
6470 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6471 list_del_init(&dp->dl_recall_lru);
6472 clp = dp->dl_stid.sc_client;
6473 /*
6474 * We skipped all entries that had a zero dl_time before,
6475 * so we can now reset the dl_time back to 0. If a delegation
6476 * break comes in now, then it won't make any difference since
6477 * we're recalling it either way.
6478 */
6479 spin_lock(&state_lock);
6480 dp->dl_time = 0;
6481 spin_unlock(&state_lock);
6482 nfsd_break_one_deleg(dp);
6483 put_client(clp);
6484 }
6485}
6486
6487u64
Jeff Layton285abde2014-07-30 08:27:24 -04006488nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006489 size_t addr_size)
6490{
6491 u64 count = 0;
6492 struct nfs4_client *clp;
6493 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6494 nfsd_net_id);
6495 LIST_HEAD(reaplist);
6496
6497 if (!nfsd_netns_ready(nn))
6498 return count;
6499
6500 spin_lock(&nn->client_lock);
6501 clp = nfsd_find_client(addr, addr_size);
6502 if (clp)
6503 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6504 spin_unlock(&nn->client_lock);
6505
6506 nfsd_recall_delegations(&reaplist);
6507 return count;
6508}
6509
6510u64
Jeff Layton285abde2014-07-30 08:27:24 -04006511nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006512{
6513 u64 count = 0;
6514 struct nfs4_client *clp, *next;
6515 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6516 nfsd_net_id);
6517 LIST_HEAD(reaplist);
6518
6519 if (!nfsd_netns_ready(nn))
6520 return count;
6521
6522 spin_lock(&nn->client_lock);
6523 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6524 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6525 if (max != 0 && ++count >= max)
6526 break;
6527 }
6528 spin_unlock(&nn->client_lock);
6529 nfsd_recall_delegations(&reaplist);
6530 return count;
6531}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006532#endif /* CONFIG_NFSD_FAULT_INJECTION */
6533
Meelap Shahc2f1a552007-07-17 04:04:39 -07006534/*
6535 * Since the lifetime of a delegation isn't limited to that of an open, a
6536 * client may quite reasonably hang on to a delegation as long as it has
6537 * the inode cached. This becomes an obvious problem the first time a
6538 * client's inode cache approaches the size of the server's total memory.
6539 *
6540 * For now we avoid this problem by imposing a hard limit on the number
6541 * of delegations, which varies according to the server's memory size.
6542 */
6543static void
6544set_max_delegations(void)
6545{
6546 /*
6547 * Allow at most 4 delegations per megabyte of RAM. Quick
6548 * estimates suggest that in the worst case (where every delegation
6549 * is for a different inode), a delegation could take about 1.5K,
6550 * giving a worst case usage of about 6% of memory.
6551 */
6552 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6553}
6554
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006555static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006556{
6557 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6558 int i;
6559
6560 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6561 CLIENT_HASH_SIZE, GFP_KERNEL);
6562 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006563 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006564 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6565 CLIENT_HASH_SIZE, GFP_KERNEL);
6566 if (!nn->unconf_id_hashtbl)
6567 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006568 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6569 SESSION_HASH_SIZE, GFP_KERNEL);
6570 if (!nn->sessionid_hashtbl)
6571 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006572
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006573 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006574 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006575 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006576 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006577 for (i = 0; i < SESSION_HASH_SIZE; i++)
6578 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006579 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006580 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03006581 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03006582 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006583 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03006584 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006585
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006586 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006587 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006588
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006589 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006590
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006591err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03006592 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006593err_unconf_id:
6594 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006595err:
6596 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006597}
6598
6599static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006600nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006601{
6602 int i;
6603 struct nfs4_client *clp = NULL;
6604 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6605
6606 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6607 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6608 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6609 destroy_client(clp);
6610 }
6611 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006612
Kinglong Mee2b905632014-03-26 22:09:30 +08006613 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6614 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6615 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6616 destroy_client(clp);
6617 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006618 }
6619
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006620 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006621 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006622 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006623 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006624}
6625
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006626int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006627nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07006628{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006629 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006630 int ret;
6631
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006632 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006633 if (ret)
6634 return ret;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006635 nn->boot_time = get_seconds();
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04006636 nn->grace_ended = false;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04006637 nn->nfsd4_manager.block_opens = true;
Jeff Laytond4318ac2014-09-12 16:40:21 -04006638 locks_start_grace(net, &nn->nfsd4_manager);
6639 nfsd4_client_tracking_init(net);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006640 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03006641 nn->nfsd4_grace, net);
6642 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006643 return 0;
6644}
6645
6646/* initialization to perform when the nfsd service is started: */
6647
6648int
6649nfs4_state_start(void)
6650{
6651 int ret;
6652
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006653 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006654 if (ret)
6655 return -ENOMEM;
Jeff Layton51a54562015-08-20 07:17:01 -04006656 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05006657 if (laundry_wq == NULL) {
6658 ret = -ENOMEM;
6659 goto out_recovery;
6660 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006661 ret = nfsd4_create_callback_queue();
6662 if (ret)
6663 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006664
Meelap Shahc2f1a552007-07-17 04:04:39 -07006665 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006666
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006667 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006668
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006669out_free_laundry:
6670 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05006671out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006672 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006673}
6674
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006675void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006676nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006680 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006682 cancel_delayed_work_sync(&nn->laundromat_work);
6683 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05006684
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04006686 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006687 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04006689 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04006690 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691 }
Benny Halevycdc97502014-05-30 09:09:30 -04006692 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693 list_for_each_safe(pos, next, &reaplist) {
6694 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006695 list_del_init(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +02006696 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -04006697 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04006698 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006699 }
6700
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006701 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006702 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703}
6704
6705void
6706nfs4_state_shutdown(void)
6707{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006708 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006709 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006711
6712static void
6713get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6714{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006715 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6716 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006717}
6718
6719static void
6720put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6721{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006722 if (cstate->minorversion) {
6723 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6724 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6725 }
6726}
6727
6728void
6729clear_current_stateid(struct nfsd4_compound_state *cstate)
6730{
6731 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006732}
6733
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006734/*
6735 * functions to set current state id
6736 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006737void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006738nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6739{
6740 put_stateid(cstate, &odp->od_stateid);
6741}
6742
6743void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006744nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6745{
6746 put_stateid(cstate, &open->op_stateid);
6747}
6748
6749void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006750nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6751{
6752 put_stateid(cstate, &close->cl_stateid);
6753}
6754
6755void
6756nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6757{
6758 put_stateid(cstate, &lock->lk_resp_stateid);
6759}
6760
6761/*
6762 * functions to consume current state id
6763 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006764
6765void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006766nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6767{
6768 get_stateid(cstate, &odp->od_stateid);
6769}
6770
6771void
6772nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6773{
6774 get_stateid(cstate, &drp->dr_stateid);
6775}
6776
6777void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006778nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6779{
6780 get_stateid(cstate, &fsp->fr_stateid);
6781}
6782
6783void
6784nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6785{
6786 get_stateid(cstate, &setattr->sa_stateid);
6787}
6788
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006789void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006790nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6791{
6792 get_stateid(cstate, &close->cl_stateid);
6793}
6794
6795void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006796nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006797{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006798 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006799}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006800
6801void
6802nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6803{
6804 get_stateid(cstate, &read->rd_stateid);
6805}
6806
6807void
6808nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6809{
6810 get_stateid(cstate, &write->wr_stateid);
6811}