blob: 1b39edf10b67da594f6a9d8deea8ba29e49794d2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
Dave Hansenaceaf782008-02-15 14:37:31 -080035#include <linux/file.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020036#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070038#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070039#include <linux/swap.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
Chuck Lever7df302f2012-05-29 13:56:37 -040041#include <linux/ratelimit.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050042#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton59766872013-02-04 12:50:00 -050043#include <linux/sunrpc/addr.h>
Daniel Borkmann87545892014-12-10 16:33:11 +010044#include <linux/jhash.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020045#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040046#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050047#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040048#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040050#include "netns.h"
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020051#include "pnfs.h"
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define NFSDDBG_FACILITY NFSDDBG_PROC
54
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050055#define all_ones {{~0,~0},~0}
56static const stateid_t one_stateid = {
57 .si_generation = ~0,
58 .si_opaque = all_ones,
59};
60static const stateid_t zero_stateid = {
61 /* all fields zero */
62};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010063static const stateid_t currentstateid = {
64 .si_generation = 1,
65};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050066
Andy Adamsonec6b5d72009-04-03 08:28:28 +030067static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070068
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050069#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
70#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010071#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040074static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040075static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
J. Bruce Fields8b671b82009-02-22 14:51:34 -080077/* Locking: */
78
J. Bruce Fields8b671b82009-02-22 14:51:34 -080079/*
80 * Currently used for the del_recall_lru and file hash table. In an
81 * effort to decrease the scope of the client_mutex, this spinlock may
82 * eventually cover more:
83 */
Benny Halevycdc97502014-05-30 09:09:30 -040084static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080085
Jeff Laytonb401be222014-07-29 21:34:33 -040086/*
87 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
88 * the refcount on the open stateid to drop.
89 */
90static DECLARE_WAIT_QUEUE_HEAD(close_wq);
91
Christoph Hellwigabf11352014-05-21 07:43:03 -070092static struct kmem_cache *openowner_slab;
93static struct kmem_cache *lockowner_slab;
94static struct kmem_cache *file_slab;
95static struct kmem_cache *stateid_slab;
96static struct kmem_cache *deleg_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +020097static struct kmem_cache *odstate_slab;
NeilBrowne60d4392005-06-23 22:03:01 -070098
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -040099static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800100
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200101static struct nfsd4_callback_ops nfsd4_cb_recall_ops;
102
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400103static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800104{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400105 return ses->se_flags & NFS4_SESSION_DEAD;
106}
107
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400108static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
109{
110 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400111 return nfserr_jukebox;
112 ses->se_flags |= NFS4_SESSION_DEAD;
113 return nfs_ok;
114}
115
J. Bruce Fields221a6872013-04-01 22:23:49 -0400116static bool is_client_expired(struct nfs4_client *clp)
117{
118 return clp->cl_time == 0;
119}
120
J. Bruce Fields221a6872013-04-01 22:23:49 -0400121static __be32 get_client_locked(struct nfs4_client *clp)
122{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400123 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
124
125 lockdep_assert_held(&nn->client_lock);
126
J. Bruce Fields221a6872013-04-01 22:23:49 -0400127 if (is_client_expired(clp))
128 return nfserr_expired;
129 atomic_inc(&clp->cl_refcount);
130 return nfs_ok;
131}
132
133/* must be called under the client_lock */
134static inline void
135renew_client_locked(struct nfs4_client *clp)
136{
137 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
138
139 if (is_client_expired(clp)) {
140 WARN_ON(1);
141 printk("%s: client (clientid %08x/%08x) already expired\n",
142 __func__,
143 clp->cl_clientid.cl_boot,
144 clp->cl_clientid.cl_id);
145 return;
146 }
147
148 dprintk("renewing client (clientid %08x/%08x)\n",
149 clp->cl_clientid.cl_boot,
150 clp->cl_clientid.cl_id);
151 list_move_tail(&clp->cl_lru, &nn->client_lru);
152 clp->cl_time = get_seconds();
153}
154
Fengguang Wuba138432013-04-16 22:14:15 -0400155static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400156{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400157 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
158
159 lockdep_assert_held(&nn->client_lock);
160
J. Bruce Fields221a6872013-04-01 22:23:49 -0400161 if (!atomic_dec_and_test(&clp->cl_refcount))
162 return;
163 if (!is_client_expired(clp))
164 renew_client_locked(clp);
165}
166
Jeff Layton4b24ca72014-06-30 11:48:44 -0400167static void put_client_renew(struct nfs4_client *clp)
168{
169 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
170
Jeff Laytond6c249b2014-07-08 14:02:50 -0400171 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
172 return;
173 if (!is_client_expired(clp))
174 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400175 spin_unlock(&nn->client_lock);
176}
177
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400178static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
179{
180 __be32 status;
181
182 if (is_session_dead(ses))
183 return nfserr_badsession;
184 status = get_client_locked(ses->se_client);
185 if (status)
186 return status;
187 atomic_inc(&ses->se_ref);
188 return nfs_ok;
189}
190
191static void nfsd4_put_session_locked(struct nfsd4_session *ses)
192{
193 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400194 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
195
196 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400197
198 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
199 free_session(ses);
200 put_client_renew_locked(clp);
201}
202
203static void nfsd4_put_session(struct nfsd4_session *ses)
204{
205 struct nfs4_client *clp = ses->se_client;
206 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
207
208 spin_lock(&nn->client_lock);
209 nfsd4_put_session_locked(ses);
210 spin_unlock(&nn->client_lock);
211}
212
Kinglong Meeb5971af2014-08-22 10:18:43 -0400213static inline struct nfs4_stateowner *
214nfs4_get_stateowner(struct nfs4_stateowner *sop)
215{
216 atomic_inc(&sop->so_count);
217 return sop;
218}
219
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400220static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400221same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400222{
223 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400224 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400225}
226
227static struct nfs4_openowner *
228find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400229 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400230{
231 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400232
Trond Myklebustd4f04892014-07-29 21:34:36 -0400233 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400234
Trond Myklebustd4f04892014-07-29 21:34:36 -0400235 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
236 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400237 if (!so->so_is_open_owner)
238 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400239 if (same_owner_str(so, &open->op_owner))
240 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400241 }
242 return NULL;
243}
244
245static struct nfs4_openowner *
246find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400247 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400248{
249 struct nfs4_openowner *oo;
250
Trond Myklebustd4f04892014-07-29 21:34:36 -0400251 spin_lock(&clp->cl_lock);
252 oo = find_openstateowner_str_locked(hashval, open, clp);
253 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400254 return oo;
255}
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257static inline u32
258opaque_hashval(const void *ptr, int nbytes)
259{
260 unsigned char *cptr = (unsigned char *) ptr;
261
262 u32 x = 0;
263 while (nbytes--) {
264 x *= 37;
265 x += *cptr++;
266 }
267 return x;
268}
269
Jeff Layton5b095e92014-10-23 08:01:02 -0400270static void nfsd4_free_file_rcu(struct rcu_head *rcu)
J. Bruce Fields32513b42011-10-13 16:00:16 -0400271{
Jeff Layton5b095e92014-10-23 08:01:02 -0400272 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
273
274 kmem_cache_free(file_slab, fp);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400275}
276
Christoph Hellwige6ba76e2014-08-14 08:50:16 +0200277void
NeilBrown13cd2182005-06-23 22:03:10 -0700278put_nfs4_file(struct nfs4_file *fi)
279{
Jeff Layton02e12152014-07-16 10:31:57 -0400280 might_lock(&state_lock);
281
Benny Halevycdc97502014-05-30 09:09:30 -0400282 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton5b095e92014-10-23 08:01:02 -0400283 hlist_del_rcu(&fi->fi_hash);
Benny Halevycdc97502014-05-30 09:09:30 -0400284 spin_unlock(&state_lock);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200285 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
Jeff Layton5b095e92014-10-23 08:01:02 -0400286 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
287 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800288 }
NeilBrown13cd2182005-06-23 22:03:10 -0700289}
290
Trond Myklebustde186432014-07-10 14:07:26 -0400291static struct file *
292__nfs4_get_fd(struct nfs4_file *f, int oflag)
293{
294 if (f->fi_fds[oflag])
295 return get_file(f->fi_fds[oflag]);
296 return NULL;
297}
298
299static struct file *
300find_writeable_file_locked(struct nfs4_file *f)
301{
302 struct file *ret;
303
304 lockdep_assert_held(&f->fi_lock);
305
306 ret = __nfs4_get_fd(f, O_WRONLY);
307 if (!ret)
308 ret = __nfs4_get_fd(f, O_RDWR);
309 return ret;
310}
311
312static struct file *
313find_writeable_file(struct nfs4_file *f)
314{
315 struct file *ret;
316
317 spin_lock(&f->fi_lock);
318 ret = find_writeable_file_locked(f);
319 spin_unlock(&f->fi_lock);
320
321 return ret;
322}
323
324static struct file *find_readable_file_locked(struct nfs4_file *f)
325{
326 struct file *ret;
327
328 lockdep_assert_held(&f->fi_lock);
329
330 ret = __nfs4_get_fd(f, O_RDONLY);
331 if (!ret)
332 ret = __nfs4_get_fd(f, O_RDWR);
333 return ret;
334}
335
336static struct file *
337find_readable_file(struct nfs4_file *f)
338{
339 struct file *ret;
340
341 spin_lock(&f->fi_lock);
342 ret = find_readable_file_locked(f);
343 spin_unlock(&f->fi_lock);
344
345 return ret;
346}
347
Christoph Hellwig4d227fc2014-08-17 07:40:00 -0500348struct file *
Trond Myklebustde186432014-07-10 14:07:26 -0400349find_any_file(struct nfs4_file *f)
350{
351 struct file *ret;
352
353 spin_lock(&f->fi_lock);
354 ret = __nfs4_get_fd(f, O_RDWR);
355 if (!ret) {
356 ret = __nfs4_get_fd(f, O_WRONLY);
357 if (!ret)
358 ret = __nfs4_get_fd(f, O_RDONLY);
359 }
360 spin_unlock(&f->fi_lock);
361 return ret;
362}
363
Trond Myklebust02a35082014-07-25 07:34:22 -0400364static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800365unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700366
367/*
368 * Open owner state (share locks)
369 */
370
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500371/* hash tables for lock and open owners */
372#define OWNER_HASH_BITS 8
373#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
374#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700375
Trond Myklebustd4f04892014-07-29 21:34:36 -0400376static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400377{
378 unsigned int ret;
379
380 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500381 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400382}
NeilBrownef0f3392006-04-10 22:55:41 -0700383
NeilBrownef0f3392006-04-10 22:55:41 -0700384/* hash table for nfs4_file */
385#define FILE_HASH_BITS 8
386#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800387
Trond Myklebustca943212014-07-23 16:17:39 -0400388static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400389{
Trond Myklebustca943212014-07-23 16:17:39 -0400390 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
391}
392
393static unsigned int file_hashval(struct knfsd_fh *fh)
394{
395 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
396}
397
Jeff Layton89876f82013-04-02 09:01:59 -0400398static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700399
Jeff Layton12659652014-07-10 14:07:28 -0400400static void
401__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400402{
Jeff Layton7214e862014-07-10 14:07:33 -0400403 lockdep_assert_held(&fp->fi_lock);
404
Jeff Layton12659652014-07-10 14:07:28 -0400405 if (access & NFS4_SHARE_ACCESS_WRITE)
406 atomic_inc(&fp->fi_access[O_WRONLY]);
407 if (access & NFS4_SHARE_ACCESS_READ)
408 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400409}
410
Jeff Layton12659652014-07-10 14:07:28 -0400411static __be32
412nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400413{
Jeff Layton7214e862014-07-10 14:07:33 -0400414 lockdep_assert_held(&fp->fi_lock);
415
Jeff Layton12659652014-07-10 14:07:28 -0400416 /* Does this access mode make sense? */
417 if (access & ~NFS4_SHARE_ACCESS_BOTH)
418 return nfserr_inval;
419
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400420 /* Does it conflict with a deny mode already set? */
421 if ((access & fp->fi_share_deny) != 0)
422 return nfserr_share_denied;
423
Jeff Layton12659652014-07-10 14:07:28 -0400424 __nfs4_file_get_access(fp, access);
425 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400426}
427
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400428static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
429{
430 /* Common case is that there is no deny mode. */
431 if (deny) {
432 /* Does this deny mode make sense? */
433 if (deny & ~NFS4_SHARE_DENY_BOTH)
434 return nfserr_inval;
435
436 if ((deny & NFS4_SHARE_DENY_READ) &&
437 atomic_read(&fp->fi_access[O_RDONLY]))
438 return nfserr_share_denied;
439
440 if ((deny & NFS4_SHARE_DENY_WRITE) &&
441 atomic_read(&fp->fi_access[O_WRONLY]))
442 return nfserr_share_denied;
443 }
444 return nfs_ok;
445}
446
J. Bruce Fields998db522010-08-07 09:21:41 -0400447static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400448{
Trond Myklebustde186432014-07-10 14:07:26 -0400449 might_lock(&fp->fi_lock);
450
451 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
452 struct file *f1 = NULL;
453 struct file *f2 = NULL;
454
Jeff Layton6d338b52014-07-10 14:07:29 -0400455 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400456 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400457 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400458 spin_unlock(&fp->fi_lock);
459 if (f1)
460 fput(f1);
461 if (f2)
462 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400463 }
464}
465
Jeff Layton12659652014-07-10 14:07:28 -0400466static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400467{
Jeff Layton12659652014-07-10 14:07:28 -0400468 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
469
470 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400471 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400472 if (access & NFS4_SHARE_ACCESS_READ)
473 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400474}
475
Sachin Bhamare8287f002015-04-27 14:50:14 +0200476/*
477 * Allocate a new open/delegation state counter. This is needed for
478 * pNFS for proper return on close semantics.
479 *
480 * Note that we only allocate it for pNFS-enabled exports, otherwise
481 * all pointers to struct nfs4_clnt_odstate are always NULL.
482 */
483static struct nfs4_clnt_odstate *
484alloc_clnt_odstate(struct nfs4_client *clp)
485{
486 struct nfs4_clnt_odstate *co;
487
488 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
489 if (co) {
490 co->co_client = clp;
491 atomic_set(&co->co_odcount, 1);
492 }
493 return co;
494}
495
496static void
497hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
498{
499 struct nfs4_file *fp = co->co_file;
500
501 lockdep_assert_held(&fp->fi_lock);
502 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
503}
504
505static inline void
506get_clnt_odstate(struct nfs4_clnt_odstate *co)
507{
508 if (co)
509 atomic_inc(&co->co_odcount);
510}
511
512static void
513put_clnt_odstate(struct nfs4_clnt_odstate *co)
514{
515 struct nfs4_file *fp;
516
517 if (!co)
518 return;
519
520 fp = co->co_file;
521 if (atomic_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
522 list_del(&co->co_perfile);
523 spin_unlock(&fp->fi_lock);
524
525 nfsd4_return_all_file_layouts(co->co_client, fp);
526 kmem_cache_free(odstate_slab, co);
527 }
528}
529
530static struct nfs4_clnt_odstate *
531find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
532{
533 struct nfs4_clnt_odstate *co;
534 struct nfs4_client *cl;
535
536 if (!new)
537 return NULL;
538
539 cl = new->co_client;
540
541 spin_lock(&fp->fi_lock);
542 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
543 if (co->co_client == cl) {
544 get_clnt_odstate(co);
545 goto out;
546 }
547 }
548 co = new;
549 co->co_file = fp;
550 hash_clnt_odstate_locked(new);
551out:
552 spin_unlock(&fp->fi_lock);
553 return co;
554}
555
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200556struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
Trond Myklebust60116952014-07-29 21:34:06 -0400557 struct kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400558{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500559 struct nfs4_stid *stid;
560 int new_id;
561
Trond Myklebustf8338832014-07-25 07:34:19 -0400562 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500563 if (!stid)
564 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400565
Jeff Layton4770d722014-07-29 21:34:10 -0400566 idr_preload(GFP_KERNEL);
567 spin_lock(&cl->cl_lock);
568 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
569 spin_unlock(&cl->cl_lock);
570 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700571 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500572 goto out_free;
573 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500574 stid->sc_stateid.si_opaque.so_id = new_id;
575 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
576 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400577 atomic_set(&stid->sc_count, 1);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500578
J. Bruce Fields996e0932011-10-17 11:14:48 -0400579 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500580 * It shouldn't be a problem to reuse an opaque stateid value.
581 * I don't think it is for 4.1. But with 4.0 I worry that, for
582 * example, a stray write retransmission could be accepted by
583 * the server when it should have been rejected. Therefore,
584 * adopt a trick from the sctp code to attempt to maximize the
585 * amount of time until an id is reused, by ensuring they always
586 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400587 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500588 return stid;
589out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800590 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500591 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400592}
593
Jeff Laytonb49e0842014-07-29 21:34:11 -0400594static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400595{
Trond Myklebust60116952014-07-29 21:34:06 -0400596 struct nfs4_stid *stid;
597 struct nfs4_ol_stateid *stp;
598
599 stid = nfs4_alloc_stid(clp, stateid_slab);
600 if (!stid)
601 return NULL;
602
603 stp = openlockstateid(stid);
604 stp->st_stid.sc_free = nfs4_free_ol_stateid;
605 return stp;
606}
607
608static void nfs4_free_deleg(struct nfs4_stid *stid)
609{
Trond Myklebust60116952014-07-29 21:34:06 -0400610 kmem_cache_free(deleg_slab, stid);
611 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400612}
613
NeilBrown6282cd52014-06-04 17:39:26 +1000614/*
615 * When we recall a delegation, we should be careful not to hand it
616 * out again straight away.
617 * To ensure this we keep a pair of bloom filters ('new' and 'old')
618 * in which the filehandles of recalled delegations are "stored".
619 * If a filehandle appear in either filter, a delegation is blocked.
620 * When a delegation is recalled, the filehandle is stored in the "new"
621 * filter.
622 * Every 30 seconds we swap the filters and clear the "new" one,
623 * unless both are empty of course.
624 *
625 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
626 * low 3 bytes as hash-table indices.
627 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400628 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000629 * is used to manage concurrent access. Testing does not need the lock
630 * except when swapping the two filters.
631 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400632static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000633static struct bloom_pair {
634 int entries, old_entries;
635 time_t swap_time;
636 int new; /* index into 'set' */
637 DECLARE_BITMAP(set[2], 256);
638} blocked_delegations;
639
640static int delegation_blocked(struct knfsd_fh *fh)
641{
642 u32 hash;
643 struct bloom_pair *bd = &blocked_delegations;
644
645 if (bd->entries == 0)
646 return 0;
647 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400648 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000649 if (seconds_since_boot() - bd->swap_time > 30) {
650 bd->entries -= bd->old_entries;
651 bd->old_entries = bd->entries;
652 memset(bd->set[bd->new], 0,
653 sizeof(bd->set[0]));
654 bd->new = 1-bd->new;
655 bd->swap_time = seconds_since_boot();
656 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400657 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000658 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100659 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000660 if (test_bit(hash&255, bd->set[0]) &&
661 test_bit((hash>>8)&255, bd->set[0]) &&
662 test_bit((hash>>16)&255, bd->set[0]))
663 return 1;
664
665 if (test_bit(hash&255, bd->set[1]) &&
666 test_bit((hash>>8)&255, bd->set[1]) &&
667 test_bit((hash>>16)&255, bd->set[1]))
668 return 1;
669
670 return 0;
671}
672
673static void block_delegations(struct knfsd_fh *fh)
674{
675 u32 hash;
676 struct bloom_pair *bd = &blocked_delegations;
677
Daniel Borkmann87545892014-12-10 16:33:11 +0100678 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000679
Jeff Laytonf54fe962014-07-25 07:34:26 -0400680 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000681 __set_bit(hash&255, bd->set[bd->new]);
682 __set_bit((hash>>8)&255, bd->set[bd->new]);
683 __set_bit((hash>>16)&255, bd->set[bd->new]);
684 if (bd->entries == 0)
685 bd->swap_time = seconds_since_boot();
686 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400687 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000688}
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690static struct nfs4_delegation *
Sachin Bhamare8287f002015-04-27 14:50:14 +0200691alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh,
692 struct nfs4_clnt_odstate *odstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400695 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400698 n = atomic_long_inc_return(&num_delegations);
699 if (n < 0 || n > max_delegations)
700 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000701 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400702 goto out_dec;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400703 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700704 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400705 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400706
707 dp->dl_stid.sc_free = nfs4_free_deleg;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400708 /*
709 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400710 * meaning of seqid 0 isn't meaningful, really, but let's avoid
711 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400712 */
713 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700714 INIT_LIST_HEAD(&dp->dl_perfile);
715 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 INIT_LIST_HEAD(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200717 dp->dl_clnt_odstate = odstate;
718 get_clnt_odstate(odstate);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400719 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200720 dp->dl_retries = 1;
721 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200722 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400724out_dec:
725 atomic_long_dec(&num_delegations);
726 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
729void
Trond Myklebust60116952014-07-29 21:34:06 -0400730nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Trond Myklebust11b91642014-07-29 21:34:08 -0400732 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400733 struct nfs4_client *clp = s->sc_client;
734
Jeff Layton4770d722014-07-29 21:34:10 -0400735 might_lock(&clp->cl_lock);
736
Jeff Laytonb401be222014-07-29 21:34:33 -0400737 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
738 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400739 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400740 }
Trond Myklebust60116952014-07-29 21:34:06 -0400741 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400742 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400743 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400744 if (fp)
745 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500748static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Jeff Layton6bcc0342014-08-09 10:22:40 -0400750 struct file *filp = NULL;
Jeff Layton417c6622014-07-21 09:34:57 -0400751
Jeff Layton6bcc0342014-08-09 10:22:40 -0400752 spin_lock(&fp->fi_lock);
Jeff Layton67db1032014-12-13 09:11:40 -0500753 if (fp->fi_deleg_file && --fp->fi_delegees == 0)
Jeff Layton6bcc0342014-08-09 10:22:40 -0400754 swap(filp, fp->fi_deleg_file);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400755 spin_unlock(&fp->fi_lock);
756
757 if (filp) {
Christoph Hellwig2ab99ee2015-01-21 19:14:02 +0100758 vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400759 fput(filp);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200763void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400764{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500765 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400766}
767
Benny Halevy931ee562014-05-30 09:09:27 -0400768static void
769hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
770{
Benny Halevycdc97502014-05-30 09:09:30 -0400771 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400772 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400773
Trond Myklebust67cb1272014-07-29 21:34:17 -0400774 atomic_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400775 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400776 list_add(&dp->dl_perfile, &fp->fi_delegations);
777 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
778}
779
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400780static bool
Jeff Layton42690672014-07-25 07:34:20 -0400781unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Trond Myklebust11b91642014-07-29 21:34:08 -0400783 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400784
Jeff Layton42690672014-07-25 07:34:20 -0400785 lockdep_assert_held(&state_lock);
786
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400787 if (list_empty(&dp->dl_perfile))
788 return false;
789
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400790 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400791 /* Ensure that deleg break won't try to requeue it */
792 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400793 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400794 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400796 list_del_init(&dp->dl_perfile);
797 spin_unlock(&fp->fi_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400798 return true;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400799}
800
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400801static void destroy_delegation(struct nfs4_delegation *dp)
802{
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400803 bool unhashed;
804
Jeff Layton42690672014-07-25 07:34:20 -0400805 spin_lock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400806 unhashed = unhash_delegation_locked(dp);
Jeff Layton42690672014-07-25 07:34:20 -0400807 spin_unlock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400808 if (unhashed) {
809 put_clnt_odstate(dp->dl_clnt_odstate);
810 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
811 nfs4_put_stid(&dp->dl_stid);
812 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400813}
814
815static void revoke_delegation(struct nfs4_delegation *dp)
816{
817 struct nfs4_client *clp = dp->dl_stid.sc_client;
818
Jeff Layton2d4a5322014-07-25 07:34:21 -0400819 WARN_ON(!list_empty(&dp->dl_recall_lru));
820
Sachin Bhamare8287f002015-04-27 14:50:14 +0200821 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -0400822 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
823
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400824 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400825 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400826 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400827 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400828 spin_lock(&clp->cl_lock);
829 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
830 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400831 }
832}
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834/*
835 * SETCLIENTID state
836 */
837
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400838static unsigned int clientid_hashval(u32 id)
839{
840 return id & CLIENT_HASH_MASK;
841}
842
843static unsigned int clientstr_hashval(const char *name)
844{
845 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
846}
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400849 * We store the NONE, READ, WRITE, and BOTH bits separately in the
850 * st_{access,deny}_bmap field of the stateid, in order to track not
851 * only what share bits are currently in force, but also what
852 * combinations of share bits previous opens have used. This allows us
853 * to enforce the recommendation of rfc 3530 14.2.19 that the server
854 * return an error if the client attempt to downgrade to a combination
855 * of share bits not explicable by closing some of its previous opens.
856 *
857 * XXX: This enforcement is actually incomplete, since we don't keep
858 * track of access/deny bit combinations; so, e.g., we allow:
859 *
860 * OPEN allow read, deny write
861 * OPEN allow both, deny none
862 * DOWNGRADE allow read, deny none
863 *
864 * which we should reject.
865 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400866static unsigned int
867bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400868 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400869 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400870
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400871 for (i = 1; i < 4; i++) {
872 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400873 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400874 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400875 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400876}
877
Jeff Layton82c5ff12012-05-11 09:45:13 -0400878/* set share access for a given stateid */
879static inline void
880set_access(u32 access, struct nfs4_ol_stateid *stp)
881{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400882 unsigned char mask = 1 << access;
883
884 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
885 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400886}
887
888/* clear share access for a given stateid */
889static inline void
890clear_access(u32 access, struct nfs4_ol_stateid *stp)
891{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400892 unsigned char mask = 1 << access;
893
894 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
895 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400896}
897
898/* test whether a given stateid has access */
899static inline bool
900test_access(u32 access, struct nfs4_ol_stateid *stp)
901{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400902 unsigned char mask = 1 << access;
903
904 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400905}
906
Jeff Laytonce0fc432012-05-11 09:45:14 -0400907/* set share deny for a given stateid */
908static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400909set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400910{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400911 unsigned char mask = 1 << deny;
912
913 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
914 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400915}
916
917/* clear share deny for a given stateid */
918static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400919clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400920{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400921 unsigned char mask = 1 << deny;
922
923 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
924 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400925}
926
927/* test whether a given stateid is denying specific access */
928static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400929test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400930{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400931 unsigned char mask = 1 << deny;
932
933 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400934}
935
936static int nfs4_access_to_omode(u32 access)
937{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400938 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400939 case NFS4_SHARE_ACCESS_READ:
940 return O_RDONLY;
941 case NFS4_SHARE_ACCESS_WRITE:
942 return O_WRONLY;
943 case NFS4_SHARE_ACCESS_BOTH:
944 return O_RDWR;
945 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500946 WARN_ON_ONCE(1);
947 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400948}
949
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400950/*
951 * A stateid that had a deny mode associated with it is being released
952 * or downgraded. Recalculate the deny mode on the file.
953 */
954static void
955recalculate_deny_mode(struct nfs4_file *fp)
956{
957 struct nfs4_ol_stateid *stp;
958
959 spin_lock(&fp->fi_lock);
960 fp->fi_share_deny = 0;
961 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
962 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
963 spin_unlock(&fp->fi_lock);
964}
965
966static void
967reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
968{
969 int i;
970 bool change = false;
971
972 for (i = 1; i < 4; i++) {
973 if ((i & deny) != i) {
974 change = true;
975 clear_deny(i, stp);
976 }
977 }
978
979 /* Recalculate per-file deny mode if there was a change */
980 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -0400981 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400982}
983
Jeff Layton82c5ff12012-05-11 09:45:13 -0400984/* release all access and file references for a given stateid */
985static void
986release_all_access(struct nfs4_ol_stateid *stp)
987{
988 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -0400989 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400990
991 if (fp && stp->st_deny_bmap != 0)
992 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400993
994 for (i = 1; i < 4; i++) {
995 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -0400996 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400997 clear_access(i, stp);
998 }
999}
1000
Kinglong Meed50ffde2015-07-16 12:05:07 +08001001static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1002{
1003 kfree(sop->so_owner.data);
1004 sop->so_ops->so_free(sop);
1005}
1006
Jeff Layton6b180f02014-07-29 21:34:26 -04001007static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1008{
Jeff Laytona819ecc2014-07-29 21:34:38 -04001009 struct nfs4_client *clp = sop->so_client;
1010
1011 might_lock(&clp->cl_lock);
1012
1013 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -04001014 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001015 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -04001016 spin_unlock(&clp->cl_lock);
Kinglong Meed50ffde2015-07-16 12:05:07 +08001017 nfs4_free_stateowner(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -04001018}
1019
Jeff Laytone8568732015-08-24 12:41:47 -04001020static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001021{
Trond Myklebust11b91642014-07-29 21:34:08 -04001022 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -04001023
Jeff Layton1c755dc2014-07-29 21:34:12 -04001024 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1025
Jeff Laytone8568732015-08-24 12:41:47 -04001026 if (list_empty(&stp->st_perfile))
1027 return false;
1028
Trond Myklebust1d31a252014-07-10 14:07:25 -04001029 spin_lock(&fp->fi_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001030 list_del_init(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -04001031 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001032 list_del(&stp->st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001033 return true;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001034}
1035
Trond Myklebust60116952014-07-29 21:34:06 -04001036static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001037{
Trond Myklebust60116952014-07-29 21:34:06 -04001038 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -04001039
Sachin Bhamare8287f002015-04-27 14:50:14 +02001040 put_clnt_odstate(stp->st_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04001041 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -04001042 if (stp->st_stateowner)
1043 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -04001044 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001045}
1046
Jeff Laytonb49e0842014-07-29 21:34:11 -04001047static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001048{
Jeff Laytonb49e0842014-07-29 21:34:11 -04001049 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1050 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001051 struct file *file;
1052
Jeff Laytonb49e0842014-07-29 21:34:11 -04001053 file = find_any_file(stp->st_stid.sc_file);
1054 if (file)
1055 filp_close(file, (fl_owner_t)lo);
1056 nfs4_free_ol_stateid(stid);
1057}
1058
Jeff Layton2c41beb2014-07-29 21:34:41 -04001059/*
1060 * Put the persistent reference to an already unhashed generic stateid, while
1061 * holding the cl_lock. If it's the last reference, then put it onto the
1062 * reaplist for later destruction.
1063 */
1064static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1065 struct list_head *reaplist)
1066{
1067 struct nfs4_stid *s = &stp->st_stid;
1068 struct nfs4_client *clp = s->sc_client;
1069
1070 lockdep_assert_held(&clp->cl_lock);
1071
1072 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1073
1074 if (!atomic_dec_and_test(&s->sc_count)) {
1075 wake_up_all(&close_wq);
1076 return;
1077 }
1078
1079 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1080 list_add(&stp->st_locks, reaplist);
1081}
1082
Jeff Laytone8568732015-08-24 12:41:47 -04001083static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Layton3c1c9952014-07-29 21:34:39 -04001084{
1085 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1086
1087 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
1088
1089 list_del_init(&stp->st_locks);
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001090 nfs4_unhash_stid(&stp->st_stid);
Jeff Laytone8568732015-08-24 12:41:47 -04001091 return unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001092}
1093
Jeff Layton5adfd882014-07-29 21:34:31 -04001094static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001095{
Jeff Layton1c755dc2014-07-29 21:34:12 -04001096 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001097 bool unhashed;
Jeff Layton1c755dc2014-07-29 21:34:12 -04001098
1099 spin_lock(&oo->oo_owner.so_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001100 unhashed = unhash_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001101 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001102 if (unhashed)
1103 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001104}
1105
Trond Myklebustc58c6612014-07-29 21:34:35 -04001106static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001107{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001108 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001109
Trond Myklebustd4f04892014-07-29 21:34:36 -04001110 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001111
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001112 list_del_init(&lo->lo_owner.so_strhash);
1113}
1114
Jeff Layton2c41beb2014-07-29 21:34:41 -04001115/*
1116 * Free a list of generic stateids that were collected earlier after being
1117 * fully unhashed.
1118 */
1119static void
1120free_ol_stateid_reaplist(struct list_head *reaplist)
1121{
1122 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001123 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001124
1125 might_sleep();
1126
1127 while (!list_empty(reaplist)) {
1128 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1129 st_locks);
1130 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001131 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001132 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001133 if (fp)
1134 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001135 }
1136}
1137
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001138static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001139{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001140 struct nfs4_client *clp = lo->lo_owner.so_client;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001141 struct nfs4_ol_stateid *stp;
1142 struct list_head reaplist;
1143
1144 INIT_LIST_HEAD(&reaplist);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001145
Trond Myklebustd4f04892014-07-29 21:34:36 -04001146 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001147 unhash_lockowner_locked(lo);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001148 while (!list_empty(&lo->lo_owner.so_stateids)) {
1149 stp = list_first_entry(&lo->lo_owner.so_stateids,
1150 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001151 WARN_ON(!unhash_lock_stateid(stp));
Jeff Layton2c41beb2014-07-29 21:34:41 -04001152 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001153 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001154 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001155 free_ol_stateid_reaplist(&reaplist);
Jeff Layton6b180f02014-07-29 21:34:26 -04001156 nfs4_put_stateowner(&lo->lo_owner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001157}
1158
Jeff Laytond83017f2014-07-29 21:34:42 -04001159static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1160 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001161{
1162 struct nfs4_ol_stateid *stp;
1163
Jeff Laytone8568732015-08-24 12:41:47 -04001164 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1165
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001166 while (!list_empty(&open_stp->st_locks)) {
1167 stp = list_entry(open_stp->st_locks.next,
1168 struct nfs4_ol_stateid, st_locks);
Jeff Laytone8568732015-08-24 12:41:47 -04001169 WARN_ON(!unhash_lock_stateid(stp));
Jeff Laytond83017f2014-07-29 21:34:42 -04001170 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001171 }
1172}
1173
Jeff Laytone8568732015-08-24 12:41:47 -04001174static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
Jeff Laytond83017f2014-07-29 21:34:42 -04001175 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001176{
Jeff Laytone8568732015-08-24 12:41:47 -04001177 bool unhashed;
1178
Jeff Layton2c41beb2014-07-29 21:34:41 -04001179 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1180
Jeff Laytone8568732015-08-24 12:41:47 -04001181 unhashed = unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001182 release_open_stateid_locks(stp, reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04001183 return unhashed;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001184}
1185
1186static void release_open_stateid(struct nfs4_ol_stateid *stp)
1187{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001188 LIST_HEAD(reaplist);
1189
1190 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001191 if (unhash_open_stateid(stp, &reaplist))
1192 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001193 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1194 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001195}
1196
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001197static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001198{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001199 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001200
Trond Myklebustd4f04892014-07-29 21:34:36 -04001201 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001202
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001203 list_del_init(&oo->oo_owner.so_strhash);
1204 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001205}
1206
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001207static void release_last_closed_stateid(struct nfs4_openowner *oo)
1208{
Jeff Layton217526e2014-07-30 08:27:11 -04001209 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1210 nfsd_net_id);
1211 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001212
Jeff Layton217526e2014-07-30 08:27:11 -04001213 spin_lock(&nn->client_lock);
1214 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001215 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001216 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001217 oo->oo_last_closed_stid = NULL;
1218 }
Jeff Layton217526e2014-07-30 08:27:11 -04001219 spin_unlock(&nn->client_lock);
1220 if (s)
1221 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001222}
1223
Jeff Layton2c41beb2014-07-29 21:34:41 -04001224static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001225{
1226 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001227 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001228 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001229
Jeff Layton2c41beb2014-07-29 21:34:41 -04001230 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001231
Trond Myklebustd4f04892014-07-29 21:34:36 -04001232 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001233 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001234 while (!list_empty(&oo->oo_owner.so_stateids)) {
1235 stp = list_first_entry(&oo->oo_owner.so_stateids,
1236 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001237 if (unhash_open_stateid(stp, &reaplist))
1238 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001239 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001240 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001241 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001242 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001243 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001244}
1245
Marc Eshel5282fd72009-04-03 08:27:52 +03001246static inline int
1247hash_sessionid(struct nfs4_sessionid *sessionid)
1248{
1249 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1250
1251 return sid->sequence % SESSION_HASH_SIZE;
1252}
1253
Mark Salter135dd002015-04-06 09:46:00 -04001254#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001255static inline void
1256dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1257{
1258 u32 *ptr = (u32 *)(&sessionid->data[0]);
1259 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1260}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001261#else
1262static inline void
1263dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1264{
1265}
1266#endif
1267
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001268/*
1269 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1270 * won't be used for replay.
1271 */
1272void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1273{
1274 struct nfs4_stateowner *so = cstate->replay_owner;
1275
1276 if (nfserr == nfserr_replay_me)
1277 return;
1278
1279 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001280 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001281 return;
1282 }
1283 if (!so)
1284 return;
1285 if (so->so_is_open_owner)
1286 release_last_closed_stateid(openowner(so));
1287 so->so_seqid++;
1288 return;
1289}
Marc Eshel5282fd72009-04-03 08:27:52 +03001290
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001291static void
1292gen_sessionid(struct nfsd4_session *ses)
1293{
1294 struct nfs4_client *clp = ses->se_client;
1295 struct nfsd4_sessionid *sid;
1296
1297 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1298 sid->clientid = clp->cl_clientid;
1299 sid->sequence = current_sessionid++;
1300 sid->reserved = 0;
1301}
1302
1303/*
Andy Adamsona6496372009-08-28 08:45:01 -04001304 * The protocol defines ca_maxresponssize_cached to include the size of
1305 * the rpc header, but all we need to cache is the data starting after
1306 * the end of the initial SEQUENCE operation--the rest we regenerate
1307 * each time. Therefore we can advertise a ca_maxresponssize_cached
1308 * value that is the number of bytes in our cache plus a few additional
1309 * bytes. In order to stay on the safe side, and not promise more than
1310 * we can cache, those additional bytes must be the minimum possible: 24
1311 * bytes of rpc header (xid through accept state, with AUTH_NULL
1312 * verifier), 12 for the compound header (with zero-length tag), and 44
1313 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001314 */
Andy Adamsona6496372009-08-28 08:45:01 -04001315#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1316
Andy Adamson557ce262009-08-28 08:45:04 -04001317static void
1318free_session_slots(struct nfsd4_session *ses)
1319{
1320 int i;
1321
1322 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1323 kfree(ses->se_slots[i]);
1324}
1325
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001326/*
1327 * We don't actually need to cache the rpc and session headers, so we
1328 * can allocate a little less for each slot:
1329 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001330static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001331{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001332 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001333
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001334 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1335 size = 0;
1336 else
1337 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1338 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001339}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001340
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001341/*
1342 * XXX: If we run out of reserved DRC memory we could (up to a point)
1343 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001344 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001345 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001346static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001347{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001348 u32 slotsize = slot_bytes(ca);
1349 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001350 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001351
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001352 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001353 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1354 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001355 num = min_t(int, num, avail / slotsize);
1356 nfsd_drc_mem_used += num * slotsize;
1357 spin_unlock(&nfsd_drc_lock);
1358
1359 return num;
1360}
1361
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001362static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001363{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001364 int slotsize = slot_bytes(ca);
1365
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001366 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001367 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001368 spin_unlock(&nfsd_drc_lock);
1369}
1370
Kinglong Mee60810e52014-01-01 00:35:47 +08001371static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1372 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001373{
Kinglong Mee60810e52014-01-01 00:35:47 +08001374 int numslots = fattrs->maxreqs;
1375 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001376 struct nfsd4_session *new;
1377 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001378
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001379 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001380 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1381 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001382
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001383 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001384 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001385 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001386 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001387 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001388 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001389 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001390 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001391 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001392
1393 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1394 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1395
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001396 return new;
1397out_free:
1398 while (i--)
1399 kfree(new->se_slots[i]);
1400 kfree(new);
1401 return NULL;
1402}
1403
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001404static void free_conn(struct nfsd4_conn *c)
1405{
1406 svc_xprt_put(c->cn_xprt);
1407 kfree(c);
1408}
1409
1410static void nfsd4_conn_lost(struct svc_xpt_user *u)
1411{
1412 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1413 struct nfs4_client *clp = c->cn_session->se_client;
1414
1415 spin_lock(&clp->cl_lock);
1416 if (!list_empty(&c->cn_persession)) {
1417 list_del(&c->cn_persession);
1418 free_conn(c);
1419 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001420 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001421 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001422}
1423
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001424static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001425{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001426 struct nfsd4_conn *conn;
1427
1428 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1429 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001430 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001431 svc_xprt_get(rqstp->rq_xprt);
1432 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001433 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001434 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1435 return conn;
1436}
1437
J. Bruce Fields328ead22010-09-29 16:11:06 -04001438static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1439{
1440 conn->cn_session = ses;
1441 list_add(&conn->cn_persession, &ses->se_conns);
1442}
1443
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001444static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1445{
1446 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001447
1448 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001449 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001450 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001451}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001452
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001453static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001454{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001455 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001456 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001457}
1458
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001459static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001460{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001461 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001462
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001463 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001464 ret = nfsd4_register_conn(conn);
1465 if (ret)
1466 /* oops; xprt is already down: */
1467 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001468 /* We may have gained or lost a callback channel: */
1469 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001470}
1471
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001472static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001473{
1474 u32 dir = NFS4_CDFC4_FORE;
1475
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001476 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001477 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001478 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001479}
1480
1481/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001482static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001483{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001484 struct nfs4_client *clp = s->se_client;
1485 struct nfsd4_conn *c;
1486
1487 spin_lock(&clp->cl_lock);
1488 while (!list_empty(&s->se_conns)) {
1489 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1490 list_del_init(&c->cn_persession);
1491 spin_unlock(&clp->cl_lock);
1492
1493 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1494 free_conn(c);
1495
1496 spin_lock(&clp->cl_lock);
1497 }
1498 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001499}
1500
J. Bruce Fields1377b692012-09-11 21:42:40 -04001501static void __free_session(struct nfsd4_session *ses)
1502{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001503 free_session_slots(ses);
1504 kfree(ses);
1505}
1506
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001507static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001508{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001509 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001510 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001511 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001512}
Andy Adamson557ce262009-08-28 08:45:04 -04001513
Fengguang Wu135ae822012-11-10 07:20:25 -05001514static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001515{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001516 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001517 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001518
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001519 new->se_client = clp;
1520 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001521
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001522 INIT_LIST_HEAD(&new->se_conns);
1523
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001524 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001525 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001526 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001527 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001528 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001529 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001530 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001531 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001532 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001533 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001534
Chuck Leverb0d2e422014-08-22 15:10:59 -04001535 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001536 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001537 /*
1538 * This is a little silly; with sessions there's no real
1539 * use for the callback address. Use the peer address
1540 * as a reasonable default for now, but consider fixing
1541 * the rpc client not to require an address in the
1542 * future:
1543 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001544 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1545 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001546 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001547}
1548
Benny Halevy9089f1b2010-05-12 00:12:26 +03001549/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001550static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001551__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001552{
1553 struct nfsd4_session *elem;
1554 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001555 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001556
Trond Myklebust0a880a22014-07-30 08:27:10 -04001557 lockdep_assert_held(&nn->client_lock);
1558
Marc Eshel5282fd72009-04-03 08:27:52 +03001559 dump_sessionid(__func__, sessionid);
1560 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001561 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001562 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001563 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1564 NFS4_MAX_SESSIONID_LEN)) {
1565 return elem;
1566 }
1567 }
1568
1569 dprintk("%s: session not found\n", __func__);
1570 return NULL;
1571}
1572
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001573static struct nfsd4_session *
1574find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1575 __be32 *ret)
1576{
1577 struct nfsd4_session *session;
1578 __be32 status = nfserr_badsession;
1579
1580 session = __find_in_sessionid_hashtbl(sessionid, net);
1581 if (!session)
1582 goto out;
1583 status = nfsd4_get_session_locked(session);
1584 if (status)
1585 session = NULL;
1586out:
1587 *ret = status;
1588 return session;
1589}
1590
Benny Halevy9089f1b2010-05-12 00:12:26 +03001591/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001592static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001593unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001594{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001595 struct nfs4_client *clp = ses->se_client;
1596 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1597
1598 lockdep_assert_held(&nn->client_lock);
1599
Andy Adamson7116ed62009-04-03 08:27:43 +03001600 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001601 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001602 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001603 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001604}
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1607static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001608STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001610 /*
1611 * We're assuming the clid was not given out from a boot
1612 * precisely 2^32 (about 136 years) before this one. That seems
1613 * a safe assumption:
1614 */
1615 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001617 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001618 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 return 1;
1620}
1621
1622/*
1623 * XXX Should we use a slab cache ?
1624 * This type of memory management is somewhat inefficient, but we use it
1625 * anyway since SETCLIENTID is not a common operation.
1626 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001627static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
1629 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001630 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001632 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1633 if (clp == NULL)
1634 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001635 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001636 if (clp->cl_name.data == NULL)
1637 goto err_no_name;
1638 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1639 OWNER_HASH_SIZE, GFP_KERNEL);
1640 if (!clp->cl_ownerstr_hashtbl)
1641 goto err_no_hashtbl;
1642 for (i = 0; i < OWNER_HASH_SIZE; i++)
1643 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001644 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001645 INIT_LIST_HEAD(&clp->cl_sessions);
1646 idr_init(&clp->cl_stateids);
1647 atomic_set(&clp->cl_refcount, 0);
1648 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1649 INIT_LIST_HEAD(&clp->cl_idhash);
1650 INIT_LIST_HEAD(&clp->cl_openowners);
1651 INIT_LIST_HEAD(&clp->cl_delegations);
1652 INIT_LIST_HEAD(&clp->cl_lru);
Trond Myklebust5694c932014-04-18 14:43:56 -04001653 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001654#ifdef CONFIG_NFSD_PNFS
1655 INIT_LIST_HEAD(&clp->cl_lo_states);
1656#endif
Trond Myklebust5694c932014-04-18 14:43:56 -04001657 spin_lock_init(&clp->cl_lock);
1658 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001660err_no_hashtbl:
1661 kfree(clp->cl_name.data);
1662err_no_name:
1663 kfree(clp);
1664 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
1666
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001667static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668free_client(struct nfs4_client *clp)
1669{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001670 while (!list_empty(&clp->cl_sessions)) {
1671 struct nfsd4_session *ses;
1672 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1673 se_perclnt);
1674 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001675 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1676 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001677 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001678 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001679 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001680 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001682 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 kfree(clp);
1684}
1685
Benny Halevy84d38ac2010-05-12 00:13:16 +03001686/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001687static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001688unhash_client_locked(struct nfs4_client *clp)
1689{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001690 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001691 struct nfsd4_session *ses;
1692
Trond Myklebust0a880a22014-07-30 08:27:10 -04001693 lockdep_assert_held(&nn->client_lock);
1694
Trond Myklebust4beb3452014-07-30 08:27:02 -04001695 /* Mark the client as expired! */
1696 clp->cl_time = 0;
1697 /* Make it invisible */
1698 if (!list_empty(&clp->cl_idhash)) {
1699 list_del_init(&clp->cl_idhash);
1700 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1701 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1702 else
1703 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1704 }
1705 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001706 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001707 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1708 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001709 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001710}
1711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001713unhash_client(struct nfs4_client *clp)
1714{
1715 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1716
1717 spin_lock(&nn->client_lock);
1718 unhash_client_locked(clp);
1719 spin_unlock(&nn->client_lock);
1720}
1721
Jeff Layton97403d92014-07-30 08:27:12 -04001722static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1723{
1724 if (atomic_read(&clp->cl_refcount))
1725 return nfserr_jukebox;
1726 unhash_client_locked(clp);
1727 return nfs_ok;
1728}
1729
Trond Myklebust4beb3452014-07-30 08:27:02 -04001730static void
1731__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001733 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 struct list_head reaplist;
1736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001738 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001739 while (!list_empty(&clp->cl_delegations)) {
1740 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001741 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04001742 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
Benny Halevycdc97502014-05-30 09:09:30 -04001744 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 while (!list_empty(&reaplist)) {
1746 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001747 list_del_init(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +02001748 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -04001749 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04001750 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001752 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05001753 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001754 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001755 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001756 }
NeilBrownea1da632005-06-23 22:04:17 -07001757 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001758 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04001759 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001760 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001762 nfsd4_return_all_client_layouts(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001763 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001764 if (clp->cl_cb_conn.cb_xprt)
1765 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001766 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767}
1768
Trond Myklebust4beb3452014-07-30 08:27:02 -04001769static void
1770destroy_client(struct nfs4_client *clp)
1771{
1772 unhash_client(clp);
1773 __destroy_client(clp);
1774}
1775
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001776static void expire_client(struct nfs4_client *clp)
1777{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001778 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001779 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001780 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001781}
1782
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001783static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1784{
1785 memcpy(target->cl_verifier.data, source->data,
1786 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787}
1788
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001789static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1790{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1792 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1793}
1794
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001795static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001796{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001797 if (source->cr_principal) {
1798 target->cr_principal =
1799 kstrdup(source->cr_principal, GFP_KERNEL);
1800 if (target->cr_principal == NULL)
1801 return -ENOMEM;
1802 } else
1803 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001804 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 target->cr_uid = source->cr_uid;
1806 target->cr_gid = source->cr_gid;
1807 target->cr_group_info = source->cr_group_info;
1808 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001809 target->cr_gss_mech = source->cr_gss_mech;
1810 if (source->cr_gss_mech)
1811 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001812 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001815static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001816compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1817{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001818 if (o1->len < o2->len)
1819 return -1;
1820 if (o1->len > o2->len)
1821 return 1;
1822 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001823}
1824
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001825static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001826{
NeilBrowna55370a2005-06-23 22:03:52 -07001827 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
1829
1830static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001831same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1832{
1833 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
1835
1836static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001837same_clid(clientid_t *cl1, clientid_t *cl2)
1838{
1839 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840}
1841
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001842static bool groups_equal(struct group_info *g1, struct group_info *g2)
1843{
1844 int i;
1845
1846 if (g1->ngroups != g2->ngroups)
1847 return false;
1848 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001849 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001850 return false;
1851 return true;
1852}
1853
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001854/*
1855 * RFC 3530 language requires clid_inuse be returned when the
1856 * "principal" associated with a requests differs from that previously
1857 * used. We use uid, gid's, and gss principal string as our best
1858 * approximation. We also don't want to allow non-gss use of a client
1859 * established using gss: in theory cr_principal should catch that
1860 * change, but in practice cr_principal can be null even in the gss case
1861 * since gssd doesn't always pass down a principal string.
1862 */
1863static bool is_gss_cred(struct svc_cred *cr)
1864{
1865 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1866 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1867}
1868
1869
Vivek Trivedi5559b502012-07-24 21:18:20 +05301870static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001871same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1872{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001873 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001874 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1875 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001876 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1877 return false;
1878 if (cr1->cr_principal == cr2->cr_principal)
1879 return true;
1880 if (!cr1->cr_principal || !cr2->cr_principal)
1881 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301882 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
1884
J. Bruce Fields57266a62013-04-13 14:27:29 -04001885static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1886{
1887 struct svc_cred *cr = &rqstp->rq_cred;
1888 u32 service;
1889
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001890 if (!cr->cr_gss_mech)
1891 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001892 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1893 return service == RPC_GSS_SVC_INTEGRITY ||
1894 service == RPC_GSS_SVC_PRIVACY;
1895}
1896
1897static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1898{
1899 struct svc_cred *cr = &rqstp->rq_cred;
1900
1901 if (!cl->cl_mach_cred)
1902 return true;
1903 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1904 return false;
1905 if (!svc_rqst_integrity_protected(rqstp))
1906 return false;
1907 if (!cr->cr_principal)
1908 return false;
1909 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1910}
1911
Jeff Layton294ac322014-07-30 08:27:15 -04001912static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001913{
Chuck Leverab4684d2012-03-02 17:13:50 -05001914 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Jeff Laytonf4199922014-06-17 07:44:11 -04001916 /*
1917 * This is opaque to client, so no need to byte-swap. Use
1918 * __force to keep sparse happy
1919 */
1920 verf[0] = (__force __be32)get_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08001921 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001922 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
1924
Jeff Layton294ac322014-07-30 08:27:15 -04001925static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1926{
1927 clp->cl_clientid.cl_boot = nn->boot_time;
1928 clp->cl_clientid.cl_id = nn->clientid_counter++;
1929 gen_confirm(clp, nn);
1930}
1931
Jeff Layton4770d722014-07-29 21:34:10 -04001932static struct nfs4_stid *
1933find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001934{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001935 struct nfs4_stid *ret;
1936
1937 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1938 if (!ret || !ret->sc_type)
1939 return NULL;
1940 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001941}
1942
Jeff Layton4770d722014-07-29 21:34:10 -04001943static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001944find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001945{
1946 struct nfs4_stid *s;
1947
Jeff Layton4770d722014-07-29 21:34:10 -04001948 spin_lock(&cl->cl_lock);
1949 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001950 if (s != NULL) {
1951 if (typemask & s->sc_type)
1952 atomic_inc(&s->sc_count);
1953 else
1954 s = NULL;
1955 }
Jeff Layton4770d722014-07-29 21:34:10 -04001956 spin_unlock(&cl->cl_lock);
1957 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001958}
1959
Jeff Layton2216d442012-11-12 15:00:57 -05001960static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001961 struct svc_rqst *rqstp, nfs4_verifier *verf)
1962{
1963 struct nfs4_client *clp;
1964 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001965 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001966 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001967
1968 clp = alloc_client(name);
1969 if (clp == NULL)
1970 return NULL;
1971
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001972 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1973 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001974 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001975 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001976 }
Christoph Hellwig0162ac22014-09-24 12:19:19 +02001977 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Benny Halevy07cd4902010-05-12 00:13:41 +03001978 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001979 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001980 copy_verf(clp, verf);
1981 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001982 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001983 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001984 return clp;
1985}
1986
NeilBrownfd39ca92005-06-23 22:04:03 -07001987static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001988add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1989{
1990 struct rb_node **new = &(root->rb_node), *parent = NULL;
1991 struct nfs4_client *clp;
1992
1993 while (*new) {
1994 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1995 parent = *new;
1996
1997 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1998 new = &((*new)->rb_left);
1999 else
2000 new = &((*new)->rb_right);
2001 }
2002
2003 rb_link_node(&new_clp->cl_namenode, parent, new);
2004 rb_insert_color(&new_clp->cl_namenode, root);
2005}
2006
2007static struct nfs4_client *
2008find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2009{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002010 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002011 struct rb_node *node = root->rb_node;
2012 struct nfs4_client *clp;
2013
2014 while (node) {
2015 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2016 cmp = compare_blob(&clp->cl_name, name);
2017 if (cmp > 0)
2018 node = node->rb_left;
2019 else if (cmp < 0)
2020 node = node->rb_right;
2021 else
2022 return clp;
2023 }
2024 return NULL;
2025}
2026
2027static void
2028add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
2030 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002031 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Trond Myklebust0a880a22014-07-30 08:27:10 -04002033 lockdep_assert_held(&nn->client_lock);
2034
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002035 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002036 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002038 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002039 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
2041
NeilBrownfd39ca92005-06-23 22:04:03 -07002042static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043move_to_confirmed(struct nfs4_client *clp)
2044{
2045 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002046 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Trond Myklebust0a880a22014-07-30 08:27:10 -04002048 lockdep_assert_held(&nn->client_lock);
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002051 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002052 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002053 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002054 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002055 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056}
2057
2058static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002059find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
2061 struct nfs4_client *clp;
2062 unsigned int idhashval = clientid_hashval(clid->cl_id);
2063
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002064 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002065 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002066 if ((bool)clp->cl_minorversion != sessions)
2067 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002068 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
2072 return NULL;
2073}
2074
2075static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002076find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2077{
2078 struct list_head *tbl = nn->conf_id_hashtbl;
2079
Trond Myklebust0a880a22014-07-30 08:27:10 -04002080 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002081 return find_client_in_id_table(tbl, clid, sessions);
2082}
2083
2084static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002085find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002087 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Trond Myklebust0a880a22014-07-30 08:27:10 -04002089 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002090 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091}
2092
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002093static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002094{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002095 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002096}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002097
NeilBrown28ce6052005-06-23 22:03:56 -07002098static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002099find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002100{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002101 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002102 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002103}
2104
2105static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002106find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002107{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002108 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002109 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002110}
2111
NeilBrownfd39ca92005-06-23 22:04:03 -07002112static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002113gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002115 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002116 struct sockaddr *sa = svc_addr(rqstp);
2117 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002118 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Jeff Layton7077ecb2009-08-14 12:57:58 -04002120 /* Currently, we only support tcp and tcp6 for the callback channel */
2121 if (se->se_callback_netid_len == 3 &&
2122 !memcmp(se->se_callback_netid_val, "tcp", 3))
2123 expected_family = AF_INET;
2124 else if (se->se_callback_netid_len == 4 &&
2125 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2126 expected_family = AF_INET6;
2127 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 goto out_err;
2129
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002130 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002131 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002132 (struct sockaddr *)&conn->cb_addr,
2133 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002134
J. Bruce Fields07263f12010-05-31 19:09:40 -04002135 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002137
J. Bruce Fields07263f12010-05-31 19:09:40 -04002138 if (conn->cb_addr.ss_family == AF_INET6)
2139 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002140
J. Bruce Fields07263f12010-05-31 19:09:40 -04002141 conn->cb_prog = se->se_callback_prog;
2142 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002143 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 return;
2145out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002146 conn->cb_addr.ss_family = AF_UNSPEC;
2147 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002148 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 "will not receive delegations\n",
2150 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 return;
2153}
2154
Andy Adamson074fe892009-04-03 08:28:15 +03002155/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002156 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002157 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002158static void
Andy Adamson074fe892009-04-03 08:28:15 +03002159nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2160{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002161 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002162 struct nfsd4_slot *slot = resp->cstate.slot;
2163 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002164
Andy Adamson557ce262009-08-28 08:45:04 -04002165 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002166
Andy Adamson557ce262009-08-28 08:45:04 -04002167 slot->sl_opcnt = resp->opcnt;
2168 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002169
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002170 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002171 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002172 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002173 return;
2174 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002175 base = resp->cstate.data_offset;
2176 slot->sl_datalen = buf->len - base;
2177 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04002178 WARN("%s: sessions DRC could not cache compound\n", __func__);
2179 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002180}
2181
2182/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002183 * Encode the replay sequence operation from the slot values.
2184 * If cachethis is FALSE encode the uncached rep error on the next
2185 * operation which sets resp->p and increments resp->opcnt for
2186 * nfs4svc_encode_compoundres.
2187 *
Andy Adamson074fe892009-04-03 08:28:15 +03002188 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002189static __be32
2190nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2191 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002192{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002193 struct nfsd4_op *op;
2194 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002195
Andy Adamsonabfabf82009-07-23 19:02:18 -04002196 /* Encode the replayed sequence operation */
2197 op = &args->ops[resp->opcnt - 1];
2198 nfsd4_encode_operation(resp, op);
2199
2200 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002201 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002202 op = &args->ops[resp->opcnt++];
2203 op->status = nfserr_retry_uncached_rep;
2204 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002205 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002206 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002207}
2208
2209/*
Andy Adamson557ce262009-08-28 08:45:04 -04002210 * The sequence operation is not cached because we can use the slot and
2211 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002212 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002213static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002214nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2215 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002216{
Andy Adamson557ce262009-08-28 08:45:04 -04002217 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002218 struct xdr_stream *xdr = &resp->xdr;
2219 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002220 __be32 status;
2221
Andy Adamson557ce262009-08-28 08:45:04 -04002222 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002223
Andy Adamsonabfabf82009-07-23 19:02:18 -04002224 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002225 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002226 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002227
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002228 p = xdr_reserve_space(xdr, slot->sl_datalen);
2229 if (!p) {
2230 WARN_ON_ONCE(1);
2231 return nfserr_serverfault;
2232 }
2233 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2234 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002235
Andy Adamson557ce262009-08-28 08:45:04 -04002236 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002237 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002238}
2239
Andy Adamson0733d212009-04-03 08:28:01 +03002240/*
2241 * Set the exchange_id flags returned by the server.
2242 */
2243static void
2244nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2245{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002246#ifdef CONFIG_NFSD_PNFS
2247 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2248#else
Andy Adamson0733d212009-04-03 08:28:01 +03002249 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002250#endif
Andy Adamson0733d212009-04-03 08:28:01 +03002251
2252 /* Referrals are supported, Migration is not. */
2253 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2254
2255 /* set the wire flags to return to client. */
2256 clid->flags = new->cl_exchange_flags;
2257}
2258
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002259static bool client_has_state(struct nfs4_client *clp)
2260{
2261 /*
2262 * Note clp->cl_openowners check isn't quite right: there's no
2263 * need to count owners without stateid's.
2264 *
2265 * Also note we should probably be using this in 4.0 case too.
2266 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002267 return !list_empty(&clp->cl_openowners)
Kinglong Mee47e970b2015-07-21 13:09:17 +08002268#ifdef CONFIG_NFSD_PNFS
2269 || !list_empty(&clp->cl_lo_states)
2270#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002271 || !list_empty(&clp->cl_delegations)
2272 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002273}
2274
Al Virob37ad282006-10-19 23:28:59 -07002275__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002276nfsd4_exchange_id(struct svc_rqst *rqstp,
2277 struct nfsd4_compound_state *cstate,
2278 struct nfsd4_exchange_id *exid)
2279{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002280 struct nfs4_client *conf, *new;
2281 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002282 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002283 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002284 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002285 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002286 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002287 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002288
Jeff Layton363168b2009-08-14 12:57:56 -04002289 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002290 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002291 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002292 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002293 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002294
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002295 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002296 return nfserr_inval;
2297
Andy Adamson0733d212009-04-03 08:28:01 +03002298 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002299 case SP4_MACH_CRED:
2300 if (!svc_rqst_integrity_protected(rqstp))
2301 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002302 case SP4_NONE:
2303 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002304 default: /* checked by xdr code */
2305 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002306 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002307 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002308 }
2309
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002310 new = create_client(exid->clname, rqstp, &verf);
2311 if (new == NULL)
2312 return nfserr_jukebox;
2313
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002314 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002315 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002316 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002317 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002318 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2319 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2320
J. Bruce Fields136e6582012-05-12 20:37:23 -04002321 if (update) {
2322 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002323 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002324 goto out;
2325 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002326 if (!mach_creds_match(conf, rqstp)) {
2327 status = nfserr_wrong_cred;
2328 goto out;
2329 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002330 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002331 status = nfserr_perm;
2332 goto out;
2333 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002334 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002335 status = nfserr_not_same;
2336 goto out;
2337 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002338 /* case 6 */
2339 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002340 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002341 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002342 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002343 if (client_has_state(conf)) {
2344 status = nfserr_clid_inuse;
2345 goto out;
2346 }
Andy Adamson0733d212009-04-03 08:28:01 +03002347 goto out_new;
2348 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002349 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002350 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002351 goto out_copy;
2352 }
2353 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002354 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002355 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002356 }
2357
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002358 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002359 status = nfserr_noent;
2360 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002361 }
2362
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002363 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002364 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002365 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002366
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002367 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002368out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002369 if (conf) {
2370 status = mark_client_expired_locked(conf);
2371 if (status)
2372 goto out;
2373 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002374 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002375 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002376
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002377 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002378 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002379 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002380out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002381 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2382 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002383
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002384 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2385 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002386
2387 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002388 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002389 status = nfs_ok;
2390
2391out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002392 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002393 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002394 expire_client(new);
2395 if (unconf)
2396 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002397 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002398}
2399
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002400static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002401check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002402{
Andy Adamson88e588d2009-07-23 19:02:15 -04002403 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2404 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002405
2406 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002407 if (slot_inuse) {
2408 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002409 return nfserr_jukebox;
2410 else
2411 return nfserr_seq_misordered;
2412 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002413 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002414 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002415 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002416 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002417 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002418 return nfserr_seq_misordered;
2419}
2420
Andy Adamson49557cc2009-07-23 19:02:16 -04002421/*
2422 * Cache the create session result into the create session single DRC
2423 * slot cache by saving the xdr structure. sl_seqid has been set.
2424 * Do this for solo or embedded create session operations.
2425 */
2426static void
2427nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002428 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002429{
2430 slot->sl_status = nfserr;
2431 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2432}
2433
2434static __be32
2435nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2436 struct nfsd4_clid_slot *slot)
2437{
2438 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2439 return slot->sl_status;
2440}
2441
Mi Jinlong1b74c252011-07-14 14:50:17 +08002442#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2443 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2444 1 + /* MIN tag is length with zero, only length */ \
2445 3 + /* version, opcount, opcode */ \
2446 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2447 /* seqid, slotID, slotID, cache */ \
2448 4 ) * sizeof(__be32))
2449
2450#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2451 2 + /* verifier: AUTH_NULL, length 0 */\
2452 1 + /* status */ \
2453 1 + /* MIN tag is length with zero, only length */ \
2454 3 + /* opcount, opcode, opstatus*/ \
2455 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2456 /* seqid, slotID, slotID, slotID, status */ \
2457 5 ) * sizeof(__be32))
2458
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002459static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002460{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002461 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2462
J. Bruce Fields373cd402013-04-08 15:42:12 -04002463 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2464 return nfserr_toosmall;
2465 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2466 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002467 ca->headerpadsz = 0;
2468 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2469 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2470 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2471 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2472 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2473 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2474 /*
2475 * Note decreasing slot size below client's request may make it
2476 * difficult for client to function correctly, whereas
2477 * decreasing the number of slots will (just?) affect
2478 * performance. When short on memory we therefore prefer to
2479 * decrease number of slots instead of their size. Clients that
2480 * request larger slots than they need will get poor results:
2481 */
2482 ca->maxreqs = nfsd4_get_drc_mem(ca);
2483 if (!ca->maxreqs)
2484 return nfserr_jukebox;
2485
J. Bruce Fields373cd402013-04-08 15:42:12 -04002486 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002487}
2488
Kinglong Mee8a891632013-12-23 18:11:02 +08002489#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2490 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2491#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2492 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2493
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002494static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2495{
2496 ca->headerpadsz = 0;
2497
2498 /*
2499 * These RPC_MAX_HEADER macros are overkill, especially since we
2500 * don't even do gss on the backchannel yet. But this is still
2501 * less than 1k. Tighten up this estimate in the unlikely event
2502 * it turns out to be a problem for some client:
2503 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002504 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002505 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002506 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002507 return nfserr_toosmall;
2508 ca->maxresp_cached = 0;
2509 if (ca->maxops < 2)
2510 return nfserr_toosmall;
2511
2512 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002513}
2514
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002515static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2516{
2517 switch (cbs->flavor) {
2518 case RPC_AUTH_NULL:
2519 case RPC_AUTH_UNIX:
2520 return nfs_ok;
2521 default:
2522 /*
2523 * GSS case: the spec doesn't allow us to return this
2524 * error. But it also doesn't allow us not to support
2525 * GSS.
2526 * I'd rather this fail hard than return some error the
2527 * client might think it can already handle:
2528 */
2529 return nfserr_encr_alg_unsupp;
2530 }
2531}
2532
Andy Adamson069b6ad2009-04-03 08:27:58 +03002533__be32
2534nfsd4_create_session(struct svc_rqst *rqstp,
2535 struct nfsd4_compound_state *cstate,
2536 struct nfsd4_create_session *cr_ses)
2537{
Jeff Layton363168b2009-08-14 12:57:56 -04002538 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002539 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002540 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002541 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002542 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002543 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002544 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002545 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002546
Mi Jinlonga62573d2011-03-23 17:57:07 +08002547 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2548 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002549 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2550 if (status)
2551 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002552 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002553 if (status)
2554 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002555 status = check_backchannel_attrs(&cr_ses->back_channel);
2556 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002557 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002558 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002559 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002560 if (!new)
2561 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002562 conn = alloc_conn_from_crses(rqstp, cr_ses);
2563 if (!conn)
2564 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002565
Jeff Laytond20c11d2014-07-30 08:27:07 -04002566 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002567 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002568 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002569 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002570
2571 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002572 status = nfserr_wrong_cred;
2573 if (!mach_creds_match(conf, rqstp))
2574 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002575 cs_slot = &conf->cl_cs_slot;
2576 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08002577 if (status) {
2578 if (status == nfserr_replay_cache)
2579 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002580 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002581 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002582 } else if (unconf) {
2583 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002584 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002585 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002586 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002587 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002588 status = nfserr_wrong_cred;
2589 if (!mach_creds_match(unconf, rqstp))
2590 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002591 cs_slot = &unconf->cl_cs_slot;
2592 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002593 if (status) {
2594 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002595 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002596 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002597 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002598 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002599 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002600 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002601 if (status) {
2602 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002603 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002604 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002605 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002606 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002607 conf = unconf;
2608 } else {
2609 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002610 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002611 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002612 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002613 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002614 * We do not support RDMA or persistent sessions
2615 */
2616 cr_ses->flags &= ~SESSION4_PERSIST;
2617 cr_ses->flags &= ~SESSION4_RDMA;
2618
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002619 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002620 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002621
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002622 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002623 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002624 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002625 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002626
Jeff Laytond20c11d2014-07-30 08:27:07 -04002627 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002628 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002629 spin_unlock(&nn->client_lock);
2630 /* init connection and backchannel */
2631 nfsd4_init_conn(rqstp, conn, new);
2632 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002633 if (old)
2634 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002635 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002636out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002637 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002638 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002639 if (old)
2640 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002641out_free_session:
2642 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002643out_release_drc_mem:
2644 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002645 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002646}
2647
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002648static __be32 nfsd4_map_bcts_dir(u32 *dir)
2649{
2650 switch (*dir) {
2651 case NFS4_CDFC4_FORE:
2652 case NFS4_CDFC4_BACK:
2653 return nfs_ok;
2654 case NFS4_CDFC4_FORE_OR_BOTH:
2655 case NFS4_CDFC4_BACK_OR_BOTH:
2656 *dir = NFS4_CDFC4_BOTH;
2657 return nfs_ok;
2658 };
2659 return nfserr_inval;
2660}
2661
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002662__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2663{
2664 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002665 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002666 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002667
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002668 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2669 if (status)
2670 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002671 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002672 session->se_cb_prog = bc->bc_cb_program;
2673 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002674 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002675
2676 nfsd4_probe_callback(session->se_client);
2677
2678 return nfs_ok;
2679}
2680
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002681__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2682 struct nfsd4_compound_state *cstate,
2683 struct nfsd4_bind_conn_to_session *bcts)
2684{
2685 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002686 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002687 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002688 struct net *net = SVC_NET(rqstp);
2689 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002690
2691 if (!nfsd4_last_compound_op(rqstp))
2692 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002693 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002694 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002695 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002696 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002697 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002698 status = nfserr_wrong_cred;
2699 if (!mach_creds_match(session->se_client, rqstp))
2700 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002701 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002702 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002703 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002704 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002705 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002706 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002707 goto out;
2708 nfsd4_init_conn(rqstp, conn, session);
2709 status = nfs_ok;
2710out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002711 nfsd4_put_session(session);
2712out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002713 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002714}
2715
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002716static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2717{
2718 if (!session)
2719 return 0;
2720 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2721}
2722
Andy Adamson069b6ad2009-04-03 08:27:58 +03002723__be32
2724nfsd4_destroy_session(struct svc_rqst *r,
2725 struct nfsd4_compound_state *cstate,
2726 struct nfsd4_destroy_session *sessionid)
2727{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002728 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002729 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002730 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002731 struct net *net = SVC_NET(r);
2732 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002733
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002734 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002735 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002736 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002737 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002738 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002739 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002740 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002741 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002742 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002743 if (!ses)
2744 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002745 status = nfserr_wrong_cred;
2746 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002747 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002748 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002749 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002750 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002751 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002752 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002753
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002754 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002755
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002756 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002757 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002758out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002759 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002760out_client_lock:
2761 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002762out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03002763 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002764}
2765
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002766static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002767{
2768 struct nfsd4_conn *c;
2769
2770 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002771 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002772 return c;
2773 }
2774 }
2775 return NULL;
2776}
2777
J. Bruce Fields57266a62013-04-13 14:27:29 -04002778static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002779{
2780 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002781 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002782 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002783 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002784
2785 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002786 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002787 if (c)
2788 goto out_free;
2789 status = nfserr_conn_not_bound_to_session;
2790 if (clp->cl_mach_cred)
2791 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002792 __nfsd4_hash_conn(new, ses);
2793 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002794 ret = nfsd4_register_conn(new);
2795 if (ret)
2796 /* oops; xprt is already down: */
2797 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002798 return nfs_ok;
2799out_free:
2800 spin_unlock(&clp->cl_lock);
2801 free_conn(new);
2802 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002803}
2804
Mi Jinlong868b89c2011-04-27 09:09:58 +08002805static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2806{
2807 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2808
2809 return args->opcnt > session->se_fchannel.maxops;
2810}
2811
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002812static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2813 struct nfsd4_session *session)
2814{
2815 struct xdr_buf *xb = &rqstp->rq_arg;
2816
2817 return xb->len > session->se_fchannel.maxreq_sz;
2818}
2819
Andy Adamson069b6ad2009-04-03 08:27:58 +03002820__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002821nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002822 struct nfsd4_compound_state *cstate,
2823 struct nfsd4_sequence *seq)
2824{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002825 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002826 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002827 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002828 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002829 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002830 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002831 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002832 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002833 struct net *net = SVC_NET(rqstp);
2834 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002835
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002836 if (resp->opcnt != 1)
2837 return nfserr_sequence_pos;
2838
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002839 /*
2840 * Will be either used or freed by nfsd4_sequence_check_conn
2841 * below.
2842 */
2843 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2844 if (!conn)
2845 return nfserr_jukebox;
2846
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002847 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002848 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002849 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002850 goto out_no_session;
2851 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002852
Mi Jinlong868b89c2011-04-27 09:09:58 +08002853 status = nfserr_too_many_ops;
2854 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002855 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002856
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002857 status = nfserr_req_too_big;
2858 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002859 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002860
Benny Halevyb85d4c02009-04-03 08:28:08 +03002861 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002862 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002863 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002864
Andy Adamson557ce262009-08-28 08:45:04 -04002865 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002866 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2867
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002868 /* We do not negotiate the number of slots yet, so set the
2869 * maxslots to the session maxreqs which is used to encode
2870 * sr_highest_slotid and the sr_target_slot id to maxslots */
2871 seq->maxslots = session->se_fchannel.maxreqs;
2872
J. Bruce Fields73e79482012-02-13 16:39:00 -05002873 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2874 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002875 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002876 status = nfserr_seq_misordered;
2877 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002878 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002879 cstate->slot = slot;
2880 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002881 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002882 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002883 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002884 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002885 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002886 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002887 }
2888 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002889 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002890
J. Bruce Fields57266a62013-04-13 14:27:29 -04002891 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002892 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002893 if (status)
2894 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002895
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002896 buflen = (seq->cachethis) ?
2897 session->se_fchannel.maxresp_cached :
2898 session->se_fchannel.maxresp_sz;
2899 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2900 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002901 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002902 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002903 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002904
2905 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002906 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002907 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002908 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002909 if (seq->cachethis)
2910 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002911 else
2912 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002913
2914 cstate->slot = slot;
2915 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002916 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002917
Benny Halevyb85d4c02009-04-03 08:28:08 +03002918out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002919 switch (clp->cl_cb_state) {
2920 case NFSD4_CB_DOWN:
2921 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2922 break;
2923 case NFSD4_CB_FAULT:
2924 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2925 break;
2926 default:
2927 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002928 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002929 if (!list_empty(&clp->cl_revoked))
2930 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002931out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002932 if (conn)
2933 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002934 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002935 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002936out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002937 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002938 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002939}
2940
Trond Myklebustb6076642014-06-30 11:48:35 -04002941void
2942nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2943{
2944 struct nfsd4_compound_state *cs = &resp->cstate;
2945
2946 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002947 if (cs->status != nfserr_replay_cache) {
2948 nfsd4_store_cache_entry(resp);
2949 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2950 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002951 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002952 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002953 } else if (cs->clp)
2954 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002955}
2956
Mi Jinlong345c2842011-10-20 17:51:39 +08002957__be32
2958nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2959{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002960 struct nfs4_client *conf, *unconf;
2961 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002962 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002963 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002964
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002965 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002966 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002967 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002968 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002969
2970 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002971 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002972 status = nfserr_clientid_busy;
2973 goto out;
2974 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04002975 status = mark_client_expired_locked(conf);
2976 if (status)
2977 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002978 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08002979 } else if (unconf)
2980 clp = unconf;
2981 else {
2982 status = nfserr_stale_clientid;
2983 goto out;
2984 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002985 if (!mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002986 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002987 status = nfserr_wrong_cred;
2988 goto out;
2989 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002990 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002991out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002992 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002993 if (clp)
2994 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002995 return status;
2996}
2997
Andy Adamson069b6ad2009-04-03 08:27:58 +03002998__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002999nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
3000{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003001 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003002
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003003 if (rc->rca_one_fs) {
3004 if (!cstate->current_fh.fh_dentry)
3005 return nfserr_nofilehandle;
3006 /*
3007 * We don't take advantage of the rca_one_fs case.
3008 * That's OK, it's optional, we can safely ignore it.
3009 */
3010 return nfs_ok;
3011 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003012
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003013 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04003014 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3015 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003016 goto out;
3017
3018 status = nfserr_stale_clientid;
3019 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003020 /*
3021 * The following error isn't really legal.
3022 * But we only get here if the client just explicitly
3023 * destroyed the client. Surely it no longer cares what
3024 * error it gets back on an operation for the dead
3025 * client.
3026 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003027 goto out;
3028
3029 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04003030 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003031out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003032 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003033}
3034
3035__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003036nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3037 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003039 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003041 struct nfs4_client *conf, *new;
3042 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003043 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003044 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3045
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003046 new = create_client(clname, rqstp, &clverifier);
3047 if (new == NULL)
3048 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003049 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003050 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003051 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07003052 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003053 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003055 if (clp_used_exchangeid(conf))
3056 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003057 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04003058 char addr_str[INET6_ADDRSTRLEN];
3059 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3060 sizeof(addr_str));
3061 dprintk("NFSD: setclientid: string in use by client "
3062 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 goto out;
3064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003066 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003067 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003068 unhash_client_locked(unconf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003069 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003070 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003072 gen_confirm(new, nn);
3073 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003074 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003075 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003076 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003077 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3079 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3080 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003081 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 status = nfs_ok;
3083out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003084 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003085 if (new)
3086 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003087 if (unconf)
3088 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 return status;
3090}
3091
3092
Al Virob37ad282006-10-19 23:28:59 -07003093__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003094nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3095 struct nfsd4_compound_state *cstate,
3096 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
NeilBrown21ab45a2005-06-23 22:04:14 -07003098 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003099 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3101 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003102 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003103 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003105 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003107
Jeff Laytond20c11d2014-07-30 08:27:07 -04003108 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003109 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003110 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003111 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003112 * We try hard to give out unique clientid's, so if we get an
3113 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003114 * the client may be buggy; this should never happen.
3115 *
3116 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003117 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003118 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04003119 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3120 goto out;
3121 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3122 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003123 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003124 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3125 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003127 else /* case 4: client hasn't noticed we rebooted yet? */
3128 status = nfserr_stale_clientid;
3129 goto out;
3130 }
3131 status = nfs_ok;
3132 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003133 old = unconf;
3134 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003135 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003136 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003137 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3138 if (old) {
3139 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003140 if (status) {
3141 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003142 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003143 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003144 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003145 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003146 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003147 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003148 get_client_locked(conf);
3149 spin_unlock(&nn->client_lock);
3150 nfsd4_probe_callback(conf);
3151 spin_lock(&nn->client_lock);
3152 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003154 spin_unlock(&nn->client_lock);
3155 if (old)
3156 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 return status;
3158}
3159
J. Bruce Fields32513b42011-10-13 16:00:16 -04003160static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003162 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3163}
3164
3165/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003166static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3167 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003168{
Trond Myklebust950e0112014-06-30 11:48:31 -04003169 lockdep_assert_held(&state_lock);
3170
J. Bruce Fields32513b42011-10-13 16:00:16 -04003171 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003172 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003173 INIT_LIST_HEAD(&fp->fi_stateids);
3174 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02003175 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003176 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003177 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003178 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003179 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003180 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3181 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003182#ifdef CONFIG_NFSD_PNFS
3183 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02003184 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003185#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04003186 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187}
3188
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003189void
NeilBrowne60d4392005-06-23 22:03:01 -07003190nfsd4_free_slabs(void)
3191{
Sachin Bhamare8287f002015-04-27 14:50:14 +02003192 kmem_cache_destroy(odstate_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003193 kmem_cache_destroy(openowner_slab);
3194 kmem_cache_destroy(lockowner_slab);
3195 kmem_cache_destroy(file_slab);
3196 kmem_cache_destroy(stateid_slab);
3197 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003198}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
Bryan Schumaker72083392011-11-01 15:24:59 -04003200int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201nfsd4_init_slabs(void)
3202{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003203 openowner_slab = kmem_cache_create("nfsd4_openowners",
3204 sizeof(struct nfs4_openowner), 0, 0, NULL);
3205 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003206 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003207 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003208 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003209 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003210 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003211 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003212 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003213 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003214 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003215 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003216 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003217 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003218 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003219 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003220 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003221 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003222 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003223 odstate_slab = kmem_cache_create("nfsd4_odstate",
3224 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3225 if (odstate_slab == NULL)
3226 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003228
Sachin Bhamare8287f002015-04-27 14:50:14 +02003229out_free_deleg_slab:
3230 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003231out_free_stateid_slab:
3232 kmem_cache_destroy(stateid_slab);
3233out_free_file_slab:
3234 kmem_cache_destroy(file_slab);
3235out_free_lockowner_slab:
3236 kmem_cache_destroy(lockowner_slab);
3237out_free_openowner_slab:
3238 kmem_cache_destroy(openowner_slab);
3239out:
NeilBrowne60d4392005-06-23 22:03:01 -07003240 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3241 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242}
3243
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003244static void init_nfs4_replay(struct nfs4_replay *rp)
3245{
3246 rp->rp_status = nfserr_serverfault;
3247 rp->rp_buflen = 0;
3248 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003249 mutex_init(&rp->rp_mutex);
3250}
3251
3252static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3253 struct nfs4_stateowner *so)
3254{
3255 if (!nfsd4_has_session(cstate)) {
3256 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003257 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04003258 }
3259}
3260
3261void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3262{
3263 struct nfs4_stateowner *so = cstate->replay_owner;
3264
3265 if (so != NULL) {
3266 cstate->replay_owner = NULL;
3267 mutex_unlock(&so->so_replay.rp_mutex);
3268 nfs4_put_stateowner(so);
3269 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003270}
3271
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003272static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273{
3274 struct nfs4_stateowner *sop;
3275
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003276 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003277 if (!sop)
3278 return NULL;
3279
3280 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3281 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003282 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003283 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003285 sop->so_owner.len = owner->len;
3286
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003287 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003288 sop->so_client = clp;
3289 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003290 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003291 return sop;
3292}
3293
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003294static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003295{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003296 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003297
Trond Myklebustd4f04892014-07-29 21:34:36 -04003298 list_add(&oo->oo_owner.so_strhash,
3299 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003300 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301}
3302
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003303static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3304{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003305 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003306}
3307
Jeff Layton6b180f02014-07-29 21:34:26 -04003308static void nfs4_free_openowner(struct nfs4_stateowner *so)
3309{
3310 struct nfs4_openowner *oo = openowner(so);
3311
3312 kmem_cache_free(openowner_slab, oo);
3313}
3314
3315static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003316 .so_unhash = nfs4_unhash_openowner,
3317 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003318};
3319
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003320static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003321alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003322 struct nfsd4_compound_state *cstate)
3323{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003324 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003325 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003327 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3328 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003330 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003331 oo->oo_owner.so_is_open_owner = 1;
3332 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003333 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003334 if (nfsd4_has_session(cstate))
3335 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003336 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003337 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003338 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003339 spin_lock(&clp->cl_lock);
3340 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003341 if (ret == NULL) {
3342 hash_openowner(oo, clp, strhashval);
3343 ret = oo;
3344 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08003345 nfs4_free_stateowner(&oo->oo_owner);
3346
Trond Myklebustd4f04892014-07-29 21:34:36 -04003347 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04003348 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349}
3350
J. Bruce Fields996e0932011-10-17 11:14:48 -04003351static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003352 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003354 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003355 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003356 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003357 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07003358 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003359 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 stp->st_access_bmap = 0;
3361 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003362 stp->st_openstp = NULL;
Jeff Layton35a92fe2015-09-17 07:47:08 -04003363 init_rwsem(&stp->st_rwsem);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003364 spin_lock(&oo->oo_owner.so_client->cl_lock);
3365 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003366 spin_lock(&fp->fi_lock);
3367 list_add(&stp->st_perfile, &fp->fi_stateids);
3368 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003369 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370}
3371
Jeff Laytond3134b12014-07-29 21:34:32 -04003372/*
3373 * In the 4.0 case we need to keep the owners around a little while to handle
3374 * CLOSE replay. We still do need to release any file access that is held by
3375 * them before returning however.
3376 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003378move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379{
Jeff Layton217526e2014-07-30 08:27:11 -04003380 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003381 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3382 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3383 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003384
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003385 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386
Jeff Laytonb401be222014-07-29 21:34:33 -04003387 /*
3388 * We know that we hold one reference via nfsd4_close, and another
3389 * "persistent" reference for the client. If the refcount is higher
3390 * than 2, then there are still calls in progress that are using this
3391 * stateid. We can't put the sc_file reference until they are finished.
3392 * Wait for the refcount to drop to 2. Since it has been unhashed,
3393 * there should be no danger of the refcount going back up again at
3394 * this point.
3395 */
3396 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3397
Jeff Laytond3134b12014-07-29 21:34:32 -04003398 release_all_access(s);
3399 if (s->st_stid.sc_file) {
3400 put_nfs4_file(s->st_stid.sc_file);
3401 s->st_stid.sc_file = NULL;
3402 }
Jeff Layton217526e2014-07-30 08:27:11 -04003403
3404 spin_lock(&nn->client_lock);
3405 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003406 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003407 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003408 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003409 spin_unlock(&nn->client_lock);
3410 if (last)
3411 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412}
3413
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414/* search file_hashtbl[] for file */
3415static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04003416find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 struct nfs4_file *fp;
3419
Jeff Layton5b095e92014-10-23 08:01:02 -04003420 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02003421 if (fh_match(&fp->fi_fhandle, fh)) {
Jeff Layton5b095e92014-10-23 08:01:02 -04003422 if (atomic_inc_not_zero(&fp->fi_ref))
3423 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 }
3426 return NULL;
3427}
3428
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02003429struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003430find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003431{
3432 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003433 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003434
Jeff Layton5b095e92014-10-23 08:01:02 -04003435 rcu_read_lock();
3436 fp = find_file_locked(fh, hashval);
3437 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04003438 return fp;
3439}
3440
3441static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003442find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003443{
3444 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003445 unsigned int hashval = file_hashval(fh);
3446
3447 rcu_read_lock();
3448 fp = find_file_locked(fh, hashval);
3449 rcu_read_unlock();
3450 if (fp)
3451 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04003452
3453 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04003454 fp = find_file_locked(fh, hashval);
3455 if (likely(fp == NULL)) {
3456 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04003457 fp = new;
3458 }
3459 spin_unlock(&state_lock);
3460
3461 return fp;
3462}
3463
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003464/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 * Called to check deny when READ with all zero stateid or
3466 * WRITE with all zero or all one stateid
3467 */
Al Virob37ad282006-10-19 23:28:59 -07003468static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3470{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003472 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
Trond Myklebustca943212014-07-23 16:17:39 -04003474 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003475 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003476 return ret;
3477 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003478 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003479 if (fp->fi_share_deny & deny_type)
3480 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003481 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003482 put_nfs4_file(fp);
3483 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484}
3485
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003486static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003488 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04003489 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3490 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003491
Trond Myklebust11b91642014-07-29 21:34:08 -04003492 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003493
Jeff Layton02e12152014-07-16 10:31:57 -04003494 /*
3495 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003496 * already holding inode->i_lock.
3497 *
Jeff Laytondff13992014-07-08 14:02:49 -04003498 * If the dl_time != 0, then we know that it has already been
3499 * queued for a lease break. Don't queue it again.
3500 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003501 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003502 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003503 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003504 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003505 }
Jeff Layton02e12152014-07-16 10:31:57 -04003506 spin_unlock(&state_lock);
3507}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003509static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3510 struct rpc_task *task)
3511{
3512 struct nfs4_delegation *dp = cb_to_delegation(cb);
3513
Andrew Elblea4579742015-08-31 12:06:41 -04003514 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
3515 return 1;
3516
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003517 switch (task->tk_status) {
3518 case 0:
3519 return 1;
3520 case -EBADHANDLE:
3521 case -NFS4ERR_BAD_STATEID:
3522 /*
3523 * Race: client probably got cb_recall before open reply
3524 * granting delegation.
3525 */
3526 if (dp->dl_retries--) {
3527 rpc_delay(task, 2 * HZ);
3528 return 0;
3529 }
3530 /*FALLTHRU*/
3531 default:
3532 return -1;
3533 }
3534}
3535
3536static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3537{
3538 struct nfs4_delegation *dp = cb_to_delegation(cb);
3539
3540 nfs4_put_stid(&dp->dl_stid);
3541}
3542
3543static struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3544 .prepare = nfsd4_cb_recall_prepare,
3545 .done = nfsd4_cb_recall_done,
3546 .release = nfsd4_cb_recall_release,
3547};
3548
Jeff Layton02e12152014-07-16 10:31:57 -04003549static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3550{
3551 /*
3552 * We're assuming the state code never drops its reference
3553 * without first removing the lease. Since we're in this lease
3554 * callback (and since the lease code is serialized by the kernel
3555 * lock) we know the server hasn't removed the lease yet, we know
3556 * it's safe to take a reference.
3557 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003558 atomic_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02003559 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003560}
3561
Jeff Layton1c8c6012013-06-21 08:58:15 -04003562/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003563static bool
3564nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003565{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003566 bool ret = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003567 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3568 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003569
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003570 if (!fp) {
3571 WARN(1, "(%p)->fl_owner NULL\n", fl);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003572 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003573 }
3574 if (fp->fi_had_conflict) {
3575 WARN(1, "duplicate break on %p\n", fp);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003576 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003577 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003578 /*
3579 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003580 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003581 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003582 */
3583 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
Jeff Layton02e12152014-07-16 10:31:57 -04003585 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003586 fp->fi_had_conflict = true;
3587 /*
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003588 * If there are no delegations on the list, then return true
3589 * so that the lease code will go ahead and delete it.
Jeff Layton417c6622014-07-21 09:34:57 -04003590 */
3591 if (list_empty(&fp->fi_delegations))
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003592 ret = true;
Jeff Layton417c6622014-07-21 09:34:57 -04003593 else
3594 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3595 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003596 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003597 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598}
3599
Jeff Laytonc45198e2014-09-01 07:12:07 -04003600static int
Jeff Layton7448cc32015-01-16 15:05:57 -05003601nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3602 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603{
3604 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04003605 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 else
3607 return -EAGAIN;
3608}
3609
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003610static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003611 .lm_break = nfsd_break_deleg_cb,
3612 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613};
3614
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003615static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3616{
3617 if (nfsd4_has_session(cstate))
3618 return nfs_ok;
3619 if (seqid == so->so_seqid - 1)
3620 return nfserr_replay_me;
3621 if (seqid == so->so_seqid)
3622 return nfs_ok;
3623 return nfserr_bad_seqid;
3624}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625
Jeff Layton4b24ca72014-06-30 11:48:44 -04003626static __be32 lookup_clientid(clientid_t *clid,
3627 struct nfsd4_compound_state *cstate,
3628 struct nfsd_net *nn)
3629{
3630 struct nfs4_client *found;
3631
3632 if (cstate->clp) {
3633 found = cstate->clp;
3634 if (!same_clid(&found->cl_clientid, clid))
3635 return nfserr_stale_clientid;
3636 return nfs_ok;
3637 }
3638
3639 if (STALE_CLIENTID(clid, nn))
3640 return nfserr_stale_clientid;
3641
3642 /*
3643 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3644 * cached already then we know this is for is for v4.0 and "sessions"
3645 * will be false.
3646 */
3647 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003648 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003649 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003650 if (!found) {
3651 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003652 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003653 }
3654 atomic_inc(&found->cl_refcount);
3655 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003656
3657 /* Cache the nfs4_client in cstate! */
3658 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003659 return nfs_ok;
3660}
3661
Al Virob37ad282006-10-19 23:28:59 -07003662__be32
Andy Adamson66689582009-04-03 08:28:45 +03003663nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003664 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 clientid_t *clientid = &open->op_clientid;
3667 struct nfs4_client *clp = NULL;
3668 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003669 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003670 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003672 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003674 /*
3675 * In case we need it later, after we've already created the
3676 * file and don't want to risk a further failure:
3677 */
3678 open->op_file = nfsd4_alloc_file();
3679 if (open->op_file == NULL)
3680 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681
Trond Myklebust2d91e892014-06-30 11:48:46 -04003682 status = lookup_clientid(clientid, cstate, nn);
3683 if (status)
3684 return status;
3685 clp = cstate->clp;
3686
Trond Myklebustd4f04892014-07-29 21:34:36 -04003687 strhashval = ownerstr_hashval(&open->op_owner);
3688 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003689 open->op_openowner = oo;
3690 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003691 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003693 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003694 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003695 release_openowner(oo);
3696 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003697 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003698 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003699 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3700 if (status)
3701 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003702 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003703new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003704 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003705 if (oo == NULL)
3706 return nfserr_jukebox;
3707 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003708alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003709 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003710 if (!open->op_stp)
3711 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003712
3713 if (nfsd4_has_session(cstate) &&
3714 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
3715 open->op_odstate = alloc_clnt_odstate(clp);
3716 if (!open->op_odstate)
3717 return nfserr_jukebox;
3718 }
3719
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003720 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721}
3722
Al Virob37ad282006-10-19 23:28:59 -07003723static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003724nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3725{
3726 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3727 return nfserr_openmode;
3728 else
3729 return nfs_ok;
3730}
3731
Daniel Mackc47d8322011-05-16 16:38:14 +02003732static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003733{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003734 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3735}
3736
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003737static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003738{
3739 struct nfs4_stid *ret;
3740
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003741 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003742 if (!ret)
3743 return NULL;
3744 return delegstateid(ret);
3745}
3746
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003747static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3748{
3749 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3750 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3751}
3752
Al Virob37ad282006-10-19 23:28:59 -07003753static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003754nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003755 struct nfs4_delegation **dp)
3756{
3757 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003758 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003759 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003760
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003761 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3762 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003763 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003764 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003765 status = nfs4_check_delegmode(deleg, flags);
3766 if (status) {
3767 nfs4_put_stid(&deleg->dl_stid);
3768 goto out;
3769 }
3770 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003771out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003772 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003773 return nfs_ok;
3774 if (status)
3775 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003776 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003777 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003778}
3779
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003780static struct nfs4_ol_stateid *
3781nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003783 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003784 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
Trond Myklebust1d31a252014-07-10 14:07:25 -04003786 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003787 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 /* ignore lock owners */
3789 if (local->st_stateowner->so_is_open_owner == 0)
3790 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003791 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003792 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003793 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003794 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003797 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003798 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799}
3800
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003801static inline int nfs4_access_to_access(u32 nfs4_access)
3802{
3803 int flags = 0;
3804
3805 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3806 flags |= NFSD_MAY_READ;
3807 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3808 flags |= NFSD_MAY_WRITE;
3809 return flags;
3810}
3811
Al Virob37ad282006-10-19 23:28:59 -07003812static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3814 struct nfsd4_open *open)
3815{
3816 struct iattr iattr = {
3817 .ia_valid = ATTR_SIZE,
3818 .ia_size = 0,
3819 };
3820 if (!open->op_truncate)
3821 return 0;
3822 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003823 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3825}
3826
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003827static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003828 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3829 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003830{
Trond Myklebustde186432014-07-10 14:07:26 -04003831 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003832 __be32 status;
3833 int oflag = nfs4_access_to_omode(open->op_share_access);
3834 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003835 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003836
Trond Myklebustde186432014-07-10 14:07:26 -04003837 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003838
3839 /*
3840 * Are we trying to set a deny mode that would conflict with
3841 * current access?
3842 */
3843 status = nfs4_file_check_deny(fp, open->op_share_deny);
3844 if (status != nfs_ok) {
3845 spin_unlock(&fp->fi_lock);
3846 goto out;
3847 }
3848
3849 /* set access to the file */
3850 status = nfs4_file_get_access(fp, open->op_share_access);
3851 if (status != nfs_ok) {
3852 spin_unlock(&fp->fi_lock);
3853 goto out;
3854 }
3855
3856 /* Set access bits in stateid */
3857 old_access_bmap = stp->st_access_bmap;
3858 set_access(open->op_share_access, stp);
3859
3860 /* Set new deny mask */
3861 old_deny_bmap = stp->st_deny_bmap;
3862 set_deny(open->op_share_deny, stp);
3863 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3864
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003865 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003866 spin_unlock(&fp->fi_lock);
3867 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003868 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003869 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003870 spin_lock(&fp->fi_lock);
3871 if (!fp->fi_fds[oflag]) {
3872 fp->fi_fds[oflag] = filp;
3873 filp = NULL;
3874 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003875 }
Trond Myklebustde186432014-07-10 14:07:26 -04003876 spin_unlock(&fp->fi_lock);
3877 if (filp)
3878 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003879
3880 status = nfsd4_truncate(rqstp, cur_fh, open);
3881 if (status)
3882 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003883out:
3884 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003885out_put_access:
3886 stp->st_access_bmap = old_access_bmap;
3887 nfs4_file_put_access(fp, open->op_share_access);
3888 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3889 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003890}
3891
Al Virob37ad282006-10-19 23:28:59 -07003892static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003893nfs4_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 -07003894{
Al Virob37ad282006-10-19 23:28:59 -07003895 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02003896 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003898 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003899 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003900
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003901 /* test and set deny mode */
3902 spin_lock(&fp->fi_lock);
3903 status = nfs4_file_check_deny(fp, open->op_share_deny);
3904 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003905 set_deny(open->op_share_deny, stp);
3906 fp->fi_share_deny |=
3907 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3908 }
3909 spin_unlock(&fp->fi_lock);
3910
3911 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003914 status = nfsd4_truncate(rqstp, cur_fh, open);
3915 if (status != nfs_ok)
3916 reset_union_bmap_deny(old_deny_bmap, stp);
3917 return status;
3918}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003920/* Should we give out recallable state?: */
3921static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3922{
3923 if (clp->cl_cb_state == NFSD4_CB_UP)
3924 return true;
3925 /*
3926 * In the sessions case, since we don't have to establish a
3927 * separate connection for callbacks, we assume it's OK
3928 * until we hear otherwise:
3929 */
3930 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3931}
3932
Jeff Laytond564fbe2014-07-16 10:31:58 -04003933static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003934{
3935 struct file_lock *fl;
3936
3937 fl = locks_alloc_lock();
3938 if (!fl)
3939 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003940 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003941 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003942 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3943 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003944 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003945 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003946 return fl;
3947}
3948
J. Bruce Fields99c41512013-05-21 16:21:25 -04003949static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003950{
Trond Myklebust11b91642014-07-29 21:34:08 -04003951 struct nfs4_file *fp = dp->dl_stid.sc_file;
Kinglong Meeefde6b42015-07-13 17:33:24 +08003952 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003953 struct file *filp;
3954 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003955
Jeff Laytond564fbe2014-07-16 10:31:58 -04003956 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003957 if (!fl)
3958 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003959 filp = find_readable_file(fp);
3960 if (!filp) {
3961 /* We should always have a readable file here */
3962 WARN_ON_ONCE(1);
Kinglong Meeaf9dbaf2015-07-13 17:30:51 +08003963 locks_free_lock(fl);
Jeff Layton417c6622014-07-21 09:34:57 -04003964 return -EBADF;
3965 }
3966 fl->fl_file = filp;
Jeff Laytone6f5c782014-08-22 10:40:25 -04003967 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04003968 if (fl)
Jeff Layton417c6622014-07-21 09:34:57 -04003969 locks_free_lock(fl);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04003970 if (status)
Jeff Layton417c6622014-07-21 09:34:57 -04003971 goto out_fput;
Benny Halevycdc97502014-05-30 09:09:30 -04003972 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003973 spin_lock(&fp->fi_lock);
3974 /* Did the lease get broken before we took the lock? */
3975 status = -EAGAIN;
3976 if (fp->fi_had_conflict)
3977 goto out_unlock;
3978 /* Race breaker */
Jeff Layton0c637be2014-08-22 12:05:43 -04003979 if (fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04003980 status = 0;
Jeff Layton67db1032014-12-13 09:11:40 -05003981 ++fp->fi_delegees;
Jeff Layton417c6622014-07-21 09:34:57 -04003982 hash_delegation_locked(dp, fp);
3983 goto out_unlock;
3984 }
Jeff Layton417c6622014-07-21 09:34:57 -04003985 fp->fi_deleg_file = filp;
Jeff Layton67db1032014-12-13 09:11:40 -05003986 fp->fi_delegees = 1;
Benny Halevy931ee562014-05-30 09:09:27 -04003987 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003988 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003989 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003990 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003991out_unlock:
3992 spin_unlock(&fp->fi_lock);
3993 spin_unlock(&state_lock);
3994out_fput:
3995 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003996 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003997}
3998
Jeff Layton0b266932014-07-25 07:34:25 -04003999static struct nfs4_delegation *
4000nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02004001 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004002{
Jeff Layton0b266932014-07-25 07:34:25 -04004003 int status;
4004 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04004005
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004006 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004007 return ERR_PTR(-EAGAIN);
4008
Sachin Bhamare8287f002015-04-27 14:50:14 +02004009 dp = alloc_init_deleg(clp, fh, odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004010 if (!dp)
4011 return ERR_PTR(-ENOMEM);
4012
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004013 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04004014 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004015 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04004016 dp->dl_stid.sc_file = fp;
Jeff Layton0c637be2014-08-22 12:05:43 -04004017 if (!fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04004018 spin_unlock(&fp->fi_lock);
4019 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04004020 status = nfs4_setlease(dp);
4021 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04004022 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004023 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04004024 status = -EAGAIN;
4025 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004026 }
Jeff Layton67db1032014-12-13 09:11:40 -05004027 ++fp->fi_delegees;
Benny Halevy931ee562014-05-30 09:09:27 -04004028 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04004029 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04004030out_unlock:
4031 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04004032 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04004033out:
4034 if (status) {
Sachin Bhamare8287f002015-04-27 14:50:14 +02004035 put_clnt_odstate(dp->dl_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04004036 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04004037 return ERR_PTR(status);
4038 }
4039 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004040}
4041
Benny Halevy4aa89132012-02-21 14:16:44 -08004042static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4043{
4044 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4045 if (status == -EAGAIN)
4046 open->op_why_no_deleg = WND4_CONTENTION;
4047 else {
4048 open->op_why_no_deleg = WND4_RESOURCE;
4049 switch (open->op_deleg_want) {
4050 case NFS4_SHARE_WANT_READ_DELEG:
4051 case NFS4_SHARE_WANT_WRITE_DELEG:
4052 case NFS4_SHARE_WANT_ANY_DELEG:
4053 break;
4054 case NFS4_SHARE_WANT_CANCEL:
4055 open->op_why_no_deleg = WND4_CANCELLED;
4056 break;
4057 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004058 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08004059 }
4060 }
4061}
4062
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063/*
4064 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04004065 *
4066 * Note we don't support write delegations, and won't until the vfs has
4067 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 */
4069static void
Jeff Layton4cf59222014-07-25 07:34:24 -04004070nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4071 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072{
4073 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04004074 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4075 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004076 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004077 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004079 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07004080 open->op_recall = 0;
4081 switch (open->op_claim_type) {
4082 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05004083 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07004084 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004085 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4086 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004087 break;
4088 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00004089 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004090 /*
4091 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004092 * had the chance to reclaim theirs, *and* until
4093 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004094 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004095 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004096 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004097 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004098 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04004099 /*
4100 * Also, if the file was opened for write or
4101 * create, there's a good chance the client's
4102 * about to write to it, resulting in an
4103 * immediate recall (since we don't support
4104 * write delegations):
4105 */
NeilBrown7b190fe2005-06-23 22:03:23 -07004106 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04004107 goto out_no_deleg;
4108 if (open->op_create == NFS4_OPEN_CREATE)
4109 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004110 break;
4111 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004112 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004113 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004114 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004115 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004116 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004118 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004120 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004121 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04004122 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04004123 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004124 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004125out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004126 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4127 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004128 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04004129 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004130 open->op_recall = 1;
4131 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04004132
4133 /* 4.1 client asking for a delegation? */
4134 if (open->op_deleg_want)
4135 nfsd4_open_deleg_none_ext(open, status);
4136 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137}
4138
Benny Halevye27f49c2012-02-21 14:16:54 -08004139static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4140 struct nfs4_delegation *dp)
4141{
4142 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4143 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4144 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4145 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4146 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4147 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4148 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4149 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4150 }
4151 /* Otherwise the client must be confused wanting a delegation
4152 * it already has, therefore we don't return
4153 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4154 */
4155}
4156
Al Virob37ad282006-10-19 23:28:59 -07004157__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4159{
Andy Adamson66689582009-04-03 08:28:45 +03004160 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004161 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004163 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004164 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004165 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 /*
4168 * Lookup file; if found, lookup stateid and check open request,
4169 * and check for delegations in the process of being recalled.
4170 * If not found, create the nfs4_file struct
4171 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004172 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004173 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004174 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004175 if (status)
4176 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04004177 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004179 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004180 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004181 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004182 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 }
4184
4185 /*
4186 * OPEN the file, or upgrade an existing OPEN.
4187 * If truncate fails, the OPEN fails.
4188 */
4189 if (stp) {
4190 /* Stateid was found, this is an OPEN upgrade */
Jeff Layton35a92fe2015-09-17 07:47:08 -04004191 down_read(&stp->st_rwsem);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004192 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04004193 if (status) {
4194 up_read(&stp->st_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04004196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004198 stp = open->op_stp;
4199 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04004200 init_open_stateid(stp, fp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04004201 down_read(&stp->st_rwsem);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004202 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4203 if (status) {
Jeff Layton35a92fe2015-09-17 07:47:08 -04004204 up_read(&stp->st_rwsem);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004205 release_open_stateid(stp);
4206 goto out;
4207 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004208
4209 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4210 open->op_odstate);
4211 if (stp->st_clnt_odstate == open->op_odstate)
4212 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004214 update_stateid(&stp->st_stid.sc_stateid);
4215 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Jeff Layton35a92fe2015-09-17 07:47:08 -04004216 up_read(&stp->st_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217
Benny Halevyd24433c2012-02-16 20:57:17 +02004218 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004219 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4220 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4221 open->op_why_no_deleg = WND4_NOT_WANTED;
4222 goto nodeleg;
4223 }
4224 }
4225
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 /*
4227 * Attempt to hand out a delegation. No error return, because the
4228 * OPEN succeeds even if we fail.
4229 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004230 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004231nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 status = nfs_ok;
4233
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004234 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004235 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004237 /* 4.1 client trying to upgrade/downgrade delegation? */
4238 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004239 open->op_deleg_want)
4240 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004241
NeilBrown13cd2182005-06-23 22:03:10 -07004242 if (fp)
4243 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004244 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08004245 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 /*
4247 * To finish the open response, we just need to set the rflags.
4248 */
4249 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004250 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004251 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004253 if (dp)
4254 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004255 if (stp)
4256 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257
4258 return status;
4259}
4260
Jeff Layton58fb12e2014-07-29 21:34:27 -04004261void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04004262 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004263{
4264 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004265 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004266
Jeff Laytond3134b12014-07-29 21:34:32 -04004267 nfsd4_cstate_assign_replay(cstate, so);
4268 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004269 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004270 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04004271 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004272 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004273 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004274 if (open->op_odstate)
4275 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004276}
4277
Al Virob37ad282006-10-19 23:28:59 -07004278__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004279nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4280 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281{
4282 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004283 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004284 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 dprintk("process_renew(%08x/%08x): starting\n",
4287 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004288 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004289 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004291 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004293 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004294 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 goto out;
4296 status = nfs_ok;
4297out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 return status;
4299}
4300
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04004301void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004302nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004303{
Jeff Layton33dcc482012-04-10 11:08:48 -04004304 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004305 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004306 return;
4307
NeilBrowna76b4312005-06-23 22:04:01 -07004308 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004309 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04004310 /*
4311 * If the server goes down again right now, an NFSv4
4312 * client will still be allowed to reclaim after it comes back up,
4313 * even if it hasn't yet had a chance to reclaim state this time.
4314 *
4315 */
Jeff Layton919b8042014-09-12 16:40:20 -04004316 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004317 /*
4318 * At this point, NFSv4 clients can still reclaim. But if the
4319 * server crashes, any that have not yet reclaimed will be out
4320 * of luck on the next boot.
4321 *
4322 * (NFSv4.1+ clients are considered to have reclaimed once they
4323 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4324 * have reclaimed after their first OPEN.)
4325 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004326 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004327 /*
4328 * At this point, and once lockd and/or any other containers
4329 * exit their grace period, further reclaims will fail and
4330 * regular locking can resume.
4331 */
NeilBrowna76b4312005-06-23 22:04:01 -07004332}
4333
NeilBrownfd39ca92005-06-23 22:04:03 -07004334static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004335nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336{
4337 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004338 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004340 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004342 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004343 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004346 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004347 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004348 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004349 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 clp = list_entry(pos, struct nfs4_client, cl_lru);
4351 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4352 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004353 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 break;
4355 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004356 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004357 dprintk("NFSD: client in use (clientid %08x)\n",
4358 clp->cl_clientid.cl_id);
4359 continue;
4360 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004361 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004362 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004363 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004364 list_for_each_safe(pos, next, &reaplist) {
4365 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 dprintk("NFSD: purging unused client (clientid %08x)\n",
4367 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004368 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 expire_client(clp);
4370 }
Benny Halevycdc97502014-05-30 09:09:30 -04004371 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004372 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4374 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004375 t = dp->dl_time - cutoff;
4376 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 break;
4378 }
Jeff Layton3fcbbd22015-08-24 12:41:48 -04004379 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04004380 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 }
Benny Halevycdc97502014-05-30 09:09:30 -04004382 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004383 while (!list_empty(&reaplist)) {
4384 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4385 dl_recall_lru);
4386 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004387 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 }
Jeff Layton217526e2014-07-30 08:27:11 -04004389
4390 spin_lock(&nn->client_lock);
4391 while (!list_empty(&nn->close_lru)) {
4392 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4393 oo_close_lru);
4394 if (time_after((unsigned long)oo->oo_time,
4395 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004396 t = oo->oo_time - cutoff;
4397 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 break;
4399 }
Jeff Layton217526e2014-07-30 08:27:11 -04004400 list_del_init(&oo->oo_close_lru);
4401 stp = oo->oo_last_closed_stid;
4402 oo->oo_last_closed_stid = NULL;
4403 spin_unlock(&nn->client_lock);
4404 nfs4_put_stid(&stp->st_stid);
4405 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 }
Jeff Layton217526e2014-07-30 08:27:11 -04004407 spin_unlock(&nn->client_lock);
4408
Jeff Laytona832e7a2014-05-30 09:09:26 -04004409 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004410 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411}
4412
Harvey Harrisona254b242008-02-20 12:49:00 -08004413static struct workqueue_struct *laundry_wq;
4414static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004415
4416static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004417laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418{
4419 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004420 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4421 work);
4422 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4423 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004425 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004427 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428}
4429
Jeff Layton8fcd4612015-07-30 06:57:46 -04004430static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004431{
Jeff Layton8fcd4612015-07-30 06:57:46 -04004432 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004433 return nfserr_bad_stateid;
4434 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435}
4436
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004438access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004440 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4441 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4442 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443}
4444
4445static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004446access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004448 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4449 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450}
4451
4452static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004453__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454{
Al Virob37ad282006-10-19 23:28:59 -07004455 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456
J. Bruce Fields02921912010-07-29 15:16:59 -04004457 /* For lock stateid's, we test the parent open, not the lock: */
4458 if (stp->st_openstp)
4459 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004460 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004462 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 goto out;
4464 status = nfs_ok;
4465out:
4466 return status;
4467}
4468
Al Virob37ad282006-10-19 23:28:59 -07004469static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004470check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004472 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004474 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004475 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 * conflicting state; so we must wait out the grace period. */
4477 return nfserr_grace;
4478 } else if (flags & WR_STATE)
4479 return nfs4_share_conflict(current_fh,
4480 NFS4_SHARE_DENY_WRITE);
4481 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4482 return nfs4_share_conflict(current_fh,
4483 NFS4_SHARE_DENY_READ);
4484}
4485
4486/*
4487 * Allow READ/WRITE during grace period on recovered state only for files
4488 * that are not able to provide mandatory locking.
4489 */
4490static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004491grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004493 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494}
4495
J. Bruce Fields81b82962011-08-23 11:03:29 -04004496/* Returns true iff a is later than b: */
4497static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4498{
Jim Rees1a9357f2013-05-17 17:33:00 -04004499 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004500}
4501
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004502static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004503{
Andy Adamson66689582009-04-03 08:28:45 +03004504 /*
4505 * When sessions are used the stateid generation number is ignored
4506 * when it is zero.
4507 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004508 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004509 return nfs_ok;
4510
4511 if (in->si_generation == ref->si_generation)
4512 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004513
J. Bruce Fields0836f582008-01-26 19:08:12 -05004514 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004515 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004516 return nfserr_bad_stateid;
4517 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004518 * However, we could see a stateid from the past, even from a
4519 * non-buggy client. For example, if the client sends a lock
4520 * while some IO is outstanding, the lock may bump si_generation
4521 * while the IO is still in flight. The client could avoid that
4522 * situation by waiting for responses on all the IO requests,
4523 * but better performance may result in retrying IO that
4524 * receives an old_stateid error if requests are rarely
4525 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004526 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004527 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004528}
4529
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004530static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4531{
4532 if (ols->st_stateowner->so_is_open_owner &&
4533 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4534 return nfserr_bad_stateid;
4535 return nfs_ok;
4536}
4537
Chuck Lever7df302f2012-05-29 13:56:37 -04004538static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004539{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004540 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004541 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004542
Chuck Lever7df302f2012-05-29 13:56:37 -04004543 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004544 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004545 /* Client debugging aid. */
4546 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4547 char addr_str[INET6_ADDRSTRLEN];
4548 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4549 sizeof(addr_str));
4550 pr_warn_ratelimited("NFSD: client %s testing state ID "
4551 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004552 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004553 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004554 spin_lock(&cl->cl_lock);
4555 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004556 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004557 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004558 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004559 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004560 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004561 switch (s->sc_type) {
4562 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004563 status = nfs_ok;
4564 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004565 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004566 status = nfserr_deleg_revoked;
4567 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004568 case NFS4_OPEN_STID:
4569 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004570 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04004571 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004572 default:
4573 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004574 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004575 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004576 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004577 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004578 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004579out_unlock:
4580 spin_unlock(&cl->cl_lock);
4581 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004582}
4583
Christoph Hellwigcd61c522014-08-14 08:44:57 +02004584__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004585nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4586 stateid_t *stateid, unsigned char typemask,
4587 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004588{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004589 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004590
4591 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4592 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004593 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004594 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004595 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004596 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004597 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004598 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004599 if (status)
4600 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004601 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004602 if (!*s)
4603 return nfserr_bad_stateid;
4604 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004605}
4606
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004607static struct file *
4608nfs4_find_file(struct nfs4_stid *s, int flags)
4609{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004610 if (!s)
4611 return NULL;
4612
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004613 switch (s->sc_type) {
4614 case NFS4_DELEG_STID:
4615 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
4616 return NULL;
4617 return get_file(s->sc_file->fi_deleg_file);
4618 case NFS4_OPEN_STID:
4619 case NFS4_LOCK_STID:
4620 if (flags & RD_STATE)
4621 return find_readable_file(s->sc_file);
4622 else
4623 return find_writeable_file(s->sc_file);
4624 break;
4625 }
4626
4627 return NULL;
4628}
4629
4630static __be32
4631nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
4632{
4633 __be32 status;
4634
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004635 status = nfsd4_check_openowner_confirmed(ols);
4636 if (status)
4637 return status;
4638 return nfs4_check_openmode(ols, flags);
4639}
4640
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004641static __be32
4642nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
4643 struct file **filpp, bool *tmp_file, int flags)
4644{
4645 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
4646 struct file *file;
4647 __be32 status;
4648
4649 file = nfs4_find_file(s, flags);
4650 if (file) {
4651 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
4652 acc | NFSD_MAY_OWNER_OVERRIDE);
4653 if (status) {
4654 fput(file);
4655 return status;
4656 }
4657
4658 *filpp = file;
4659 } else {
4660 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
4661 if (status)
4662 return status;
4663
4664 if (tmp_file)
4665 *tmp_file = true;
4666 }
4667
4668 return 0;
4669}
4670
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004672 * Checks for stateid operations
4673 */
Al Virob37ad282006-10-19 23:28:59 -07004674__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004675nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
4676 struct nfsd4_compound_state *cstate, stateid_t *stateid,
4677 int flags, struct file **filpp, bool *tmp_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004679 struct svc_fh *fhp = &cstate->current_fh;
4680 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004681 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004682 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004683 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004684 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 if (filpp)
4687 *filpp = NULL;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004688 if (tmp_file)
4689 *tmp_file = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004691 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 return nfserr_grace;
4693
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004694 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
4695 status = check_special_stateids(net, fhp, stateid, flags);
4696 goto done;
4697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004699 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004700 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004701 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004702 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04004703 return status;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004704 status = check_stateid_generation(stateid, &s->sc_stateid,
4705 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04004706 if (status)
4707 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004708
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004709 switch (s->sc_type) {
4710 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004711 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004712 break;
4713 case NFS4_OPEN_STID:
4714 case NFS4_LOCK_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004715 status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004716 break;
4717 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004718 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004719 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04004721 if (status)
4722 goto out;
4723 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004724
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004725done:
4726 if (!status && filpp)
4727 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728out:
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004729 if (s)
4730 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 return status;
4732}
4733
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004734/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004735 * Test if the stateid is valid
4736 */
4737__be32
4738nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4739 struct nfsd4_test_stateid *test_stateid)
4740{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004741 struct nfsd4_test_stateid_id *stateid;
4742 struct nfs4_client *cl = cstate->session->se_client;
4743
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004744 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004745 stateid->ts_id_status =
4746 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004747
Bryan Schumaker17456802011-07-13 10:50:48 -04004748 return nfs_ok;
4749}
4750
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004751__be32
4752nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4753 struct nfsd4_free_stateid *free_stateid)
4754{
4755 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004756 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004757 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004758 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004759 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004760 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004761
Jeff Layton1af71cc2014-07-29 21:34:14 -04004762 spin_lock(&cl->cl_lock);
4763 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004764 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004765 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004766 switch (s->sc_type) {
4767 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004768 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004769 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004770 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004771 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4772 if (ret)
4773 break;
4774 ret = nfserr_locks_held;
4775 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004776 case NFS4_LOCK_STID:
4777 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4778 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004779 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004780 stp = openlockstateid(s);
4781 ret = nfserr_locks_held;
4782 if (check_for_locks(stp->st_stid.sc_file,
4783 lockowner(stp->st_stateowner)))
4784 break;
Jeff Laytone8568732015-08-24 12:41:47 -04004785 WARN_ON(!unhash_lock_stateid(stp));
Jeff Layton1af71cc2014-07-29 21:34:14 -04004786 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004787 nfs4_put_stid(s);
4788 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004789 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004790 case NFS4_REVOKED_DELEG_STID:
4791 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004792 list_del_init(&dp->dl_recall_lru);
4793 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004794 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004795 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004796 goto out;
4797 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004798 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004799out_unlock:
4800 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004801out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004802 return ret;
4803}
4804
NeilBrown4c4cd222005-07-07 17:59:27 -07004805static inline int
4806setlkflg (int type)
4807{
4808 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4809 RD_STATE : WR_STATE;
4810}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004812static __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 -04004813{
4814 struct svc_fh *current_fh = &cstate->current_fh;
4815 struct nfs4_stateowner *sop = stp->st_stateowner;
4816 __be32 status;
4817
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004818 status = nfsd4_check_seqid(cstate, sop, seqid);
4819 if (status)
4820 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004821 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4822 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004823 /*
4824 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004825 * nfserr_replay_me from the previous step, and
4826 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004827 */
4828 return nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04004829 down_write(&stp->st_rwsem);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004830 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04004831 if (status == nfs_ok)
4832 status = nfs4_check_fh(current_fh, &stp->st_stid);
4833 if (status != nfs_ok)
4834 up_write(&stp->st_rwsem);
4835 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004836}
4837
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838/*
4839 * Checks for sequence id mutating operations.
4840 */
Al Virob37ad282006-10-19 23:28:59 -07004841static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004842nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004843 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004844 struct nfs4_ol_stateid **stpp,
4845 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004847 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004848 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004849 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004851 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4852 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004853
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004855 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004856 if (status)
4857 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004858 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004859 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004861 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004862 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004863 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004864 else
4865 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004866 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004867}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004868
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004869static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4870 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004871{
4872 __be32 status;
4873 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004874 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004876 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004877 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004878 if (status)
4879 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004880 oo = openowner(stp->st_stateowner);
4881 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Jeff Layton35a92fe2015-09-17 07:47:08 -04004882 up_write(&stp->st_rwsem);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004883 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004884 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004885 }
4886 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004887 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888}
4889
Al Virob37ad282006-10-19 23:28:59 -07004890__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004891nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004892 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893{
Al Virob37ad282006-10-19 23:28:59 -07004894 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004895 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004896 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004897 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898
Al Viroa6a9f182013-09-16 10:57:01 -04004899 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4900 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004902 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004903 if (status)
4904 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004906 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004907 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004908 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004909 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004910 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004911 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004912 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04004913 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
4914 up_write(&stp->st_rwsem);
Trond Myklebust2585fc72014-07-29 21:34:21 -04004915 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04004916 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004917 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004918 update_stateid(&stp->st_stid.sc_stateid);
4919 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Jeff Layton35a92fe2015-09-17 07:47:08 -04004920 up_write(&stp->st_rwsem);
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004921 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004922 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004923
Jeff Layton2a4317c2012-03-21 16:42:43 -04004924 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004925 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004926put_stateid:
4927 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004929 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 return status;
4931}
4932
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004933static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004935 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004936 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004937 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004938 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004939}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004940
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004941static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4942{
4943 switch (to_access) {
4944 case NFS4_SHARE_ACCESS_READ:
4945 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4946 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4947 break;
4948 case NFS4_SHARE_ACCESS_WRITE:
4949 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4950 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4951 break;
4952 case NFS4_SHARE_ACCESS_BOTH:
4953 break;
4954 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004955 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 }
4957}
4958
Al Virob37ad282006-10-19 23:28:59 -07004959__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004960nfsd4_open_downgrade(struct svc_rqst *rqstp,
4961 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004962 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963{
Al Virob37ad282006-10-19 23:28:59 -07004964 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004965 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004966 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967
Al Viroa6a9f182013-09-16 10:57:01 -04004968 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4969 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004971 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004972 if (od->od_deleg_want)
4973 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4974 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004976 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004977 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004978 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004981 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004982 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004984 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004986 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004987 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004989 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004991 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992
Jeff Laytonce0fc432012-05-11 09:45:14 -04004993 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004995 update_stateid(&stp->st_stid.sc_stateid);
4996 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004998put_stateid:
Jeff Layton35a92fe2015-09-17 07:47:08 -04004999 up_write(&stp->st_rwsem);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005000 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005002 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 return status;
5004}
5005
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005006static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5007{
Trond Myklebustacf92952014-06-30 11:48:37 -04005008 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04005009 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04005010 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005011
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005012 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04005013 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04005014 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005015
Jeff Laytond83017f2014-07-29 21:34:42 -04005016 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04005017 if (unhashed)
5018 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04005019 spin_unlock(&clp->cl_lock);
5020 free_ol_stateid_reaplist(&reaplist);
5021 } else {
5022 spin_unlock(&clp->cl_lock);
5023 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04005024 if (unhashed)
5025 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04005026 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04005027}
5028
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029/*
5030 * nfs4_unlock_state() called after encode
5031 */
Al Virob37ad282006-10-19 23:28:59 -07005032__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005033nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005034 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035{
Al Virob37ad282006-10-19 23:28:59 -07005036 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005037 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005038 struct net *net = SVC_NET(rqstp);
5039 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040
Al Viroa6a9f182013-09-16 10:57:01 -04005041 dprintk("NFSD: nfsd4_close on file %pd\n",
5042 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005044 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5045 &close->cl_stateid,
5046 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005047 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005048 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005049 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005051 update_stateid(&stp->st_stid.sc_stateid);
5052 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Jeff Layton35a92fe2015-09-17 07:47:08 -04005053 up_write(&stp->st_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005055 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005056
5057 /* put reference from nfs4_preprocess_seqid_op */
5058 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 return status;
5061}
5062
Al Virob37ad282006-10-19 23:28:59 -07005063__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005064nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5065 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005067 struct nfs4_delegation *dp;
5068 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005069 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07005070 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005071 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005073 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005074 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005076 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005077 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005078 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005079 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04005080 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005081 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04005082 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005083
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005084 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005085put_stateid:
5086 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087out:
5088 return status;
5089}
5090
Benny Halevy87df4de2008-12-15 19:42:03 +02005091static inline u64
5092end_offset(u64 start, u64 len)
5093{
5094 u64 end;
5095
5096 end = start + len;
5097 return end >= start ? end: NFS4_MAX_UINT64;
5098}
5099
5100/* last octet in a range */
5101static inline u64
5102last_byte_offset(u64 start, u64 len)
5103{
5104 u64 end;
5105
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005106 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02005107 end = start + len;
5108 return end > start ? end - 1: NFS4_MAX_UINT64;
5109}
5110
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111/*
5112 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5113 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5114 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5115 * locking, this prevents us from being completely protocol-compliant. The
5116 * real solution to this problem is to start using unsigned file offsets in
5117 * the VFS, but this is a very deep change!
5118 */
5119static inline void
5120nfs4_transform_lock_offset(struct file_lock *lock)
5121{
5122 if (lock->fl_start < 0)
5123 lock->fl_start = OFFSET_MAX;
5124 if (lock->fl_end < 0)
5125 lock->fl_end = OFFSET_MAX;
5126}
5127
Jeff Laytoncae80b32015-04-03 09:04:04 -04005128static fl_owner_t
5129nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005130{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005131 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5132
5133 nfs4_get_stateowner(&lo->lo_owner);
5134 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005135}
5136
Jeff Laytoncae80b32015-04-03 09:04:04 -04005137static void
5138nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005139{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005140 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005141
Jeff Laytoncae80b32015-04-03 09:04:04 -04005142 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04005143 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04005144}
5145
Alexey Dobriyan7b021962009-09-21 17:01:12 -07005146static const struct lock_manager_operations nfsd_posix_mng_ops = {
Kinglong Meeaef95832014-08-22 10:18:44 -04005147 .lm_get_owner = nfsd4_fl_get_owner,
5148 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07005149};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150
5151static inline void
5152nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5153{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005154 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155
NeilBrownd5b90262006-04-10 22:55:22 -07005156 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005157 lo = (struct nfs4_lockowner *) fl->fl_owner;
5158 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5159 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005160 if (!deny->ld_owner.data)
5161 /* We just don't care that much */
5162 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005163 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5164 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07005165 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005166nevermind:
5167 deny->ld_owner.len = 0;
5168 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07005169 deny->ld_clientid.cl_boot = 0;
5170 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 }
5172 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02005173 deny->ld_length = NFS4_MAX_UINT64;
5174 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5176 deny->ld_type = NFS4_READ_LT;
5177 if (fl->fl_type != F_RDLCK)
5178 deny->ld_type = NFS4_WRITE_LT;
5179}
5180
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005181static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005182find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183{
Trond Myklebustd4f04892014-07-29 21:34:36 -04005184 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005185 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186
Trond Myklebust0a880a22014-07-30 08:27:10 -04005187 lockdep_assert_held(&clp->cl_lock);
5188
Trond Myklebustd4f04892014-07-29 21:34:36 -04005189 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5190 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005191 if (so->so_is_open_owner)
5192 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005193 if (same_owner_str(so, owner))
5194 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 }
5196 return NULL;
5197}
5198
Trond Myklebustc58c6612014-07-29 21:34:35 -04005199static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005200find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04005201{
5202 struct nfs4_lockowner *lo;
5203
Trond Myklebustd4f04892014-07-29 21:34:36 -04005204 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005205 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005206 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005207 return lo;
5208}
5209
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005210static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5211{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005212 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005213}
5214
Jeff Layton6b180f02014-07-29 21:34:26 -04005215static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5216{
5217 struct nfs4_lockowner *lo = lockowner(sop);
5218
5219 kmem_cache_free(lockowner_slab, lo);
5220}
5221
5222static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005223 .so_unhash = nfs4_unhash_lockowner,
5224 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005225};
5226
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227/*
5228 * Alloc a lock owner structure.
5229 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005230 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005232 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005234static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005235alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5236 struct nfs4_ol_stateid *open_stp,
5237 struct nfsd4_lock *lock)
5238{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005239 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005241 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5242 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005244 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5245 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005246 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005247 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005248 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005249 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005250 if (ret == NULL) {
5251 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005252 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005253 ret = lo;
5254 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08005255 nfs4_free_stateowner(&lo->lo_owner);
5256
Trond Myklebustd4f04892014-07-29 21:34:36 -04005257 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04005258 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259}
5260
Jeff Layton356a95e2014-07-29 21:34:13 -04005261static void
5262init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5263 struct nfs4_file *fp, struct inode *inode,
5264 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005266 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267
Jeff Layton356a95e2014-07-29 21:34:13 -04005268 lockdep_assert_held(&clp->cl_lock);
5269
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005270 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005271 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005272 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07005273 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005274 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04005275 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005276 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005278 stp->st_openstp = open_stp;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005279 init_rwsem(&stp->st_rwsem);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005280 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005281 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005282 spin_lock(&fp->fi_lock);
5283 list_add(&stp->st_perfile, &fp->fi_stateids);
5284 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285}
5286
Jeff Laytonc53530d2014-06-30 11:48:39 -04005287static struct nfs4_ol_stateid *
5288find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5289{
5290 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005291 struct nfs4_client *clp = lo->lo_owner.so_client;
5292
5293 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005294
5295 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005296 if (lst->st_stid.sc_file == fp) {
5297 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005298 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005299 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005300 }
5301 return NULL;
5302}
5303
Jeff Layton356a95e2014-07-29 21:34:13 -04005304static struct nfs4_ol_stateid *
5305find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5306 struct inode *inode, struct nfs4_ol_stateid *ost,
5307 bool *new)
5308{
5309 struct nfs4_stid *ns = NULL;
5310 struct nfs4_ol_stateid *lst;
5311 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5312 struct nfs4_client *clp = oo->oo_owner.so_client;
5313
5314 spin_lock(&clp->cl_lock);
5315 lst = find_lock_stateid(lo, fi);
5316 if (lst == NULL) {
5317 spin_unlock(&clp->cl_lock);
5318 ns = nfs4_alloc_stid(clp, stateid_slab);
5319 if (ns == NULL)
5320 return NULL;
5321
5322 spin_lock(&clp->cl_lock);
5323 lst = find_lock_stateid(lo, fi);
5324 if (likely(!lst)) {
5325 lst = openlockstateid(ns);
5326 init_lock_stateid(lst, lo, fi, inode, ost);
5327 ns = NULL;
5328 *new = true;
5329 }
5330 }
5331 spin_unlock(&clp->cl_lock);
5332 if (ns)
5333 nfs4_put_stid(ns);
5334 return lst;
5335}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005336
NeilBrownfd39ca92005-06-23 22:04:03 -07005337static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338check_lock_length(u64 offset, u64 length)
5339{
Kinglong Meee7969312015-07-13 17:34:19 +08005340 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5341 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342}
5343
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005344static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005345{
Trond Myklebust11b91642014-07-29 21:34:08 -04005346 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005347
Jeff Layton7214e862014-07-10 14:07:33 -04005348 lockdep_assert_held(&fp->fi_lock);
5349
Jeff Layton82c5ff12012-05-11 09:45:13 -04005350 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005351 return;
Jeff Layton12659652014-07-10 14:07:28 -04005352 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005353 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005354}
5355
Jeff Layton356a95e2014-07-29 21:34:13 -04005356static __be32
5357lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5358 struct nfs4_ol_stateid *ost,
5359 struct nfsd4_lock *lock,
5360 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005361{
Jeff Layton5db1c032014-07-29 21:34:28 -04005362 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005363 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005364 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5365 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00005366 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005367 struct nfs4_lockowner *lo;
5368 unsigned int strhashval;
5369
Kinglong Meec8623992015-07-13 17:35:37 +08005370 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005371 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005372 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005373 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5374 if (lo == NULL)
5375 return nfserr_jukebox;
5376 } else {
5377 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005378 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005379 if (!cstate->minorversion &&
5380 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005381 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005382 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005383
Jeff Layton356a95e2014-07-29 21:34:13 -04005384 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005385 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005386 status = nfserr_jukebox;
5387 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005388 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005389 status = nfs_ok;
5390out:
5391 nfs4_put_stateowner(&lo->lo_owner);
5392 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005393}
5394
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395/*
5396 * LOCK operation
5397 */
Al Virob37ad282006-10-19 23:28:59 -07005398__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005399nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005400 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005402 struct nfs4_openowner *open_sop = NULL;
5403 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005404 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005405 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005406 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005407 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005408 struct file_lock *file_lock = NULL;
5409 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005410 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005411 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005412 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005413 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005414 struct net *net = SVC_NET(rqstp);
5415 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416
5417 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5418 (long long) lock->lk_offset,
5419 (long long) lock->lk_length);
5420
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 if (check_lock_length(lock->lk_offset, lock->lk_length))
5422 return nfserr_inval;
5423
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005424 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005425 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005426 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5427 return status;
5428 }
5429
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005431 if (nfsd4_has_session(cstate))
5432 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005433 memcpy(&lock->lk_new_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04005434 &cstate->session->se_client->cl_clientid,
5435 sizeof(clientid_t));
5436
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005438 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005442 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 lock->lk_new_open_seqid,
5444 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005445 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005446 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005448 up_write(&open_stp->st_rwsem);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005449 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005450 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005451 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005452 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005453 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005454 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005455 &lock_stp, &new);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005456 if (status == nfs_ok)
5457 down_write(&lock_stp->st_rwsem);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005458 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005459 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005460 lock->lk_old_lock_seqid,
5461 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005462 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005463 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005464 if (status)
5465 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005466 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005468 lkflg = setlkflg(lock->lk_type);
5469 status = nfs4_check_openmode(lock_stp, lkflg);
5470 if (status)
5471 goto out;
5472
NeilBrown0dd395d2005-07-07 17:59:15 -07005473 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005474 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005475 goto out;
5476 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005477 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005478 goto out;
5479
Jeff Layton21179d82012-08-21 08:03:32 -04005480 file_lock = locks_alloc_lock();
5481 if (!file_lock) {
5482 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5483 status = nfserr_jukebox;
5484 goto out;
5485 }
5486
Trond Myklebust11b91642014-07-29 21:34:08 -04005487 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 switch (lock->lk_type) {
5489 case NFS4_READ_LT:
5490 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005491 spin_lock(&fp->fi_lock);
5492 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005493 if (filp)
5494 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005495 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005496 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005497 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 case NFS4_WRITE_LT:
5499 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005500 spin_lock(&fp->fi_lock);
5501 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005502 if (filp)
5503 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005504 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005505 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005506 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 default:
5508 status = nfserr_inval;
5509 goto out;
5510 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005511 if (!filp) {
5512 status = nfserr_openmode;
5513 goto out;
5514 }
Kinglong Meeaef95832014-08-22 10:18:44 -04005515
5516 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04005517 file_lock->fl_pid = current->tgid;
5518 file_lock->fl_file = filp;
5519 file_lock->fl_flags = FL_POSIX;
5520 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5521 file_lock->fl_start = lock->lk_offset;
5522 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5523 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524
Jeff Layton21179d82012-08-21 08:03:32 -04005525 conflock = locks_alloc_lock();
5526 if (!conflock) {
5527 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5528 status = nfserr_jukebox;
5529 goto out;
5530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531
Jeff Layton21179d82012-08-21 08:03:32 -04005532 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005533 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005535 update_stateid(&lock_stp->st_stid.sc_stateid);
5536 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005538 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005539 break;
5540 case (EAGAIN): /* conflock holds conflicting lock */
5541 status = nfserr_denied;
5542 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005543 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005544 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 case (EDEADLK):
5546 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005547 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005548 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005549 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005550 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005551 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553out:
Trond Myklebustde186432014-07-10 14:07:26 -04005554 if (filp)
5555 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005556 if (lock_stp) {
5557 /* Bump seqid manually if the 4.0 replay owner is openowner */
5558 if (cstate->replay_owner &&
5559 cstate->replay_owner != &lock_sop->lo_owner &&
5560 seqid_mutating_err(ntohl(status)))
5561 lock_sop->lo_owner.so_seqid++;
5562
Jeff Layton35a92fe2015-09-17 07:47:08 -04005563 up_write(&lock_stp->st_rwsem);
5564
Jeff Layton5db1c032014-07-29 21:34:28 -04005565 /*
5566 * If this is a new, never-before-used stateid, and we are
5567 * returning an error, then just go ahead and release it.
5568 */
5569 if (status && new)
5570 release_lock_stateid(lock_stp);
5571
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005572 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005573 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005574 if (open_stp)
5575 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005576 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005577 if (file_lock)
5578 locks_free_lock(file_lock);
5579 if (conflock)
5580 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 return status;
5582}
5583
5584/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005585 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5586 * so we do a temporary open here just to get an open file to pass to
5587 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5588 * inode operation.)
5589 */
Al Viro04da6e92012-04-13 00:00:04 -04005590static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005591{
5592 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005593 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5594 if (!err) {
5595 err = nfserrno(vfs_test_lock(file, lock));
Christoph Hellwigfd891452015-04-28 15:41:16 +02005596 fput(file);
Al Viro04da6e92012-04-13 00:00:04 -04005597 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005598 return err;
5599}
5600
5601/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 * LOCKT operation
5603 */
Al Virob37ad282006-10-19 23:28:59 -07005604__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005605nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5606 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607{
Jeff Layton21179d82012-08-21 08:03:32 -04005608 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005609 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005610 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005611 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005613 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 return nfserr_grace;
5615
5616 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5617 return nfserr_inval;
5618
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005619 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005620 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005621 if (status)
5622 goto out;
5623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005625 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627
Jeff Layton21179d82012-08-21 08:03:32 -04005628 file_lock = locks_alloc_lock();
5629 if (!file_lock) {
5630 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5631 status = nfserr_jukebox;
5632 goto out;
5633 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005634
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 switch (lockt->lt_type) {
5636 case NFS4_READ_LT:
5637 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005638 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 break;
5640 case NFS4_WRITE_LT:
5641 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005642 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 break;
5644 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005645 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 status = nfserr_inval;
5647 goto out;
5648 }
5649
Kinglong Meec8623992015-07-13 17:35:37 +08005650 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005651 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005652 file_lock->fl_owner = (fl_owner_t)lo;
5653 file_lock->fl_pid = current->tgid;
5654 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655
Jeff Layton21179d82012-08-21 08:03:32 -04005656 file_lock->fl_start = lockt->lt_offset;
5657 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658
Jeff Layton21179d82012-08-21 08:03:32 -04005659 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660
Jeff Layton21179d82012-08-21 08:03:32 -04005661 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005662 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005663 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005664
Jeff Layton21179d82012-08-21 08:03:32 -04005665 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005667 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 }
5669out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005670 if (lo)
5671 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04005672 if (file_lock)
5673 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 return status;
5675}
5676
Al Virob37ad282006-10-19 23:28:59 -07005677__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005678nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005679 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005681 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005683 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005684 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005685 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005686 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5687
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5689 (long long) locku->lu_offset,
5690 (long long) locku->lu_length);
5691
5692 if (check_lock_length(locku->lu_offset, locku->lu_length))
5693 return nfserr_inval;
5694
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005695 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005696 &locku->lu_stateid, NFS4_LOCK_STID,
5697 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005698 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005700 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005701 if (!filp) {
5702 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005703 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005704 }
Jeff Layton21179d82012-08-21 08:03:32 -04005705 file_lock = locks_alloc_lock();
5706 if (!file_lock) {
5707 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5708 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005709 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005710 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005711
Jeff Layton21179d82012-08-21 08:03:32 -04005712 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04005713 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04005714 file_lock->fl_pid = current->tgid;
5715 file_lock->fl_file = filp;
5716 file_lock->fl_flags = FL_POSIX;
5717 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5718 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719
Jeff Layton21179d82012-08-21 08:03:32 -04005720 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5721 locku->lu_length);
5722 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723
Jeff Layton21179d82012-08-21 08:03:32 -04005724 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005725 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005726 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 goto out_nfserr;
5728 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005729 update_stateid(&stp->st_stid.sc_stateid);
5730 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005731fput:
5732 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005733put_stateid:
Jeff Layton35a92fe2015-09-17 07:47:08 -04005734 up_write(&stp->st_rwsem);
Trond Myklebust858cc572014-07-29 21:34:16 -04005735 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005737 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005738 if (file_lock)
5739 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 return status;
5741
5742out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005743 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005744 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745}
5746
5747/*
5748 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005749 * true: locks held by lockowner
5750 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005752static bool
5753check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005755 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005756 int status = false;
5757 struct file *filp = find_any_file(fp);
5758 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005759 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005760
5761 if (!filp) {
5762 /* Any valid lock stateid should have some sort of access */
5763 WARN_ON_ONCE(1);
5764 return status;
5765 }
5766
5767 inode = file_inode(filp);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005768 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005770 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05005771 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005772 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
5773 if (fl->fl_owner == (fl_owner_t)lowner) {
5774 status = true;
5775 break;
5776 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005777 }
Jeff Layton6109c852015-01-16 15:05:57 -05005778 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 }
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005780 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 return status;
5782}
5783
Al Virob37ad282006-10-19 23:28:59 -07005784__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005785nfsd4_release_lockowner(struct svc_rqst *rqstp,
5786 struct nfsd4_compound_state *cstate,
5787 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788{
5789 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005790 struct nfs4_stateowner *sop;
5791 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005792 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005794 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005795 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005796 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005797 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798
5799 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5800 clid->cl_boot, clid->cl_id);
5801
Jeff Layton4b24ca72014-06-30 11:48:44 -04005802 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005803 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04005804 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005805
Trond Myklebustd4f04892014-07-29 21:34:36 -04005806 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005807 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005808 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005809 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005810 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005811
5812 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005813 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005814
Jeff Layton882e9d22014-07-29 21:34:37 -04005815 /* see if there are still any locks associated with it */
5816 lo = lockowner(sop);
5817 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5818 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5819 status = nfserr_locks_held;
5820 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04005821 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04005822 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005823 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005824
Kinglong Meeb5971af2014-08-22 10:18:43 -04005825 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04005826 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005827 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005828 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005829 if (lo)
5830 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 return status;
5832}
5833
5834static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005835alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836{
NeilBrowna55370a2005-06-23 22:03:52 -07005837 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838}
5839
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005840bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005841nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005842{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005843 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005844
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005845 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005846 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005847}
5848
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849/*
5850 * failure => all reset bets are off, nfserr_no_grace...
5851 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005852struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005853nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854{
5855 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005856 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857
NeilBrowna55370a2005-06-23 22:03:52 -07005858 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5859 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005860 if (crp) {
5861 strhashval = clientstr_hashval(name);
5862 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005863 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005864 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005865 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005866 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005867 }
5868 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869}
5870
Jeff Layton2a4317c2012-03-21 16:42:43 -04005871void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005872nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005873{
5874 list_del(&crp->cr_strhash);
5875 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005876 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005877}
5878
5879void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005880nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881{
5882 struct nfs4_client_reclaim *crp = NULL;
5883 int i;
5884
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005886 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5887 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005889 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 }
5891 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005892 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893}
5894
5895/*
5896 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005897struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005898nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899{
5900 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 struct nfs4_client_reclaim *crp = NULL;
5902
Jeff Layton278c9312012-11-12 15:00:52 -05005903 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904
Jeff Layton278c9312012-11-12 15:00:52 -05005905 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005906 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005907 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 return crp;
5909 }
5910 }
5911 return NULL;
5912}
5913
5914/*
5915* Called from OPEN. Look for clientid in reclaim list.
5916*/
Al Virob37ad282006-10-19 23:28:59 -07005917__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005918nfs4_check_open_reclaim(clientid_t *clid,
5919 struct nfsd4_compound_state *cstate,
5920 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005922 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005923
5924 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005925 status = lookup_clientid(clid, cstate, nn);
5926 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005927 return nfserr_reclaim_bad;
5928
Jeff Layton3b3e7b72014-09-12 16:40:22 -04005929 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
5930 return nfserr_no_grace;
5931
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005932 if (nfsd4_client_record_check(cstate->clp))
5933 return nfserr_reclaim_bad;
5934
5935 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936}
5937
Bryan Schumaker65178db2011-11-01 13:35:21 -04005938#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04005939static inline void
5940put_client(struct nfs4_client *clp)
5941{
5942 atomic_dec(&clp->cl_refcount);
5943}
5944
Jeff Layton285abde2014-07-30 08:27:24 -04005945static struct nfs4_client *
5946nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5947{
5948 struct nfs4_client *clp;
5949 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5950 nfsd_net_id);
5951
5952 if (!nfsd_netns_ready(nn))
5953 return NULL;
5954
5955 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5956 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5957 return clp;
5958 }
5959 return NULL;
5960}
5961
Jeff Layton7ec0e362014-07-30 08:27:17 -04005962u64
Jeff Layton285abde2014-07-30 08:27:24 -04005963nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04005964{
5965 struct nfs4_client *clp;
5966 u64 count = 0;
5967 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5968 nfsd_net_id);
5969 char buf[INET6_ADDRSTRLEN];
5970
5971 if (!nfsd_netns_ready(nn))
5972 return 0;
5973
5974 spin_lock(&nn->client_lock);
5975 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5976 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5977 pr_info("NFS Client: %s\n", buf);
5978 ++count;
5979 }
5980 spin_unlock(&nn->client_lock);
5981
5982 return count;
5983}
Bryan Schumaker65178db2011-11-01 13:35:21 -04005984
Jeff Laytona0926d12014-07-30 08:27:18 -04005985u64
Jeff Layton285abde2014-07-30 08:27:24 -04005986nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04005987{
5988 u64 count = 0;
5989 struct nfs4_client *clp;
5990 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5991 nfsd_net_id);
5992
5993 if (!nfsd_netns_ready(nn))
5994 return count;
5995
5996 spin_lock(&nn->client_lock);
5997 clp = nfsd_find_client(addr, addr_size);
5998 if (clp) {
5999 if (mark_client_expired_locked(clp) == nfs_ok)
6000 ++count;
6001 else
6002 clp = NULL;
6003 }
6004 spin_unlock(&nn->client_lock);
6005
6006 if (clp)
6007 expire_client(clp);
6008
6009 return count;
6010}
6011
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006012u64
Jeff Layton285abde2014-07-30 08:27:24 -04006013nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006014{
6015 u64 count = 0;
6016 struct nfs4_client *clp, *next;
6017 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6018 nfsd_net_id);
6019 LIST_HEAD(reaplist);
6020
6021 if (!nfsd_netns_ready(nn))
6022 return count;
6023
6024 spin_lock(&nn->client_lock);
6025 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6026 if (mark_client_expired_locked(clp) == nfs_ok) {
6027 list_add(&clp->cl_lru, &reaplist);
6028 if (max != 0 && ++count >= max)
6029 break;
6030 }
6031 }
6032 spin_unlock(&nn->client_lock);
6033
6034 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6035 expire_client(clp);
6036
6037 return count;
6038}
6039
Bryan Schumaker184c1842012-11-29 11:40:44 -05006040static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6041 const char *type)
6042{
6043 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05006044 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05006045 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6046}
6047
Jeff Layton016200c2014-07-30 08:27:21 -04006048static void
6049nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6050 struct list_head *collect)
6051{
6052 struct nfs4_client *clp = lst->st_stid.sc_client;
6053 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6054 nfsd_net_id);
6055
6056 if (!collect)
6057 return;
6058
6059 lockdep_assert_held(&nn->client_lock);
6060 atomic_inc(&clp->cl_refcount);
6061 list_add(&lst->st_locks, collect);
6062}
6063
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006064static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04006065 struct list_head *collect,
Jeff Laytone8568732015-08-24 12:41:47 -04006066 bool (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05006067{
6068 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006069 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006070 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006071 u64 count = 0;
6072
Jeff Layton016200c2014-07-30 08:27:21 -04006073 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006074 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006075 list_for_each_entry_safe(stp, st_next,
6076 &oop->oo_owner.so_stateids, st_perstateowner) {
6077 list_for_each_entry_safe(lst, lst_next,
6078 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04006079 if (func) {
Jeff Laytone8568732015-08-24 12:41:47 -04006080 if (func(lst))
6081 nfsd_inject_add_lock_to_list(lst,
6082 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04006083 }
Jeff Layton016200c2014-07-30 08:27:21 -04006084 ++count;
6085 /*
6086 * Despite the fact that these functions deal
6087 * with 64-bit integers for "count", we must
6088 * ensure that it doesn't blow up the
6089 * clp->cl_refcount. Throw a warning if we
6090 * start to approach INT_MAX here.
6091 */
6092 WARN_ON_ONCE(count == (INT_MAX / 2));
6093 if (count == max)
6094 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006095 }
6096 }
6097 }
Jeff Layton016200c2014-07-30 08:27:21 -04006098out:
6099 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006100
6101 return count;
6102}
6103
Jeff Layton016200c2014-07-30 08:27:21 -04006104static u64
6105nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6106 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05006107{
Jeff Layton016200c2014-07-30 08:27:21 -04006108 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006109}
6110
Jeff Layton016200c2014-07-30 08:27:21 -04006111static u64
6112nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006113{
Jeff Layton016200c2014-07-30 08:27:21 -04006114 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006115 nfsd_print_count(clp, count, "locked files");
6116 return count;
6117}
6118
Jeff Layton016200c2014-07-30 08:27:21 -04006119u64
Jeff Layton285abde2014-07-30 08:27:24 -04006120nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04006121{
6122 struct nfs4_client *clp;
6123 u64 count = 0;
6124 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6125 nfsd_net_id);
6126
6127 if (!nfsd_netns_ready(nn))
6128 return 0;
6129
6130 spin_lock(&nn->client_lock);
6131 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6132 count += nfsd_print_client_locks(clp);
6133 spin_unlock(&nn->client_lock);
6134
6135 return count;
6136}
6137
6138static void
6139nfsd_reap_locks(struct list_head *reaplist)
6140{
6141 struct nfs4_client *clp;
6142 struct nfs4_ol_stateid *stp, *next;
6143
6144 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6145 list_del_init(&stp->st_locks);
6146 clp = stp->st_stid.sc_client;
6147 nfs4_put_stid(&stp->st_stid);
6148 put_client(clp);
6149 }
6150}
6151
6152u64
Jeff Layton285abde2014-07-30 08:27:24 -04006153nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04006154{
6155 unsigned int count = 0;
6156 struct nfs4_client *clp;
6157 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6158 nfsd_net_id);
6159 LIST_HEAD(reaplist);
6160
6161 if (!nfsd_netns_ready(nn))
6162 return count;
6163
6164 spin_lock(&nn->client_lock);
6165 clp = nfsd_find_client(addr, addr_size);
6166 if (clp)
6167 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6168 spin_unlock(&nn->client_lock);
6169 nfsd_reap_locks(&reaplist);
6170 return count;
6171}
6172
6173u64
Jeff Layton285abde2014-07-30 08:27:24 -04006174nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04006175{
6176 u64 count = 0;
6177 struct nfs4_client *clp;
6178 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6179 nfsd_net_id);
6180 LIST_HEAD(reaplist);
6181
6182 if (!nfsd_netns_ready(nn))
6183 return count;
6184
6185 spin_lock(&nn->client_lock);
6186 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6187 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6188 if (max != 0 && count >= max)
6189 break;
6190 }
6191 spin_unlock(&nn->client_lock);
6192 nfsd_reap_locks(&reaplist);
6193 return count;
6194}
6195
Jeff Layton82e05ef2014-07-30 08:27:22 -04006196static u64
6197nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6198 struct list_head *collect,
6199 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006200{
6201 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04006202 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6203 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006204 u64 count = 0;
6205
Jeff Layton82e05ef2014-07-30 08:27:22 -04006206 lockdep_assert_held(&nn->client_lock);
6207
6208 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006209 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04006210 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006211 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04006212 if (collect) {
6213 atomic_inc(&clp->cl_refcount);
6214 list_add(&oop->oo_perclient, collect);
6215 }
6216 }
6217 ++count;
6218 /*
6219 * Despite the fact that these functions deal with
6220 * 64-bit integers for "count", we must ensure that
6221 * it doesn't blow up the clp->cl_refcount. Throw a
6222 * warning if we start to approach INT_MAX here.
6223 */
6224 WARN_ON_ONCE(count == (INT_MAX / 2));
6225 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006226 break;
6227 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04006228 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006229
6230 return count;
6231}
6232
Jeff Layton82e05ef2014-07-30 08:27:22 -04006233static u64
6234nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006235{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006236 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6237
6238 nfsd_print_count(clp, count, "openowners");
6239 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006240}
6241
Jeff Layton82e05ef2014-07-30 08:27:22 -04006242static u64
6243nfsd_collect_client_openowners(struct nfs4_client *clp,
6244 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006245{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006246 return nfsd_foreach_client_openowner(clp, max, collect,
6247 unhash_openowner_locked);
6248}
6249
6250u64
Jeff Layton285abde2014-07-30 08:27:24 -04006251nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006252{
6253 struct nfs4_client *clp;
6254 u64 count = 0;
6255 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6256 nfsd_net_id);
6257
6258 if (!nfsd_netns_ready(nn))
6259 return 0;
6260
6261 spin_lock(&nn->client_lock);
6262 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6263 count += nfsd_print_client_openowners(clp);
6264 spin_unlock(&nn->client_lock);
6265
6266 return count;
6267}
6268
6269static void
6270nfsd_reap_openowners(struct list_head *reaplist)
6271{
6272 struct nfs4_client *clp;
6273 struct nfs4_openowner *oop, *next;
6274
6275 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6276 list_del_init(&oop->oo_perclient);
6277 clp = oop->oo_owner.so_client;
6278 release_openowner(oop);
6279 put_client(clp);
6280 }
6281}
6282
6283u64
Jeff Layton285abde2014-07-30 08:27:24 -04006284nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6285 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006286{
6287 unsigned int count = 0;
6288 struct nfs4_client *clp;
6289 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6290 nfsd_net_id);
6291 LIST_HEAD(reaplist);
6292
6293 if (!nfsd_netns_ready(nn))
6294 return count;
6295
6296 spin_lock(&nn->client_lock);
6297 clp = nfsd_find_client(addr, addr_size);
6298 if (clp)
6299 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6300 spin_unlock(&nn->client_lock);
6301 nfsd_reap_openowners(&reaplist);
6302 return count;
6303}
6304
6305u64
Jeff Layton285abde2014-07-30 08:27:24 -04006306nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006307{
6308 u64 count = 0;
6309 struct nfs4_client *clp;
6310 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6311 nfsd_net_id);
6312 LIST_HEAD(reaplist);
6313
6314 if (!nfsd_netns_ready(nn))
6315 return count;
6316
6317 spin_lock(&nn->client_lock);
6318 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6319 count += nfsd_collect_client_openowners(clp, &reaplist,
6320 max - count);
6321 if (max != 0 && count >= max)
6322 break;
6323 }
6324 spin_unlock(&nn->client_lock);
6325 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006326 return count;
6327}
6328
Bryan Schumaker269de302012-11-29 11:40:42 -05006329static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6330 struct list_head *victims)
6331{
6332 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006333 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6334 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006335 u64 count = 0;
6336
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006337 lockdep_assert_held(&nn->client_lock);
6338
6339 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006340 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006341 if (victims) {
6342 /*
6343 * It's not safe to mess with delegations that have a
6344 * non-zero dl_time. They might have already been broken
6345 * and could be processed by the laundromat outside of
6346 * the state_lock. Just leave them be.
6347 */
6348 if (dp->dl_time != 0)
6349 continue;
6350
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006351 atomic_inc(&clp->cl_refcount);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04006352 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04006353 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006354 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006355 ++count;
6356 /*
6357 * Despite the fact that these functions deal with
6358 * 64-bit integers for "count", we must ensure that
6359 * it doesn't blow up the clp->cl_refcount. Throw a
6360 * warning if we start to approach INT_MAX here.
6361 */
6362 WARN_ON_ONCE(count == (INT_MAX / 2));
6363 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006364 break;
6365 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006366 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006367 return count;
6368}
6369
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006370static u64
6371nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006372{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006373 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006374
6375 nfsd_print_count(clp, count, "delegations");
6376 return count;
6377}
6378
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006379u64
Jeff Layton285abde2014-07-30 08:27:24 -04006380nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006381{
6382 struct nfs4_client *clp;
6383 u64 count = 0;
6384 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6385 nfsd_net_id);
6386
6387 if (!nfsd_netns_ready(nn))
6388 return 0;
6389
6390 spin_lock(&nn->client_lock);
6391 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6392 count += nfsd_print_client_delegations(clp);
6393 spin_unlock(&nn->client_lock);
6394
6395 return count;
6396}
6397
6398static void
6399nfsd_forget_delegations(struct list_head *reaplist)
6400{
6401 struct nfs4_client *clp;
6402 struct nfs4_delegation *dp, *next;
6403
6404 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6405 list_del_init(&dp->dl_recall_lru);
6406 clp = dp->dl_stid.sc_client;
6407 revoke_delegation(dp);
6408 put_client(clp);
6409 }
6410}
6411
6412u64
Jeff Layton285abde2014-07-30 08:27:24 -04006413nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6414 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006415{
6416 u64 count = 0;
6417 struct nfs4_client *clp;
6418 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6419 nfsd_net_id);
6420 LIST_HEAD(reaplist);
6421
6422 if (!nfsd_netns_ready(nn))
6423 return count;
6424
6425 spin_lock(&nn->client_lock);
6426 clp = nfsd_find_client(addr, addr_size);
6427 if (clp)
6428 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6429 spin_unlock(&nn->client_lock);
6430
6431 nfsd_forget_delegations(&reaplist);
6432 return count;
6433}
6434
6435u64
Jeff Layton285abde2014-07-30 08:27:24 -04006436nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006437{
6438 u64 count = 0;
6439 struct nfs4_client *clp;
6440 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6441 nfsd_net_id);
6442 LIST_HEAD(reaplist);
6443
6444 if (!nfsd_netns_ready(nn))
6445 return count;
6446
6447 spin_lock(&nn->client_lock);
6448 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6449 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6450 if (max != 0 && count >= max)
6451 break;
6452 }
6453 spin_unlock(&nn->client_lock);
6454 nfsd_forget_delegations(&reaplist);
6455 return count;
6456}
6457
6458static void
6459nfsd_recall_delegations(struct list_head *reaplist)
6460{
6461 struct nfs4_client *clp;
6462 struct nfs4_delegation *dp, *next;
6463
6464 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6465 list_del_init(&dp->dl_recall_lru);
6466 clp = dp->dl_stid.sc_client;
6467 /*
6468 * We skipped all entries that had a zero dl_time before,
6469 * so we can now reset the dl_time back to 0. If a delegation
6470 * break comes in now, then it won't make any difference since
6471 * we're recalling it either way.
6472 */
6473 spin_lock(&state_lock);
6474 dp->dl_time = 0;
6475 spin_unlock(&state_lock);
6476 nfsd_break_one_deleg(dp);
6477 put_client(clp);
6478 }
6479}
6480
6481u64
Jeff Layton285abde2014-07-30 08:27:24 -04006482nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006483 size_t addr_size)
6484{
6485 u64 count = 0;
6486 struct nfs4_client *clp;
6487 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6488 nfsd_net_id);
6489 LIST_HEAD(reaplist);
6490
6491 if (!nfsd_netns_ready(nn))
6492 return count;
6493
6494 spin_lock(&nn->client_lock);
6495 clp = nfsd_find_client(addr, addr_size);
6496 if (clp)
6497 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6498 spin_unlock(&nn->client_lock);
6499
6500 nfsd_recall_delegations(&reaplist);
6501 return count;
6502}
6503
6504u64
Jeff Layton285abde2014-07-30 08:27:24 -04006505nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006506{
6507 u64 count = 0;
6508 struct nfs4_client *clp, *next;
6509 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6510 nfsd_net_id);
6511 LIST_HEAD(reaplist);
6512
6513 if (!nfsd_netns_ready(nn))
6514 return count;
6515
6516 spin_lock(&nn->client_lock);
6517 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6518 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6519 if (max != 0 && ++count >= max)
6520 break;
6521 }
6522 spin_unlock(&nn->client_lock);
6523 nfsd_recall_delegations(&reaplist);
6524 return count;
6525}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006526#endif /* CONFIG_NFSD_FAULT_INJECTION */
6527
Meelap Shahc2f1a552007-07-17 04:04:39 -07006528/*
6529 * Since the lifetime of a delegation isn't limited to that of an open, a
6530 * client may quite reasonably hang on to a delegation as long as it has
6531 * the inode cached. This becomes an obvious problem the first time a
6532 * client's inode cache approaches the size of the server's total memory.
6533 *
6534 * For now we avoid this problem by imposing a hard limit on the number
6535 * of delegations, which varies according to the server's memory size.
6536 */
6537static void
6538set_max_delegations(void)
6539{
6540 /*
6541 * Allow at most 4 delegations per megabyte of RAM. Quick
6542 * estimates suggest that in the worst case (where every delegation
6543 * is for a different inode), a delegation could take about 1.5K,
6544 * giving a worst case usage of about 6% of memory.
6545 */
6546 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6547}
6548
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006549static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006550{
6551 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6552 int i;
6553
6554 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6555 CLIENT_HASH_SIZE, GFP_KERNEL);
6556 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006557 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006558 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6559 CLIENT_HASH_SIZE, GFP_KERNEL);
6560 if (!nn->unconf_id_hashtbl)
6561 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006562 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6563 SESSION_HASH_SIZE, GFP_KERNEL);
6564 if (!nn->sessionid_hashtbl)
6565 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006566
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006567 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006568 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006569 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006570 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006571 for (i = 0; i < SESSION_HASH_SIZE; i++)
6572 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006573 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006574 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03006575 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03006576 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006577 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03006578 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006579
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006580 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006581 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006582
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006583 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006584
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006585err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03006586 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006587err_unconf_id:
6588 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006589err:
6590 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006591}
6592
6593static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006594nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006595{
6596 int i;
6597 struct nfs4_client *clp = NULL;
6598 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6599
6600 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6601 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6602 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6603 destroy_client(clp);
6604 }
6605 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006606
Kinglong Mee2b905632014-03-26 22:09:30 +08006607 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6608 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6609 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6610 destroy_client(clp);
6611 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006612 }
6613
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006614 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006615 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006616 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006617 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006618}
6619
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006620int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006621nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07006622{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006623 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006624 int ret;
6625
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006626 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006627 if (ret)
6628 return ret;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006629 nn->boot_time = get_seconds();
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04006630 nn->grace_ended = false;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04006631 nn->nfsd4_manager.block_opens = true;
Jeff Laytond4318ac2014-09-12 16:40:21 -04006632 locks_start_grace(net, &nn->nfsd4_manager);
6633 nfsd4_client_tracking_init(net);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006634 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03006635 nn->nfsd4_grace, net);
6636 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006637 return 0;
6638}
6639
6640/* initialization to perform when the nfsd service is started: */
6641
6642int
6643nfs4_state_start(void)
6644{
6645 int ret;
6646
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006647 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006648 if (ret)
6649 return -ENOMEM;
Jeff Layton51a54562015-08-20 07:17:01 -04006650 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05006651 if (laundry_wq == NULL) {
6652 ret = -ENOMEM;
6653 goto out_recovery;
6654 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006655 ret = nfsd4_create_callback_queue();
6656 if (ret)
6657 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006658
Meelap Shahc2f1a552007-07-17 04:04:39 -07006659 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006660
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006661 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006662
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006663out_free_laundry:
6664 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05006665out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006666 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667}
6668
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006669void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006670nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006673 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006674 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006676 cancel_delayed_work_sync(&nn->laundromat_work);
6677 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05006678
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04006680 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006681 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006682 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04006683 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04006684 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685 }
Benny Halevycdc97502014-05-30 09:09:30 -04006686 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687 list_for_each_safe(pos, next, &reaplist) {
6688 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006689 list_del_init(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +02006690 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -04006691 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04006692 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693 }
6694
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006695 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006696 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697}
6698
6699void
6700nfs4_state_shutdown(void)
6701{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006702 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006703 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006705
6706static void
6707get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6708{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006709 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6710 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006711}
6712
6713static void
6714put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6715{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006716 if (cstate->minorversion) {
6717 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6718 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6719 }
6720}
6721
6722void
6723clear_current_stateid(struct nfsd4_compound_state *cstate)
6724{
6725 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006726}
6727
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006728/*
6729 * functions to set current state id
6730 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006731void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006732nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6733{
6734 put_stateid(cstate, &odp->od_stateid);
6735}
6736
6737void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006738nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6739{
6740 put_stateid(cstate, &open->op_stateid);
6741}
6742
6743void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006744nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6745{
6746 put_stateid(cstate, &close->cl_stateid);
6747}
6748
6749void
6750nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6751{
6752 put_stateid(cstate, &lock->lk_resp_stateid);
6753}
6754
6755/*
6756 * functions to consume current state id
6757 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006758
6759void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006760nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6761{
6762 get_stateid(cstate, &odp->od_stateid);
6763}
6764
6765void
6766nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6767{
6768 get_stateid(cstate, &drp->dr_stateid);
6769}
6770
6771void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006772nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6773{
6774 get_stateid(cstate, &fsp->fr_stateid);
6775}
6776
6777void
6778nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6779{
6780 get_stateid(cstate, &setattr->sa_stateid);
6781}
6782
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006783void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006784nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6785{
6786 get_stateid(cstate, &close->cl_stateid);
6787}
6788
6789void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006790nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006791{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006792 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006793}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006794
6795void
6796nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6797{
6798 get_stateid(cstate, &read->rd_stateid);
6799}
6800
6801void
6802nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6803{
6804 get_stateid(cstate, &write->wr_stateid);
6805}