blob: 0f1d5691b795751553d02003c32c4ceb90a1ee5c [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 Halevycdc975052014-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 Halevycdc975052014-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 Halevycdc975052014-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 Halevycdc975052014-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 Adamsona649637c72009-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 Adamsona649637c72009-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 Fieldsac7c46f22010-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 Halevycdc975052014-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 Halevycdc975052014-05-30 09:09:30 -04001744 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 while (!list_empty(&reaplist)) {
1746 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001747 list_del_init(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +02001748 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -04001749 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04001750 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001752 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05001753 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001754 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001755 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001756 }
NeilBrownea1da632005-06-23 22:04:17 -07001757 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001758 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04001759 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001760 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001762 nfsd4_return_all_client_layouts(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001763 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001764 if (clp->cl_cb_conn.cb_xprt)
1765 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001766 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767}
1768
Trond Myklebust4beb3452014-07-30 08:27:02 -04001769static void
1770destroy_client(struct nfs4_client *clp)
1771{
1772 unhash_client(clp);
1773 __destroy_client(clp);
1774}
1775
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001776static void expire_client(struct nfs4_client *clp)
1777{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001778 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001779 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001780 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001781}
1782
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001783static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1784{
1785 memcpy(target->cl_verifier.data, source->data,
1786 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787}
1788
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001789static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1790{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1792 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1793}
1794
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001795static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001796{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001797 if (source->cr_principal) {
1798 target->cr_principal =
1799 kstrdup(source->cr_principal, GFP_KERNEL);
1800 if (target->cr_principal == NULL)
1801 return -ENOMEM;
1802 } else
1803 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001804 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 target->cr_uid = source->cr_uid;
1806 target->cr_gid = source->cr_gid;
1807 target->cr_group_info = source->cr_group_info;
1808 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001809 target->cr_gss_mech = source->cr_gss_mech;
1810 if (source->cr_gss_mech)
1811 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001812 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001815static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001816compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1817{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001818 if (o1->len < o2->len)
1819 return -1;
1820 if (o1->len > o2->len)
1821 return 1;
1822 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001823}
1824
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001825static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001826{
NeilBrowna55370a2005-06-23 22:03:52 -07001827 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
1829
1830static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001831same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1832{
1833 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
1835
1836static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001837same_clid(clientid_t *cl1, clientid_t *cl2)
1838{
1839 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840}
1841
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001842static bool groups_equal(struct group_info *g1, struct group_info *g2)
1843{
1844 int i;
1845
1846 if (g1->ngroups != g2->ngroups)
1847 return false;
1848 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001849 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001850 return false;
1851 return true;
1852}
1853
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001854/*
1855 * RFC 3530 language requires clid_inuse be returned when the
1856 * "principal" associated with a requests differs from that previously
1857 * used. We use uid, gid's, and gss principal string as our best
1858 * approximation. We also don't want to allow non-gss use of a client
1859 * established using gss: in theory cr_principal should catch that
1860 * change, but in practice cr_principal can be null even in the gss case
1861 * since gssd doesn't always pass down a principal string.
1862 */
1863static bool is_gss_cred(struct svc_cred *cr)
1864{
1865 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1866 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1867}
1868
1869
Vivek Trivedi5559b502012-07-24 21:18:20 +05301870static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001871same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1872{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001873 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001874 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1875 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001876 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1877 return false;
1878 if (cr1->cr_principal == cr2->cr_principal)
1879 return true;
1880 if (!cr1->cr_principal || !cr2->cr_principal)
1881 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301882 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
1884
J. Bruce Fields57266a62013-04-13 14:27:29 -04001885static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1886{
1887 struct svc_cred *cr = &rqstp->rq_cred;
1888 u32 service;
1889
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001890 if (!cr->cr_gss_mech)
1891 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001892 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1893 return service == RPC_GSS_SVC_INTEGRITY ||
1894 service == RPC_GSS_SVC_PRIVACY;
1895}
1896
1897static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1898{
1899 struct svc_cred *cr = &rqstp->rq_cred;
1900
1901 if (!cl->cl_mach_cred)
1902 return true;
1903 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1904 return false;
1905 if (!svc_rqst_integrity_protected(rqstp))
1906 return false;
1907 if (!cr->cr_principal)
1908 return false;
1909 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1910}
1911
Jeff Layton294ac322014-07-30 08:27:15 -04001912static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001913{
Chuck Leverab4684d2012-03-02 17:13:50 -05001914 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Jeff Laytonf4199922014-06-17 07:44:11 -04001916 /*
1917 * This is opaque to client, so no need to byte-swap. Use
1918 * __force to keep sparse happy
1919 */
1920 verf[0] = (__force __be32)get_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08001921 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001922 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
1924
Jeff Layton294ac322014-07-30 08:27:15 -04001925static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1926{
1927 clp->cl_clientid.cl_boot = nn->boot_time;
1928 clp->cl_clientid.cl_id = nn->clientid_counter++;
1929 gen_confirm(clp, nn);
1930}
1931
Jeff Layton4770d722014-07-29 21:34:10 -04001932static struct nfs4_stid *
1933find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001934{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001935 struct nfs4_stid *ret;
1936
1937 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1938 if (!ret || !ret->sc_type)
1939 return NULL;
1940 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001941}
1942
Jeff Layton4770d722014-07-29 21:34:10 -04001943static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001944find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001945{
1946 struct nfs4_stid *s;
1947
Jeff Layton4770d722014-07-29 21:34:10 -04001948 spin_lock(&cl->cl_lock);
1949 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001950 if (s != NULL) {
1951 if (typemask & s->sc_type)
1952 atomic_inc(&s->sc_count);
1953 else
1954 s = NULL;
1955 }
Jeff Layton4770d722014-07-29 21:34:10 -04001956 spin_unlock(&cl->cl_lock);
1957 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001958}
1959
Jeff Layton2216d442012-11-12 15:00:57 -05001960static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001961 struct svc_rqst *rqstp, nfs4_verifier *verf)
1962{
1963 struct nfs4_client *clp;
1964 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001965 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001966 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001967
1968 clp = alloc_client(name);
1969 if (clp == NULL)
1970 return NULL;
1971
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001972 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1973 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001974 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001975 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001976 }
Christoph Hellwig0162ac22014-09-24 12:19:19 +02001977 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Benny Halevy07cd4902010-05-12 00:13:41 +03001978 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001979 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001980 copy_verf(clp, verf);
1981 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001982 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001983 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001984 return clp;
1985}
1986
NeilBrownfd39ca92005-06-23 22:04:03 -07001987static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001988add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1989{
1990 struct rb_node **new = &(root->rb_node), *parent = NULL;
1991 struct nfs4_client *clp;
1992
1993 while (*new) {
1994 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1995 parent = *new;
1996
1997 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1998 new = &((*new)->rb_left);
1999 else
2000 new = &((*new)->rb_right);
2001 }
2002
2003 rb_link_node(&new_clp->cl_namenode, parent, new);
2004 rb_insert_color(&new_clp->cl_namenode, root);
2005}
2006
2007static struct nfs4_client *
2008find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2009{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002010 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002011 struct rb_node *node = root->rb_node;
2012 struct nfs4_client *clp;
2013
2014 while (node) {
2015 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2016 cmp = compare_blob(&clp->cl_name, name);
2017 if (cmp > 0)
2018 node = node->rb_left;
2019 else if (cmp < 0)
2020 node = node->rb_right;
2021 else
2022 return clp;
2023 }
2024 return NULL;
2025}
2026
2027static void
2028add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
2030 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002031 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Trond Myklebust0a880a22014-07-30 08:27:10 -04002033 lockdep_assert_held(&nn->client_lock);
2034
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002035 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002036 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002038 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002039 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
2041
NeilBrownfd39ca92005-06-23 22:04:03 -07002042static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043move_to_confirmed(struct nfs4_client *clp)
2044{
2045 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002046 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Trond Myklebust0a880a22014-07-30 08:27:10 -04002048 lockdep_assert_held(&nn->client_lock);
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002051 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002052 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002053 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002054 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002055 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056}
2057
2058static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002059find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
2061 struct nfs4_client *clp;
2062 unsigned int idhashval = clientid_hashval(clid->cl_id);
2063
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002064 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002065 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002066 if ((bool)clp->cl_minorversion != sessions)
2067 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002068 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
2072 return NULL;
2073}
2074
2075static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002076find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2077{
2078 struct list_head *tbl = nn->conf_id_hashtbl;
2079
Trond Myklebust0a880a22014-07-30 08:27:10 -04002080 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002081 return find_client_in_id_table(tbl, clid, sessions);
2082}
2083
2084static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002085find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002087 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Trond Myklebust0a880a22014-07-30 08:27:10 -04002089 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002090 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091}
2092
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002093static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002094{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002095 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002096}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002097
NeilBrown28ce6052005-06-23 22:03:56 -07002098static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002099find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002100{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002101 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002102 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002103}
2104
2105static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002106find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002107{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002108 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002109 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002110}
2111
NeilBrownfd39ca92005-06-23 22:04:03 -07002112static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002113gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002115 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002116 struct sockaddr *sa = svc_addr(rqstp);
2117 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002118 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Jeff Layton7077ecb2009-08-14 12:57:58 -04002120 /* Currently, we only support tcp and tcp6 for the callback channel */
2121 if (se->se_callback_netid_len == 3 &&
2122 !memcmp(se->se_callback_netid_val, "tcp", 3))
2123 expected_family = AF_INET;
2124 else if (se->se_callback_netid_len == 4 &&
2125 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2126 expected_family = AF_INET6;
2127 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 goto out_err;
2129
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002130 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002131 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002132 (struct sockaddr *)&conn->cb_addr,
2133 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002134
J. Bruce Fields07263f12010-05-31 19:09:40 -04002135 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002137
J. Bruce Fields07263f12010-05-31 19:09:40 -04002138 if (conn->cb_addr.ss_family == AF_INET6)
2139 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002140
J. Bruce Fields07263f12010-05-31 19:09:40 -04002141 conn->cb_prog = se->se_callback_prog;
2142 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002143 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 return;
2145out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002146 conn->cb_addr.ss_family = AF_UNSPEC;
2147 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002148 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 "will not receive delegations\n",
2150 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 return;
2153}
2154
Andy Adamson074fe892009-04-03 08:28:15 +03002155/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002156 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002157 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002158static void
Andy Adamson074fe892009-04-03 08:28:15 +03002159nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2160{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002161 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002162 struct nfsd4_slot *slot = resp->cstate.slot;
2163 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002164
Andy Adamson557ce262009-08-28 08:45:04 -04002165 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002166
Andy Adamson557ce262009-08-28 08:45:04 -04002167 slot->sl_opcnt = resp->opcnt;
2168 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002169
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002170 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002171 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002172 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002173 return;
2174 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002175 base = resp->cstate.data_offset;
2176 slot->sl_datalen = buf->len - base;
2177 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04002178 WARN("%s: sessions DRC could not cache compound\n", __func__);
2179 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002180}
2181
2182/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002183 * Encode the replay sequence operation from the slot values.
2184 * If cachethis is FALSE encode the uncached rep error on the next
2185 * operation which sets resp->p and increments resp->opcnt for
2186 * nfs4svc_encode_compoundres.
2187 *
Andy Adamson074fe892009-04-03 08:28:15 +03002188 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002189static __be32
2190nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2191 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002192{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002193 struct nfsd4_op *op;
2194 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002195
Andy Adamsonabfabf82009-07-23 19:02:18 -04002196 /* Encode the replayed sequence operation */
2197 op = &args->ops[resp->opcnt - 1];
2198 nfsd4_encode_operation(resp, op);
2199
2200 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002201 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002202 op = &args->ops[resp->opcnt++];
2203 op->status = nfserr_retry_uncached_rep;
2204 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002205 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002206 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002207}
2208
2209/*
Andy Adamson557ce262009-08-28 08:45:04 -04002210 * The sequence operation is not cached because we can use the slot and
2211 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002212 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002213static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002214nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2215 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002216{
Andy Adamson557ce262009-08-28 08:45:04 -04002217 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002218 struct xdr_stream *xdr = &resp->xdr;
2219 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002220 __be32 status;
2221
Andy Adamson557ce262009-08-28 08:45:04 -04002222 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002223
Andy Adamsonabfabf82009-07-23 19:02:18 -04002224 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002225 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002226 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002227
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002228 p = xdr_reserve_space(xdr, slot->sl_datalen);
2229 if (!p) {
2230 WARN_ON_ONCE(1);
2231 return nfserr_serverfault;
2232 }
2233 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2234 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002235
Andy Adamson557ce262009-08-28 08:45:04 -04002236 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002237 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002238}
2239
Andy Adamson0733d212009-04-03 08:28:01 +03002240/*
2241 * Set the exchange_id flags returned by the server.
2242 */
2243static void
2244nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2245{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002246#ifdef CONFIG_NFSD_PNFS
2247 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2248#else
Andy Adamson0733d212009-04-03 08:28:01 +03002249 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002250#endif
Andy Adamson0733d212009-04-03 08:28:01 +03002251
2252 /* Referrals are supported, Migration is not. */
2253 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2254
2255 /* set the wire flags to return to client. */
2256 clid->flags = new->cl_exchange_flags;
2257}
2258
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002259static bool client_has_state(struct nfs4_client *clp)
2260{
2261 /*
2262 * Note clp->cl_openowners check isn't quite right: there's no
2263 * need to count owners without stateid's.
2264 *
2265 * Also note we should probably be using this in 4.0 case too.
2266 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002267 return !list_empty(&clp->cl_openowners)
Kinglong Mee47e970b2015-07-21 13:09:17 +08002268#ifdef CONFIG_NFSD_PNFS
2269 || !list_empty(&clp->cl_lo_states)
2270#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002271 || !list_empty(&clp->cl_delegations)
2272 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002273}
2274
Al Virob37ad282006-10-19 23:28:59 -07002275__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002276nfsd4_exchange_id(struct svc_rqst *rqstp,
2277 struct nfsd4_compound_state *cstate,
2278 struct nfsd4_exchange_id *exid)
2279{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002280 struct nfs4_client *conf, *new;
2281 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002282 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002283 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002284 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002285 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002286 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002287 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002288
Jeff Layton363168b2009-08-14 12:57:56 -04002289 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002290 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002291 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002292 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002293 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002294
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002295 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002296 return nfserr_inval;
2297
Andy Adamson0733d212009-04-03 08:28:01 +03002298 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002299 case SP4_MACH_CRED:
2300 if (!svc_rqst_integrity_protected(rqstp))
2301 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002302 case SP4_NONE:
2303 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002304 default: /* checked by xdr code */
2305 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002306 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002307 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002308 }
2309
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002310 new = create_client(exid->clname, rqstp, &verf);
2311 if (new == NULL)
2312 return nfserr_jukebox;
2313
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002314 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002315 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002316 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002317 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002318 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2319 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2320
J. Bruce Fields136e6582012-05-12 20:37:23 -04002321 if (update) {
2322 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002323 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002324 goto out;
2325 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002326 if (!mach_creds_match(conf, rqstp)) {
2327 status = nfserr_wrong_cred;
2328 goto out;
2329 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002330 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002331 status = nfserr_perm;
2332 goto out;
2333 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002334 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002335 status = nfserr_not_same;
2336 goto out;
2337 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002338 /* case 6 */
2339 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002340 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002341 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002342 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002343 if (client_has_state(conf)) {
2344 status = nfserr_clid_inuse;
2345 goto out;
2346 }
Andy Adamson0733d212009-04-03 08:28:01 +03002347 goto out_new;
2348 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002349 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002350 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002351 goto out_copy;
2352 }
2353 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002354 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002355 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002356 }
2357
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002358 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002359 status = nfserr_noent;
2360 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002361 }
2362
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002363 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002364 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002365 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002366
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002367 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002368out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002369 if (conf) {
2370 status = mark_client_expired_locked(conf);
2371 if (status)
2372 goto out;
2373 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002374 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002375 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002376
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002377 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002378 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002379 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002380out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002381 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2382 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002383
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002384 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2385 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002386
2387 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002388 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002389 status = nfs_ok;
2390
2391out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002392 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002393 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002394 expire_client(new);
2395 if (unconf)
2396 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002397 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002398}
2399
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002400static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002401check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002402{
Andy Adamson88e588d2009-07-23 19:02:15 -04002403 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2404 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002405
2406 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002407 if (slot_inuse) {
2408 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002409 return nfserr_jukebox;
2410 else
2411 return nfserr_seq_misordered;
2412 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002413 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002414 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002415 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002416 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002417 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002418 return nfserr_seq_misordered;
2419}
2420
Andy Adamson49557cc2009-07-23 19:02:16 -04002421/*
2422 * Cache the create session result into the create session single DRC
2423 * slot cache by saving the xdr structure. sl_seqid has been set.
2424 * Do this for solo or embedded create session operations.
2425 */
2426static void
2427nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002428 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002429{
2430 slot->sl_status = nfserr;
2431 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2432}
2433
2434static __be32
2435nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2436 struct nfsd4_clid_slot *slot)
2437{
2438 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2439 return slot->sl_status;
2440}
2441
Mi Jinlong1b74c252011-07-14 14:50:17 +08002442#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2443 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2444 1 + /* MIN tag is length with zero, only length */ \
2445 3 + /* version, opcount, opcode */ \
2446 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2447 /* seqid, slotID, slotID, cache */ \
2448 4 ) * sizeof(__be32))
2449
2450#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2451 2 + /* verifier: AUTH_NULL, length 0 */\
2452 1 + /* status */ \
2453 1 + /* MIN tag is length with zero, only length */ \
2454 3 + /* opcount, opcode, opstatus*/ \
2455 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2456 /* seqid, slotID, slotID, slotID, status */ \
2457 5 ) * sizeof(__be32))
2458
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002459static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002460{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002461 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2462
J. Bruce Fields373cd402013-04-08 15:42:12 -04002463 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2464 return nfserr_toosmall;
2465 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2466 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002467 ca->headerpadsz = 0;
2468 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2469 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2470 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2471 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2472 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2473 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2474 /*
2475 * Note decreasing slot size below client's request may make it
2476 * difficult for client to function correctly, whereas
2477 * decreasing the number of slots will (just?) affect
2478 * performance. When short on memory we therefore prefer to
2479 * decrease number of slots instead of their size. Clients that
2480 * request larger slots than they need will get poor results:
2481 */
2482 ca->maxreqs = nfsd4_get_drc_mem(ca);
2483 if (!ca->maxreqs)
2484 return nfserr_jukebox;
2485
J. Bruce Fields373cd402013-04-08 15:42:12 -04002486 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002487}
2488
Kinglong Mee8a891632013-12-23 18:11:02 +08002489#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2490 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2491#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2492 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2493
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002494static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2495{
2496 ca->headerpadsz = 0;
2497
2498 /*
2499 * These RPC_MAX_HEADER macros are overkill, especially since we
2500 * don't even do gss on the backchannel yet. But this is still
2501 * less than 1k. Tighten up this estimate in the unlikely event
2502 * it turns out to be a problem for some client:
2503 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002504 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002505 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002506 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002507 return nfserr_toosmall;
2508 ca->maxresp_cached = 0;
2509 if (ca->maxops < 2)
2510 return nfserr_toosmall;
2511
2512 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002513}
2514
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002515static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2516{
2517 switch (cbs->flavor) {
2518 case RPC_AUTH_NULL:
2519 case RPC_AUTH_UNIX:
2520 return nfs_ok;
2521 default:
2522 /*
2523 * GSS case: the spec doesn't allow us to return this
2524 * error. But it also doesn't allow us not to support
2525 * GSS.
2526 * I'd rather this fail hard than return some error the
2527 * client might think it can already handle:
2528 */
2529 return nfserr_encr_alg_unsupp;
2530 }
2531}
2532
Andy Adamson069b6ad2009-04-03 08:27:58 +03002533__be32
2534nfsd4_create_session(struct svc_rqst *rqstp,
2535 struct nfsd4_compound_state *cstate,
2536 struct nfsd4_create_session *cr_ses)
2537{
Jeff Layton363168b2009-08-14 12:57:56 -04002538 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002539 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002540 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04002541 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002542 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002543 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002544 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002545 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002546
Mi Jinlonga62573d2011-03-23 17:57:07 +08002547 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2548 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002549 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2550 if (status)
2551 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002552 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002553 if (status)
2554 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002555 status = check_backchannel_attrs(&cr_ses->back_channel);
2556 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002557 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002558 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002559 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002560 if (!new)
2561 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002562 conn = alloc_conn_from_crses(rqstp, cr_ses);
2563 if (!conn)
2564 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002565
Jeff Laytond20c11d2014-07-30 08:27:07 -04002566 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002567 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002568 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002569 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002570
2571 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002572 status = nfserr_wrong_cred;
2573 if (!mach_creds_match(conf, rqstp))
2574 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002575 cs_slot = &conf->cl_cs_slot;
2576 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08002577 if (status) {
2578 if (status == nfserr_replay_cache)
2579 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002580 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002581 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002582 } else if (unconf) {
2583 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002584 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002585 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002586 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002587 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002588 status = nfserr_wrong_cred;
2589 if (!mach_creds_match(unconf, rqstp))
2590 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002591 cs_slot = &unconf->cl_cs_slot;
2592 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002593 if (status) {
2594 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002595 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002596 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002597 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002598 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002599 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002600 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002601 if (status) {
2602 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002603 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002604 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002605 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002606 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002607 conf = unconf;
2608 } else {
2609 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002610 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002611 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002612 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002613 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002614 * We do not support RDMA or persistent sessions
2615 */
2616 cr_ses->flags &= ~SESSION4_PERSIST;
2617 cr_ses->flags &= ~SESSION4_RDMA;
2618
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002619 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002620 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002621
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04002622 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002623 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002624 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002625 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002626
Jeff Laytond20c11d2014-07-30 08:27:07 -04002627 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002628 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002629 spin_unlock(&nn->client_lock);
2630 /* init connection and backchannel */
2631 nfsd4_init_conn(rqstp, conn, new);
2632 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002633 if (old)
2634 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002635 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002636out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002637 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002638 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002639 if (old)
2640 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002641out_free_session:
2642 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002643out_release_drc_mem:
2644 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002645 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002646}
2647
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002648static __be32 nfsd4_map_bcts_dir(u32 *dir)
2649{
2650 switch (*dir) {
2651 case NFS4_CDFC4_FORE:
2652 case NFS4_CDFC4_BACK:
2653 return nfs_ok;
2654 case NFS4_CDFC4_FORE_OR_BOTH:
2655 case NFS4_CDFC4_BACK_OR_BOTH:
2656 *dir = NFS4_CDFC4_BOTH;
2657 return nfs_ok;
2658 };
2659 return nfserr_inval;
2660}
2661
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002662__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2663{
2664 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002665 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002666 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002667
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002668 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2669 if (status)
2670 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002671 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002672 session->se_cb_prog = bc->bc_cb_program;
2673 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002674 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002675
2676 nfsd4_probe_callback(session->se_client);
2677
2678 return nfs_ok;
2679}
2680
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002681__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2682 struct nfsd4_compound_state *cstate,
2683 struct nfsd4_bind_conn_to_session *bcts)
2684{
2685 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002686 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002687 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002688 struct net *net = SVC_NET(rqstp);
2689 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002690
2691 if (!nfsd4_last_compound_op(rqstp))
2692 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002693 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002694 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002695 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002696 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002697 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002698 status = nfserr_wrong_cred;
2699 if (!mach_creds_match(session->se_client, rqstp))
2700 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002701 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002702 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002703 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002704 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002705 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002706 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002707 goto out;
2708 nfsd4_init_conn(rqstp, conn, session);
2709 status = nfs_ok;
2710out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002711 nfsd4_put_session(session);
2712out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002713 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002714}
2715
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002716static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2717{
2718 if (!session)
2719 return 0;
2720 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2721}
2722
Andy Adamson069b6ad2009-04-03 08:27:58 +03002723__be32
2724nfsd4_destroy_session(struct svc_rqst *r,
2725 struct nfsd4_compound_state *cstate,
2726 struct nfsd4_destroy_session *sessionid)
2727{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002728 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002729 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002730 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002731 struct net *net = SVC_NET(r);
2732 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002733
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002734 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002735 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002736 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002737 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002738 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002739 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002740 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002741 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002742 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002743 if (!ses)
2744 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002745 status = nfserr_wrong_cred;
2746 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002747 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002748 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002749 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002750 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002751 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002752 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002753
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002754 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002755
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002756 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002757 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002758out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002759 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002760out_client_lock:
2761 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002762out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03002763 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002764}
2765
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002766static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002767{
2768 struct nfsd4_conn *c;
2769
2770 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002771 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002772 return c;
2773 }
2774 }
2775 return NULL;
2776}
2777
J. Bruce Fields57266a62013-04-13 14:27:29 -04002778static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002779{
2780 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002781 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002782 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002783 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002784
2785 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002786 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002787 if (c)
2788 goto out_free;
2789 status = nfserr_conn_not_bound_to_session;
2790 if (clp->cl_mach_cred)
2791 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002792 __nfsd4_hash_conn(new, ses);
2793 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002794 ret = nfsd4_register_conn(new);
2795 if (ret)
2796 /* oops; xprt is already down: */
2797 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002798 return nfs_ok;
2799out_free:
2800 spin_unlock(&clp->cl_lock);
2801 free_conn(new);
2802 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002803}
2804
Mi Jinlong868b89c2011-04-27 09:09:58 +08002805static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2806{
2807 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2808
2809 return args->opcnt > session->se_fchannel.maxops;
2810}
2811
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002812static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2813 struct nfsd4_session *session)
2814{
2815 struct xdr_buf *xb = &rqstp->rq_arg;
2816
2817 return xb->len > session->se_fchannel.maxreq_sz;
2818}
2819
Andy Adamson069b6ad2009-04-03 08:27:58 +03002820__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002821nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002822 struct nfsd4_compound_state *cstate,
2823 struct nfsd4_sequence *seq)
2824{
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002825 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002826 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002827 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002828 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002829 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002830 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002831 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002832 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002833 struct net *net = SVC_NET(rqstp);
2834 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002835
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002836 if (resp->opcnt != 1)
2837 return nfserr_sequence_pos;
2838
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002839 /*
2840 * Will be either used or freed by nfsd4_sequence_check_conn
2841 * below.
2842 */
2843 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2844 if (!conn)
2845 return nfserr_jukebox;
2846
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002847 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002848 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002849 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002850 goto out_no_session;
2851 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002852
Mi Jinlong868b89c2011-04-27 09:09:58 +08002853 status = nfserr_too_many_ops;
2854 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002855 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002856
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002857 status = nfserr_req_too_big;
2858 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002859 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002860
Benny Halevyb85d4c02009-04-03 08:28:08 +03002861 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002862 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002863 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002864
Andy Adamson557ce262009-08-28 08:45:04 -04002865 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002866 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2867
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002868 /* We do not negotiate the number of slots yet, so set the
2869 * maxslots to the session maxreqs which is used to encode
2870 * sr_highest_slotid and the sr_target_slot id to maxslots */
2871 seq->maxslots = session->se_fchannel.maxreqs;
2872
J. Bruce Fields73e79482012-02-13 16:39:00 -05002873 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2874 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002875 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002876 status = nfserr_seq_misordered;
2877 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002878 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002879 cstate->slot = slot;
2880 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002881 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002882 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002883 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002884 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002885 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002886 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002887 }
2888 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002889 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002890
J. Bruce Fields57266a62013-04-13 14:27:29 -04002891 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002892 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002893 if (status)
2894 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002895
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002896 buflen = (seq->cachethis) ?
2897 session->se_fchannel.maxresp_cached :
2898 session->se_fchannel.maxresp_sz;
2899 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2900 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002901 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002902 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002903 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002904
2905 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002906 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002907 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002908 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002909 if (seq->cachethis)
2910 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002911 else
2912 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002913
2914 cstate->slot = slot;
2915 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002916 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002917
Benny Halevyb85d4c02009-04-03 08:28:08 +03002918out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002919 switch (clp->cl_cb_state) {
2920 case NFSD4_CB_DOWN:
2921 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2922 break;
2923 case NFSD4_CB_FAULT:
2924 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2925 break;
2926 default:
2927 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002928 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002929 if (!list_empty(&clp->cl_revoked))
2930 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002931out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002932 if (conn)
2933 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002934 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002935 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002936out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002937 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002938 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002939}
2940
Trond Myklebustb6076642014-06-30 11:48:35 -04002941void
2942nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2943{
2944 struct nfsd4_compound_state *cs = &resp->cstate;
2945
2946 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002947 if (cs->status != nfserr_replay_cache) {
2948 nfsd4_store_cache_entry(resp);
2949 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2950 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002951 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002952 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002953 } else if (cs->clp)
2954 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002955}
2956
Mi Jinlong345c2842011-10-20 17:51:39 +08002957__be32
2958nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2959{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002960 struct nfs4_client *conf, *unconf;
2961 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002962 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002963 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002964
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002965 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002966 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002967 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002968 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002969
2970 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002971 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002972 status = nfserr_clientid_busy;
2973 goto out;
2974 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04002975 status = mark_client_expired_locked(conf);
2976 if (status)
2977 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002978 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08002979 } else if (unconf)
2980 clp = unconf;
2981 else {
2982 status = nfserr_stale_clientid;
2983 goto out;
2984 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002985 if (!mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002986 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002987 status = nfserr_wrong_cred;
2988 goto out;
2989 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002990 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002991out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002992 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002993 if (clp)
2994 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002995 return status;
2996}
2997
Andy Adamson069b6ad2009-04-03 08:27:58 +03002998__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002999nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
3000{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003001 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003002
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003003 if (rc->rca_one_fs) {
3004 if (!cstate->current_fh.fh_dentry)
3005 return nfserr_nofilehandle;
3006 /*
3007 * We don't take advantage of the rca_one_fs case.
3008 * That's OK, it's optional, we can safely ignore it.
3009 */
3010 return nfs_ok;
3011 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003012
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003013 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04003014 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3015 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003016 goto out;
3017
3018 status = nfserr_stale_clientid;
3019 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003020 /*
3021 * The following error isn't really legal.
3022 * But we only get here if the client just explicitly
3023 * destroyed the client. Surely it no longer cares what
3024 * error it gets back on an operation for the dead
3025 * client.
3026 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003027 goto out;
3028
3029 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04003030 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003031out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003032 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003033}
3034
3035__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003036nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3037 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003039 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003041 struct nfs4_client *conf, *new;
3042 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003043 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003044 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3045
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003046 new = create_client(clname, rqstp, &clverifier);
3047 if (new == NULL)
3048 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003049 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003050 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003051 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07003052 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003053 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003055 if (clp_used_exchangeid(conf))
3056 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003057 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04003058 char addr_str[INET6_ADDRSTRLEN];
3059 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3060 sizeof(addr_str));
3061 dprintk("NFSD: setclientid: string in use by client "
3062 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 goto out;
3064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003066 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003067 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003068 unhash_client_locked(unconf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003069 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003070 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003072 gen_confirm(new, nn);
3073 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003074 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003075 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003076 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003077 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3079 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3080 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003081 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 status = nfs_ok;
3083out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003084 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003085 if (new)
3086 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003087 if (unconf)
3088 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 return status;
3090}
3091
3092
Al Virob37ad282006-10-19 23:28:59 -07003093__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003094nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3095 struct nfsd4_compound_state *cstate,
3096 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
NeilBrown21ab45a2005-06-23 22:04:14 -07003098 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003099 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3101 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003102 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003103 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003105 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003107
Jeff Laytond20c11d2014-07-30 08:27:07 -04003108 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003109 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003110 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003111 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003112 * We try hard to give out unique clientid's, so if we get an
3113 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003114 * the client may be buggy; this should never happen.
3115 *
3116 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003117 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003118 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04003119 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3120 goto out;
3121 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3122 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003123 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003124 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3125 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003127 else /* case 4: client hasn't noticed we rebooted yet? */
3128 status = nfserr_stale_clientid;
3129 goto out;
3130 }
3131 status = nfs_ok;
3132 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003133 old = unconf;
3134 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003135 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003136 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003137 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3138 if (old) {
3139 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003140 if (status) {
3141 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003142 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003143 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003144 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003145 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003146 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003147 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003148 get_client_locked(conf);
3149 spin_unlock(&nn->client_lock);
3150 nfsd4_probe_callback(conf);
3151 spin_lock(&nn->client_lock);
3152 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003154 spin_unlock(&nn->client_lock);
3155 if (old)
3156 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 return status;
3158}
3159
J. Bruce Fields32513b42011-10-13 16:00:16 -04003160static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003162 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3163}
3164
3165/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003166static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3167 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003168{
Trond Myklebust950e0112014-06-30 11:48:31 -04003169 lockdep_assert_held(&state_lock);
3170
J. Bruce Fields32513b42011-10-13 16:00:16 -04003171 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003172 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003173 INIT_LIST_HEAD(&fp->fi_stateids);
3174 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02003175 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003176 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003177 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003178 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003179 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003180 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3181 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003182#ifdef CONFIG_NFSD_PNFS
3183 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02003184 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003185#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04003186 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187}
3188
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003189void
NeilBrowne60d4392005-06-23 22:03:01 -07003190nfsd4_free_slabs(void)
3191{
Sachin Bhamare8287f002015-04-27 14:50:14 +02003192 kmem_cache_destroy(odstate_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003193 kmem_cache_destroy(openowner_slab);
3194 kmem_cache_destroy(lockowner_slab);
3195 kmem_cache_destroy(file_slab);
3196 kmem_cache_destroy(stateid_slab);
3197 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003198}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
Bryan Schumaker72083392011-11-01 15:24:59 -04003200int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201nfsd4_init_slabs(void)
3202{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003203 openowner_slab = kmem_cache_create("nfsd4_openowners",
3204 sizeof(struct nfs4_openowner), 0, 0, NULL);
3205 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003206 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003207 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003208 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003209 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003210 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003211 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003212 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003213 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003214 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003215 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003216 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003217 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003218 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003219 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003220 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003221 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003222 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003223 odstate_slab = kmem_cache_create("nfsd4_odstate",
3224 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3225 if (odstate_slab == NULL)
3226 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003228
Sachin Bhamare8287f002015-04-27 14:50:14 +02003229out_free_deleg_slab:
3230 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003231out_free_stateid_slab:
3232 kmem_cache_destroy(stateid_slab);
3233out_free_file_slab:
3234 kmem_cache_destroy(file_slab);
3235out_free_lockowner_slab:
3236 kmem_cache_destroy(lockowner_slab);
3237out_free_openowner_slab:
3238 kmem_cache_destroy(openowner_slab);
3239out:
NeilBrowne60d4392005-06-23 22:03:01 -07003240 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3241 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242}
3243
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003244static void init_nfs4_replay(struct nfs4_replay *rp)
3245{
3246 rp->rp_status = nfserr_serverfault;
3247 rp->rp_buflen = 0;
3248 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003249 mutex_init(&rp->rp_mutex);
3250}
3251
3252static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3253 struct nfs4_stateowner *so)
3254{
3255 if (!nfsd4_has_session(cstate)) {
3256 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003257 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04003258 }
3259}
3260
3261void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3262{
3263 struct nfs4_stateowner *so = cstate->replay_owner;
3264
3265 if (so != NULL) {
3266 cstate->replay_owner = NULL;
3267 mutex_unlock(&so->so_replay.rp_mutex);
3268 nfs4_put_stateowner(so);
3269 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003270}
3271
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003272static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273{
3274 struct nfs4_stateowner *sop;
3275
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003276 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003277 if (!sop)
3278 return NULL;
3279
3280 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3281 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003282 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003283 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003285 sop->so_owner.len = owner->len;
3286
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003287 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003288 sop->so_client = clp;
3289 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003290 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003291 return sop;
3292}
3293
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003294static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003295{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003296 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003297
Trond Myklebustd4f04892014-07-29 21:34:36 -04003298 list_add(&oo->oo_owner.so_strhash,
3299 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003300 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301}
3302
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003303static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3304{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003305 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003306}
3307
Jeff Layton6b180f02014-07-29 21:34:26 -04003308static void nfs4_free_openowner(struct nfs4_stateowner *so)
3309{
3310 struct nfs4_openowner *oo = openowner(so);
3311
3312 kmem_cache_free(openowner_slab, oo);
3313}
3314
3315static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003316 .so_unhash = nfs4_unhash_openowner,
3317 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003318};
3319
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003320static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003321alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003322 struct nfsd4_compound_state *cstate)
3323{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003324 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003325 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003327 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3328 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003330 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003331 oo->oo_owner.so_is_open_owner = 1;
3332 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003333 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003334 if (nfsd4_has_session(cstate))
3335 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003336 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003337 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003338 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003339 spin_lock(&clp->cl_lock);
3340 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003341 if (ret == NULL) {
3342 hash_openowner(oo, clp, strhashval);
3343 ret = oo;
3344 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08003345 nfs4_free_stateowner(&oo->oo_owner);
3346
Trond Myklebustd4f04892014-07-29 21:34:36 -04003347 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04003348 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349}
3350
J. Bruce Fields996e0932011-10-17 11:14:48 -04003351static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003352 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003354 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003355 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003356 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003357 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07003358 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003359 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 stp->st_access_bmap = 0;
3361 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003362 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003363 spin_lock(&oo->oo_owner.so_client->cl_lock);
3364 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003365 spin_lock(&fp->fi_lock);
3366 list_add(&stp->st_perfile, &fp->fi_stateids);
3367 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003368 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369}
3370
Jeff Laytond3134b12014-07-29 21:34:32 -04003371/*
3372 * In the 4.0 case we need to keep the owners around a little while to handle
3373 * CLOSE replay. We still do need to release any file access that is held by
3374 * them before returning however.
3375 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003377move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378{
Jeff Layton217526e2014-07-30 08:27:11 -04003379 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003380 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3381 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3382 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003383
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003384 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
Jeff Laytonb401be222014-07-29 21:34:33 -04003386 /*
3387 * We know that we hold one reference via nfsd4_close, and another
3388 * "persistent" reference for the client. If the refcount is higher
3389 * than 2, then there are still calls in progress that are using this
3390 * stateid. We can't put the sc_file reference until they are finished.
3391 * Wait for the refcount to drop to 2. Since it has been unhashed,
3392 * there should be no danger of the refcount going back up again at
3393 * this point.
3394 */
3395 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3396
Jeff Laytond3134b12014-07-29 21:34:32 -04003397 release_all_access(s);
3398 if (s->st_stid.sc_file) {
3399 put_nfs4_file(s->st_stid.sc_file);
3400 s->st_stid.sc_file = NULL;
3401 }
Jeff Layton217526e2014-07-30 08:27:11 -04003402
3403 spin_lock(&nn->client_lock);
3404 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003405 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003406 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003407 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003408 spin_unlock(&nn->client_lock);
3409 if (last)
3410 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411}
3412
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413/* search file_hashtbl[] for file */
3414static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04003415find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 struct nfs4_file *fp;
3418
Jeff Layton5b095e92014-10-23 08:01:02 -04003419 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02003420 if (fh_match(&fp->fi_fhandle, fh)) {
Jeff Layton5b095e92014-10-23 08:01:02 -04003421 if (atomic_inc_not_zero(&fp->fi_ref))
3422 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 }
3425 return NULL;
3426}
3427
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02003428struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003429find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003430{
3431 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003432 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003433
Jeff Layton5b095e92014-10-23 08:01:02 -04003434 rcu_read_lock();
3435 fp = find_file_locked(fh, hashval);
3436 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04003437 return fp;
3438}
3439
3440static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003441find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003442{
3443 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003444 unsigned int hashval = file_hashval(fh);
3445
3446 rcu_read_lock();
3447 fp = find_file_locked(fh, hashval);
3448 rcu_read_unlock();
3449 if (fp)
3450 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04003451
3452 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04003453 fp = find_file_locked(fh, hashval);
3454 if (likely(fp == NULL)) {
3455 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04003456 fp = new;
3457 }
3458 spin_unlock(&state_lock);
3459
3460 return fp;
3461}
3462
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003463/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 * Called to check deny when READ with all zero stateid or
3465 * WRITE with all zero or all one stateid
3466 */
Al Virob37ad282006-10-19 23:28:59 -07003467static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3469{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003471 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
Trond Myklebustca943212014-07-23 16:17:39 -04003473 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003474 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003475 return ret;
3476 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003477 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003478 if (fp->fi_share_deny & deny_type)
3479 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003480 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003481 put_nfs4_file(fp);
3482 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483}
3484
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003485static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003487 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04003488 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3489 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003490
Trond Myklebust11b91642014-07-29 21:34:08 -04003491 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003492
Jeff Layton02e12152014-07-16 10:31:57 -04003493 /*
3494 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003495 * already holding inode->i_lock.
3496 *
Jeff Laytondff13992014-07-08 14:02:49 -04003497 * If the dl_time != 0, then we know that it has already been
3498 * queued for a lease break. Don't queue it again.
3499 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003500 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003501 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003502 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003503 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003504 }
Jeff Layton02e12152014-07-16 10:31:57 -04003505 spin_unlock(&state_lock);
3506}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003508static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3509 struct rpc_task *task)
3510{
3511 struct nfs4_delegation *dp = cb_to_delegation(cb);
3512
Andrew Elblea4579742015-08-31 12:06:41 -04003513 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
3514 return 1;
3515
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003516 switch (task->tk_status) {
3517 case 0:
3518 return 1;
3519 case -EBADHANDLE:
3520 case -NFS4ERR_BAD_STATEID:
3521 /*
3522 * Race: client probably got cb_recall before open reply
3523 * granting delegation.
3524 */
3525 if (dp->dl_retries--) {
3526 rpc_delay(task, 2 * HZ);
3527 return 0;
3528 }
3529 /*FALLTHRU*/
3530 default:
3531 return -1;
3532 }
3533}
3534
3535static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3536{
3537 struct nfs4_delegation *dp = cb_to_delegation(cb);
3538
3539 nfs4_put_stid(&dp->dl_stid);
3540}
3541
3542static struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3543 .prepare = nfsd4_cb_recall_prepare,
3544 .done = nfsd4_cb_recall_done,
3545 .release = nfsd4_cb_recall_release,
3546};
3547
Jeff Layton02e12152014-07-16 10:31:57 -04003548static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3549{
3550 /*
3551 * We're assuming the state code never drops its reference
3552 * without first removing the lease. Since we're in this lease
3553 * callback (and since the lease code is serialized by the kernel
3554 * lock) we know the server hasn't removed the lease yet, we know
3555 * it's safe to take a reference.
3556 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003557 atomic_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02003558 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003559}
3560
Jeff Layton1c8c6012013-06-21 08:58:15 -04003561/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003562static bool
3563nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003564{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003565 bool ret = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003566 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3567 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003568
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003569 if (!fp) {
3570 WARN(1, "(%p)->fl_owner NULL\n", fl);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003571 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003572 }
3573 if (fp->fi_had_conflict) {
3574 WARN(1, "duplicate break on %p\n", fp);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003575 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003576 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003577 /*
3578 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003579 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003580 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003581 */
3582 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
Jeff Layton02e12152014-07-16 10:31:57 -04003584 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003585 fp->fi_had_conflict = true;
3586 /*
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003587 * If there are no delegations on the list, then return true
3588 * so that the lease code will go ahead and delete it.
Jeff Layton417c6622014-07-21 09:34:57 -04003589 */
3590 if (list_empty(&fp->fi_delegations))
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003591 ret = true;
Jeff Layton417c6622014-07-21 09:34:57 -04003592 else
3593 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3594 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003595 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003596 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597}
3598
Jeff Laytonc45198e2014-09-01 07:12:07 -04003599static int
Jeff Layton7448cc32015-01-16 15:05:57 -05003600nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3601 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602{
3603 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04003604 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 else
3606 return -EAGAIN;
3607}
3608
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003609static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003610 .lm_break = nfsd_break_deleg_cb,
3611 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612};
3613
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003614static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3615{
3616 if (nfsd4_has_session(cstate))
3617 return nfs_ok;
3618 if (seqid == so->so_seqid - 1)
3619 return nfserr_replay_me;
3620 if (seqid == so->so_seqid)
3621 return nfs_ok;
3622 return nfserr_bad_seqid;
3623}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624
Jeff Layton4b24ca72014-06-30 11:48:44 -04003625static __be32 lookup_clientid(clientid_t *clid,
3626 struct nfsd4_compound_state *cstate,
3627 struct nfsd_net *nn)
3628{
3629 struct nfs4_client *found;
3630
3631 if (cstate->clp) {
3632 found = cstate->clp;
3633 if (!same_clid(&found->cl_clientid, clid))
3634 return nfserr_stale_clientid;
3635 return nfs_ok;
3636 }
3637
3638 if (STALE_CLIENTID(clid, nn))
3639 return nfserr_stale_clientid;
3640
3641 /*
3642 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3643 * cached already then we know this is for is for v4.0 and "sessions"
3644 * will be false.
3645 */
3646 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003647 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003648 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003649 if (!found) {
3650 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003651 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003652 }
3653 atomic_inc(&found->cl_refcount);
3654 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003655
3656 /* Cache the nfs4_client in cstate! */
3657 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003658 return nfs_ok;
3659}
3660
Al Virob37ad282006-10-19 23:28:59 -07003661__be32
Andy Adamson66689582009-04-03 08:28:45 +03003662nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003663 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 clientid_t *clientid = &open->op_clientid;
3666 struct nfs4_client *clp = NULL;
3667 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003668 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003669 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003671 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003673 /*
3674 * In case we need it later, after we've already created the
3675 * file and don't want to risk a further failure:
3676 */
3677 open->op_file = nfsd4_alloc_file();
3678 if (open->op_file == NULL)
3679 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
Trond Myklebust2d91e892014-06-30 11:48:46 -04003681 status = lookup_clientid(clientid, cstate, nn);
3682 if (status)
3683 return status;
3684 clp = cstate->clp;
3685
Trond Myklebustd4f04892014-07-29 21:34:36 -04003686 strhashval = ownerstr_hashval(&open->op_owner);
3687 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003688 open->op_openowner = oo;
3689 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003690 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003692 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003693 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003694 release_openowner(oo);
3695 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003696 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003697 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003698 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3699 if (status)
3700 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003701 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003702new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003703 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003704 if (oo == NULL)
3705 return nfserr_jukebox;
3706 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003707alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003708 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003709 if (!open->op_stp)
3710 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003711
3712 if (nfsd4_has_session(cstate) &&
3713 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
3714 open->op_odstate = alloc_clnt_odstate(clp);
3715 if (!open->op_odstate)
3716 return nfserr_jukebox;
3717 }
3718
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003719 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720}
3721
Al Virob37ad282006-10-19 23:28:59 -07003722static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003723nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3724{
3725 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3726 return nfserr_openmode;
3727 else
3728 return nfs_ok;
3729}
3730
Daniel Mackc47d8322011-05-16 16:38:14 +02003731static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003732{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003733 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3734}
3735
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003736static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003737{
3738 struct nfs4_stid *ret;
3739
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003740 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003741 if (!ret)
3742 return NULL;
3743 return delegstateid(ret);
3744}
3745
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003746static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3747{
3748 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3749 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3750}
3751
Al Virob37ad282006-10-19 23:28:59 -07003752static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003753nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003754 struct nfs4_delegation **dp)
3755{
3756 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003757 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003758 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003759
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003760 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3761 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003762 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003763 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003764 status = nfs4_check_delegmode(deleg, flags);
3765 if (status) {
3766 nfs4_put_stid(&deleg->dl_stid);
3767 goto out;
3768 }
3769 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003770out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003771 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003772 return nfs_ok;
3773 if (status)
3774 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003775 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003776 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003777}
3778
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003779static struct nfs4_ol_stateid *
3780nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003782 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003783 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784
Trond Myklebust1d31a252014-07-10 14:07:25 -04003785 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003786 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 /* ignore lock owners */
3788 if (local->st_stateowner->so_is_open_owner == 0)
3789 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003790 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003791 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003792 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003793 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003796 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003797 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798}
3799
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003800static inline int nfs4_access_to_access(u32 nfs4_access)
3801{
3802 int flags = 0;
3803
3804 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3805 flags |= NFSD_MAY_READ;
3806 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3807 flags |= NFSD_MAY_WRITE;
3808 return flags;
3809}
3810
Al Virob37ad282006-10-19 23:28:59 -07003811static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3813 struct nfsd4_open *open)
3814{
3815 struct iattr iattr = {
3816 .ia_valid = ATTR_SIZE,
3817 .ia_size = 0,
3818 };
3819 if (!open->op_truncate)
3820 return 0;
3821 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003822 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3824}
3825
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003826static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003827 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3828 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003829{
Trond Myklebustde186432014-07-10 14:07:26 -04003830 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003831 __be32 status;
3832 int oflag = nfs4_access_to_omode(open->op_share_access);
3833 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003834 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003835
Trond Myklebustde186432014-07-10 14:07:26 -04003836 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003837
3838 /*
3839 * Are we trying to set a deny mode that would conflict with
3840 * current access?
3841 */
3842 status = nfs4_file_check_deny(fp, open->op_share_deny);
3843 if (status != nfs_ok) {
3844 spin_unlock(&fp->fi_lock);
3845 goto out;
3846 }
3847
3848 /* set access to the file */
3849 status = nfs4_file_get_access(fp, open->op_share_access);
3850 if (status != nfs_ok) {
3851 spin_unlock(&fp->fi_lock);
3852 goto out;
3853 }
3854
3855 /* Set access bits in stateid */
3856 old_access_bmap = stp->st_access_bmap;
3857 set_access(open->op_share_access, stp);
3858
3859 /* Set new deny mask */
3860 old_deny_bmap = stp->st_deny_bmap;
3861 set_deny(open->op_share_deny, stp);
3862 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3863
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003864 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003865 spin_unlock(&fp->fi_lock);
3866 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003867 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003868 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003869 spin_lock(&fp->fi_lock);
3870 if (!fp->fi_fds[oflag]) {
3871 fp->fi_fds[oflag] = filp;
3872 filp = NULL;
3873 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003874 }
Trond Myklebustde186432014-07-10 14:07:26 -04003875 spin_unlock(&fp->fi_lock);
3876 if (filp)
3877 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003878
3879 status = nfsd4_truncate(rqstp, cur_fh, open);
3880 if (status)
3881 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003882out:
3883 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003884out_put_access:
3885 stp->st_access_bmap = old_access_bmap;
3886 nfs4_file_put_access(fp, open->op_share_access);
3887 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3888 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003889}
3890
Al Virob37ad282006-10-19 23:28:59 -07003891static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003892nfs4_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 -07003893{
Al Virob37ad282006-10-19 23:28:59 -07003894 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02003895 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003897 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003898 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003899
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003900 /* test and set deny mode */
3901 spin_lock(&fp->fi_lock);
3902 status = nfs4_file_check_deny(fp, open->op_share_deny);
3903 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003904 set_deny(open->op_share_deny, stp);
3905 fp->fi_share_deny |=
3906 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3907 }
3908 spin_unlock(&fp->fi_lock);
3909
3910 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003913 status = nfsd4_truncate(rqstp, cur_fh, open);
3914 if (status != nfs_ok)
3915 reset_union_bmap_deny(old_deny_bmap, stp);
3916 return status;
3917}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003919/* Should we give out recallable state?: */
3920static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3921{
3922 if (clp->cl_cb_state == NFSD4_CB_UP)
3923 return true;
3924 /*
3925 * In the sessions case, since we don't have to establish a
3926 * separate connection for callbacks, we assume it's OK
3927 * until we hear otherwise:
3928 */
3929 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3930}
3931
Jeff Laytond564fbe2014-07-16 10:31:58 -04003932static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003933{
3934 struct file_lock *fl;
3935
3936 fl = locks_alloc_lock();
3937 if (!fl)
3938 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003939 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003940 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003941 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3942 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003943 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003944 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003945 return fl;
3946}
3947
J. Bruce Fields99c41512013-05-21 16:21:25 -04003948static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003949{
Trond Myklebust11b91642014-07-29 21:34:08 -04003950 struct nfs4_file *fp = dp->dl_stid.sc_file;
Kinglong Meeefde6b42015-07-13 17:33:24 +08003951 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003952 struct file *filp;
3953 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003954
Jeff Laytond564fbe2014-07-16 10:31:58 -04003955 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003956 if (!fl)
3957 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003958 filp = find_readable_file(fp);
3959 if (!filp) {
3960 /* We should always have a readable file here */
3961 WARN_ON_ONCE(1);
Kinglong Meeaf9dbaf2015-07-13 17:30:51 +08003962 locks_free_lock(fl);
Jeff Layton417c6622014-07-21 09:34:57 -04003963 return -EBADF;
3964 }
3965 fl->fl_file = filp;
Jeff Laytone6f5c782014-08-22 10:40:25 -04003966 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04003967 if (fl)
Jeff Layton417c6622014-07-21 09:34:57 -04003968 locks_free_lock(fl);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04003969 if (status)
Jeff Layton417c6622014-07-21 09:34:57 -04003970 goto out_fput;
Benny Halevycdc975052014-05-30 09:09:30 -04003971 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003972 spin_lock(&fp->fi_lock);
3973 /* Did the lease get broken before we took the lock? */
3974 status = -EAGAIN;
3975 if (fp->fi_had_conflict)
3976 goto out_unlock;
3977 /* Race breaker */
Jeff Layton0c637be2014-08-22 12:05:43 -04003978 if (fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04003979 status = 0;
Jeff Layton67db1032014-12-13 09:11:40 -05003980 ++fp->fi_delegees;
Jeff Layton417c6622014-07-21 09:34:57 -04003981 hash_delegation_locked(dp, fp);
3982 goto out_unlock;
3983 }
Jeff Layton417c6622014-07-21 09:34:57 -04003984 fp->fi_deleg_file = filp;
Jeff Layton67db1032014-12-13 09:11:40 -05003985 fp->fi_delegees = 1;
Benny Halevy931ee562014-05-30 09:09:27 -04003986 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003987 spin_unlock(&fp->fi_lock);
Benny Halevycdc975052014-05-30 09:09:30 -04003988 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003989 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003990out_unlock:
3991 spin_unlock(&fp->fi_lock);
3992 spin_unlock(&state_lock);
3993out_fput:
3994 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003995 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003996}
3997
Jeff Layton0b266932014-07-25 07:34:25 -04003998static struct nfs4_delegation *
3999nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02004000 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004001{
Jeff Layton0b266932014-07-25 07:34:25 -04004002 int status;
4003 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04004004
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004005 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004006 return ERR_PTR(-EAGAIN);
4007
Sachin Bhamare8287f002015-04-27 14:50:14 +02004008 dp = alloc_init_deleg(clp, fh, odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004009 if (!dp)
4010 return ERR_PTR(-ENOMEM);
4011
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004012 get_nfs4_file(fp);
Benny Halevycdc975052014-05-30 09:09:30 -04004013 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004014 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04004015 dp->dl_stid.sc_file = fp;
Jeff Layton0c637be2014-08-22 12:05:43 -04004016 if (!fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04004017 spin_unlock(&fp->fi_lock);
4018 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04004019 status = nfs4_setlease(dp);
4020 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04004021 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004022 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04004023 status = -EAGAIN;
4024 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004025 }
Jeff Layton67db1032014-12-13 09:11:40 -05004026 ++fp->fi_delegees;
Benny Halevy931ee562014-05-30 09:09:27 -04004027 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04004028 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04004029out_unlock:
4030 spin_unlock(&fp->fi_lock);
Benny Halevycdc975052014-05-30 09:09:30 -04004031 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04004032out:
4033 if (status) {
Sachin Bhamare8287f002015-04-27 14:50:14 +02004034 put_clnt_odstate(dp->dl_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04004035 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04004036 return ERR_PTR(status);
4037 }
4038 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004039}
4040
Benny Halevy4aa89132012-02-21 14:16:44 -08004041static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4042{
4043 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4044 if (status == -EAGAIN)
4045 open->op_why_no_deleg = WND4_CONTENTION;
4046 else {
4047 open->op_why_no_deleg = WND4_RESOURCE;
4048 switch (open->op_deleg_want) {
4049 case NFS4_SHARE_WANT_READ_DELEG:
4050 case NFS4_SHARE_WANT_WRITE_DELEG:
4051 case NFS4_SHARE_WANT_ANY_DELEG:
4052 break;
4053 case NFS4_SHARE_WANT_CANCEL:
4054 open->op_why_no_deleg = WND4_CANCELLED;
4055 break;
4056 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004057 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08004058 }
4059 }
4060}
4061
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062/*
4063 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04004064 *
4065 * Note we don't support write delegations, and won't until the vfs has
4066 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 */
4068static void
Jeff Layton4cf59222014-07-25 07:34:24 -04004069nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4070 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071{
4072 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04004073 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4074 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004075 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004076 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004078 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07004079 open->op_recall = 0;
4080 switch (open->op_claim_type) {
4081 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05004082 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07004083 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004084 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4085 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004086 break;
4087 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00004088 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004089 /*
4090 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004091 * had the chance to reclaim theirs, *and* until
4092 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004093 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004094 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004095 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004096 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004097 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04004098 /*
4099 * Also, if the file was opened for write or
4100 * create, there's a good chance the client's
4101 * about to write to it, resulting in an
4102 * immediate recall (since we don't support
4103 * write delegations):
4104 */
NeilBrown7b190fe2005-06-23 22:03:23 -07004105 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04004106 goto out_no_deleg;
4107 if (open->op_create == NFS4_OPEN_CREATE)
4108 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004109 break;
4110 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004111 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004112 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004113 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004114 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004115 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004117 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004119 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004120 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04004121 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04004122 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004123 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004124out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004125 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4126 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004127 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04004128 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004129 open->op_recall = 1;
4130 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04004131
4132 /* 4.1 client asking for a delegation? */
4133 if (open->op_deleg_want)
4134 nfsd4_open_deleg_none_ext(open, status);
4135 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136}
4137
Benny Halevye27f49c2012-02-21 14:16:54 -08004138static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4139 struct nfs4_delegation *dp)
4140{
4141 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4142 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4143 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4144 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4145 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4146 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4147 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4148 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4149 }
4150 /* Otherwise the client must be confused wanting a delegation
4151 * it already has, therefore we don't return
4152 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4153 */
4154}
4155
Al Virob37ad282006-10-19 23:28:59 -07004156__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4158{
Andy Adamson66689582009-04-03 08:28:45 +03004159 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004160 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004162 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004163 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004164 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 /*
4167 * Lookup file; if found, lookup stateid and check open request,
4168 * and check for delegations in the process of being recalled.
4169 * If not found, create the nfs4_file struct
4170 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004171 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004172 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004173 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004174 if (status)
4175 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04004176 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004178 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004179 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004180 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004181 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 }
4183
4184 /*
4185 * OPEN the file, or upgrade an existing OPEN.
4186 * If truncate fails, the OPEN fails.
4187 */
4188 if (stp) {
4189 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004190 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 if (status)
4192 goto out;
4193 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004194 stp = open->op_stp;
4195 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04004196 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004197 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4198 if (status) {
4199 release_open_stateid(stp);
4200 goto out;
4201 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004202
4203 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4204 open->op_odstate);
4205 if (stp->st_clnt_odstate == open->op_odstate)
4206 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004208 update_stateid(&stp->st_stid.sc_stateid);
4209 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
Benny Halevyd24433c2012-02-16 20:57:17 +02004211 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004212 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4213 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4214 open->op_why_no_deleg = WND4_NOT_WANTED;
4215 goto nodeleg;
4216 }
4217 }
4218
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 /*
4220 * Attempt to hand out a delegation. No error return, because the
4221 * OPEN succeeds even if we fail.
4222 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004223 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004224nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 status = nfs_ok;
4226
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004227 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004228 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004230 /* 4.1 client trying to upgrade/downgrade delegation? */
4231 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004232 open->op_deleg_want)
4233 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004234
NeilBrown13cd2182005-06-23 22:03:10 -07004235 if (fp)
4236 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004237 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08004238 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 /*
4240 * To finish the open response, we just need to set the rflags.
4241 */
4242 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004243 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004244 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004246 if (dp)
4247 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004248 if (stp)
4249 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
4251 return status;
4252}
4253
Jeff Layton58fb12e2014-07-29 21:34:27 -04004254void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04004255 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004256{
4257 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004258 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004259
Jeff Laytond3134b12014-07-29 21:34:32 -04004260 nfsd4_cstate_assign_replay(cstate, so);
4261 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004262 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004263 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04004264 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004265 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004266 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004267 if (open->op_odstate)
4268 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004269}
4270
Al Virob37ad282006-10-19 23:28:59 -07004271__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004272nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4273 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274{
4275 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004276 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004277 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 dprintk("process_renew(%08x/%08x): starting\n",
4280 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004281 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004282 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004284 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004286 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004287 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 goto out;
4289 status = nfs_ok;
4290out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 return status;
4292}
4293
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04004294void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004295nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004296{
Jeff Layton33dcc482012-04-10 11:08:48 -04004297 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004298 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004299 return;
4300
NeilBrowna76b4312005-06-23 22:04:01 -07004301 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004302 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04004303 /*
4304 * If the server goes down again right now, an NFSv4
4305 * client will still be allowed to reclaim after it comes back up,
4306 * even if it hasn't yet had a chance to reclaim state this time.
4307 *
4308 */
Jeff Layton919b8042014-09-12 16:40:20 -04004309 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004310 /*
4311 * At this point, NFSv4 clients can still reclaim. But if the
4312 * server crashes, any that have not yet reclaimed will be out
4313 * of luck on the next boot.
4314 *
4315 * (NFSv4.1+ clients are considered to have reclaimed once they
4316 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4317 * have reclaimed after their first OPEN.)
4318 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004319 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004320 /*
4321 * At this point, and once lockd and/or any other containers
4322 * exit their grace period, further reclaims will fail and
4323 * regular locking can resume.
4324 */
NeilBrowna76b4312005-06-23 22:04:01 -07004325}
4326
NeilBrownfd39ca92005-06-23 22:04:03 -07004327static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004328nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329{
4330 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004331 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004333 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004335 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004336 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004339 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004340 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004341 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004342 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 clp = list_entry(pos, struct nfs4_client, cl_lru);
4344 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4345 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004346 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 break;
4348 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004349 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004350 dprintk("NFSD: client in use (clientid %08x)\n",
4351 clp->cl_clientid.cl_id);
4352 continue;
4353 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004354 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004355 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004356 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004357 list_for_each_safe(pos, next, &reaplist) {
4358 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 dprintk("NFSD: purging unused client (clientid %08x)\n",
4360 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004361 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 expire_client(clp);
4363 }
Benny Halevycdc975052014-05-30 09:09:30 -04004364 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004365 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4367 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004368 t = dp->dl_time - cutoff;
4369 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 break;
4371 }
Jeff Layton3fcbbd22015-08-24 12:41:48 -04004372 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04004373 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 }
Benny Halevycdc975052014-05-30 09:09:30 -04004375 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004376 while (!list_empty(&reaplist)) {
4377 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4378 dl_recall_lru);
4379 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004380 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 }
Jeff Layton217526e2014-07-30 08:27:11 -04004382
4383 spin_lock(&nn->client_lock);
4384 while (!list_empty(&nn->close_lru)) {
4385 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4386 oo_close_lru);
4387 if (time_after((unsigned long)oo->oo_time,
4388 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004389 t = oo->oo_time - cutoff;
4390 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 break;
4392 }
Jeff Layton217526e2014-07-30 08:27:11 -04004393 list_del_init(&oo->oo_close_lru);
4394 stp = oo->oo_last_closed_stid;
4395 oo->oo_last_closed_stid = NULL;
4396 spin_unlock(&nn->client_lock);
4397 nfs4_put_stid(&stp->st_stid);
4398 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 }
Jeff Layton217526e2014-07-30 08:27:11 -04004400 spin_unlock(&nn->client_lock);
4401
Jeff Laytona832e7a2014-05-30 09:09:26 -04004402 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004403 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404}
4405
Harvey Harrisona254b242008-02-20 12:49:00 -08004406static struct workqueue_struct *laundry_wq;
4407static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004408
4409static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004410laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411{
4412 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004413 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4414 work);
4415 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4416 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004418 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004420 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421}
4422
Jeff Layton8fcd4612015-07-30 06:57:46 -04004423static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004424{
Jeff Layton8fcd4612015-07-30 06:57:46 -04004425 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004426 return nfserr_bad_stateid;
4427 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428}
4429
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004431access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004433 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4434 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4435 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436}
4437
4438static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004439access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004441 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4442 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443}
4444
4445static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004446__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447{
Al Virob37ad282006-10-19 23:28:59 -07004448 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449
J. Bruce Fields02921912010-07-29 15:16:59 -04004450 /* For lock stateid's, we test the parent open, not the lock: */
4451 if (stp->st_openstp)
4452 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004453 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004455 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 goto out;
4457 status = nfs_ok;
4458out:
4459 return status;
4460}
4461
Al Virob37ad282006-10-19 23:28:59 -07004462static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004463check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004465 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004467 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004468 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 * conflicting state; so we must wait out the grace period. */
4470 return nfserr_grace;
4471 } else if (flags & WR_STATE)
4472 return nfs4_share_conflict(current_fh,
4473 NFS4_SHARE_DENY_WRITE);
4474 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4475 return nfs4_share_conflict(current_fh,
4476 NFS4_SHARE_DENY_READ);
4477}
4478
4479/*
4480 * Allow READ/WRITE during grace period on recovered state only for files
4481 * that are not able to provide mandatory locking.
4482 */
4483static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004484grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004486 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487}
4488
J. Bruce Fields81b82962011-08-23 11:03:29 -04004489/* Returns true iff a is later than b: */
4490static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4491{
Jim Rees1a9357f2013-05-17 17:33:00 -04004492 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004493}
4494
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004495static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004496{
Andy Adamson66689582009-04-03 08:28:45 +03004497 /*
4498 * When sessions are used the stateid generation number is ignored
4499 * when it is zero.
4500 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004501 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004502 return nfs_ok;
4503
4504 if (in->si_generation == ref->si_generation)
4505 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004506
J. Bruce Fields0836f582008-01-26 19:08:12 -05004507 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004508 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004509 return nfserr_bad_stateid;
4510 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004511 * However, we could see a stateid from the past, even from a
4512 * non-buggy client. For example, if the client sends a lock
4513 * while some IO is outstanding, the lock may bump si_generation
4514 * while the IO is still in flight. The client could avoid that
4515 * situation by waiting for responses on all the IO requests,
4516 * but better performance may result in retrying IO that
4517 * receives an old_stateid error if requests are rarely
4518 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004519 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004520 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004521}
4522
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004523static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4524{
4525 if (ols->st_stateowner->so_is_open_owner &&
4526 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4527 return nfserr_bad_stateid;
4528 return nfs_ok;
4529}
4530
Chuck Lever7df302f2012-05-29 13:56:37 -04004531static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004532{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004533 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004534 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004535
Chuck Lever7df302f2012-05-29 13:56:37 -04004536 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004537 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004538 /* Client debugging aid. */
4539 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4540 char addr_str[INET6_ADDRSTRLEN];
4541 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4542 sizeof(addr_str));
4543 pr_warn_ratelimited("NFSD: client %s testing state ID "
4544 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004545 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004546 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004547 spin_lock(&cl->cl_lock);
4548 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004549 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004550 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004551 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004552 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004553 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004554 switch (s->sc_type) {
4555 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004556 status = nfs_ok;
4557 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004558 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004559 status = nfserr_deleg_revoked;
4560 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004561 case NFS4_OPEN_STID:
4562 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004563 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04004564 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004565 default:
4566 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004567 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004568 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004569 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004570 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004571 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004572out_unlock:
4573 spin_unlock(&cl->cl_lock);
4574 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004575}
4576
Christoph Hellwigcd61c522014-08-14 08:44:57 +02004577__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004578nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4579 stateid_t *stateid, unsigned char typemask,
4580 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004581{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004582 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004583
4584 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4585 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004586 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004587 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004588 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004589 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004590 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004591 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004592 if (status)
4593 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004594 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004595 if (!*s)
4596 return nfserr_bad_stateid;
4597 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004598}
4599
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004600static struct file *
4601nfs4_find_file(struct nfs4_stid *s, int flags)
4602{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004603 if (!s)
4604 return NULL;
4605
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004606 switch (s->sc_type) {
4607 case NFS4_DELEG_STID:
4608 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
4609 return NULL;
4610 return get_file(s->sc_file->fi_deleg_file);
4611 case NFS4_OPEN_STID:
4612 case NFS4_LOCK_STID:
4613 if (flags & RD_STATE)
4614 return find_readable_file(s->sc_file);
4615 else
4616 return find_writeable_file(s->sc_file);
4617 break;
4618 }
4619
4620 return NULL;
4621}
4622
4623static __be32
4624nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
4625{
4626 __be32 status;
4627
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004628 status = nfsd4_check_openowner_confirmed(ols);
4629 if (status)
4630 return status;
4631 return nfs4_check_openmode(ols, flags);
4632}
4633
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004634static __be32
4635nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
4636 struct file **filpp, bool *tmp_file, int flags)
4637{
4638 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
4639 struct file *file;
4640 __be32 status;
4641
4642 file = nfs4_find_file(s, flags);
4643 if (file) {
4644 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
4645 acc | NFSD_MAY_OWNER_OVERRIDE);
4646 if (status) {
4647 fput(file);
4648 return status;
4649 }
4650
4651 *filpp = file;
4652 } else {
4653 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
4654 if (status)
4655 return status;
4656
4657 if (tmp_file)
4658 *tmp_file = true;
4659 }
4660
4661 return 0;
4662}
4663
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004665 * Checks for stateid operations
4666 */
Al Virob37ad282006-10-19 23:28:59 -07004667__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004668nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
4669 struct nfsd4_compound_state *cstate, stateid_t *stateid,
4670 int flags, struct file **filpp, bool *tmp_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004672 struct svc_fh *fhp = &cstate->current_fh;
4673 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004674 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004675 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004676 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004677 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 if (filpp)
4680 *filpp = NULL;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004681 if (tmp_file)
4682 *tmp_file = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004684 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 return nfserr_grace;
4686
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004687 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
4688 status = check_special_stateids(net, fhp, stateid, flags);
4689 goto done;
4690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004692 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004693 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004694 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004695 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04004696 return status;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004697 status = check_stateid_generation(stateid, &s->sc_stateid,
4698 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04004699 if (status)
4700 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004701
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004702 switch (s->sc_type) {
4703 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004704 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004705 break;
4706 case NFS4_OPEN_STID:
4707 case NFS4_LOCK_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004708 status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004709 break;
4710 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004711 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004712 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04004714 if (status)
4715 goto out;
4716 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004717
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004718done:
4719 if (!status && filpp)
4720 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721out:
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004722 if (s)
4723 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 return status;
4725}
4726
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004727/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004728 * Test if the stateid is valid
4729 */
4730__be32
4731nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4732 struct nfsd4_test_stateid *test_stateid)
4733{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004734 struct nfsd4_test_stateid_id *stateid;
4735 struct nfs4_client *cl = cstate->session->se_client;
4736
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004737 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004738 stateid->ts_id_status =
4739 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004740
Bryan Schumaker17456802011-07-13 10:50:48 -04004741 return nfs_ok;
4742}
4743
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004744__be32
4745nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4746 struct nfsd4_free_stateid *free_stateid)
4747{
4748 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004749 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004750 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004751 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004752 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004753 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004754
Jeff Layton1af71cc2014-07-29 21:34:14 -04004755 spin_lock(&cl->cl_lock);
4756 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004757 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004758 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004759 switch (s->sc_type) {
4760 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004761 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004762 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004763 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004764 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4765 if (ret)
4766 break;
4767 ret = nfserr_locks_held;
4768 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004769 case NFS4_LOCK_STID:
4770 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4771 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004772 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004773 stp = openlockstateid(s);
4774 ret = nfserr_locks_held;
4775 if (check_for_locks(stp->st_stid.sc_file,
4776 lockowner(stp->st_stateowner)))
4777 break;
Jeff Laytone8568732015-08-24 12:41:47 -04004778 WARN_ON(!unhash_lock_stateid(stp));
Jeff Layton1af71cc2014-07-29 21:34:14 -04004779 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004780 nfs4_put_stid(s);
4781 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004782 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004783 case NFS4_REVOKED_DELEG_STID:
4784 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004785 list_del_init(&dp->dl_recall_lru);
4786 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004787 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004788 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004789 goto out;
4790 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004791 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004792out_unlock:
4793 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004794out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004795 return ret;
4796}
4797
NeilBrown4c4cd222005-07-07 17:59:27 -07004798static inline int
4799setlkflg (int type)
4800{
4801 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4802 RD_STATE : WR_STATE;
4803}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004805static __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 -04004806{
4807 struct svc_fh *current_fh = &cstate->current_fh;
4808 struct nfs4_stateowner *sop = stp->st_stateowner;
4809 __be32 status;
4810
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004811 status = nfsd4_check_seqid(cstate, sop, seqid);
4812 if (status)
4813 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004814 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4815 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004816 /*
4817 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004818 * nfserr_replay_me from the previous step, and
4819 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004820 */
4821 return nfserr_bad_stateid;
4822 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4823 if (status)
4824 return status;
Jeff Layton8fcd4612015-07-30 06:57:46 -04004825 return nfs4_check_fh(current_fh, &stp->st_stid);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004826}
4827
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828/*
4829 * Checks for sequence id mutating operations.
4830 */
Al Virob37ad282006-10-19 23:28:59 -07004831static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004832nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004833 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004834 struct nfs4_ol_stateid **stpp,
4835 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004837 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004838 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004839 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004841 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4842 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004843
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004845 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004846 if (status)
4847 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004848 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004849 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004851 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004852 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004853 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004854 else
4855 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004856 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004857}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004858
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004859static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4860 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004861{
4862 __be32 status;
4863 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004864 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004866 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004867 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004868 if (status)
4869 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004870 oo = openowner(stp->st_stateowner);
4871 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4872 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004873 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004874 }
4875 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004876 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877}
4878
Al Virob37ad282006-10-19 23:28:59 -07004879__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004880nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004881 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882{
Al Virob37ad282006-10-19 23:28:59 -07004883 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004884 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004885 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004886 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887
Al Viroa6a9f182013-09-16 10:57:01 -04004888 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4889 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004891 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004892 if (status)
4893 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004895 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004896 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004897 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004898 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004899 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004900 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004901 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004902 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004903 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004904 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004905 update_stateid(&stp->st_stid.sc_stateid);
4906 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004907 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004908 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004909
Jeff Layton2a4317c2012-03-21 16:42:43 -04004910 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004911 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004912put_stateid:
4913 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004915 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 return status;
4917}
4918
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004919static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004921 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004922 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004923 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004924 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004925}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004926
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004927static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4928{
4929 switch (to_access) {
4930 case NFS4_SHARE_ACCESS_READ:
4931 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4932 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4933 break;
4934 case NFS4_SHARE_ACCESS_WRITE:
4935 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4936 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4937 break;
4938 case NFS4_SHARE_ACCESS_BOTH:
4939 break;
4940 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004941 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 }
4943}
4944
Al Virob37ad282006-10-19 23:28:59 -07004945__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004946nfsd4_open_downgrade(struct svc_rqst *rqstp,
4947 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004948 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949{
Al Virob37ad282006-10-19 23:28:59 -07004950 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004951 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004952 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953
Al Viroa6a9f182013-09-16 10:57:01 -04004954 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4955 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004957 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004958 if (od->od_deleg_want)
4959 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4960 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004962 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004963 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004964 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004967 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004968 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004970 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004972 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004973 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004975 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004977 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978
Jeff Laytonce0fc432012-05-11 09:45:14 -04004979 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004981 update_stateid(&stp->st_stid.sc_stateid);
4982 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004984put_stateid:
4985 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004987 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 return status;
4989}
4990
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004991static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4992{
Trond Myklebustacf92952014-06-30 11:48:37 -04004993 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04004994 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04004995 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004996
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004997 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04004998 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04004999 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005000
Jeff Laytond83017f2014-07-29 21:34:42 -04005001 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04005002 if (unhashed)
5003 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04005004 spin_unlock(&clp->cl_lock);
5005 free_ol_stateid_reaplist(&reaplist);
5006 } else {
5007 spin_unlock(&clp->cl_lock);
5008 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04005009 if (unhashed)
5010 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04005011 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04005012}
5013
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014/*
5015 * nfs4_unlock_state() called after encode
5016 */
Al Virob37ad282006-10-19 23:28:59 -07005017__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005018nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005019 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020{
Al Virob37ad282006-10-19 23:28:59 -07005021 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005022 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005023 struct net *net = SVC_NET(rqstp);
5024 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025
Al Viroa6a9f182013-09-16 10:57:01 -04005026 dprintk("NFSD: nfsd4_close on file %pd\n",
5027 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005029 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5030 &close->cl_stateid,
5031 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005032 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005033 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005034 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005036 update_stateid(&stp->st_stid.sc_stateid);
5037 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005039 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005040
5041 /* put reference from nfs4_preprocess_seqid_op */
5042 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 return status;
5045}
5046
Al Virob37ad282006-10-19 23:28:59 -07005047__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005048nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5049 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005051 struct nfs4_delegation *dp;
5052 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005053 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07005054 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005055 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005057 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005058 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005060 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005061 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005062 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005063 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04005064 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005065 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04005066 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005067
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005068 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005069put_stateid:
5070 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071out:
5072 return status;
5073}
5074
Benny Halevy87df4de2008-12-15 19:42:03 +02005075static inline u64
5076end_offset(u64 start, u64 len)
5077{
5078 u64 end;
5079
5080 end = start + len;
5081 return end >= start ? end: NFS4_MAX_UINT64;
5082}
5083
5084/* last octet in a range */
5085static inline u64
5086last_byte_offset(u64 start, u64 len)
5087{
5088 u64 end;
5089
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005090 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02005091 end = start + len;
5092 return end > start ? end - 1: NFS4_MAX_UINT64;
5093}
5094
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095/*
5096 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5097 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5098 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5099 * locking, this prevents us from being completely protocol-compliant. The
5100 * real solution to this problem is to start using unsigned file offsets in
5101 * the VFS, but this is a very deep change!
5102 */
5103static inline void
5104nfs4_transform_lock_offset(struct file_lock *lock)
5105{
5106 if (lock->fl_start < 0)
5107 lock->fl_start = OFFSET_MAX;
5108 if (lock->fl_end < 0)
5109 lock->fl_end = OFFSET_MAX;
5110}
5111
Jeff Laytoncae80b32015-04-03 09:04:04 -04005112static fl_owner_t
5113nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005114{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005115 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5116
5117 nfs4_get_stateowner(&lo->lo_owner);
5118 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005119}
5120
Jeff Laytoncae80b32015-04-03 09:04:04 -04005121static void
5122nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005123{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005124 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005125
Jeff Laytoncae80b32015-04-03 09:04:04 -04005126 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04005127 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04005128}
5129
Alexey Dobriyan7b021962009-09-21 17:01:12 -07005130static const struct lock_manager_operations nfsd_posix_mng_ops = {
Kinglong Meeaef95832014-08-22 10:18:44 -04005131 .lm_get_owner = nfsd4_fl_get_owner,
5132 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07005133};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134
5135static inline void
5136nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5137{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005138 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139
NeilBrownd5b90262006-04-10 22:55:22 -07005140 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005141 lo = (struct nfs4_lockowner *) fl->fl_owner;
5142 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5143 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005144 if (!deny->ld_owner.data)
5145 /* We just don't care that much */
5146 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005147 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5148 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07005149 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005150nevermind:
5151 deny->ld_owner.len = 0;
5152 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07005153 deny->ld_clientid.cl_boot = 0;
5154 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 }
5156 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02005157 deny->ld_length = NFS4_MAX_UINT64;
5158 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5160 deny->ld_type = NFS4_READ_LT;
5161 if (fl->fl_type != F_RDLCK)
5162 deny->ld_type = NFS4_WRITE_LT;
5163}
5164
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005165static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005166find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167{
Trond Myklebustd4f04892014-07-29 21:34:36 -04005168 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005169 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170
Trond Myklebust0a880a22014-07-30 08:27:10 -04005171 lockdep_assert_held(&clp->cl_lock);
5172
Trond Myklebustd4f04892014-07-29 21:34:36 -04005173 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5174 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005175 if (so->so_is_open_owner)
5176 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005177 if (same_owner_str(so, owner))
5178 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 }
5180 return NULL;
5181}
5182
Trond Myklebustc58c6612014-07-29 21:34:35 -04005183static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005184find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04005185{
5186 struct nfs4_lockowner *lo;
5187
Trond Myklebustd4f04892014-07-29 21:34:36 -04005188 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005189 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005190 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005191 return lo;
5192}
5193
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005194static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5195{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005196 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005197}
5198
Jeff Layton6b180f02014-07-29 21:34:26 -04005199static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5200{
5201 struct nfs4_lockowner *lo = lockowner(sop);
5202
5203 kmem_cache_free(lockowner_slab, lo);
5204}
5205
5206static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005207 .so_unhash = nfs4_unhash_lockowner,
5208 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005209};
5210
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211/*
5212 * Alloc a lock owner structure.
5213 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005214 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005216 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005218static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005219alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5220 struct nfs4_ol_stateid *open_stp,
5221 struct nfsd4_lock *lock)
5222{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005223 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005225 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5226 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005228 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5229 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005230 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005231 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005232 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005233 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005234 if (ret == NULL) {
5235 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005236 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005237 ret = lo;
5238 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08005239 nfs4_free_stateowner(&lo->lo_owner);
5240
Trond Myklebustd4f04892014-07-29 21:34:36 -04005241 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04005242 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243}
5244
Jeff Layton356a95e2014-07-29 21:34:13 -04005245static void
5246init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5247 struct nfs4_file *fp, struct inode *inode,
5248 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005250 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251
Jeff Layton356a95e2014-07-29 21:34:13 -04005252 lockdep_assert_held(&clp->cl_lock);
5253
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005254 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005255 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005256 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07005257 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005258 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04005259 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005260 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005262 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005263 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005264 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005265 spin_lock(&fp->fi_lock);
5266 list_add(&stp->st_perfile, &fp->fi_stateids);
5267 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268}
5269
Jeff Laytonc53530d2014-06-30 11:48:39 -04005270static struct nfs4_ol_stateid *
5271find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5272{
5273 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005274 struct nfs4_client *clp = lo->lo_owner.so_client;
5275
5276 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005277
5278 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005279 if (lst->st_stid.sc_file == fp) {
5280 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005281 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005282 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005283 }
5284 return NULL;
5285}
5286
Jeff Layton356a95e2014-07-29 21:34:13 -04005287static struct nfs4_ol_stateid *
5288find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5289 struct inode *inode, struct nfs4_ol_stateid *ost,
5290 bool *new)
5291{
5292 struct nfs4_stid *ns = NULL;
5293 struct nfs4_ol_stateid *lst;
5294 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5295 struct nfs4_client *clp = oo->oo_owner.so_client;
5296
5297 spin_lock(&clp->cl_lock);
5298 lst = find_lock_stateid(lo, fi);
5299 if (lst == NULL) {
5300 spin_unlock(&clp->cl_lock);
5301 ns = nfs4_alloc_stid(clp, stateid_slab);
5302 if (ns == NULL)
5303 return NULL;
5304
5305 spin_lock(&clp->cl_lock);
5306 lst = find_lock_stateid(lo, fi);
5307 if (likely(!lst)) {
5308 lst = openlockstateid(ns);
5309 init_lock_stateid(lst, lo, fi, inode, ost);
5310 ns = NULL;
5311 *new = true;
5312 }
5313 }
5314 spin_unlock(&clp->cl_lock);
5315 if (ns)
5316 nfs4_put_stid(ns);
5317 return lst;
5318}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005319
NeilBrownfd39ca92005-06-23 22:04:03 -07005320static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321check_lock_length(u64 offset, u64 length)
5322{
Kinglong Meee7969312015-07-13 17:34:19 +08005323 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5324 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325}
5326
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005327static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005328{
Trond Myklebust11b91642014-07-29 21:34:08 -04005329 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005330
Jeff Layton7214e862014-07-10 14:07:33 -04005331 lockdep_assert_held(&fp->fi_lock);
5332
Jeff Layton82c5ff12012-05-11 09:45:13 -04005333 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005334 return;
Jeff Layton12659652014-07-10 14:07:28 -04005335 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005336 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005337}
5338
Jeff Layton356a95e2014-07-29 21:34:13 -04005339static __be32
5340lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5341 struct nfs4_ol_stateid *ost,
5342 struct nfsd4_lock *lock,
5343 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005344{
Jeff Layton5db1c032014-07-29 21:34:28 -04005345 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005346 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005347 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5348 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00005349 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005350 struct nfs4_lockowner *lo;
5351 unsigned int strhashval;
5352
Kinglong Meec8623992015-07-13 17:35:37 +08005353 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005354 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005355 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005356 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5357 if (lo == NULL)
5358 return nfserr_jukebox;
5359 } else {
5360 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005361 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005362 if (!cstate->minorversion &&
5363 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005364 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005365 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005366
Jeff Layton356a95e2014-07-29 21:34:13 -04005367 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005368 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005369 status = nfserr_jukebox;
5370 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005371 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005372 status = nfs_ok;
5373out:
5374 nfs4_put_stateowner(&lo->lo_owner);
5375 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005376}
5377
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378/*
5379 * LOCK operation
5380 */
Al Virob37ad282006-10-19 23:28:59 -07005381__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005382nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005383 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005385 struct nfs4_openowner *open_sop = NULL;
5386 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005387 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005388 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005389 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005390 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005391 struct file_lock *file_lock = NULL;
5392 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005393 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005394 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005395 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005396 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005397 struct net *net = SVC_NET(rqstp);
5398 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399
5400 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5401 (long long) lock->lk_offset,
5402 (long long) lock->lk_length);
5403
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 if (check_lock_length(lock->lk_offset, lock->lk_length))
5405 return nfserr_inval;
5406
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005407 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005408 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005409 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5410 return status;
5411 }
5412
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005414 if (nfsd4_has_session(cstate))
5415 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005416 memcpy(&lock->lk_new_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04005417 &cstate->session->se_client->cl_clientid,
5418 sizeof(clientid_t));
5419
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005421 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005425 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 lock->lk_new_open_seqid,
5427 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005428 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005429 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005431 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005432 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005433 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005434 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005435 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005436 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005437 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005438 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005439 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005440 lock->lk_old_lock_seqid,
5441 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005442 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005443 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005444 if (status)
5445 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005446 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005448 lkflg = setlkflg(lock->lk_type);
5449 status = nfs4_check_openmode(lock_stp, lkflg);
5450 if (status)
5451 goto out;
5452
NeilBrown0dd395d2005-07-07 17:59:15 -07005453 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005454 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005455 goto out;
5456 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005457 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005458 goto out;
5459
Jeff Layton21179d82012-08-21 08:03:32 -04005460 file_lock = locks_alloc_lock();
5461 if (!file_lock) {
5462 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5463 status = nfserr_jukebox;
5464 goto out;
5465 }
5466
Trond Myklebust11b91642014-07-29 21:34:08 -04005467 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 switch (lock->lk_type) {
5469 case NFS4_READ_LT:
5470 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005471 spin_lock(&fp->fi_lock);
5472 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005473 if (filp)
5474 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005475 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005476 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005477 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 case NFS4_WRITE_LT:
5479 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005480 spin_lock(&fp->fi_lock);
5481 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005482 if (filp)
5483 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005484 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005485 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005486 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 default:
5488 status = nfserr_inval;
5489 goto out;
5490 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005491 if (!filp) {
5492 status = nfserr_openmode;
5493 goto out;
5494 }
Kinglong Meeaef95832014-08-22 10:18:44 -04005495
5496 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04005497 file_lock->fl_pid = current->tgid;
5498 file_lock->fl_file = filp;
5499 file_lock->fl_flags = FL_POSIX;
5500 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5501 file_lock->fl_start = lock->lk_offset;
5502 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5503 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504
Jeff Layton21179d82012-08-21 08:03:32 -04005505 conflock = locks_alloc_lock();
5506 if (!conflock) {
5507 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5508 status = nfserr_jukebox;
5509 goto out;
5510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511
Jeff Layton21179d82012-08-21 08:03:32 -04005512 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005513 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005515 update_stateid(&lock_stp->st_stid.sc_stateid);
5516 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005518 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005519 break;
5520 case (EAGAIN): /* conflock holds conflicting lock */
5521 status = nfserr_denied;
5522 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005523 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005524 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 case (EDEADLK):
5526 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005527 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005528 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005529 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005530 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005531 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533out:
Trond Myklebustde186432014-07-10 14:07:26 -04005534 if (filp)
5535 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005536 if (lock_stp) {
5537 /* Bump seqid manually if the 4.0 replay owner is openowner */
5538 if (cstate->replay_owner &&
5539 cstate->replay_owner != &lock_sop->lo_owner &&
5540 seqid_mutating_err(ntohl(status)))
5541 lock_sop->lo_owner.so_seqid++;
5542
5543 /*
5544 * If this is a new, never-before-used stateid, and we are
5545 * returning an error, then just go ahead and release it.
5546 */
5547 if (status && new)
5548 release_lock_stateid(lock_stp);
5549
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005550 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005551 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005552 if (open_stp)
5553 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005554 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005555 if (file_lock)
5556 locks_free_lock(file_lock);
5557 if (conflock)
5558 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 return status;
5560}
5561
5562/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005563 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5564 * so we do a temporary open here just to get an open file to pass to
5565 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5566 * inode operation.)
5567 */
Al Viro04da6e92012-04-13 00:00:04 -04005568static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005569{
5570 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005571 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5572 if (!err) {
5573 err = nfserrno(vfs_test_lock(file, lock));
Christoph Hellwigfd891452015-04-28 15:41:16 +02005574 fput(file);
Al Viro04da6e92012-04-13 00:00:04 -04005575 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005576 return err;
5577}
5578
5579/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 * LOCKT operation
5581 */
Al Virob37ad282006-10-19 23:28:59 -07005582__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005583nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5584 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585{
Jeff Layton21179d82012-08-21 08:03:32 -04005586 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005587 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005588 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005589 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005591 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 return nfserr_grace;
5593
5594 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5595 return nfserr_inval;
5596
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005597 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005598 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005599 if (status)
5600 goto out;
5601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005603 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605
Jeff Layton21179d82012-08-21 08:03:32 -04005606 file_lock = locks_alloc_lock();
5607 if (!file_lock) {
5608 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5609 status = nfserr_jukebox;
5610 goto out;
5611 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005612
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 switch (lockt->lt_type) {
5614 case NFS4_READ_LT:
5615 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005616 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 break;
5618 case NFS4_WRITE_LT:
5619 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005620 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 break;
5622 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005623 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 status = nfserr_inval;
5625 goto out;
5626 }
5627
Kinglong Meec8623992015-07-13 17:35:37 +08005628 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005629 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005630 file_lock->fl_owner = (fl_owner_t)lo;
5631 file_lock->fl_pid = current->tgid;
5632 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633
Jeff Layton21179d82012-08-21 08:03:32 -04005634 file_lock->fl_start = lockt->lt_offset;
5635 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636
Jeff Layton21179d82012-08-21 08:03:32 -04005637 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638
Jeff Layton21179d82012-08-21 08:03:32 -04005639 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005640 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005641 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005642
Jeff Layton21179d82012-08-21 08:03:32 -04005643 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005645 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 }
5647out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005648 if (lo)
5649 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04005650 if (file_lock)
5651 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 return status;
5653}
5654
Al Virob37ad282006-10-19 23:28:59 -07005655__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005656nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005657 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005659 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005661 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005662 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005663 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005664 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5665
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5667 (long long) locku->lu_offset,
5668 (long long) locku->lu_length);
5669
5670 if (check_lock_length(locku->lu_offset, locku->lu_length))
5671 return nfserr_inval;
5672
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005673 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005674 &locku->lu_stateid, NFS4_LOCK_STID,
5675 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005676 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005678 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005679 if (!filp) {
5680 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005681 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005682 }
Jeff Layton21179d82012-08-21 08:03:32 -04005683 file_lock = locks_alloc_lock();
5684 if (!file_lock) {
5685 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5686 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005687 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005688 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005689
Jeff Layton21179d82012-08-21 08:03:32 -04005690 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04005691 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04005692 file_lock->fl_pid = current->tgid;
5693 file_lock->fl_file = filp;
5694 file_lock->fl_flags = FL_POSIX;
5695 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5696 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697
Jeff Layton21179d82012-08-21 08:03:32 -04005698 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5699 locku->lu_length);
5700 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701
Jeff Layton21179d82012-08-21 08:03:32 -04005702 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005703 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005704 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 goto out_nfserr;
5706 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005707 update_stateid(&stp->st_stid.sc_stateid);
5708 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005709fput:
5710 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005711put_stateid:
5712 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005714 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005715 if (file_lock)
5716 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 return status;
5718
5719out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005720 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005721 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722}
5723
5724/*
5725 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005726 * true: locks held by lockowner
5727 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005729static bool
5730check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005732 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005733 int status = false;
5734 struct file *filp = find_any_file(fp);
5735 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005736 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005737
5738 if (!filp) {
5739 /* Any valid lock stateid should have some sort of access */
5740 WARN_ON_ONCE(1);
5741 return status;
5742 }
5743
5744 inode = file_inode(filp);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005745 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005747 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05005748 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005749 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
5750 if (fl->fl_owner == (fl_owner_t)lowner) {
5751 status = true;
5752 break;
5753 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005754 }
Jeff Layton6109c852015-01-16 15:05:57 -05005755 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 }
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005757 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 return status;
5759}
5760
Al Virob37ad282006-10-19 23:28:59 -07005761__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005762nfsd4_release_lockowner(struct svc_rqst *rqstp,
5763 struct nfsd4_compound_state *cstate,
5764 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765{
5766 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005767 struct nfs4_stateowner *sop;
5768 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005769 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005771 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005772 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005773 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005774 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775
5776 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5777 clid->cl_boot, clid->cl_id);
5778
Jeff Layton4b24ca72014-06-30 11:48:44 -04005779 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005780 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04005781 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005782
Trond Myklebustd4f04892014-07-29 21:34:36 -04005783 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005784 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005785 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005786 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005787 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005788
5789 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005790 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005791
Jeff Layton882e9d22014-07-29 21:34:37 -04005792 /* see if there are still any locks associated with it */
5793 lo = lockowner(sop);
5794 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5795 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5796 status = nfserr_locks_held;
5797 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04005798 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04005799 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005800 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005801
Kinglong Meeb5971af2014-08-22 10:18:43 -04005802 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04005803 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005804 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005805 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005806 if (lo)
5807 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 return status;
5809}
5810
5811static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005812alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813{
NeilBrowna55370a2005-06-23 22:03:52 -07005814 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815}
5816
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005817bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005818nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005819{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005820 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005821
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005822 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005823 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005824}
5825
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826/*
5827 * failure => all reset bets are off, nfserr_no_grace...
5828 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005829struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005830nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831{
5832 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005833 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834
NeilBrowna55370a2005-06-23 22:03:52 -07005835 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5836 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005837 if (crp) {
5838 strhashval = clientstr_hashval(name);
5839 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005840 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005841 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005842 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005843 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005844 }
5845 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846}
5847
Jeff Layton2a4317c2012-03-21 16:42:43 -04005848void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005849nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005850{
5851 list_del(&crp->cr_strhash);
5852 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005853 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005854}
5855
5856void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005857nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858{
5859 struct nfs4_client_reclaim *crp = NULL;
5860 int i;
5861
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005863 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5864 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005866 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 }
5868 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005869 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870}
5871
5872/*
5873 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005874struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005875nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876{
5877 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 struct nfs4_client_reclaim *crp = NULL;
5879
Jeff Layton278c9312012-11-12 15:00:52 -05005880 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881
Jeff Layton278c9312012-11-12 15:00:52 -05005882 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005883 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005884 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 return crp;
5886 }
5887 }
5888 return NULL;
5889}
5890
5891/*
5892* Called from OPEN. Look for clientid in reclaim list.
5893*/
Al Virob37ad282006-10-19 23:28:59 -07005894__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005895nfs4_check_open_reclaim(clientid_t *clid,
5896 struct nfsd4_compound_state *cstate,
5897 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005899 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005900
5901 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005902 status = lookup_clientid(clid, cstate, nn);
5903 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005904 return nfserr_reclaim_bad;
5905
Jeff Layton3b3e7b72014-09-12 16:40:22 -04005906 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
5907 return nfserr_no_grace;
5908
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005909 if (nfsd4_client_record_check(cstate->clp))
5910 return nfserr_reclaim_bad;
5911
5912 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913}
5914
Bryan Schumaker65178db2011-11-01 13:35:21 -04005915#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04005916static inline void
5917put_client(struct nfs4_client *clp)
5918{
5919 atomic_dec(&clp->cl_refcount);
5920}
5921
Jeff Layton285abde2014-07-30 08:27:24 -04005922static struct nfs4_client *
5923nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5924{
5925 struct nfs4_client *clp;
5926 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5927 nfsd_net_id);
5928
5929 if (!nfsd_netns_ready(nn))
5930 return NULL;
5931
5932 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5933 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5934 return clp;
5935 }
5936 return NULL;
5937}
5938
Jeff Layton7ec0e362014-07-30 08:27:17 -04005939u64
Jeff Layton285abde2014-07-30 08:27:24 -04005940nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04005941{
5942 struct nfs4_client *clp;
5943 u64 count = 0;
5944 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5945 nfsd_net_id);
5946 char buf[INET6_ADDRSTRLEN];
5947
5948 if (!nfsd_netns_ready(nn))
5949 return 0;
5950
5951 spin_lock(&nn->client_lock);
5952 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5953 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5954 pr_info("NFS Client: %s\n", buf);
5955 ++count;
5956 }
5957 spin_unlock(&nn->client_lock);
5958
5959 return count;
5960}
Bryan Schumaker65178db2011-11-01 13:35:21 -04005961
Jeff Laytona0926d12014-07-30 08:27:18 -04005962u64
Jeff Layton285abde2014-07-30 08:27:24 -04005963nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04005964{
5965 u64 count = 0;
5966 struct nfs4_client *clp;
5967 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5968 nfsd_net_id);
5969
5970 if (!nfsd_netns_ready(nn))
5971 return count;
5972
5973 spin_lock(&nn->client_lock);
5974 clp = nfsd_find_client(addr, addr_size);
5975 if (clp) {
5976 if (mark_client_expired_locked(clp) == nfs_ok)
5977 ++count;
5978 else
5979 clp = NULL;
5980 }
5981 spin_unlock(&nn->client_lock);
5982
5983 if (clp)
5984 expire_client(clp);
5985
5986 return count;
5987}
5988
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005989u64
Jeff Layton285abde2014-07-30 08:27:24 -04005990nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005991{
5992 u64 count = 0;
5993 struct nfs4_client *clp, *next;
5994 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5995 nfsd_net_id);
5996 LIST_HEAD(reaplist);
5997
5998 if (!nfsd_netns_ready(nn))
5999 return count;
6000
6001 spin_lock(&nn->client_lock);
6002 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6003 if (mark_client_expired_locked(clp) == nfs_ok) {
6004 list_add(&clp->cl_lru, &reaplist);
6005 if (max != 0 && ++count >= max)
6006 break;
6007 }
6008 }
6009 spin_unlock(&nn->client_lock);
6010
6011 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6012 expire_client(clp);
6013
6014 return count;
6015}
6016
Bryan Schumaker184c1842012-11-29 11:40:44 -05006017static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6018 const char *type)
6019{
6020 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05006021 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05006022 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6023}
6024
Jeff Layton016200c2014-07-30 08:27:21 -04006025static void
6026nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6027 struct list_head *collect)
6028{
6029 struct nfs4_client *clp = lst->st_stid.sc_client;
6030 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6031 nfsd_net_id);
6032
6033 if (!collect)
6034 return;
6035
6036 lockdep_assert_held(&nn->client_lock);
6037 atomic_inc(&clp->cl_refcount);
6038 list_add(&lst->st_locks, collect);
6039}
6040
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006041static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04006042 struct list_head *collect,
Jeff Laytone8568732015-08-24 12:41:47 -04006043 bool (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05006044{
6045 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006046 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006047 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006048 u64 count = 0;
6049
Jeff Layton016200c2014-07-30 08:27:21 -04006050 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006051 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006052 list_for_each_entry_safe(stp, st_next,
6053 &oop->oo_owner.so_stateids, st_perstateowner) {
6054 list_for_each_entry_safe(lst, lst_next,
6055 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04006056 if (func) {
Jeff Laytone8568732015-08-24 12:41:47 -04006057 if (func(lst))
6058 nfsd_inject_add_lock_to_list(lst,
6059 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04006060 }
Jeff Layton016200c2014-07-30 08:27:21 -04006061 ++count;
6062 /*
6063 * Despite the fact that these functions deal
6064 * with 64-bit integers for "count", we must
6065 * ensure that it doesn't blow up the
6066 * clp->cl_refcount. Throw a warning if we
6067 * start to approach INT_MAX here.
6068 */
6069 WARN_ON_ONCE(count == (INT_MAX / 2));
6070 if (count == max)
6071 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006072 }
6073 }
6074 }
Jeff Layton016200c2014-07-30 08:27:21 -04006075out:
6076 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006077
6078 return count;
6079}
6080
Jeff Layton016200c2014-07-30 08:27:21 -04006081static u64
6082nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6083 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05006084{
Jeff Layton016200c2014-07-30 08:27:21 -04006085 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006086}
6087
Jeff Layton016200c2014-07-30 08:27:21 -04006088static u64
6089nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006090{
Jeff Layton016200c2014-07-30 08:27:21 -04006091 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006092 nfsd_print_count(clp, count, "locked files");
6093 return count;
6094}
6095
Jeff Layton016200c2014-07-30 08:27:21 -04006096u64
Jeff Layton285abde2014-07-30 08:27:24 -04006097nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04006098{
6099 struct nfs4_client *clp;
6100 u64 count = 0;
6101 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6102 nfsd_net_id);
6103
6104 if (!nfsd_netns_ready(nn))
6105 return 0;
6106
6107 spin_lock(&nn->client_lock);
6108 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6109 count += nfsd_print_client_locks(clp);
6110 spin_unlock(&nn->client_lock);
6111
6112 return count;
6113}
6114
6115static void
6116nfsd_reap_locks(struct list_head *reaplist)
6117{
6118 struct nfs4_client *clp;
6119 struct nfs4_ol_stateid *stp, *next;
6120
6121 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6122 list_del_init(&stp->st_locks);
6123 clp = stp->st_stid.sc_client;
6124 nfs4_put_stid(&stp->st_stid);
6125 put_client(clp);
6126 }
6127}
6128
6129u64
Jeff Layton285abde2014-07-30 08:27:24 -04006130nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04006131{
6132 unsigned int count = 0;
6133 struct nfs4_client *clp;
6134 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6135 nfsd_net_id);
6136 LIST_HEAD(reaplist);
6137
6138 if (!nfsd_netns_ready(nn))
6139 return count;
6140
6141 spin_lock(&nn->client_lock);
6142 clp = nfsd_find_client(addr, addr_size);
6143 if (clp)
6144 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6145 spin_unlock(&nn->client_lock);
6146 nfsd_reap_locks(&reaplist);
6147 return count;
6148}
6149
6150u64
Jeff Layton285abde2014-07-30 08:27:24 -04006151nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04006152{
6153 u64 count = 0;
6154 struct nfs4_client *clp;
6155 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6156 nfsd_net_id);
6157 LIST_HEAD(reaplist);
6158
6159 if (!nfsd_netns_ready(nn))
6160 return count;
6161
6162 spin_lock(&nn->client_lock);
6163 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6164 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6165 if (max != 0 && count >= max)
6166 break;
6167 }
6168 spin_unlock(&nn->client_lock);
6169 nfsd_reap_locks(&reaplist);
6170 return count;
6171}
6172
Jeff Layton82e05ef2014-07-30 08:27:22 -04006173static u64
6174nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6175 struct list_head *collect,
6176 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006177{
6178 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04006179 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6180 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006181 u64 count = 0;
6182
Jeff Layton82e05ef2014-07-30 08:27:22 -04006183 lockdep_assert_held(&nn->client_lock);
6184
6185 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006186 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04006187 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006188 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04006189 if (collect) {
6190 atomic_inc(&clp->cl_refcount);
6191 list_add(&oop->oo_perclient, collect);
6192 }
6193 }
6194 ++count;
6195 /*
6196 * Despite the fact that these functions deal with
6197 * 64-bit integers for "count", we must ensure that
6198 * it doesn't blow up the clp->cl_refcount. Throw a
6199 * warning if we start to approach INT_MAX here.
6200 */
6201 WARN_ON_ONCE(count == (INT_MAX / 2));
6202 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006203 break;
6204 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04006205 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006206
6207 return count;
6208}
6209
Jeff Layton82e05ef2014-07-30 08:27:22 -04006210static u64
6211nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006212{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006213 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6214
6215 nfsd_print_count(clp, count, "openowners");
6216 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006217}
6218
Jeff Layton82e05ef2014-07-30 08:27:22 -04006219static u64
6220nfsd_collect_client_openowners(struct nfs4_client *clp,
6221 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006222{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006223 return nfsd_foreach_client_openowner(clp, max, collect,
6224 unhash_openowner_locked);
6225}
6226
6227u64
Jeff Layton285abde2014-07-30 08:27:24 -04006228nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006229{
6230 struct nfs4_client *clp;
6231 u64 count = 0;
6232 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6233 nfsd_net_id);
6234
6235 if (!nfsd_netns_ready(nn))
6236 return 0;
6237
6238 spin_lock(&nn->client_lock);
6239 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6240 count += nfsd_print_client_openowners(clp);
6241 spin_unlock(&nn->client_lock);
6242
6243 return count;
6244}
6245
6246static void
6247nfsd_reap_openowners(struct list_head *reaplist)
6248{
6249 struct nfs4_client *clp;
6250 struct nfs4_openowner *oop, *next;
6251
6252 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6253 list_del_init(&oop->oo_perclient);
6254 clp = oop->oo_owner.so_client;
6255 release_openowner(oop);
6256 put_client(clp);
6257 }
6258}
6259
6260u64
Jeff Layton285abde2014-07-30 08:27:24 -04006261nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6262 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006263{
6264 unsigned int count = 0;
6265 struct nfs4_client *clp;
6266 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6267 nfsd_net_id);
6268 LIST_HEAD(reaplist);
6269
6270 if (!nfsd_netns_ready(nn))
6271 return count;
6272
6273 spin_lock(&nn->client_lock);
6274 clp = nfsd_find_client(addr, addr_size);
6275 if (clp)
6276 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6277 spin_unlock(&nn->client_lock);
6278 nfsd_reap_openowners(&reaplist);
6279 return count;
6280}
6281
6282u64
Jeff Layton285abde2014-07-30 08:27:24 -04006283nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006284{
6285 u64 count = 0;
6286 struct nfs4_client *clp;
6287 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6288 nfsd_net_id);
6289 LIST_HEAD(reaplist);
6290
6291 if (!nfsd_netns_ready(nn))
6292 return count;
6293
6294 spin_lock(&nn->client_lock);
6295 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6296 count += nfsd_collect_client_openowners(clp, &reaplist,
6297 max - count);
6298 if (max != 0 && count >= max)
6299 break;
6300 }
6301 spin_unlock(&nn->client_lock);
6302 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006303 return count;
6304}
6305
Bryan Schumaker269de302012-11-29 11:40:42 -05006306static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6307 struct list_head *victims)
6308{
6309 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006310 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6311 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006312 u64 count = 0;
6313
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006314 lockdep_assert_held(&nn->client_lock);
6315
6316 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006317 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006318 if (victims) {
6319 /*
6320 * It's not safe to mess with delegations that have a
6321 * non-zero dl_time. They might have already been broken
6322 * and could be processed by the laundromat outside of
6323 * the state_lock. Just leave them be.
6324 */
6325 if (dp->dl_time != 0)
6326 continue;
6327
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006328 atomic_inc(&clp->cl_refcount);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04006329 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04006330 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006331 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006332 ++count;
6333 /*
6334 * Despite the fact that these functions deal with
6335 * 64-bit integers for "count", we must ensure that
6336 * it doesn't blow up the clp->cl_refcount. Throw a
6337 * warning if we start to approach INT_MAX here.
6338 */
6339 WARN_ON_ONCE(count == (INT_MAX / 2));
6340 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006341 break;
6342 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006343 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006344 return count;
6345}
6346
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006347static u64
6348nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006349{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006350 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006351
6352 nfsd_print_count(clp, count, "delegations");
6353 return count;
6354}
6355
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006356u64
Jeff Layton285abde2014-07-30 08:27:24 -04006357nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006358{
6359 struct nfs4_client *clp;
6360 u64 count = 0;
6361 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6362 nfsd_net_id);
6363
6364 if (!nfsd_netns_ready(nn))
6365 return 0;
6366
6367 spin_lock(&nn->client_lock);
6368 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6369 count += nfsd_print_client_delegations(clp);
6370 spin_unlock(&nn->client_lock);
6371
6372 return count;
6373}
6374
6375static void
6376nfsd_forget_delegations(struct list_head *reaplist)
6377{
6378 struct nfs4_client *clp;
6379 struct nfs4_delegation *dp, *next;
6380
6381 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6382 list_del_init(&dp->dl_recall_lru);
6383 clp = dp->dl_stid.sc_client;
6384 revoke_delegation(dp);
6385 put_client(clp);
6386 }
6387}
6388
6389u64
Jeff Layton285abde2014-07-30 08:27:24 -04006390nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6391 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006392{
6393 u64 count = 0;
6394 struct nfs4_client *clp;
6395 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6396 nfsd_net_id);
6397 LIST_HEAD(reaplist);
6398
6399 if (!nfsd_netns_ready(nn))
6400 return count;
6401
6402 spin_lock(&nn->client_lock);
6403 clp = nfsd_find_client(addr, addr_size);
6404 if (clp)
6405 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6406 spin_unlock(&nn->client_lock);
6407
6408 nfsd_forget_delegations(&reaplist);
6409 return count;
6410}
6411
6412u64
Jeff Layton285abde2014-07-30 08:27:24 -04006413nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006414{
6415 u64 count = 0;
6416 struct nfs4_client *clp;
6417 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6418 nfsd_net_id);
6419 LIST_HEAD(reaplist);
6420
6421 if (!nfsd_netns_ready(nn))
6422 return count;
6423
6424 spin_lock(&nn->client_lock);
6425 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6426 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6427 if (max != 0 && count >= max)
6428 break;
6429 }
6430 spin_unlock(&nn->client_lock);
6431 nfsd_forget_delegations(&reaplist);
6432 return count;
6433}
6434
6435static void
6436nfsd_recall_delegations(struct list_head *reaplist)
6437{
6438 struct nfs4_client *clp;
6439 struct nfs4_delegation *dp, *next;
6440
6441 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6442 list_del_init(&dp->dl_recall_lru);
6443 clp = dp->dl_stid.sc_client;
6444 /*
6445 * We skipped all entries that had a zero dl_time before,
6446 * so we can now reset the dl_time back to 0. If a delegation
6447 * break comes in now, then it won't make any difference since
6448 * we're recalling it either way.
6449 */
6450 spin_lock(&state_lock);
6451 dp->dl_time = 0;
6452 spin_unlock(&state_lock);
6453 nfsd_break_one_deleg(dp);
6454 put_client(clp);
6455 }
6456}
6457
6458u64
Jeff Layton285abde2014-07-30 08:27:24 -04006459nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006460 size_t addr_size)
6461{
6462 u64 count = 0;
6463 struct nfs4_client *clp;
6464 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6465 nfsd_net_id);
6466 LIST_HEAD(reaplist);
6467
6468 if (!nfsd_netns_ready(nn))
6469 return count;
6470
6471 spin_lock(&nn->client_lock);
6472 clp = nfsd_find_client(addr, addr_size);
6473 if (clp)
6474 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6475 spin_unlock(&nn->client_lock);
6476
6477 nfsd_recall_delegations(&reaplist);
6478 return count;
6479}
6480
6481u64
Jeff Layton285abde2014-07-30 08:27:24 -04006482nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006483{
6484 u64 count = 0;
6485 struct nfs4_client *clp, *next;
6486 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6487 nfsd_net_id);
6488 LIST_HEAD(reaplist);
6489
6490 if (!nfsd_netns_ready(nn))
6491 return count;
6492
6493 spin_lock(&nn->client_lock);
6494 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6495 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6496 if (max != 0 && ++count >= max)
6497 break;
6498 }
6499 spin_unlock(&nn->client_lock);
6500 nfsd_recall_delegations(&reaplist);
6501 return count;
6502}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006503#endif /* CONFIG_NFSD_FAULT_INJECTION */
6504
Meelap Shahc2f1a552007-07-17 04:04:39 -07006505/*
6506 * Since the lifetime of a delegation isn't limited to that of an open, a
6507 * client may quite reasonably hang on to a delegation as long as it has
6508 * the inode cached. This becomes an obvious problem the first time a
6509 * client's inode cache approaches the size of the server's total memory.
6510 *
6511 * For now we avoid this problem by imposing a hard limit on the number
6512 * of delegations, which varies according to the server's memory size.
6513 */
6514static void
6515set_max_delegations(void)
6516{
6517 /*
6518 * Allow at most 4 delegations per megabyte of RAM. Quick
6519 * estimates suggest that in the worst case (where every delegation
6520 * is for a different inode), a delegation could take about 1.5K,
6521 * giving a worst case usage of about 6% of memory.
6522 */
6523 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6524}
6525
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006526static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006527{
6528 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6529 int i;
6530
6531 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6532 CLIENT_HASH_SIZE, GFP_KERNEL);
6533 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006534 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006535 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6536 CLIENT_HASH_SIZE, GFP_KERNEL);
6537 if (!nn->unconf_id_hashtbl)
6538 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006539 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6540 SESSION_HASH_SIZE, GFP_KERNEL);
6541 if (!nn->sessionid_hashtbl)
6542 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006543
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006544 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006545 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006546 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006547 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006548 for (i = 0; i < SESSION_HASH_SIZE; i++)
6549 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006550 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006551 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03006552 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03006553 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006554 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03006555 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006556
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006557 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006558 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006559
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006560 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006561
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006562err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03006563 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006564err_unconf_id:
6565 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006566err:
6567 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006568}
6569
6570static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006571nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006572{
6573 int i;
6574 struct nfs4_client *clp = NULL;
6575 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6576
6577 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6578 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6579 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6580 destroy_client(clp);
6581 }
6582 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006583
Kinglong Mee2b905632014-03-26 22:09:30 +08006584 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6585 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6586 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6587 destroy_client(clp);
6588 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006589 }
6590
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006591 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006592 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006593 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006594 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006595}
6596
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006597int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006598nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07006599{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006600 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006601 int ret;
6602
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006603 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006604 if (ret)
6605 return ret;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006606 nn->boot_time = get_seconds();
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04006607 nn->grace_ended = false;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04006608 nn->nfsd4_manager.block_opens = true;
Jeff Laytond4318ac2014-09-12 16:40:21 -04006609 locks_start_grace(net, &nn->nfsd4_manager);
6610 nfsd4_client_tracking_init(net);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006611 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03006612 nn->nfsd4_grace, net);
6613 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006614 return 0;
6615}
6616
6617/* initialization to perform when the nfsd service is started: */
6618
6619int
6620nfs4_state_start(void)
6621{
6622 int ret;
6623
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006624 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006625 if (ret)
6626 return -ENOMEM;
Jeff Layton51a54562015-08-20 07:17:01 -04006627 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05006628 if (laundry_wq == NULL) {
6629 ret = -ENOMEM;
6630 goto out_recovery;
6631 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006632 ret = nfsd4_create_callback_queue();
6633 if (ret)
6634 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006635
Meelap Shahc2f1a552007-07-17 04:04:39 -07006636 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006637
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006638 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006639
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006640out_free_laundry:
6641 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05006642out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006643 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644}
6645
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006646void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006647nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006649 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006651 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006653 cancel_delayed_work_sync(&nn->laundromat_work);
6654 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05006655
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04006657 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006658 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04006660 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04006661 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662 }
Benny Halevycdc975052014-05-30 09:09:30 -04006663 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664 list_for_each_safe(pos, next, &reaplist) {
6665 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006666 list_del_init(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +02006667 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -04006668 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04006669 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670 }
6671
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006672 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006673 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674}
6675
6676void
6677nfs4_state_shutdown(void)
6678{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006679 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006680 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006682
6683static void
6684get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6685{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006686 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6687 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006688}
6689
6690static void
6691put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6692{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006693 if (cstate->minorversion) {
6694 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6695 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6696 }
6697}
6698
6699void
6700clear_current_stateid(struct nfsd4_compound_state *cstate)
6701{
6702 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006703}
6704
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006705/*
6706 * functions to set current state id
6707 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006708void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006709nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6710{
6711 put_stateid(cstate, &odp->od_stateid);
6712}
6713
6714void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006715nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6716{
6717 put_stateid(cstate, &open->op_stateid);
6718}
6719
6720void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006721nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6722{
6723 put_stateid(cstate, &close->cl_stateid);
6724}
6725
6726void
6727nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6728{
6729 put_stateid(cstate, &lock->lk_resp_stateid);
6730}
6731
6732/*
6733 * functions to consume current state id
6734 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006735
6736void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006737nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6738{
6739 get_stateid(cstate, &odp->od_stateid);
6740}
6741
6742void
6743nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6744{
6745 get_stateid(cstate, &drp->dr_stateid);
6746}
6747
6748void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006749nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6750{
6751 get_stateid(cstate, &fsp->fr_stateid);
6752}
6753
6754void
6755nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6756{
6757 get_stateid(cstate, &setattr->sa_stateid);
6758}
6759
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006760void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006761nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6762{
6763 get_stateid(cstate, &close->cl_stateid);
6764}
6765
6766void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006767nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006768{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006769 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006770}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006771
6772void
6773nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6774{
6775 get_stateid(cstate, &read->rd_stateid);
6776}
6777
6778void
6779nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6780{
6781 get_stateid(cstate, &write->wr_stateid);
6782}