blob: f2ea343086b85f6b01d6c371e0c2d89c217320e7 [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 Fields4eaea132015-10-15 16:11:01 -04002259static bool client_has_openowners(struct nfs4_client *clp)
2260{
2261 struct nfs4_openowner *oo;
2262
2263 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2264 if (!list_empty(&oo->oo_owner.so_stateids))
2265 return true;
2266 }
2267 return false;
2268}
2269
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002270static bool client_has_state(struct nfs4_client *clp)
2271{
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002272 return client_has_openowners(clp)
Kinglong Mee47e970b2015-07-21 13:09:17 +08002273#ifdef CONFIG_NFSD_PNFS
2274 || !list_empty(&clp->cl_lo_states)
2275#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002276 || !list_empty(&clp->cl_delegations)
2277 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002278}
2279
Al Virob37ad282006-10-19 23:28:59 -07002280__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002281nfsd4_exchange_id(struct svc_rqst *rqstp,
2282 struct nfsd4_compound_state *cstate,
2283 struct nfsd4_exchange_id *exid)
2284{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002285 struct nfs4_client *conf, *new;
2286 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002287 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002288 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002289 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002290 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002291 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002292 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002293
Jeff Layton363168b2009-08-14 12:57:56 -04002294 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002295 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002296 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002297 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002298 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002299
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002300 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002301 return nfserr_inval;
2302
Andy Adamson0733d212009-04-03 08:28:01 +03002303 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002304 case SP4_MACH_CRED:
2305 if (!svc_rqst_integrity_protected(rqstp))
2306 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002307 case SP4_NONE:
2308 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002309 default: /* checked by xdr code */
2310 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002311 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002312 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002313 }
2314
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002315 new = create_client(exid->clname, rqstp, &verf);
2316 if (new == NULL)
2317 return nfserr_jukebox;
2318
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002319 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002320 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002321 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002322 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002323 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2324 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2325
J. Bruce Fields136e6582012-05-12 20:37:23 -04002326 if (update) {
2327 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002328 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002329 goto out;
2330 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002331 if (!mach_creds_match(conf, rqstp)) {
2332 status = nfserr_wrong_cred;
2333 goto out;
2334 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002335 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002336 status = nfserr_perm;
2337 goto out;
2338 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002339 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002340 status = nfserr_not_same;
2341 goto out;
2342 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002343 /* case 6 */
2344 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002345 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002346 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002347 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002348 if (client_has_state(conf)) {
2349 status = nfserr_clid_inuse;
2350 goto out;
2351 }
Andy Adamson0733d212009-04-03 08:28:01 +03002352 goto out_new;
2353 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002354 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002355 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002356 goto out_copy;
2357 }
2358 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002359 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002360 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002361 }
2362
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002363 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002364 status = nfserr_noent;
2365 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002366 }
2367
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002368 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002369 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002370 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002371
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002372 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002373out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002374 if (conf) {
2375 status = mark_client_expired_locked(conf);
2376 if (status)
2377 goto out;
2378 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002379 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002380 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002381
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002382 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002383 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002384 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002385out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002386 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2387 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002388
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002389 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2390 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002391
2392 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002393 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002394 status = nfs_ok;
2395
2396out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002397 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002398 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002399 expire_client(new);
2400 if (unconf)
2401 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002402 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002403}
2404
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002405static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002406check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002407{
Andy Adamson88e588d2009-07-23 19:02:15 -04002408 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2409 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002410
2411 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002412 if (slot_inuse) {
2413 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002414 return nfserr_jukebox;
2415 else
2416 return nfserr_seq_misordered;
2417 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002418 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002419 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002420 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002421 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002422 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002423 return nfserr_seq_misordered;
2424}
2425
Andy Adamson49557cc2009-07-23 19:02:16 -04002426/*
2427 * Cache the create session result into the create session single DRC
2428 * slot cache by saving the xdr structure. sl_seqid has been set.
2429 * Do this for solo or embedded create session operations.
2430 */
2431static void
2432nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002433 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002434{
2435 slot->sl_status = nfserr;
2436 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2437}
2438
2439static __be32
2440nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2441 struct nfsd4_clid_slot *slot)
2442{
2443 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2444 return slot->sl_status;
2445}
2446
Mi Jinlong1b74c252011-07-14 14:50:17 +08002447#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2448 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2449 1 + /* MIN tag is length with zero, only length */ \
2450 3 + /* version, opcount, opcode */ \
2451 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2452 /* seqid, slotID, slotID, cache */ \
2453 4 ) * sizeof(__be32))
2454
2455#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2456 2 + /* verifier: AUTH_NULL, length 0 */\
2457 1 + /* status */ \
2458 1 + /* MIN tag is length with zero, only length */ \
2459 3 + /* opcount, opcode, opstatus*/ \
2460 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2461 /* seqid, slotID, slotID, slotID, status */ \
2462 5 ) * sizeof(__be32))
2463
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002464static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002465{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002466 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2467
J. Bruce Fields373cd402013-04-08 15:42:12 -04002468 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2469 return nfserr_toosmall;
2470 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2471 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002472 ca->headerpadsz = 0;
2473 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2474 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2475 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2476 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2477 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2478 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2479 /*
2480 * Note decreasing slot size below client's request may make it
2481 * difficult for client to function correctly, whereas
2482 * decreasing the number of slots will (just?) affect
2483 * performance. When short on memory we therefore prefer to
2484 * decrease number of slots instead of their size. Clients that
2485 * request larger slots than they need will get poor results:
2486 */
2487 ca->maxreqs = nfsd4_get_drc_mem(ca);
2488 if (!ca->maxreqs)
2489 return nfserr_jukebox;
2490
J. Bruce Fields373cd402013-04-08 15:42:12 -04002491 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002492}
2493
Kinglong Mee8a891632013-12-23 18:11:02 +08002494#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2495 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2496#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2497 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2498
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002499static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2500{
2501 ca->headerpadsz = 0;
2502
2503 /*
2504 * These RPC_MAX_HEADER macros are overkill, especially since we
2505 * don't even do gss on the backchannel yet. But this is still
2506 * less than 1k. Tighten up this estimate in the unlikely event
2507 * it turns out to be a problem for some client:
2508 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002509 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002510 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002511 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002512 return nfserr_toosmall;
2513 ca->maxresp_cached = 0;
2514 if (ca->maxops < 2)
2515 return nfserr_toosmall;
2516
2517 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002518}
2519
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002520static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2521{
2522 switch (cbs->flavor) {
2523 case RPC_AUTH_NULL:
2524 case RPC_AUTH_UNIX:
2525 return nfs_ok;
2526 default:
2527 /*
2528 * GSS case: the spec doesn't allow us to return this
2529 * error. But it also doesn't allow us not to support
2530 * GSS.
2531 * I'd rather this fail hard than return some error the
2532 * client might think it can already handle:
2533 */
2534 return nfserr_encr_alg_unsupp;
2535 }
2536}
2537
Andy Adamson069b6ad2009-04-03 08:27:58 +03002538__be32
2539nfsd4_create_session(struct svc_rqst *rqstp,
2540 struct nfsd4_compound_state *cstate,
2541 struct nfsd4_create_session *cr_ses)
2542{
Jeff Layton363168b2009-08-14 12:57:56 -04002543 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002544 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002545 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002546 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002547 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002548 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002549 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002550 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002551
Mi Jinlonga62573d2011-03-23 17:57:07 +08002552 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2553 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002554 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2555 if (status)
2556 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002557 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002558 if (status)
2559 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002560 status = check_backchannel_attrs(&cr_ses->back_channel);
2561 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002562 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002563 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002564 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002565 if (!new)
2566 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002567 conn = alloc_conn_from_crses(rqstp, cr_ses);
2568 if (!conn)
2569 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002570
Jeff Laytond20c11d2014-07-30 08:27:07 -04002571 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002572 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002573 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002574 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002575
2576 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002577 status = nfserr_wrong_cred;
2578 if (!mach_creds_match(conf, rqstp))
2579 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002580 cs_slot = &conf->cl_cs_slot;
2581 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08002582 if (status) {
2583 if (status == nfserr_replay_cache)
2584 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002585 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002586 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002587 } else if (unconf) {
2588 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002589 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002590 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002591 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002592 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002593 status = nfserr_wrong_cred;
2594 if (!mach_creds_match(unconf, rqstp))
2595 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002596 cs_slot = &unconf->cl_cs_slot;
2597 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002598 if (status) {
2599 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002600 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002601 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002602 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002603 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002604 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002605 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002606 if (status) {
2607 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002608 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002609 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002610 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002611 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002612 conf = unconf;
2613 } else {
2614 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002615 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002616 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002617 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002618 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002619 * We do not support RDMA or persistent sessions
2620 */
2621 cr_ses->flags &= ~SESSION4_PERSIST;
2622 cr_ses->flags &= ~SESSION4_RDMA;
2623
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002624 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002625 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002626
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002627 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002628 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002629 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002630 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002631
Jeff Laytond20c11d2014-07-30 08:27:07 -04002632 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002633 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002634 spin_unlock(&nn->client_lock);
2635 /* init connection and backchannel */
2636 nfsd4_init_conn(rqstp, conn, new);
2637 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002638 if (old)
2639 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002640 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002641out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002642 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002643 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002644 if (old)
2645 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002646out_free_session:
2647 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002648out_release_drc_mem:
2649 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002650 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002651}
2652
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002653static __be32 nfsd4_map_bcts_dir(u32 *dir)
2654{
2655 switch (*dir) {
2656 case NFS4_CDFC4_FORE:
2657 case NFS4_CDFC4_BACK:
2658 return nfs_ok;
2659 case NFS4_CDFC4_FORE_OR_BOTH:
2660 case NFS4_CDFC4_BACK_OR_BOTH:
2661 *dir = NFS4_CDFC4_BOTH;
2662 return nfs_ok;
2663 };
2664 return nfserr_inval;
2665}
2666
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002667__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2668{
2669 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002670 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002671 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002672
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002673 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2674 if (status)
2675 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002676 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002677 session->se_cb_prog = bc->bc_cb_program;
2678 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002679 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002680
2681 nfsd4_probe_callback(session->se_client);
2682
2683 return nfs_ok;
2684}
2685
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002686__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2687 struct nfsd4_compound_state *cstate,
2688 struct nfsd4_bind_conn_to_session *bcts)
2689{
2690 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002691 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002692 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002693 struct net *net = SVC_NET(rqstp);
2694 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002695
2696 if (!nfsd4_last_compound_op(rqstp))
2697 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002698 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002699 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002700 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002701 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002702 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002703 status = nfserr_wrong_cred;
2704 if (!mach_creds_match(session->se_client, rqstp))
2705 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002706 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002707 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002708 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002709 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002710 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002711 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002712 goto out;
2713 nfsd4_init_conn(rqstp, conn, session);
2714 status = nfs_ok;
2715out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002716 nfsd4_put_session(session);
2717out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002718 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002719}
2720
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002721static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2722{
2723 if (!session)
2724 return 0;
2725 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2726}
2727
Andy Adamson069b6ad2009-04-03 08:27:58 +03002728__be32
2729nfsd4_destroy_session(struct svc_rqst *r,
2730 struct nfsd4_compound_state *cstate,
2731 struct nfsd4_destroy_session *sessionid)
2732{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002733 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002734 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002735 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002736 struct net *net = SVC_NET(r);
2737 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002738
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002739 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002740 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002741 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002742 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002743 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002744 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002745 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002746 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002747 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002748 if (!ses)
2749 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002750 status = nfserr_wrong_cred;
2751 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002752 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002753 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002754 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002755 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002756 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002757 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002758
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002759 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002760
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002761 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002762 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002763out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002764 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002765out_client_lock:
2766 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002767out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03002768 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002769}
2770
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002771static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002772{
2773 struct nfsd4_conn *c;
2774
2775 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002776 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002777 return c;
2778 }
2779 }
2780 return NULL;
2781}
2782
J. Bruce Fields57266a62013-04-13 14:27:29 -04002783static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002784{
2785 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002786 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002787 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002788 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002789
2790 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002791 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002792 if (c)
2793 goto out_free;
2794 status = nfserr_conn_not_bound_to_session;
2795 if (clp->cl_mach_cred)
2796 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002797 __nfsd4_hash_conn(new, ses);
2798 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002799 ret = nfsd4_register_conn(new);
2800 if (ret)
2801 /* oops; xprt is already down: */
2802 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002803 return nfs_ok;
2804out_free:
2805 spin_unlock(&clp->cl_lock);
2806 free_conn(new);
2807 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002808}
2809
Mi Jinlong868b89c2011-04-27 09:09:58 +08002810static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2811{
2812 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2813
2814 return args->opcnt > session->se_fchannel.maxops;
2815}
2816
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002817static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2818 struct nfsd4_session *session)
2819{
2820 struct xdr_buf *xb = &rqstp->rq_arg;
2821
2822 return xb->len > session->se_fchannel.maxreq_sz;
2823}
2824
Andy Adamson069b6ad2009-04-03 08:27:58 +03002825__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002826nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002827 struct nfsd4_compound_state *cstate,
2828 struct nfsd4_sequence *seq)
2829{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002830 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002831 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002832 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002833 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002834 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002835 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002836 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002837 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002838 struct net *net = SVC_NET(rqstp);
2839 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002840
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002841 if (resp->opcnt != 1)
2842 return nfserr_sequence_pos;
2843
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002844 /*
2845 * Will be either used or freed by nfsd4_sequence_check_conn
2846 * below.
2847 */
2848 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2849 if (!conn)
2850 return nfserr_jukebox;
2851
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002852 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002853 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002854 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002855 goto out_no_session;
2856 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002857
Mi Jinlong868b89c2011-04-27 09:09:58 +08002858 status = nfserr_too_many_ops;
2859 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002860 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002861
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002862 status = nfserr_req_too_big;
2863 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002864 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002865
Benny Halevyb85d4c02009-04-03 08:28:08 +03002866 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002867 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002868 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002869
Andy Adamson557ce262009-08-28 08:45:04 -04002870 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002871 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2872
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002873 /* We do not negotiate the number of slots yet, so set the
2874 * maxslots to the session maxreqs which is used to encode
2875 * sr_highest_slotid and the sr_target_slot id to maxslots */
2876 seq->maxslots = session->se_fchannel.maxreqs;
2877
J. Bruce Fields73e79482012-02-13 16:39:00 -05002878 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2879 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002880 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002881 status = nfserr_seq_misordered;
2882 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002883 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002884 cstate->slot = slot;
2885 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002886 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002887 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002888 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002889 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002890 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002891 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002892 }
2893 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002894 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002895
J. Bruce Fields57266a62013-04-13 14:27:29 -04002896 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002897 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002898 if (status)
2899 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002900
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002901 buflen = (seq->cachethis) ?
2902 session->se_fchannel.maxresp_cached :
2903 session->se_fchannel.maxresp_sz;
2904 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2905 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002906 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002907 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002908 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002909
2910 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002911 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002912 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002913 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002914 if (seq->cachethis)
2915 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002916 else
2917 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002918
2919 cstate->slot = slot;
2920 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002921 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002922
Benny Halevyb85d4c02009-04-03 08:28:08 +03002923out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002924 switch (clp->cl_cb_state) {
2925 case NFSD4_CB_DOWN:
2926 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2927 break;
2928 case NFSD4_CB_FAULT:
2929 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2930 break;
2931 default:
2932 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002933 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002934 if (!list_empty(&clp->cl_revoked))
2935 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002936out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002937 if (conn)
2938 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002939 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002940 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002941out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002942 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002943 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002944}
2945
Trond Myklebustb6076642014-06-30 11:48:35 -04002946void
2947nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2948{
2949 struct nfsd4_compound_state *cs = &resp->cstate;
2950
2951 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002952 if (cs->status != nfserr_replay_cache) {
2953 nfsd4_store_cache_entry(resp);
2954 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2955 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002956 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002957 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002958 } else if (cs->clp)
2959 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002960}
2961
Mi Jinlong345c2842011-10-20 17:51:39 +08002962__be32
2963nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2964{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002965 struct nfs4_client *conf, *unconf;
2966 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002967 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002968 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002969
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002970 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002971 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002972 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002973 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002974
2975 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002976 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002977 status = nfserr_clientid_busy;
2978 goto out;
2979 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04002980 status = mark_client_expired_locked(conf);
2981 if (status)
2982 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002983 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08002984 } else if (unconf)
2985 clp = unconf;
2986 else {
2987 status = nfserr_stale_clientid;
2988 goto out;
2989 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002990 if (!mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002991 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002992 status = nfserr_wrong_cred;
2993 goto out;
2994 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002995 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002996out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002997 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002998 if (clp)
2999 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003000 return status;
3001}
3002
Andy Adamson069b6ad2009-04-03 08:27:58 +03003003__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003004nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
3005{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003006 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003007
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003008 if (rc->rca_one_fs) {
3009 if (!cstate->current_fh.fh_dentry)
3010 return nfserr_nofilehandle;
3011 /*
3012 * We don't take advantage of the rca_one_fs case.
3013 * That's OK, it's optional, we can safely ignore it.
3014 */
3015 return nfs_ok;
3016 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003017
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003018 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04003019 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3020 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003021 goto out;
3022
3023 status = nfserr_stale_clientid;
3024 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003025 /*
3026 * The following error isn't really legal.
3027 * But we only get here if the client just explicitly
3028 * destroyed the client. Surely it no longer cares what
3029 * error it gets back on an operation for the dead
3030 * client.
3031 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003032 goto out;
3033
3034 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04003035 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003036out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003037 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003038}
3039
3040__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003041nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3042 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003044 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003046 struct nfs4_client *conf, *new;
3047 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003048 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003049 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3050
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003051 new = create_client(clname, rqstp, &clverifier);
3052 if (new == NULL)
3053 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003054 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003055 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003056 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04003057 if (conf && client_has_state(conf)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003058 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003060 if (clp_used_exchangeid(conf))
3061 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003062 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04003063 char addr_str[INET6_ADDRSTRLEN];
3064 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3065 sizeof(addr_str));
3066 dprintk("NFSD: setclientid: string in use by client "
3067 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 goto out;
3069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003071 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003072 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003073 unhash_client_locked(unconf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003074 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003075 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003077 gen_confirm(new, nn);
3078 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003079 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003080 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003081 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003082 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3084 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3085 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003086 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 status = nfs_ok;
3088out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003089 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003090 if (new)
3091 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003092 if (unconf)
3093 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 return status;
3095}
3096
3097
Al Virob37ad282006-10-19 23:28:59 -07003098__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003099nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3100 struct nfsd4_compound_state *cstate,
3101 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102{
NeilBrown21ab45a2005-06-23 22:04:14 -07003103 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003104 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3106 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003107 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003108 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003110 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003112
Jeff Laytond20c11d2014-07-30 08:27:07 -04003113 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003114 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003115 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003116 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003117 * We try hard to give out unique clientid's, so if we get an
3118 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003119 * the client may be buggy; this should never happen.
3120 *
3121 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003122 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003123 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04003124 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3125 goto out;
3126 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3127 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003128 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003129 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3130 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003132 else /* case 4: client hasn't noticed we rebooted yet? */
3133 status = nfserr_stale_clientid;
3134 goto out;
3135 }
3136 status = nfs_ok;
3137 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003138 old = unconf;
3139 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003140 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003141 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003142 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3143 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04003144 status = nfserr_clid_inuse;
3145 if (client_has_state(old)
3146 && !same_creds(&unconf->cl_cred,
3147 &old->cl_cred))
3148 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003149 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003150 if (status) {
3151 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003152 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003153 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003154 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003155 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003156 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003157 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003158 get_client_locked(conf);
3159 spin_unlock(&nn->client_lock);
3160 nfsd4_probe_callback(conf);
3161 spin_lock(&nn->client_lock);
3162 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003164 spin_unlock(&nn->client_lock);
3165 if (old)
3166 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 return status;
3168}
3169
J. Bruce Fields32513b42011-10-13 16:00:16 -04003170static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003172 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3173}
3174
3175/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003176static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3177 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003178{
Trond Myklebust950e0112014-06-30 11:48:31 -04003179 lockdep_assert_held(&state_lock);
3180
J. Bruce Fields32513b42011-10-13 16:00:16 -04003181 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003182 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003183 INIT_LIST_HEAD(&fp->fi_stateids);
3184 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02003185 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003186 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003187 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003188 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003189 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003190 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3191 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003192#ifdef CONFIG_NFSD_PNFS
3193 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02003194 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003195#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04003196 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197}
3198
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003199void
NeilBrowne60d4392005-06-23 22:03:01 -07003200nfsd4_free_slabs(void)
3201{
Sachin Bhamare8287f002015-04-27 14:50:14 +02003202 kmem_cache_destroy(odstate_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003203 kmem_cache_destroy(openowner_slab);
3204 kmem_cache_destroy(lockowner_slab);
3205 kmem_cache_destroy(file_slab);
3206 kmem_cache_destroy(stateid_slab);
3207 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003208}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
Bryan Schumaker72083392011-11-01 15:24:59 -04003210int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211nfsd4_init_slabs(void)
3212{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003213 openowner_slab = kmem_cache_create("nfsd4_openowners",
3214 sizeof(struct nfs4_openowner), 0, 0, NULL);
3215 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003216 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003217 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003218 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003219 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003220 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003221 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003222 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003223 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003224 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003225 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003226 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003227 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003228 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003229 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003230 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003231 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003232 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003233 odstate_slab = kmem_cache_create("nfsd4_odstate",
3234 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3235 if (odstate_slab == NULL)
3236 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003238
Sachin Bhamare8287f002015-04-27 14:50:14 +02003239out_free_deleg_slab:
3240 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003241out_free_stateid_slab:
3242 kmem_cache_destroy(stateid_slab);
3243out_free_file_slab:
3244 kmem_cache_destroy(file_slab);
3245out_free_lockowner_slab:
3246 kmem_cache_destroy(lockowner_slab);
3247out_free_openowner_slab:
3248 kmem_cache_destroy(openowner_slab);
3249out:
NeilBrowne60d4392005-06-23 22:03:01 -07003250 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3251 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252}
3253
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003254static void init_nfs4_replay(struct nfs4_replay *rp)
3255{
3256 rp->rp_status = nfserr_serverfault;
3257 rp->rp_buflen = 0;
3258 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003259 mutex_init(&rp->rp_mutex);
3260}
3261
3262static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3263 struct nfs4_stateowner *so)
3264{
3265 if (!nfsd4_has_session(cstate)) {
3266 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003267 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04003268 }
3269}
3270
3271void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3272{
3273 struct nfs4_stateowner *so = cstate->replay_owner;
3274
3275 if (so != NULL) {
3276 cstate->replay_owner = NULL;
3277 mutex_unlock(&so->so_replay.rp_mutex);
3278 nfs4_put_stateowner(so);
3279 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003280}
3281
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003282static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283{
3284 struct nfs4_stateowner *sop;
3285
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003286 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003287 if (!sop)
3288 return NULL;
3289
3290 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3291 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003292 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003293 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003295 sop->so_owner.len = owner->len;
3296
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003297 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003298 sop->so_client = clp;
3299 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003300 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003301 return sop;
3302}
3303
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003304static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003305{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003306 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003307
Trond Myklebustd4f04892014-07-29 21:34:36 -04003308 list_add(&oo->oo_owner.so_strhash,
3309 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003310 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311}
3312
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003313static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3314{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003315 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003316}
3317
Jeff Layton6b180f02014-07-29 21:34:26 -04003318static void nfs4_free_openowner(struct nfs4_stateowner *so)
3319{
3320 struct nfs4_openowner *oo = openowner(so);
3321
3322 kmem_cache_free(openowner_slab, oo);
3323}
3324
3325static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003326 .so_unhash = nfs4_unhash_openowner,
3327 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003328};
3329
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003330static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003331alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003332 struct nfsd4_compound_state *cstate)
3333{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003334 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003335 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003337 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3338 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003340 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003341 oo->oo_owner.so_is_open_owner = 1;
3342 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003343 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003344 if (nfsd4_has_session(cstate))
3345 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003346 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003347 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003348 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003349 spin_lock(&clp->cl_lock);
3350 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003351 if (ret == NULL) {
3352 hash_openowner(oo, clp, strhashval);
3353 ret = oo;
3354 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08003355 nfs4_free_stateowner(&oo->oo_owner);
3356
Trond Myklebustd4f04892014-07-29 21:34:36 -04003357 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04003358 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359}
3360
J. Bruce Fields996e0932011-10-17 11:14:48 -04003361static 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 -04003362 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003364 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003365 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003366 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003367 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07003368 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003369 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 stp->st_access_bmap = 0;
3371 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003372 stp->st_openstp = NULL;
Jeff Layton35a92fe2015-09-17 07:47:08 -04003373 init_rwsem(&stp->st_rwsem);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003374 spin_lock(&oo->oo_owner.so_client->cl_lock);
3375 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003376 spin_lock(&fp->fi_lock);
3377 list_add(&stp->st_perfile, &fp->fi_stateids);
3378 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003379 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380}
3381
Jeff Laytond3134b12014-07-29 21:34:32 -04003382/*
3383 * In the 4.0 case we need to keep the owners around a little while to handle
3384 * CLOSE replay. We still do need to release any file access that is held by
3385 * them before returning however.
3386 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003388move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389{
Jeff Layton217526e2014-07-30 08:27:11 -04003390 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003391 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3392 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3393 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003394
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003395 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
Jeff Laytonb401be222014-07-29 21:34:33 -04003397 /*
3398 * We know that we hold one reference via nfsd4_close, and another
3399 * "persistent" reference for the client. If the refcount is higher
3400 * than 2, then there are still calls in progress that are using this
3401 * stateid. We can't put the sc_file reference until they are finished.
3402 * Wait for the refcount to drop to 2. Since it has been unhashed,
3403 * there should be no danger of the refcount going back up again at
3404 * this point.
3405 */
3406 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3407
Jeff Laytond3134b12014-07-29 21:34:32 -04003408 release_all_access(s);
3409 if (s->st_stid.sc_file) {
3410 put_nfs4_file(s->st_stid.sc_file);
3411 s->st_stid.sc_file = NULL;
3412 }
Jeff Layton217526e2014-07-30 08:27:11 -04003413
3414 spin_lock(&nn->client_lock);
3415 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003416 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003417 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003418 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003419 spin_unlock(&nn->client_lock);
3420 if (last)
3421 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422}
3423
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424/* search file_hashtbl[] for file */
3425static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04003426find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 struct nfs4_file *fp;
3429
Jeff Layton5b095e92014-10-23 08:01:02 -04003430 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02003431 if (fh_match(&fp->fi_fhandle, fh)) {
Jeff Layton5b095e92014-10-23 08:01:02 -04003432 if (atomic_inc_not_zero(&fp->fi_ref))
3433 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 }
3436 return NULL;
3437}
3438
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02003439struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003440find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003441{
3442 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003443 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003444
Jeff Layton5b095e92014-10-23 08:01:02 -04003445 rcu_read_lock();
3446 fp = find_file_locked(fh, hashval);
3447 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04003448 return fp;
3449}
3450
3451static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003452find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003453{
3454 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003455 unsigned int hashval = file_hashval(fh);
3456
3457 rcu_read_lock();
3458 fp = find_file_locked(fh, hashval);
3459 rcu_read_unlock();
3460 if (fp)
3461 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04003462
3463 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04003464 fp = find_file_locked(fh, hashval);
3465 if (likely(fp == NULL)) {
3466 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04003467 fp = new;
3468 }
3469 spin_unlock(&state_lock);
3470
3471 return fp;
3472}
3473
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003474/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 * Called to check deny when READ with all zero stateid or
3476 * WRITE with all zero or all one stateid
3477 */
Al Virob37ad282006-10-19 23:28:59 -07003478static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3480{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003482 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
Trond Myklebustca943212014-07-23 16:17:39 -04003484 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003485 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003486 return ret;
3487 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003488 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003489 if (fp->fi_share_deny & deny_type)
3490 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003491 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003492 put_nfs4_file(fp);
3493 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494}
3495
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003496static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003498 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04003499 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3500 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003501
Trond Myklebust11b91642014-07-29 21:34:08 -04003502 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003503
Jeff Layton02e12152014-07-16 10:31:57 -04003504 /*
3505 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003506 * already holding inode->i_lock.
3507 *
Jeff Laytondff13992014-07-08 14:02:49 -04003508 * If the dl_time != 0, then we know that it has already been
3509 * queued for a lease break. Don't queue it again.
3510 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003511 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003512 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003513 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003514 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003515 }
Jeff Layton02e12152014-07-16 10:31:57 -04003516 spin_unlock(&state_lock);
3517}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003519static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3520 struct rpc_task *task)
3521{
3522 struct nfs4_delegation *dp = cb_to_delegation(cb);
3523
Andrew Elblea4579742015-08-31 12:06:41 -04003524 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
3525 return 1;
3526
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003527 switch (task->tk_status) {
3528 case 0:
3529 return 1;
3530 case -EBADHANDLE:
3531 case -NFS4ERR_BAD_STATEID:
3532 /*
3533 * Race: client probably got cb_recall before open reply
3534 * granting delegation.
3535 */
3536 if (dp->dl_retries--) {
3537 rpc_delay(task, 2 * HZ);
3538 return 0;
3539 }
3540 /*FALLTHRU*/
3541 default:
3542 return -1;
3543 }
3544}
3545
3546static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3547{
3548 struct nfs4_delegation *dp = cb_to_delegation(cb);
3549
3550 nfs4_put_stid(&dp->dl_stid);
3551}
3552
3553static struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3554 .prepare = nfsd4_cb_recall_prepare,
3555 .done = nfsd4_cb_recall_done,
3556 .release = nfsd4_cb_recall_release,
3557};
3558
Jeff Layton02e12152014-07-16 10:31:57 -04003559static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3560{
3561 /*
3562 * We're assuming the state code never drops its reference
3563 * without first removing the lease. Since we're in this lease
3564 * callback (and since the lease code is serialized by the kernel
3565 * lock) we know the server hasn't removed the lease yet, we know
3566 * it's safe to take a reference.
3567 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003568 atomic_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02003569 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003570}
3571
Jeff Layton1c8c6012013-06-21 08:58:15 -04003572/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003573static bool
3574nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003575{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003576 bool ret = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003577 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3578 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003579
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003580 if (!fp) {
3581 WARN(1, "(%p)->fl_owner NULL\n", fl);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003582 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003583 }
3584 if (fp->fi_had_conflict) {
3585 WARN(1, "duplicate break on %p\n", fp);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003586 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003587 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003588 /*
3589 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003590 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003591 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003592 */
3593 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
Jeff Layton02e12152014-07-16 10:31:57 -04003595 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003596 fp->fi_had_conflict = true;
3597 /*
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003598 * If there are no delegations on the list, then return true
3599 * so that the lease code will go ahead and delete it.
Jeff Layton417c6622014-07-21 09:34:57 -04003600 */
3601 if (list_empty(&fp->fi_delegations))
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003602 ret = true;
Jeff Layton417c6622014-07-21 09:34:57 -04003603 else
3604 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3605 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003606 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003607 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608}
3609
Jeff Laytonc45198e2014-09-01 07:12:07 -04003610static int
Jeff Layton7448cc32015-01-16 15:05:57 -05003611nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3612 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613{
3614 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04003615 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 else
3617 return -EAGAIN;
3618}
3619
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003620static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003621 .lm_break = nfsd_break_deleg_cb,
3622 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623};
3624
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003625static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3626{
3627 if (nfsd4_has_session(cstate))
3628 return nfs_ok;
3629 if (seqid == so->so_seqid - 1)
3630 return nfserr_replay_me;
3631 if (seqid == so->so_seqid)
3632 return nfs_ok;
3633 return nfserr_bad_seqid;
3634}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635
Jeff Layton4b24ca72014-06-30 11:48:44 -04003636static __be32 lookup_clientid(clientid_t *clid,
3637 struct nfsd4_compound_state *cstate,
3638 struct nfsd_net *nn)
3639{
3640 struct nfs4_client *found;
3641
3642 if (cstate->clp) {
3643 found = cstate->clp;
3644 if (!same_clid(&found->cl_clientid, clid))
3645 return nfserr_stale_clientid;
3646 return nfs_ok;
3647 }
3648
3649 if (STALE_CLIENTID(clid, nn))
3650 return nfserr_stale_clientid;
3651
3652 /*
3653 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3654 * cached already then we know this is for is for v4.0 and "sessions"
3655 * will be false.
3656 */
3657 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003658 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003659 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003660 if (!found) {
3661 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003662 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003663 }
3664 atomic_inc(&found->cl_refcount);
3665 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003666
3667 /* Cache the nfs4_client in cstate! */
3668 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003669 return nfs_ok;
3670}
3671
Al Virob37ad282006-10-19 23:28:59 -07003672__be32
Andy Adamson66689582009-04-03 08:28:45 +03003673nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003674 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 clientid_t *clientid = &open->op_clientid;
3677 struct nfs4_client *clp = NULL;
3678 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003679 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003680 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003682 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003684 /*
3685 * In case we need it later, after we've already created the
3686 * file and don't want to risk a further failure:
3687 */
3688 open->op_file = nfsd4_alloc_file();
3689 if (open->op_file == NULL)
3690 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691
Trond Myklebust2d91e892014-06-30 11:48:46 -04003692 status = lookup_clientid(clientid, cstate, nn);
3693 if (status)
3694 return status;
3695 clp = cstate->clp;
3696
Trond Myklebustd4f04892014-07-29 21:34:36 -04003697 strhashval = ownerstr_hashval(&open->op_owner);
3698 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003699 open->op_openowner = oo;
3700 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003701 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003703 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003704 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003705 release_openowner(oo);
3706 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003707 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003708 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003709 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3710 if (status)
3711 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003712 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003713new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003714 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003715 if (oo == NULL)
3716 return nfserr_jukebox;
3717 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003718alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003719 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003720 if (!open->op_stp)
3721 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003722
3723 if (nfsd4_has_session(cstate) &&
3724 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
3725 open->op_odstate = alloc_clnt_odstate(clp);
3726 if (!open->op_odstate)
3727 return nfserr_jukebox;
3728 }
3729
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003730 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731}
3732
Al Virob37ad282006-10-19 23:28:59 -07003733static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003734nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3735{
3736 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3737 return nfserr_openmode;
3738 else
3739 return nfs_ok;
3740}
3741
Daniel Mackc47d8322011-05-16 16:38:14 +02003742static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003743{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003744 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3745}
3746
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003747static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003748{
3749 struct nfs4_stid *ret;
3750
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003751 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003752 if (!ret)
3753 return NULL;
3754 return delegstateid(ret);
3755}
3756
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003757static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3758{
3759 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3760 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3761}
3762
Al Virob37ad282006-10-19 23:28:59 -07003763static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003764nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003765 struct nfs4_delegation **dp)
3766{
3767 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003768 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003769 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003770
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003771 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3772 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003773 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003774 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003775 status = nfs4_check_delegmode(deleg, flags);
3776 if (status) {
3777 nfs4_put_stid(&deleg->dl_stid);
3778 goto out;
3779 }
3780 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003781out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003782 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003783 return nfs_ok;
3784 if (status)
3785 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003786 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003787 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003788}
3789
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003790static struct nfs4_ol_stateid *
3791nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003793 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003794 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
Trond Myklebust1d31a252014-07-10 14:07:25 -04003796 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003797 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 /* ignore lock owners */
3799 if (local->st_stateowner->so_is_open_owner == 0)
3800 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003801 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003802 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003803 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003804 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003807 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003808 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809}
3810
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003811static inline int nfs4_access_to_access(u32 nfs4_access)
3812{
3813 int flags = 0;
3814
3815 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3816 flags |= NFSD_MAY_READ;
3817 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3818 flags |= NFSD_MAY_WRITE;
3819 return flags;
3820}
3821
Al Virob37ad282006-10-19 23:28:59 -07003822static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3824 struct nfsd4_open *open)
3825{
3826 struct iattr iattr = {
3827 .ia_valid = ATTR_SIZE,
3828 .ia_size = 0,
3829 };
3830 if (!open->op_truncate)
3831 return 0;
3832 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003833 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3835}
3836
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003837static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003838 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3839 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003840{
Trond Myklebustde186432014-07-10 14:07:26 -04003841 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003842 __be32 status;
3843 int oflag = nfs4_access_to_omode(open->op_share_access);
3844 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003845 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003846
Trond Myklebustde186432014-07-10 14:07:26 -04003847 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003848
3849 /*
3850 * Are we trying to set a deny mode that would conflict with
3851 * current access?
3852 */
3853 status = nfs4_file_check_deny(fp, open->op_share_deny);
3854 if (status != nfs_ok) {
3855 spin_unlock(&fp->fi_lock);
3856 goto out;
3857 }
3858
3859 /* set access to the file */
3860 status = nfs4_file_get_access(fp, open->op_share_access);
3861 if (status != nfs_ok) {
3862 spin_unlock(&fp->fi_lock);
3863 goto out;
3864 }
3865
3866 /* Set access bits in stateid */
3867 old_access_bmap = stp->st_access_bmap;
3868 set_access(open->op_share_access, stp);
3869
3870 /* Set new deny mask */
3871 old_deny_bmap = stp->st_deny_bmap;
3872 set_deny(open->op_share_deny, stp);
3873 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3874
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003875 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003876 spin_unlock(&fp->fi_lock);
3877 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003878 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003879 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003880 spin_lock(&fp->fi_lock);
3881 if (!fp->fi_fds[oflag]) {
3882 fp->fi_fds[oflag] = filp;
3883 filp = NULL;
3884 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003885 }
Trond Myklebustde186432014-07-10 14:07:26 -04003886 spin_unlock(&fp->fi_lock);
3887 if (filp)
3888 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003889
3890 status = nfsd4_truncate(rqstp, cur_fh, open);
3891 if (status)
3892 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003893out:
3894 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003895out_put_access:
3896 stp->st_access_bmap = old_access_bmap;
3897 nfs4_file_put_access(fp, open->op_share_access);
3898 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3899 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003900}
3901
Al Virob37ad282006-10-19 23:28:59 -07003902static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003903nfs4_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 -07003904{
Al Virob37ad282006-10-19 23:28:59 -07003905 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02003906 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003908 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003909 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003910
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003911 /* test and set deny mode */
3912 spin_lock(&fp->fi_lock);
3913 status = nfs4_file_check_deny(fp, open->op_share_deny);
3914 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003915 set_deny(open->op_share_deny, stp);
3916 fp->fi_share_deny |=
3917 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3918 }
3919 spin_unlock(&fp->fi_lock);
3920
3921 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003924 status = nfsd4_truncate(rqstp, cur_fh, open);
3925 if (status != nfs_ok)
3926 reset_union_bmap_deny(old_deny_bmap, stp);
3927 return status;
3928}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003930/* Should we give out recallable state?: */
3931static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3932{
3933 if (clp->cl_cb_state == NFSD4_CB_UP)
3934 return true;
3935 /*
3936 * In the sessions case, since we don't have to establish a
3937 * separate connection for callbacks, we assume it's OK
3938 * until we hear otherwise:
3939 */
3940 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3941}
3942
Jeff Laytond564fbe2014-07-16 10:31:58 -04003943static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003944{
3945 struct file_lock *fl;
3946
3947 fl = locks_alloc_lock();
3948 if (!fl)
3949 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003950 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003951 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003952 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3953 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003954 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003955 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003956 return fl;
3957}
3958
J. Bruce Fields99c41512013-05-21 16:21:25 -04003959static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003960{
Trond Myklebust11b91642014-07-29 21:34:08 -04003961 struct nfs4_file *fp = dp->dl_stid.sc_file;
Kinglong Meeefde6b42015-07-13 17:33:24 +08003962 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003963 struct file *filp;
3964 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003965
Jeff Laytond564fbe2014-07-16 10:31:58 -04003966 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003967 if (!fl)
3968 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003969 filp = find_readable_file(fp);
3970 if (!filp) {
3971 /* We should always have a readable file here */
3972 WARN_ON_ONCE(1);
Kinglong Meeaf9dbaf2015-07-13 17:30:51 +08003973 locks_free_lock(fl);
Jeff Layton417c6622014-07-21 09:34:57 -04003974 return -EBADF;
3975 }
3976 fl->fl_file = filp;
Jeff Laytone6f5c782014-08-22 10:40:25 -04003977 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04003978 if (fl)
Jeff Layton417c6622014-07-21 09:34:57 -04003979 locks_free_lock(fl);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04003980 if (status)
Jeff Layton417c6622014-07-21 09:34:57 -04003981 goto out_fput;
Benny Halevycdc97502014-05-30 09:09:30 -04003982 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003983 spin_lock(&fp->fi_lock);
3984 /* Did the lease get broken before we took the lock? */
3985 status = -EAGAIN;
3986 if (fp->fi_had_conflict)
3987 goto out_unlock;
3988 /* Race breaker */
Jeff Layton0c637be2014-08-22 12:05:43 -04003989 if (fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04003990 status = 0;
Jeff Layton67db1032014-12-13 09:11:40 -05003991 ++fp->fi_delegees;
Jeff Layton417c6622014-07-21 09:34:57 -04003992 hash_delegation_locked(dp, fp);
3993 goto out_unlock;
3994 }
Jeff Layton417c6622014-07-21 09:34:57 -04003995 fp->fi_deleg_file = filp;
Jeff Layton67db1032014-12-13 09:11:40 -05003996 fp->fi_delegees = 1;
Benny Halevy931ee562014-05-30 09:09:27 -04003997 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003998 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003999 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004000 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04004001out_unlock:
4002 spin_unlock(&fp->fi_lock);
4003 spin_unlock(&state_lock);
4004out_fput:
4005 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05004006 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004007}
4008
Jeff Layton0b266932014-07-25 07:34:25 -04004009static struct nfs4_delegation *
4010nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02004011 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004012{
Jeff Layton0b266932014-07-25 07:34:25 -04004013 int status;
4014 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04004015
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004016 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004017 return ERR_PTR(-EAGAIN);
4018
Sachin Bhamare8287f002015-04-27 14:50:14 +02004019 dp = alloc_init_deleg(clp, fh, odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004020 if (!dp)
4021 return ERR_PTR(-ENOMEM);
4022
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004023 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04004024 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004025 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04004026 dp->dl_stid.sc_file = fp;
Jeff Layton0c637be2014-08-22 12:05:43 -04004027 if (!fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04004028 spin_unlock(&fp->fi_lock);
4029 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04004030 status = nfs4_setlease(dp);
4031 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04004032 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004033 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04004034 status = -EAGAIN;
4035 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004036 }
Jeff Layton67db1032014-12-13 09:11:40 -05004037 ++fp->fi_delegees;
Benny Halevy931ee562014-05-30 09:09:27 -04004038 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04004039 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04004040out_unlock:
4041 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04004042 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04004043out:
4044 if (status) {
Sachin Bhamare8287f002015-04-27 14:50:14 +02004045 put_clnt_odstate(dp->dl_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04004046 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04004047 return ERR_PTR(status);
4048 }
4049 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004050}
4051
Benny Halevy4aa89132012-02-21 14:16:44 -08004052static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4053{
4054 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4055 if (status == -EAGAIN)
4056 open->op_why_no_deleg = WND4_CONTENTION;
4057 else {
4058 open->op_why_no_deleg = WND4_RESOURCE;
4059 switch (open->op_deleg_want) {
4060 case NFS4_SHARE_WANT_READ_DELEG:
4061 case NFS4_SHARE_WANT_WRITE_DELEG:
4062 case NFS4_SHARE_WANT_ANY_DELEG:
4063 break;
4064 case NFS4_SHARE_WANT_CANCEL:
4065 open->op_why_no_deleg = WND4_CANCELLED;
4066 break;
4067 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004068 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08004069 }
4070 }
4071}
4072
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073/*
4074 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04004075 *
4076 * Note we don't support write delegations, and won't until the vfs has
4077 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 */
4079static void
Jeff Layton4cf59222014-07-25 07:34:24 -04004080nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4081 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082{
4083 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04004084 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4085 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004086 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004087 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004089 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07004090 open->op_recall = 0;
4091 switch (open->op_claim_type) {
4092 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05004093 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07004094 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004095 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4096 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004097 break;
4098 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00004099 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004100 /*
4101 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004102 * had the chance to reclaim theirs, *and* until
4103 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004104 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004105 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004106 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004107 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004108 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04004109 /*
4110 * Also, if the file was opened for write or
4111 * create, there's a good chance the client's
4112 * about to write to it, resulting in an
4113 * immediate recall (since we don't support
4114 * write delegations):
4115 */
NeilBrown7b190fe2005-06-23 22:03:23 -07004116 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04004117 goto out_no_deleg;
4118 if (open->op_create == NFS4_OPEN_CREATE)
4119 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004120 break;
4121 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004122 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004123 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004124 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004125 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004126 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004128 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004130 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004131 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04004132 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04004133 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004134 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004135out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004136 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4137 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004138 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04004139 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004140 open->op_recall = 1;
4141 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04004142
4143 /* 4.1 client asking for a delegation? */
4144 if (open->op_deleg_want)
4145 nfsd4_open_deleg_none_ext(open, status);
4146 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147}
4148
Benny Halevye27f49c2012-02-21 14:16:54 -08004149static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4150 struct nfs4_delegation *dp)
4151{
4152 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4153 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4154 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4155 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4156 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4157 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4158 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4159 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4160 }
4161 /* Otherwise the client must be confused wanting a delegation
4162 * it already has, therefore we don't return
4163 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4164 */
4165}
4166
Al Virob37ad282006-10-19 23:28:59 -07004167__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4169{
Andy Adamson66689582009-04-03 08:28:45 +03004170 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004171 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004173 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004174 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004175 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 /*
4178 * Lookup file; if found, lookup stateid and check open request,
4179 * and check for delegations in the process of being recalled.
4180 * If not found, create the nfs4_file struct
4181 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004182 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004183 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004184 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004185 if (status)
4186 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04004187 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004189 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004190 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004191 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004192 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 }
4194
4195 /*
4196 * OPEN the file, or upgrade an existing OPEN.
4197 * If truncate fails, the OPEN fails.
4198 */
4199 if (stp) {
4200 /* Stateid was found, this is an OPEN upgrade */
Jeff Layton35a92fe2015-09-17 07:47:08 -04004201 down_read(&stp->st_rwsem);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004202 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04004203 if (status) {
4204 up_read(&stp->st_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04004206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004208 stp = open->op_stp;
4209 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04004210 init_open_stateid(stp, fp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04004211 down_read(&stp->st_rwsem);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004212 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4213 if (status) {
Jeff Layton35a92fe2015-09-17 07:47:08 -04004214 up_read(&stp->st_rwsem);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004215 release_open_stateid(stp);
4216 goto out;
4217 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004218
4219 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4220 open->op_odstate);
4221 if (stp->st_clnt_odstate == open->op_odstate)
4222 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004224 update_stateid(&stp->st_stid.sc_stateid);
4225 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Jeff Layton35a92fe2015-09-17 07:47:08 -04004226 up_read(&stp->st_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
Benny Halevyd24433c2012-02-16 20:57:17 +02004228 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004229 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4230 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4231 open->op_why_no_deleg = WND4_NOT_WANTED;
4232 goto nodeleg;
4233 }
4234 }
4235
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 /*
4237 * Attempt to hand out a delegation. No error return, because the
4238 * OPEN succeeds even if we fail.
4239 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004240 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004241nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 status = nfs_ok;
4243
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004244 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004245 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004247 /* 4.1 client trying to upgrade/downgrade delegation? */
4248 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004249 open->op_deleg_want)
4250 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004251
NeilBrown13cd2182005-06-23 22:03:10 -07004252 if (fp)
4253 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004254 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08004255 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 /*
4257 * To finish the open response, we just need to set the rflags.
4258 */
4259 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004260 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004261 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004263 if (dp)
4264 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004265 if (stp)
4266 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
4268 return status;
4269}
4270
Jeff Layton58fb12e2014-07-29 21:34:27 -04004271void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04004272 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004273{
4274 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004275 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004276
Jeff Laytond3134b12014-07-29 21:34:32 -04004277 nfsd4_cstate_assign_replay(cstate, so);
4278 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004279 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004280 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04004281 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004282 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004283 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004284 if (open->op_odstate)
4285 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004286}
4287
Al Virob37ad282006-10-19 23:28:59 -07004288__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004289nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4290 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291{
4292 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004293 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004294 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 dprintk("process_renew(%08x/%08x): starting\n",
4297 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004298 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004299 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004301 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004303 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004304 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 goto out;
4306 status = nfs_ok;
4307out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 return status;
4309}
4310
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04004311void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004312nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004313{
Jeff Layton33dcc482012-04-10 11:08:48 -04004314 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004315 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004316 return;
4317
NeilBrowna76b4312005-06-23 22:04:01 -07004318 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004319 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04004320 /*
4321 * If the server goes down again right now, an NFSv4
4322 * client will still be allowed to reclaim after it comes back up,
4323 * even if it hasn't yet had a chance to reclaim state this time.
4324 *
4325 */
Jeff Layton919b8042014-09-12 16:40:20 -04004326 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004327 /*
4328 * At this point, NFSv4 clients can still reclaim. But if the
4329 * server crashes, any that have not yet reclaimed will be out
4330 * of luck on the next boot.
4331 *
4332 * (NFSv4.1+ clients are considered to have reclaimed once they
4333 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4334 * have reclaimed after their first OPEN.)
4335 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004336 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004337 /*
4338 * At this point, and once lockd and/or any other containers
4339 * exit their grace period, further reclaims will fail and
4340 * regular locking can resume.
4341 */
NeilBrowna76b4312005-06-23 22:04:01 -07004342}
4343
NeilBrownfd39ca92005-06-23 22:04:03 -07004344static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004345nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346{
4347 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004348 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004350 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004352 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004353 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004356 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004357 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004358 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004359 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 clp = list_entry(pos, struct nfs4_client, cl_lru);
4361 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4362 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004363 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 break;
4365 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004366 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004367 dprintk("NFSD: client in use (clientid %08x)\n",
4368 clp->cl_clientid.cl_id);
4369 continue;
4370 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004371 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004372 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004373 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004374 list_for_each_safe(pos, next, &reaplist) {
4375 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 dprintk("NFSD: purging unused client (clientid %08x)\n",
4377 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004378 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 expire_client(clp);
4380 }
Benny Halevycdc97502014-05-30 09:09:30 -04004381 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004382 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4384 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004385 t = dp->dl_time - cutoff;
4386 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 break;
4388 }
Jeff Layton3fcbbd22015-08-24 12:41:48 -04004389 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04004390 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 }
Benny Halevycdc97502014-05-30 09:09:30 -04004392 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004393 while (!list_empty(&reaplist)) {
4394 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4395 dl_recall_lru);
4396 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004397 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 }
Jeff Layton217526e2014-07-30 08:27:11 -04004399
4400 spin_lock(&nn->client_lock);
4401 while (!list_empty(&nn->close_lru)) {
4402 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4403 oo_close_lru);
4404 if (time_after((unsigned long)oo->oo_time,
4405 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004406 t = oo->oo_time - cutoff;
4407 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 break;
4409 }
Jeff Layton217526e2014-07-30 08:27:11 -04004410 list_del_init(&oo->oo_close_lru);
4411 stp = oo->oo_last_closed_stid;
4412 oo->oo_last_closed_stid = NULL;
4413 spin_unlock(&nn->client_lock);
4414 nfs4_put_stid(&stp->st_stid);
4415 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 }
Jeff Layton217526e2014-07-30 08:27:11 -04004417 spin_unlock(&nn->client_lock);
4418
Jeff Laytona832e7a2014-05-30 09:09:26 -04004419 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004420 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421}
4422
Harvey Harrisona254b242008-02-20 12:49:00 -08004423static struct workqueue_struct *laundry_wq;
4424static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004425
4426static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004427laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428{
4429 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004430 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4431 work);
4432 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4433 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004435 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004437 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438}
4439
Jeff Layton8fcd4612015-07-30 06:57:46 -04004440static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004441{
Jeff Layton8fcd4612015-07-30 06:57:46 -04004442 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004443 return nfserr_bad_stateid;
4444 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445}
4446
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004448access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004450 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4451 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4452 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453}
4454
4455static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004456access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004458 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4459 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460}
4461
4462static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004463__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464{
Al Virob37ad282006-10-19 23:28:59 -07004465 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466
J. Bruce Fields02921912010-07-29 15:16:59 -04004467 /* For lock stateid's, we test the parent open, not the lock: */
4468 if (stp->st_openstp)
4469 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004470 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004472 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 goto out;
4474 status = nfs_ok;
4475out:
4476 return status;
4477}
4478
Al Virob37ad282006-10-19 23:28:59 -07004479static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004480check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004482 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004484 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004485 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 * conflicting state; so we must wait out the grace period. */
4487 return nfserr_grace;
4488 } else if (flags & WR_STATE)
4489 return nfs4_share_conflict(current_fh,
4490 NFS4_SHARE_DENY_WRITE);
4491 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4492 return nfs4_share_conflict(current_fh,
4493 NFS4_SHARE_DENY_READ);
4494}
4495
4496/*
4497 * Allow READ/WRITE during grace period on recovered state only for files
4498 * that are not able to provide mandatory locking.
4499 */
4500static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004501grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004503 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504}
4505
J. Bruce Fields81b82962011-08-23 11:03:29 -04004506/* Returns true iff a is later than b: */
4507static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4508{
Jim Rees1a9357f2013-05-17 17:33:00 -04004509 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004510}
4511
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004512static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004513{
Andy Adamson66689582009-04-03 08:28:45 +03004514 /*
4515 * When sessions are used the stateid generation number is ignored
4516 * when it is zero.
4517 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004518 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004519 return nfs_ok;
4520
4521 if (in->si_generation == ref->si_generation)
4522 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004523
J. Bruce Fields0836f582008-01-26 19:08:12 -05004524 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004525 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004526 return nfserr_bad_stateid;
4527 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004528 * However, we could see a stateid from the past, even from a
4529 * non-buggy client. For example, if the client sends a lock
4530 * while some IO is outstanding, the lock may bump si_generation
4531 * while the IO is still in flight. The client could avoid that
4532 * situation by waiting for responses on all the IO requests,
4533 * but better performance may result in retrying IO that
4534 * receives an old_stateid error if requests are rarely
4535 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004536 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004537 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004538}
4539
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004540static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4541{
4542 if (ols->st_stateowner->so_is_open_owner &&
4543 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4544 return nfserr_bad_stateid;
4545 return nfs_ok;
4546}
4547
Chuck Lever7df302f2012-05-29 13:56:37 -04004548static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004549{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004550 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004551 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004552
Chuck Lever7df302f2012-05-29 13:56:37 -04004553 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004554 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004555 /* Client debugging aid. */
4556 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4557 char addr_str[INET6_ADDRSTRLEN];
4558 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4559 sizeof(addr_str));
4560 pr_warn_ratelimited("NFSD: client %s testing state ID "
4561 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004562 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004563 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004564 spin_lock(&cl->cl_lock);
4565 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004566 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004567 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004568 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004569 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004570 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004571 switch (s->sc_type) {
4572 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004573 status = nfs_ok;
4574 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004575 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004576 status = nfserr_deleg_revoked;
4577 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004578 case NFS4_OPEN_STID:
4579 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004580 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04004581 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004582 default:
4583 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004584 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004585 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004586 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004587 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004588 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004589out_unlock:
4590 spin_unlock(&cl->cl_lock);
4591 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004592}
4593
Christoph Hellwigcd61c522014-08-14 08:44:57 +02004594__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004595nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4596 stateid_t *stateid, unsigned char typemask,
4597 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004598{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004599 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004600
4601 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4602 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004603 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004604 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004605 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004606 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004607 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004608 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004609 if (status)
4610 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004611 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004612 if (!*s)
4613 return nfserr_bad_stateid;
4614 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004615}
4616
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004617static struct file *
4618nfs4_find_file(struct nfs4_stid *s, int flags)
4619{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004620 if (!s)
4621 return NULL;
4622
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004623 switch (s->sc_type) {
4624 case NFS4_DELEG_STID:
4625 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
4626 return NULL;
4627 return get_file(s->sc_file->fi_deleg_file);
4628 case NFS4_OPEN_STID:
4629 case NFS4_LOCK_STID:
4630 if (flags & RD_STATE)
4631 return find_readable_file(s->sc_file);
4632 else
4633 return find_writeable_file(s->sc_file);
4634 break;
4635 }
4636
4637 return NULL;
4638}
4639
4640static __be32
4641nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
4642{
4643 __be32 status;
4644
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004645 status = nfsd4_check_openowner_confirmed(ols);
4646 if (status)
4647 return status;
4648 return nfs4_check_openmode(ols, flags);
4649}
4650
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004651static __be32
4652nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
4653 struct file **filpp, bool *tmp_file, int flags)
4654{
4655 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
4656 struct file *file;
4657 __be32 status;
4658
4659 file = nfs4_find_file(s, flags);
4660 if (file) {
4661 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
4662 acc | NFSD_MAY_OWNER_OVERRIDE);
4663 if (status) {
4664 fput(file);
4665 return status;
4666 }
4667
4668 *filpp = file;
4669 } else {
4670 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
4671 if (status)
4672 return status;
4673
4674 if (tmp_file)
4675 *tmp_file = true;
4676 }
4677
4678 return 0;
4679}
4680
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004682 * Checks for stateid operations
4683 */
Al Virob37ad282006-10-19 23:28:59 -07004684__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004685nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
4686 struct nfsd4_compound_state *cstate, stateid_t *stateid,
4687 int flags, struct file **filpp, bool *tmp_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004689 struct svc_fh *fhp = &cstate->current_fh;
4690 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004691 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004692 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004693 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004694 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 if (filpp)
4697 *filpp = NULL;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004698 if (tmp_file)
4699 *tmp_file = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004701 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 return nfserr_grace;
4703
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004704 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
4705 status = check_special_stateids(net, fhp, stateid, flags);
4706 goto done;
4707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004709 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004710 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004711 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004712 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04004713 return status;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004714 status = check_stateid_generation(stateid, &s->sc_stateid,
4715 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04004716 if (status)
4717 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004718
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004719 switch (s->sc_type) {
4720 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004721 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004722 break;
4723 case NFS4_OPEN_STID:
4724 case NFS4_LOCK_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004725 status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004726 break;
4727 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004728 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004729 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04004731 if (status)
4732 goto out;
4733 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004734
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004735done:
4736 if (!status && filpp)
4737 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738out:
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004739 if (s)
4740 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 return status;
4742}
4743
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004744/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004745 * Test if the stateid is valid
4746 */
4747__be32
4748nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4749 struct nfsd4_test_stateid *test_stateid)
4750{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004751 struct nfsd4_test_stateid_id *stateid;
4752 struct nfs4_client *cl = cstate->session->se_client;
4753
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004754 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004755 stateid->ts_id_status =
4756 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004757
Bryan Schumaker17456802011-07-13 10:50:48 -04004758 return nfs_ok;
4759}
4760
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004761__be32
4762nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4763 struct nfsd4_free_stateid *free_stateid)
4764{
4765 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004766 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004767 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004768 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004769 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004770 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004771
Jeff Layton1af71cc2014-07-29 21:34:14 -04004772 spin_lock(&cl->cl_lock);
4773 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004774 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004775 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004776 switch (s->sc_type) {
4777 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004778 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004779 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004780 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004781 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4782 if (ret)
4783 break;
4784 ret = nfserr_locks_held;
4785 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004786 case NFS4_LOCK_STID:
4787 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4788 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004789 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004790 stp = openlockstateid(s);
4791 ret = nfserr_locks_held;
4792 if (check_for_locks(stp->st_stid.sc_file,
4793 lockowner(stp->st_stateowner)))
4794 break;
Jeff Laytone8568732015-08-24 12:41:47 -04004795 WARN_ON(!unhash_lock_stateid(stp));
Jeff Layton1af71cc2014-07-29 21:34:14 -04004796 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004797 nfs4_put_stid(s);
4798 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004799 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004800 case NFS4_REVOKED_DELEG_STID:
4801 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004802 list_del_init(&dp->dl_recall_lru);
4803 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004804 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004805 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004806 goto out;
4807 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004808 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004809out_unlock:
4810 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004811out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004812 return ret;
4813}
4814
NeilBrown4c4cd222005-07-07 17:59:27 -07004815static inline int
4816setlkflg (int type)
4817{
4818 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4819 RD_STATE : WR_STATE;
4820}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004822static __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 -04004823{
4824 struct svc_fh *current_fh = &cstate->current_fh;
4825 struct nfs4_stateowner *sop = stp->st_stateowner;
4826 __be32 status;
4827
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004828 status = nfsd4_check_seqid(cstate, sop, seqid);
4829 if (status)
4830 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004831 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4832 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004833 /*
4834 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004835 * nfserr_replay_me from the previous step, and
4836 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004837 */
4838 return nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04004839 down_write(&stp->st_rwsem);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004840 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04004841 if (status == nfs_ok)
4842 status = nfs4_check_fh(current_fh, &stp->st_stid);
4843 if (status != nfs_ok)
4844 up_write(&stp->st_rwsem);
4845 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004846}
4847
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848/*
4849 * Checks for sequence id mutating operations.
4850 */
Al Virob37ad282006-10-19 23:28:59 -07004851static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004852nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004853 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004854 struct nfs4_ol_stateid **stpp,
4855 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004857 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004858 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004859 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004861 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4862 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004863
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004865 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004866 if (status)
4867 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004868 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004869 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004871 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004872 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004873 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004874 else
4875 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004876 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004877}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004878
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004879static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4880 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004881{
4882 __be32 status;
4883 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004884 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004886 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004887 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004888 if (status)
4889 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004890 oo = openowner(stp->st_stateowner);
4891 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Jeff Layton35a92fe2015-09-17 07:47:08 -04004892 up_write(&stp->st_rwsem);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004893 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004894 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004895 }
4896 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004897 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898}
4899
Al Virob37ad282006-10-19 23:28:59 -07004900__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004901nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004902 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903{
Al Virob37ad282006-10-19 23:28:59 -07004904 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004905 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004906 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004907 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908
Al Viroa6a9f182013-09-16 10:57:01 -04004909 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4910 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004912 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004913 if (status)
4914 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004916 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004917 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004918 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004919 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004920 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004921 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004922 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04004923 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
4924 up_write(&stp->st_rwsem);
Trond Myklebust2585fc72014-07-29 21:34:21 -04004925 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04004926 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004927 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004928 update_stateid(&stp->st_stid.sc_stateid);
4929 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Jeff Layton35a92fe2015-09-17 07:47:08 -04004930 up_write(&stp->st_rwsem);
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004931 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004932 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004933
Jeff Layton2a4317c2012-03-21 16:42:43 -04004934 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004935 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004936put_stateid:
4937 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004939 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 return status;
4941}
4942
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004943static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004945 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004946 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004947 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004948 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004949}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004950
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004951static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4952{
4953 switch (to_access) {
4954 case NFS4_SHARE_ACCESS_READ:
4955 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4956 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4957 break;
4958 case NFS4_SHARE_ACCESS_WRITE:
4959 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4960 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4961 break;
4962 case NFS4_SHARE_ACCESS_BOTH:
4963 break;
4964 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004965 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 }
4967}
4968
Al Virob37ad282006-10-19 23:28:59 -07004969__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004970nfsd4_open_downgrade(struct svc_rqst *rqstp,
4971 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004972 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973{
Al Virob37ad282006-10-19 23:28:59 -07004974 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004975 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004976 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
Al Viroa6a9f182013-09-16 10:57:01 -04004978 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4979 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004981 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004982 if (od->od_deleg_want)
4983 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4984 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004986 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004987 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004988 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004991 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004992 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004994 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004996 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004997 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004999 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005001 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002
Jeff Laytonce0fc432012-05-11 09:45:14 -04005003 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005005 update_stateid(&stp->st_stid.sc_stateid);
5006 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005008put_stateid:
Jeff Layton35a92fe2015-09-17 07:47:08 -04005009 up_write(&stp->st_rwsem);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005010 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005012 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 return status;
5014}
5015
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005016static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5017{
Trond Myklebustacf92952014-06-30 11:48:37 -04005018 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04005019 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04005020 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005021
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005022 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04005023 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04005024 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005025
Jeff Laytond83017f2014-07-29 21:34:42 -04005026 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04005027 if (unhashed)
5028 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04005029 spin_unlock(&clp->cl_lock);
5030 free_ol_stateid_reaplist(&reaplist);
5031 } else {
5032 spin_unlock(&clp->cl_lock);
5033 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04005034 if (unhashed)
5035 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04005036 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04005037}
5038
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039/*
5040 * nfs4_unlock_state() called after encode
5041 */
Al Virob37ad282006-10-19 23:28:59 -07005042__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005043nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005044 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045{
Al Virob37ad282006-10-19 23:28:59 -07005046 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005047 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005048 struct net *net = SVC_NET(rqstp);
5049 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050
Al Viroa6a9f182013-09-16 10:57:01 -04005051 dprintk("NFSD: nfsd4_close on file %pd\n",
5052 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005054 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5055 &close->cl_stateid,
5056 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005057 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005058 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005059 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005061 update_stateid(&stp->st_stid.sc_stateid);
5062 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Jeff Layton35a92fe2015-09-17 07:47:08 -04005063 up_write(&stp->st_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005065 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005066
5067 /* put reference from nfs4_preprocess_seqid_op */
5068 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 return status;
5071}
5072
Al Virob37ad282006-10-19 23:28:59 -07005073__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005074nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5075 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005077 struct nfs4_delegation *dp;
5078 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005079 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07005080 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005081 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005083 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005084 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005086 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005087 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005088 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005089 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04005090 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005091 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04005092 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005093
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005094 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005095put_stateid:
5096 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097out:
5098 return status;
5099}
5100
Benny Halevy87df4de2008-12-15 19:42:03 +02005101static inline u64
5102end_offset(u64 start, u64 len)
5103{
5104 u64 end;
5105
5106 end = start + len;
5107 return end >= start ? end: NFS4_MAX_UINT64;
5108}
5109
5110/* last octet in a range */
5111static inline u64
5112last_byte_offset(u64 start, u64 len)
5113{
5114 u64 end;
5115
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005116 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02005117 end = start + len;
5118 return end > start ? end - 1: NFS4_MAX_UINT64;
5119}
5120
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121/*
5122 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5123 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5124 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5125 * locking, this prevents us from being completely protocol-compliant. The
5126 * real solution to this problem is to start using unsigned file offsets in
5127 * the VFS, but this is a very deep change!
5128 */
5129static inline void
5130nfs4_transform_lock_offset(struct file_lock *lock)
5131{
5132 if (lock->fl_start < 0)
5133 lock->fl_start = OFFSET_MAX;
5134 if (lock->fl_end < 0)
5135 lock->fl_end = OFFSET_MAX;
5136}
5137
Jeff Laytoncae80b32015-04-03 09:04:04 -04005138static fl_owner_t
5139nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005140{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005141 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5142
5143 nfs4_get_stateowner(&lo->lo_owner);
5144 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005145}
5146
Jeff Laytoncae80b32015-04-03 09:04:04 -04005147static void
5148nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005149{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005150 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005151
Jeff Laytoncae80b32015-04-03 09:04:04 -04005152 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04005153 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04005154}
5155
Alexey Dobriyan7b021962009-09-21 17:01:12 -07005156static const struct lock_manager_operations nfsd_posix_mng_ops = {
Kinglong Meeaef95832014-08-22 10:18:44 -04005157 .lm_get_owner = nfsd4_fl_get_owner,
5158 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07005159};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160
5161static inline void
5162nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5163{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005164 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165
NeilBrownd5b90262006-04-10 22:55:22 -07005166 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005167 lo = (struct nfs4_lockowner *) fl->fl_owner;
5168 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5169 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005170 if (!deny->ld_owner.data)
5171 /* We just don't care that much */
5172 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005173 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5174 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07005175 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005176nevermind:
5177 deny->ld_owner.len = 0;
5178 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07005179 deny->ld_clientid.cl_boot = 0;
5180 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 }
5182 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02005183 deny->ld_length = NFS4_MAX_UINT64;
5184 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5186 deny->ld_type = NFS4_READ_LT;
5187 if (fl->fl_type != F_RDLCK)
5188 deny->ld_type = NFS4_WRITE_LT;
5189}
5190
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005191static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005192find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193{
Trond Myklebustd4f04892014-07-29 21:34:36 -04005194 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005195 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196
Trond Myklebust0a880a22014-07-30 08:27:10 -04005197 lockdep_assert_held(&clp->cl_lock);
5198
Trond Myklebustd4f04892014-07-29 21:34:36 -04005199 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5200 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005201 if (so->so_is_open_owner)
5202 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005203 if (same_owner_str(so, owner))
5204 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 }
5206 return NULL;
5207}
5208
Trond Myklebustc58c6612014-07-29 21:34:35 -04005209static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005210find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04005211{
5212 struct nfs4_lockowner *lo;
5213
Trond Myklebustd4f04892014-07-29 21:34:36 -04005214 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005215 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005216 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005217 return lo;
5218}
5219
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005220static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5221{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005222 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005223}
5224
Jeff Layton6b180f02014-07-29 21:34:26 -04005225static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5226{
5227 struct nfs4_lockowner *lo = lockowner(sop);
5228
5229 kmem_cache_free(lockowner_slab, lo);
5230}
5231
5232static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005233 .so_unhash = nfs4_unhash_lockowner,
5234 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005235};
5236
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237/*
5238 * Alloc a lock owner structure.
5239 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005240 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005242 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005244static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005245alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5246 struct nfs4_ol_stateid *open_stp,
5247 struct nfsd4_lock *lock)
5248{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005249 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005251 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5252 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005254 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5255 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005256 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005257 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005258 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005259 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005260 if (ret == NULL) {
5261 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005262 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005263 ret = lo;
5264 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08005265 nfs4_free_stateowner(&lo->lo_owner);
5266
Trond Myklebustd4f04892014-07-29 21:34:36 -04005267 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04005268 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269}
5270
Jeff Layton356a95e2014-07-29 21:34:13 -04005271static void
5272init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5273 struct nfs4_file *fp, struct inode *inode,
5274 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005276 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277
Jeff Layton356a95e2014-07-29 21:34:13 -04005278 lockdep_assert_held(&clp->cl_lock);
5279
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005280 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005281 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005282 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07005283 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005284 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04005285 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005286 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005288 stp->st_openstp = open_stp;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005289 init_rwsem(&stp->st_rwsem);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005290 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005291 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005292 spin_lock(&fp->fi_lock);
5293 list_add(&stp->st_perfile, &fp->fi_stateids);
5294 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295}
5296
Jeff Laytonc53530d2014-06-30 11:48:39 -04005297static struct nfs4_ol_stateid *
5298find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5299{
5300 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005301 struct nfs4_client *clp = lo->lo_owner.so_client;
5302
5303 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005304
5305 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005306 if (lst->st_stid.sc_file == fp) {
5307 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005308 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005309 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005310 }
5311 return NULL;
5312}
5313
Jeff Layton356a95e2014-07-29 21:34:13 -04005314static struct nfs4_ol_stateid *
5315find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5316 struct inode *inode, struct nfs4_ol_stateid *ost,
5317 bool *new)
5318{
5319 struct nfs4_stid *ns = NULL;
5320 struct nfs4_ol_stateid *lst;
5321 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5322 struct nfs4_client *clp = oo->oo_owner.so_client;
5323
5324 spin_lock(&clp->cl_lock);
5325 lst = find_lock_stateid(lo, fi);
5326 if (lst == NULL) {
5327 spin_unlock(&clp->cl_lock);
5328 ns = nfs4_alloc_stid(clp, stateid_slab);
5329 if (ns == NULL)
5330 return NULL;
5331
5332 spin_lock(&clp->cl_lock);
5333 lst = find_lock_stateid(lo, fi);
5334 if (likely(!lst)) {
5335 lst = openlockstateid(ns);
5336 init_lock_stateid(lst, lo, fi, inode, ost);
5337 ns = NULL;
5338 *new = true;
5339 }
5340 }
5341 spin_unlock(&clp->cl_lock);
5342 if (ns)
5343 nfs4_put_stid(ns);
5344 return lst;
5345}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005346
NeilBrownfd39ca92005-06-23 22:04:03 -07005347static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348check_lock_length(u64 offset, u64 length)
5349{
Kinglong Meee7969312015-07-13 17:34:19 +08005350 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5351 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352}
5353
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005354static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005355{
Trond Myklebust11b91642014-07-29 21:34:08 -04005356 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005357
Jeff Layton7214e862014-07-10 14:07:33 -04005358 lockdep_assert_held(&fp->fi_lock);
5359
Jeff Layton82c5ff12012-05-11 09:45:13 -04005360 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005361 return;
Jeff Layton12659652014-07-10 14:07:28 -04005362 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005363 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005364}
5365
Jeff Layton356a95e2014-07-29 21:34:13 -04005366static __be32
5367lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5368 struct nfs4_ol_stateid *ost,
5369 struct nfsd4_lock *lock,
5370 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005371{
Jeff Layton5db1c032014-07-29 21:34:28 -04005372 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005373 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005374 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5375 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00005376 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005377 struct nfs4_lockowner *lo;
5378 unsigned int strhashval;
5379
Kinglong Meec8623992015-07-13 17:35:37 +08005380 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005381 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005382 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005383 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5384 if (lo == NULL)
5385 return nfserr_jukebox;
5386 } else {
5387 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005388 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005389 if (!cstate->minorversion &&
5390 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005391 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005392 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005393
Jeff Layton356a95e2014-07-29 21:34:13 -04005394 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005395 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005396 status = nfserr_jukebox;
5397 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005398 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005399 status = nfs_ok;
5400out:
5401 nfs4_put_stateowner(&lo->lo_owner);
5402 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005403}
5404
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405/*
5406 * LOCK operation
5407 */
Al Virob37ad282006-10-19 23:28:59 -07005408__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005409nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005410 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005412 struct nfs4_openowner *open_sop = NULL;
5413 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005414 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005415 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005416 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005417 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005418 struct file_lock *file_lock = NULL;
5419 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005420 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005421 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005422 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005423 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005424 struct net *net = SVC_NET(rqstp);
5425 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426
5427 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5428 (long long) lock->lk_offset,
5429 (long long) lock->lk_length);
5430
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 if (check_lock_length(lock->lk_offset, lock->lk_length))
5432 return nfserr_inval;
5433
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005434 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005435 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005436 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5437 return status;
5438 }
5439
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005441 if (nfsd4_has_session(cstate))
5442 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005443 memcpy(&lock->lk_new_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04005444 &cstate->session->se_client->cl_clientid,
5445 sizeof(clientid_t));
5446
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005448 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005452 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 lock->lk_new_open_seqid,
5454 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005455 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005456 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005458 up_write(&open_stp->st_rwsem);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005459 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005460 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005461 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005462 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005463 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005464 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005465 &lock_stp, &new);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005466 if (status == nfs_ok)
5467 down_write(&lock_stp->st_rwsem);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005468 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005469 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005470 lock->lk_old_lock_seqid,
5471 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005472 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005473 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005474 if (status)
5475 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005476 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005478 lkflg = setlkflg(lock->lk_type);
5479 status = nfs4_check_openmode(lock_stp, lkflg);
5480 if (status)
5481 goto out;
5482
NeilBrown0dd395d2005-07-07 17:59:15 -07005483 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005484 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005485 goto out;
5486 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005487 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005488 goto out;
5489
Jeff Layton21179d82012-08-21 08:03:32 -04005490 file_lock = locks_alloc_lock();
5491 if (!file_lock) {
5492 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5493 status = nfserr_jukebox;
5494 goto out;
5495 }
5496
Trond Myklebust11b91642014-07-29 21:34:08 -04005497 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 switch (lock->lk_type) {
5499 case NFS4_READ_LT:
5500 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005501 spin_lock(&fp->fi_lock);
5502 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005503 if (filp)
5504 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005505 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005506 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005507 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 case NFS4_WRITE_LT:
5509 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005510 spin_lock(&fp->fi_lock);
5511 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005512 if (filp)
5513 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005514 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005515 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005516 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 default:
5518 status = nfserr_inval;
5519 goto out;
5520 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005521 if (!filp) {
5522 status = nfserr_openmode;
5523 goto out;
5524 }
Kinglong Meeaef95832014-08-22 10:18:44 -04005525
5526 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04005527 file_lock->fl_pid = current->tgid;
5528 file_lock->fl_file = filp;
5529 file_lock->fl_flags = FL_POSIX;
5530 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5531 file_lock->fl_start = lock->lk_offset;
5532 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5533 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534
Jeff Layton21179d82012-08-21 08:03:32 -04005535 conflock = locks_alloc_lock();
5536 if (!conflock) {
5537 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5538 status = nfserr_jukebox;
5539 goto out;
5540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541
Jeff Layton21179d82012-08-21 08:03:32 -04005542 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005543 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005545 update_stateid(&lock_stp->st_stid.sc_stateid);
5546 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005548 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005549 break;
5550 case (EAGAIN): /* conflock holds conflicting lock */
5551 status = nfserr_denied;
5552 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005553 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005554 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 case (EDEADLK):
5556 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005557 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005558 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005559 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005560 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005561 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563out:
Trond Myklebustde186432014-07-10 14:07:26 -04005564 if (filp)
5565 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005566 if (lock_stp) {
5567 /* Bump seqid manually if the 4.0 replay owner is openowner */
5568 if (cstate->replay_owner &&
5569 cstate->replay_owner != &lock_sop->lo_owner &&
5570 seqid_mutating_err(ntohl(status)))
5571 lock_sop->lo_owner.so_seqid++;
5572
Jeff Layton35a92fe2015-09-17 07:47:08 -04005573 up_write(&lock_stp->st_rwsem);
5574
Jeff Layton5db1c032014-07-29 21:34:28 -04005575 /*
5576 * If this is a new, never-before-used stateid, and we are
5577 * returning an error, then just go ahead and release it.
5578 */
5579 if (status && new)
5580 release_lock_stateid(lock_stp);
5581
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005582 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005583 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005584 if (open_stp)
5585 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005586 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005587 if (file_lock)
5588 locks_free_lock(file_lock);
5589 if (conflock)
5590 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 return status;
5592}
5593
5594/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005595 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5596 * so we do a temporary open here just to get an open file to pass to
5597 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5598 * inode operation.)
5599 */
Al Viro04da6e92012-04-13 00:00:04 -04005600static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005601{
5602 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005603 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5604 if (!err) {
5605 err = nfserrno(vfs_test_lock(file, lock));
Christoph Hellwigfd891452015-04-28 15:41:16 +02005606 fput(file);
Al Viro04da6e92012-04-13 00:00:04 -04005607 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005608 return err;
5609}
5610
5611/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 * LOCKT operation
5613 */
Al Virob37ad282006-10-19 23:28:59 -07005614__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005615nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5616 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617{
Jeff Layton21179d82012-08-21 08:03:32 -04005618 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005619 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005620 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005621 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005623 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 return nfserr_grace;
5625
5626 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5627 return nfserr_inval;
5628
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005629 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005630 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005631 if (status)
5632 goto out;
5633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005635 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637
Jeff Layton21179d82012-08-21 08:03:32 -04005638 file_lock = locks_alloc_lock();
5639 if (!file_lock) {
5640 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5641 status = nfserr_jukebox;
5642 goto out;
5643 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005644
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 switch (lockt->lt_type) {
5646 case NFS4_READ_LT:
5647 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005648 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 break;
5650 case NFS4_WRITE_LT:
5651 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005652 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 break;
5654 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005655 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 status = nfserr_inval;
5657 goto out;
5658 }
5659
Kinglong Meec8623992015-07-13 17:35:37 +08005660 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005661 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005662 file_lock->fl_owner = (fl_owner_t)lo;
5663 file_lock->fl_pid = current->tgid;
5664 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665
Jeff Layton21179d82012-08-21 08:03:32 -04005666 file_lock->fl_start = lockt->lt_offset;
5667 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668
Jeff Layton21179d82012-08-21 08:03:32 -04005669 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670
Jeff Layton21179d82012-08-21 08:03:32 -04005671 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005672 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005673 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005674
Jeff Layton21179d82012-08-21 08:03:32 -04005675 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005677 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 }
5679out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005680 if (lo)
5681 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04005682 if (file_lock)
5683 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 return status;
5685}
5686
Al Virob37ad282006-10-19 23:28:59 -07005687__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005688nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005689 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005691 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005693 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005694 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005695 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005696 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5697
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5699 (long long) locku->lu_offset,
5700 (long long) locku->lu_length);
5701
5702 if (check_lock_length(locku->lu_offset, locku->lu_length))
5703 return nfserr_inval;
5704
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005705 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005706 &locku->lu_stateid, NFS4_LOCK_STID,
5707 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005708 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005710 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005711 if (!filp) {
5712 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005713 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005714 }
Jeff Layton21179d82012-08-21 08:03:32 -04005715 file_lock = locks_alloc_lock();
5716 if (!file_lock) {
5717 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5718 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005719 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005720 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005721
Jeff Layton21179d82012-08-21 08:03:32 -04005722 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04005723 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04005724 file_lock->fl_pid = current->tgid;
5725 file_lock->fl_file = filp;
5726 file_lock->fl_flags = FL_POSIX;
5727 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5728 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729
Jeff Layton21179d82012-08-21 08:03:32 -04005730 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5731 locku->lu_length);
5732 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733
Jeff Layton21179d82012-08-21 08:03:32 -04005734 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005735 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005736 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 goto out_nfserr;
5738 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005739 update_stateid(&stp->st_stid.sc_stateid);
5740 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005741fput:
5742 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005743put_stateid:
Jeff Layton35a92fe2015-09-17 07:47:08 -04005744 up_write(&stp->st_rwsem);
Trond Myklebust858cc572014-07-29 21:34:16 -04005745 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005747 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005748 if (file_lock)
5749 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 return status;
5751
5752out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005753 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005754 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755}
5756
5757/*
5758 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005759 * true: locks held by lockowner
5760 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005762static bool
5763check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005765 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005766 int status = false;
5767 struct file *filp = find_any_file(fp);
5768 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005769 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005770
5771 if (!filp) {
5772 /* Any valid lock stateid should have some sort of access */
5773 WARN_ON_ONCE(1);
5774 return status;
5775 }
5776
5777 inode = file_inode(filp);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005778 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005780 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05005781 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005782 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
5783 if (fl->fl_owner == (fl_owner_t)lowner) {
5784 status = true;
5785 break;
5786 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005787 }
Jeff Layton6109c852015-01-16 15:05:57 -05005788 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 }
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005790 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 return status;
5792}
5793
Al Virob37ad282006-10-19 23:28:59 -07005794__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005795nfsd4_release_lockowner(struct svc_rqst *rqstp,
5796 struct nfsd4_compound_state *cstate,
5797 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798{
5799 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005800 struct nfs4_stateowner *sop;
5801 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005802 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005804 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005805 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005806 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005807 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808
5809 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5810 clid->cl_boot, clid->cl_id);
5811
Jeff Layton4b24ca72014-06-30 11:48:44 -04005812 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005813 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04005814 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005815
Trond Myklebustd4f04892014-07-29 21:34:36 -04005816 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005817 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005818 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005819 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005820 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005821
5822 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005823 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005824
Jeff Layton882e9d22014-07-29 21:34:37 -04005825 /* see if there are still any locks associated with it */
5826 lo = lockowner(sop);
5827 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5828 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5829 status = nfserr_locks_held;
5830 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04005831 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04005832 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005833 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005834
Kinglong Meeb5971af2014-08-22 10:18:43 -04005835 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04005836 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005837 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005838 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005839 if (lo)
5840 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 return status;
5842}
5843
5844static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005845alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846{
NeilBrowna55370a2005-06-23 22:03:52 -07005847 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848}
5849
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005850bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005851nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005852{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005853 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005854
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005855 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005856 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005857}
5858
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859/*
5860 * failure => all reset bets are off, nfserr_no_grace...
5861 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005862struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005863nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864{
5865 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005866 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867
NeilBrowna55370a2005-06-23 22:03:52 -07005868 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5869 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005870 if (crp) {
5871 strhashval = clientstr_hashval(name);
5872 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005873 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005874 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005875 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005876 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005877 }
5878 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879}
5880
Jeff Layton2a4317c2012-03-21 16:42:43 -04005881void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005882nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005883{
5884 list_del(&crp->cr_strhash);
5885 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005886 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005887}
5888
5889void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005890nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891{
5892 struct nfs4_client_reclaim *crp = NULL;
5893 int i;
5894
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005896 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5897 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005899 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 }
5901 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005902 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903}
5904
5905/*
5906 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005907struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005908nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909{
5910 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 struct nfs4_client_reclaim *crp = NULL;
5912
Jeff Layton278c9312012-11-12 15:00:52 -05005913 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914
Jeff Layton278c9312012-11-12 15:00:52 -05005915 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005916 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005917 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 return crp;
5919 }
5920 }
5921 return NULL;
5922}
5923
5924/*
5925* Called from OPEN. Look for clientid in reclaim list.
5926*/
Al Virob37ad282006-10-19 23:28:59 -07005927__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005928nfs4_check_open_reclaim(clientid_t *clid,
5929 struct nfsd4_compound_state *cstate,
5930 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005932 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005933
5934 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005935 status = lookup_clientid(clid, cstate, nn);
5936 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005937 return nfserr_reclaim_bad;
5938
Jeff Layton3b3e7b72014-09-12 16:40:22 -04005939 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
5940 return nfserr_no_grace;
5941
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005942 if (nfsd4_client_record_check(cstate->clp))
5943 return nfserr_reclaim_bad;
5944
5945 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946}
5947
Bryan Schumaker65178db2011-11-01 13:35:21 -04005948#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04005949static inline void
5950put_client(struct nfs4_client *clp)
5951{
5952 atomic_dec(&clp->cl_refcount);
5953}
5954
Jeff Layton285abde2014-07-30 08:27:24 -04005955static struct nfs4_client *
5956nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5957{
5958 struct nfs4_client *clp;
5959 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5960 nfsd_net_id);
5961
5962 if (!nfsd_netns_ready(nn))
5963 return NULL;
5964
5965 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5966 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5967 return clp;
5968 }
5969 return NULL;
5970}
5971
Jeff Layton7ec0e362014-07-30 08:27:17 -04005972u64
Jeff Layton285abde2014-07-30 08:27:24 -04005973nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04005974{
5975 struct nfs4_client *clp;
5976 u64 count = 0;
5977 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5978 nfsd_net_id);
5979 char buf[INET6_ADDRSTRLEN];
5980
5981 if (!nfsd_netns_ready(nn))
5982 return 0;
5983
5984 spin_lock(&nn->client_lock);
5985 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5986 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5987 pr_info("NFS Client: %s\n", buf);
5988 ++count;
5989 }
5990 spin_unlock(&nn->client_lock);
5991
5992 return count;
5993}
Bryan Schumaker65178db2011-11-01 13:35:21 -04005994
Jeff Laytona0926d12014-07-30 08:27:18 -04005995u64
Jeff Layton285abde2014-07-30 08:27:24 -04005996nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04005997{
5998 u64 count = 0;
5999 struct nfs4_client *clp;
6000 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6001 nfsd_net_id);
6002
6003 if (!nfsd_netns_ready(nn))
6004 return count;
6005
6006 spin_lock(&nn->client_lock);
6007 clp = nfsd_find_client(addr, addr_size);
6008 if (clp) {
6009 if (mark_client_expired_locked(clp) == nfs_ok)
6010 ++count;
6011 else
6012 clp = NULL;
6013 }
6014 spin_unlock(&nn->client_lock);
6015
6016 if (clp)
6017 expire_client(clp);
6018
6019 return count;
6020}
6021
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006022u64
Jeff Layton285abde2014-07-30 08:27:24 -04006023nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006024{
6025 u64 count = 0;
6026 struct nfs4_client *clp, *next;
6027 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6028 nfsd_net_id);
6029 LIST_HEAD(reaplist);
6030
6031 if (!nfsd_netns_ready(nn))
6032 return count;
6033
6034 spin_lock(&nn->client_lock);
6035 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6036 if (mark_client_expired_locked(clp) == nfs_ok) {
6037 list_add(&clp->cl_lru, &reaplist);
6038 if (max != 0 && ++count >= max)
6039 break;
6040 }
6041 }
6042 spin_unlock(&nn->client_lock);
6043
6044 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6045 expire_client(clp);
6046
6047 return count;
6048}
6049
Bryan Schumaker184c1842012-11-29 11:40:44 -05006050static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6051 const char *type)
6052{
6053 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05006054 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05006055 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6056}
6057
Jeff Layton016200c2014-07-30 08:27:21 -04006058static void
6059nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6060 struct list_head *collect)
6061{
6062 struct nfs4_client *clp = lst->st_stid.sc_client;
6063 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6064 nfsd_net_id);
6065
6066 if (!collect)
6067 return;
6068
6069 lockdep_assert_held(&nn->client_lock);
6070 atomic_inc(&clp->cl_refcount);
6071 list_add(&lst->st_locks, collect);
6072}
6073
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006074static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04006075 struct list_head *collect,
Jeff Laytone8568732015-08-24 12:41:47 -04006076 bool (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05006077{
6078 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006079 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006080 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006081 u64 count = 0;
6082
Jeff Layton016200c2014-07-30 08:27:21 -04006083 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006084 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006085 list_for_each_entry_safe(stp, st_next,
6086 &oop->oo_owner.so_stateids, st_perstateowner) {
6087 list_for_each_entry_safe(lst, lst_next,
6088 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04006089 if (func) {
Jeff Laytone8568732015-08-24 12:41:47 -04006090 if (func(lst))
6091 nfsd_inject_add_lock_to_list(lst,
6092 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04006093 }
Jeff Layton016200c2014-07-30 08:27:21 -04006094 ++count;
6095 /*
6096 * Despite the fact that these functions deal
6097 * with 64-bit integers for "count", we must
6098 * ensure that it doesn't blow up the
6099 * clp->cl_refcount. Throw a warning if we
6100 * start to approach INT_MAX here.
6101 */
6102 WARN_ON_ONCE(count == (INT_MAX / 2));
6103 if (count == max)
6104 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006105 }
6106 }
6107 }
Jeff Layton016200c2014-07-30 08:27:21 -04006108out:
6109 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006110
6111 return count;
6112}
6113
Jeff Layton016200c2014-07-30 08:27:21 -04006114static u64
6115nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6116 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05006117{
Jeff Layton016200c2014-07-30 08:27:21 -04006118 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006119}
6120
Jeff Layton016200c2014-07-30 08:27:21 -04006121static u64
6122nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006123{
Jeff Layton016200c2014-07-30 08:27:21 -04006124 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006125 nfsd_print_count(clp, count, "locked files");
6126 return count;
6127}
6128
Jeff Layton016200c2014-07-30 08:27:21 -04006129u64
Jeff Layton285abde2014-07-30 08:27:24 -04006130nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04006131{
6132 struct nfs4_client *clp;
6133 u64 count = 0;
6134 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6135 nfsd_net_id);
6136
6137 if (!nfsd_netns_ready(nn))
6138 return 0;
6139
6140 spin_lock(&nn->client_lock);
6141 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6142 count += nfsd_print_client_locks(clp);
6143 spin_unlock(&nn->client_lock);
6144
6145 return count;
6146}
6147
6148static void
6149nfsd_reap_locks(struct list_head *reaplist)
6150{
6151 struct nfs4_client *clp;
6152 struct nfs4_ol_stateid *stp, *next;
6153
6154 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6155 list_del_init(&stp->st_locks);
6156 clp = stp->st_stid.sc_client;
6157 nfs4_put_stid(&stp->st_stid);
6158 put_client(clp);
6159 }
6160}
6161
6162u64
Jeff Layton285abde2014-07-30 08:27:24 -04006163nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04006164{
6165 unsigned int count = 0;
6166 struct nfs4_client *clp;
6167 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6168 nfsd_net_id);
6169 LIST_HEAD(reaplist);
6170
6171 if (!nfsd_netns_ready(nn))
6172 return count;
6173
6174 spin_lock(&nn->client_lock);
6175 clp = nfsd_find_client(addr, addr_size);
6176 if (clp)
6177 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6178 spin_unlock(&nn->client_lock);
6179 nfsd_reap_locks(&reaplist);
6180 return count;
6181}
6182
6183u64
Jeff Layton285abde2014-07-30 08:27:24 -04006184nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04006185{
6186 u64 count = 0;
6187 struct nfs4_client *clp;
6188 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6189 nfsd_net_id);
6190 LIST_HEAD(reaplist);
6191
6192 if (!nfsd_netns_ready(nn))
6193 return count;
6194
6195 spin_lock(&nn->client_lock);
6196 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6197 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6198 if (max != 0 && count >= max)
6199 break;
6200 }
6201 spin_unlock(&nn->client_lock);
6202 nfsd_reap_locks(&reaplist);
6203 return count;
6204}
6205
Jeff Layton82e05ef2014-07-30 08:27:22 -04006206static u64
6207nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6208 struct list_head *collect,
6209 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006210{
6211 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04006212 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6213 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006214 u64 count = 0;
6215
Jeff Layton82e05ef2014-07-30 08:27:22 -04006216 lockdep_assert_held(&nn->client_lock);
6217
6218 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006219 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04006220 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006221 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04006222 if (collect) {
6223 atomic_inc(&clp->cl_refcount);
6224 list_add(&oop->oo_perclient, collect);
6225 }
6226 }
6227 ++count;
6228 /*
6229 * Despite the fact that these functions deal with
6230 * 64-bit integers for "count", we must ensure that
6231 * it doesn't blow up the clp->cl_refcount. Throw a
6232 * warning if we start to approach INT_MAX here.
6233 */
6234 WARN_ON_ONCE(count == (INT_MAX / 2));
6235 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006236 break;
6237 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04006238 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006239
6240 return count;
6241}
6242
Jeff Layton82e05ef2014-07-30 08:27:22 -04006243static u64
6244nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006245{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006246 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6247
6248 nfsd_print_count(clp, count, "openowners");
6249 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006250}
6251
Jeff Layton82e05ef2014-07-30 08:27:22 -04006252static u64
6253nfsd_collect_client_openowners(struct nfs4_client *clp,
6254 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006255{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006256 return nfsd_foreach_client_openowner(clp, max, collect,
6257 unhash_openowner_locked);
6258}
6259
6260u64
Jeff Layton285abde2014-07-30 08:27:24 -04006261nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006262{
6263 struct nfs4_client *clp;
6264 u64 count = 0;
6265 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6266 nfsd_net_id);
6267
6268 if (!nfsd_netns_ready(nn))
6269 return 0;
6270
6271 spin_lock(&nn->client_lock);
6272 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6273 count += nfsd_print_client_openowners(clp);
6274 spin_unlock(&nn->client_lock);
6275
6276 return count;
6277}
6278
6279static void
6280nfsd_reap_openowners(struct list_head *reaplist)
6281{
6282 struct nfs4_client *clp;
6283 struct nfs4_openowner *oop, *next;
6284
6285 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6286 list_del_init(&oop->oo_perclient);
6287 clp = oop->oo_owner.so_client;
6288 release_openowner(oop);
6289 put_client(clp);
6290 }
6291}
6292
6293u64
Jeff Layton285abde2014-07-30 08:27:24 -04006294nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6295 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006296{
6297 unsigned int count = 0;
6298 struct nfs4_client *clp;
6299 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6300 nfsd_net_id);
6301 LIST_HEAD(reaplist);
6302
6303 if (!nfsd_netns_ready(nn))
6304 return count;
6305
6306 spin_lock(&nn->client_lock);
6307 clp = nfsd_find_client(addr, addr_size);
6308 if (clp)
6309 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6310 spin_unlock(&nn->client_lock);
6311 nfsd_reap_openowners(&reaplist);
6312 return count;
6313}
6314
6315u64
Jeff Layton285abde2014-07-30 08:27:24 -04006316nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006317{
6318 u64 count = 0;
6319 struct nfs4_client *clp;
6320 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6321 nfsd_net_id);
6322 LIST_HEAD(reaplist);
6323
6324 if (!nfsd_netns_ready(nn))
6325 return count;
6326
6327 spin_lock(&nn->client_lock);
6328 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6329 count += nfsd_collect_client_openowners(clp, &reaplist,
6330 max - count);
6331 if (max != 0 && count >= max)
6332 break;
6333 }
6334 spin_unlock(&nn->client_lock);
6335 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006336 return count;
6337}
6338
Bryan Schumaker269de302012-11-29 11:40:42 -05006339static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6340 struct list_head *victims)
6341{
6342 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006343 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6344 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006345 u64 count = 0;
6346
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006347 lockdep_assert_held(&nn->client_lock);
6348
6349 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006350 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006351 if (victims) {
6352 /*
6353 * It's not safe to mess with delegations that have a
6354 * non-zero dl_time. They might have already been broken
6355 * and could be processed by the laundromat outside of
6356 * the state_lock. Just leave them be.
6357 */
6358 if (dp->dl_time != 0)
6359 continue;
6360
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006361 atomic_inc(&clp->cl_refcount);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04006362 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04006363 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006364 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006365 ++count;
6366 /*
6367 * Despite the fact that these functions deal with
6368 * 64-bit integers for "count", we must ensure that
6369 * it doesn't blow up the clp->cl_refcount. Throw a
6370 * warning if we start to approach INT_MAX here.
6371 */
6372 WARN_ON_ONCE(count == (INT_MAX / 2));
6373 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006374 break;
6375 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006376 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006377 return count;
6378}
6379
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006380static u64
6381nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006382{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006383 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006384
6385 nfsd_print_count(clp, count, "delegations");
6386 return count;
6387}
6388
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006389u64
Jeff Layton285abde2014-07-30 08:27:24 -04006390nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006391{
6392 struct nfs4_client *clp;
6393 u64 count = 0;
6394 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6395 nfsd_net_id);
6396
6397 if (!nfsd_netns_ready(nn))
6398 return 0;
6399
6400 spin_lock(&nn->client_lock);
6401 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6402 count += nfsd_print_client_delegations(clp);
6403 spin_unlock(&nn->client_lock);
6404
6405 return count;
6406}
6407
6408static void
6409nfsd_forget_delegations(struct list_head *reaplist)
6410{
6411 struct nfs4_client *clp;
6412 struct nfs4_delegation *dp, *next;
6413
6414 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6415 list_del_init(&dp->dl_recall_lru);
6416 clp = dp->dl_stid.sc_client;
6417 revoke_delegation(dp);
6418 put_client(clp);
6419 }
6420}
6421
6422u64
Jeff Layton285abde2014-07-30 08:27:24 -04006423nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6424 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006425{
6426 u64 count = 0;
6427 struct nfs4_client *clp;
6428 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6429 nfsd_net_id);
6430 LIST_HEAD(reaplist);
6431
6432 if (!nfsd_netns_ready(nn))
6433 return count;
6434
6435 spin_lock(&nn->client_lock);
6436 clp = nfsd_find_client(addr, addr_size);
6437 if (clp)
6438 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6439 spin_unlock(&nn->client_lock);
6440
6441 nfsd_forget_delegations(&reaplist);
6442 return count;
6443}
6444
6445u64
Jeff Layton285abde2014-07-30 08:27:24 -04006446nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006447{
6448 u64 count = 0;
6449 struct nfs4_client *clp;
6450 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6451 nfsd_net_id);
6452 LIST_HEAD(reaplist);
6453
6454 if (!nfsd_netns_ready(nn))
6455 return count;
6456
6457 spin_lock(&nn->client_lock);
6458 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6459 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6460 if (max != 0 && count >= max)
6461 break;
6462 }
6463 spin_unlock(&nn->client_lock);
6464 nfsd_forget_delegations(&reaplist);
6465 return count;
6466}
6467
6468static void
6469nfsd_recall_delegations(struct list_head *reaplist)
6470{
6471 struct nfs4_client *clp;
6472 struct nfs4_delegation *dp, *next;
6473
6474 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6475 list_del_init(&dp->dl_recall_lru);
6476 clp = dp->dl_stid.sc_client;
6477 /*
6478 * We skipped all entries that had a zero dl_time before,
6479 * so we can now reset the dl_time back to 0. If a delegation
6480 * break comes in now, then it won't make any difference since
6481 * we're recalling it either way.
6482 */
6483 spin_lock(&state_lock);
6484 dp->dl_time = 0;
6485 spin_unlock(&state_lock);
6486 nfsd_break_one_deleg(dp);
6487 put_client(clp);
6488 }
6489}
6490
6491u64
Jeff Layton285abde2014-07-30 08:27:24 -04006492nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006493 size_t addr_size)
6494{
6495 u64 count = 0;
6496 struct nfs4_client *clp;
6497 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6498 nfsd_net_id);
6499 LIST_HEAD(reaplist);
6500
6501 if (!nfsd_netns_ready(nn))
6502 return count;
6503
6504 spin_lock(&nn->client_lock);
6505 clp = nfsd_find_client(addr, addr_size);
6506 if (clp)
6507 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6508 spin_unlock(&nn->client_lock);
6509
6510 nfsd_recall_delegations(&reaplist);
6511 return count;
6512}
6513
6514u64
Jeff Layton285abde2014-07-30 08:27:24 -04006515nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006516{
6517 u64 count = 0;
6518 struct nfs4_client *clp, *next;
6519 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6520 nfsd_net_id);
6521 LIST_HEAD(reaplist);
6522
6523 if (!nfsd_netns_ready(nn))
6524 return count;
6525
6526 spin_lock(&nn->client_lock);
6527 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6528 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6529 if (max != 0 && ++count >= max)
6530 break;
6531 }
6532 spin_unlock(&nn->client_lock);
6533 nfsd_recall_delegations(&reaplist);
6534 return count;
6535}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006536#endif /* CONFIG_NFSD_FAULT_INJECTION */
6537
Meelap Shahc2f1a552007-07-17 04:04:39 -07006538/*
6539 * Since the lifetime of a delegation isn't limited to that of an open, a
6540 * client may quite reasonably hang on to a delegation as long as it has
6541 * the inode cached. This becomes an obvious problem the first time a
6542 * client's inode cache approaches the size of the server's total memory.
6543 *
6544 * For now we avoid this problem by imposing a hard limit on the number
6545 * of delegations, which varies according to the server's memory size.
6546 */
6547static void
6548set_max_delegations(void)
6549{
6550 /*
6551 * Allow at most 4 delegations per megabyte of RAM. Quick
6552 * estimates suggest that in the worst case (where every delegation
6553 * is for a different inode), a delegation could take about 1.5K,
6554 * giving a worst case usage of about 6% of memory.
6555 */
6556 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6557}
6558
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006559static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006560{
6561 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6562 int i;
6563
6564 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6565 CLIENT_HASH_SIZE, GFP_KERNEL);
6566 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006567 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006568 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6569 CLIENT_HASH_SIZE, GFP_KERNEL);
6570 if (!nn->unconf_id_hashtbl)
6571 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006572 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6573 SESSION_HASH_SIZE, GFP_KERNEL);
6574 if (!nn->sessionid_hashtbl)
6575 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006576
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006577 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006578 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006579 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006580 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006581 for (i = 0; i < SESSION_HASH_SIZE; i++)
6582 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006583 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006584 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03006585 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03006586 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006587 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03006588 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006589
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006590 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006591 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006592
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006593 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006594
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006595err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03006596 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006597err_unconf_id:
6598 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006599err:
6600 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006601}
6602
6603static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006604nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006605{
6606 int i;
6607 struct nfs4_client *clp = NULL;
6608 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6609
6610 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6611 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6612 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6613 destroy_client(clp);
6614 }
6615 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006616
Kinglong Mee2b905632014-03-26 22:09:30 +08006617 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6618 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6619 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6620 destroy_client(clp);
6621 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006622 }
6623
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006624 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006625 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006626 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006627 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006628}
6629
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006630int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006631nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07006632{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006633 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006634 int ret;
6635
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006636 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006637 if (ret)
6638 return ret;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006639 nn->boot_time = get_seconds();
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04006640 nn->grace_ended = false;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04006641 nn->nfsd4_manager.block_opens = true;
Jeff Laytond4318ac2014-09-12 16:40:21 -04006642 locks_start_grace(net, &nn->nfsd4_manager);
6643 nfsd4_client_tracking_init(net);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006644 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03006645 nn->nfsd4_grace, net);
6646 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006647 return 0;
6648}
6649
6650/* initialization to perform when the nfsd service is started: */
6651
6652int
6653nfs4_state_start(void)
6654{
6655 int ret;
6656
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006657 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006658 if (ret)
6659 return -ENOMEM;
Jeff Layton51a54562015-08-20 07:17:01 -04006660 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05006661 if (laundry_wq == NULL) {
6662 ret = -ENOMEM;
6663 goto out_recovery;
6664 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006665 ret = nfsd4_create_callback_queue();
6666 if (ret)
6667 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006668
Meelap Shahc2f1a552007-07-17 04:04:39 -07006669 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006670
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006671 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006672
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006673out_free_laundry:
6674 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05006675out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006676 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677}
6678
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006679void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006680nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006682 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006684 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006686 cancel_delayed_work_sync(&nn->laundromat_work);
6687 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05006688
Linus Torvalds1da177e2005-04-16 15:20:36 -07006689 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04006690 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006691 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04006693 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04006694 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695 }
Benny Halevycdc97502014-05-30 09:09:30 -04006696 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697 list_for_each_safe(pos, next, &reaplist) {
6698 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006699 list_del_init(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +02006700 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -04006701 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04006702 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 }
6704
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006705 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006706 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707}
6708
6709void
6710nfs4_state_shutdown(void)
6711{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006712 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006713 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006715
6716static void
6717get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6718{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006719 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6720 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006721}
6722
6723static void
6724put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6725{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006726 if (cstate->minorversion) {
6727 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6728 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6729 }
6730}
6731
6732void
6733clear_current_stateid(struct nfsd4_compound_state *cstate)
6734{
6735 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006736}
6737
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006738/*
6739 * functions to set current state id
6740 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006741void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006742nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6743{
6744 put_stateid(cstate, &odp->od_stateid);
6745}
6746
6747void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006748nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6749{
6750 put_stateid(cstate, &open->op_stateid);
6751}
6752
6753void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006754nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6755{
6756 put_stateid(cstate, &close->cl_stateid);
6757}
6758
6759void
6760nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6761{
6762 put_stateid(cstate, &lock->lk_resp_stateid);
6763}
6764
6765/*
6766 * functions to consume current state id
6767 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006768
6769void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006770nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6771{
6772 get_stateid(cstate, &odp->od_stateid);
6773}
6774
6775void
6776nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6777{
6778 get_stateid(cstate, &drp->dr_stateid);
6779}
6780
6781void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006782nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6783{
6784 get_stateid(cstate, &fsp->fr_stateid);
6785}
6786
6787void
6788nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6789{
6790 get_stateid(cstate, &setattr->sa_stateid);
6791}
6792
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006793void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006794nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6795{
6796 get_stateid(cstate, &close->cl_stateid);
6797}
6798
6799void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006800nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006801{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006802 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006803}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006804
6805void
6806nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6807{
6808 get_stateid(cstate, &read->rd_stateid);
6809}
6810
6811void
6812nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6813{
6814 get_stateid(cstate, &write->wr_stateid);
6815}