blob: 037bb924ce63623cc0b90aa97a9e215b8b6aa4c7 [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>
NeilBrown6282cd52014-06-04 17:39:26 +100044#include <linux/hash.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"
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define NFSDDBG_FACILITY NFSDDBG_PROC
53
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050054#define all_ones {{~0,~0},~0}
55static const stateid_t one_stateid = {
56 .si_generation = ~0,
57 .si_opaque = all_ones,
58};
59static const stateid_t zero_stateid = {
60 /* all fields zero */
61};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010062static const stateid_t currentstateid = {
63 .si_generation = 1,
64};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050065
Andy Adamsonec6b5d72009-04-03 08:28:28 +030066static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070067
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050068#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
69#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010070#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040073static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040074static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
J. Bruce Fields8b671b82009-02-22 14:51:34 -080076/* Locking: */
77
78/* Currently used for almost all code touching nfsv4 state: */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080079static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
J. Bruce Fields8b671b82009-02-22 14:51:34 -080081/*
82 * Currently used for the del_recall_lru and file hash table. In an
83 * effort to decrease the scope of the client_mutex, this spinlock may
84 * eventually cover more:
85 */
Benny Halevycdc975052014-05-30 09:09:30 -040086static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080087
Jeff Laytonb401be222014-07-29 21:34:33 -040088/*
89 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
90 * the refcount on the open stateid to drop.
91 */
92static DECLARE_WAIT_QUEUE_HEAD(close_wq);
93
Christoph Hellwigabf11352014-05-21 07:43:03 -070094static struct kmem_cache *openowner_slab;
95static struct kmem_cache *lockowner_slab;
96static struct kmem_cache *file_slab;
97static struct kmem_cache *stateid_slab;
98static struct kmem_cache *deleg_slab;
NeilBrowne60d4392005-06-23 22:03:01 -070099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100void
101nfs4_lock_state(void)
102{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800103 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400106static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800107
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400108static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800109{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400110 return ses->se_flags & NFS4_SESSION_DEAD;
111}
112
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400113static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
114{
115 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400116 return nfserr_jukebox;
117 ses->se_flags |= NFS4_SESSION_DEAD;
118 return nfs_ok;
119}
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121void
122nfs4_unlock_state(void)
123{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800124 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
J. Bruce Fields221a6872013-04-01 22:23:49 -0400127static bool is_client_expired(struct nfs4_client *clp)
128{
129 return clp->cl_time == 0;
130}
131
J. Bruce Fields221a6872013-04-01 22:23:49 -0400132static __be32 get_client_locked(struct nfs4_client *clp)
133{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400134 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
135
136 lockdep_assert_held(&nn->client_lock);
137
J. Bruce Fields221a6872013-04-01 22:23:49 -0400138 if (is_client_expired(clp))
139 return nfserr_expired;
140 atomic_inc(&clp->cl_refcount);
141 return nfs_ok;
142}
143
144/* must be called under the client_lock */
145static inline void
146renew_client_locked(struct nfs4_client *clp)
147{
148 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
149
150 if (is_client_expired(clp)) {
151 WARN_ON(1);
152 printk("%s: client (clientid %08x/%08x) already expired\n",
153 __func__,
154 clp->cl_clientid.cl_boot,
155 clp->cl_clientid.cl_id);
156 return;
157 }
158
159 dprintk("renewing client (clientid %08x/%08x)\n",
160 clp->cl_clientid.cl_boot,
161 clp->cl_clientid.cl_id);
162 list_move_tail(&clp->cl_lru, &nn->client_lru);
163 clp->cl_time = get_seconds();
164}
165
166static inline void
167renew_client(struct nfs4_client *clp)
168{
169 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
170
171 spin_lock(&nn->client_lock);
172 renew_client_locked(clp);
173 spin_unlock(&nn->client_lock);
174}
175
Fengguang Wuba138432013-04-16 22:14:15 -0400176static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400177{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400178 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
179
180 lockdep_assert_held(&nn->client_lock);
181
J. Bruce Fields221a6872013-04-01 22:23:49 -0400182 if (!atomic_dec_and_test(&clp->cl_refcount))
183 return;
184 if (!is_client_expired(clp))
185 renew_client_locked(clp);
186}
187
Jeff Layton4b24ca72014-06-30 11:48:44 -0400188static void put_client_renew(struct nfs4_client *clp)
189{
190 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
191
Jeff Laytond6c249b2014-07-08 14:02:50 -0400192 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
193 return;
194 if (!is_client_expired(clp))
195 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400196 spin_unlock(&nn->client_lock);
197}
198
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400199static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
200{
201 __be32 status;
202
203 if (is_session_dead(ses))
204 return nfserr_badsession;
205 status = get_client_locked(ses->se_client);
206 if (status)
207 return status;
208 atomic_inc(&ses->se_ref);
209 return nfs_ok;
210}
211
212static void nfsd4_put_session_locked(struct nfsd4_session *ses)
213{
214 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400215 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
216
217 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400218
219 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
220 free_session(ses);
221 put_client_renew_locked(clp);
222}
223
224static void nfsd4_put_session(struct nfsd4_session *ses)
225{
226 struct nfs4_client *clp = ses->se_client;
227 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
228
229 spin_lock(&nn->client_lock);
230 nfsd4_put_session_locked(ses);
231 spin_unlock(&nn->client_lock);
232}
233
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400234static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400235same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400236{
237 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400238 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400239}
240
241static struct nfs4_openowner *
242find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400243 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400244{
245 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400246
Trond Myklebustd4f04892014-07-29 21:34:36 -0400247 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400248
Trond Myklebustd4f04892014-07-29 21:34:36 -0400249 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
250 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400251 if (!so->so_is_open_owner)
252 continue;
Trond Myklebustd4f04892014-07-29 21:34:36 -0400253 if (same_owner_str(so, &open->op_owner)) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400254 atomic_inc(&so->so_count);
Trond Myklebustd4f04892014-07-29 21:34:36 -0400255 return openowner(so);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400256 }
257 }
258 return NULL;
259}
260
261static struct nfs4_openowner *
262find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400263 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400264{
265 struct nfs4_openowner *oo;
266
Trond Myklebustd4f04892014-07-29 21:34:36 -0400267 spin_lock(&clp->cl_lock);
268 oo = find_openstateowner_str_locked(hashval, open, clp);
269 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400270 return oo;
271}
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273static inline u32
274opaque_hashval(const void *ptr, int nbytes)
275{
276 unsigned char *cptr = (unsigned char *) ptr;
277
278 u32 x = 0;
279 while (nbytes--) {
280 x *= 37;
281 x += *cptr++;
282 }
283 return x;
284}
285
J. Bruce Fields32513b42011-10-13 16:00:16 -0400286static void nfsd4_free_file(struct nfs4_file *f)
287{
288 kmem_cache_free(file_slab, f);
289}
290
NeilBrown13cd2182005-06-23 22:03:10 -0700291static inline void
292put_nfs4_file(struct nfs4_file *fi)
293{
Jeff Layton02e12152014-07-16 10:31:57 -0400294 might_lock(&state_lock);
295
Benny Halevycdc975052014-05-30 09:09:30 -0400296 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton89876f82013-04-02 09:01:59 -0400297 hlist_del(&fi->fi_hash);
Benny Halevycdc975052014-05-30 09:09:30 -0400298 spin_unlock(&state_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400299 nfsd4_free_file(fi);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800300 }
NeilBrown13cd2182005-06-23 22:03:10 -0700301}
302
303static inline void
304get_nfs4_file(struct nfs4_file *fi)
305{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800306 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700307}
308
Trond Myklebustde186432014-07-10 14:07:26 -0400309static struct file *
310__nfs4_get_fd(struct nfs4_file *f, int oflag)
311{
312 if (f->fi_fds[oflag])
313 return get_file(f->fi_fds[oflag]);
314 return NULL;
315}
316
317static struct file *
318find_writeable_file_locked(struct nfs4_file *f)
319{
320 struct file *ret;
321
322 lockdep_assert_held(&f->fi_lock);
323
324 ret = __nfs4_get_fd(f, O_WRONLY);
325 if (!ret)
326 ret = __nfs4_get_fd(f, O_RDWR);
327 return ret;
328}
329
330static struct file *
331find_writeable_file(struct nfs4_file *f)
332{
333 struct file *ret;
334
335 spin_lock(&f->fi_lock);
336 ret = find_writeable_file_locked(f);
337 spin_unlock(&f->fi_lock);
338
339 return ret;
340}
341
342static struct file *find_readable_file_locked(struct nfs4_file *f)
343{
344 struct file *ret;
345
346 lockdep_assert_held(&f->fi_lock);
347
348 ret = __nfs4_get_fd(f, O_RDONLY);
349 if (!ret)
350 ret = __nfs4_get_fd(f, O_RDWR);
351 return ret;
352}
353
354static struct file *
355find_readable_file(struct nfs4_file *f)
356{
357 struct file *ret;
358
359 spin_lock(&f->fi_lock);
360 ret = find_readable_file_locked(f);
361 spin_unlock(&f->fi_lock);
362
363 return ret;
364}
365
366static struct file *
367find_any_file(struct nfs4_file *f)
368{
369 struct file *ret;
370
371 spin_lock(&f->fi_lock);
372 ret = __nfs4_get_fd(f, O_RDWR);
373 if (!ret) {
374 ret = __nfs4_get_fd(f, O_WRONLY);
375 if (!ret)
376 ret = __nfs4_get_fd(f, O_RDONLY);
377 }
378 spin_unlock(&f->fi_lock);
379 return ret;
380}
381
Trond Myklebust02a35082014-07-25 07:34:22 -0400382static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800383unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700384
385/*
386 * Open owner state (share locks)
387 */
388
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500389/* hash tables for lock and open owners */
390#define OWNER_HASH_BITS 8
391#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
392#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700393
Trond Myklebustd4f04892014-07-29 21:34:36 -0400394static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400395{
396 unsigned int ret;
397
398 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500399 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400400}
NeilBrownef0f3392006-04-10 22:55:41 -0700401
NeilBrownef0f3392006-04-10 22:55:41 -0700402/* hash table for nfs4_file */
403#define FILE_HASH_BITS 8
404#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800405
Trond Myklebustca943212014-07-23 16:17:39 -0400406static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400407{
Trond Myklebustca943212014-07-23 16:17:39 -0400408 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
409}
410
411static unsigned int file_hashval(struct knfsd_fh *fh)
412{
413 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
414}
415
416static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
417{
418 return fh1->fh_size == fh2->fh_size &&
419 !memcmp(fh1->fh_base.fh_pad,
420 fh2->fh_base.fh_pad,
421 fh1->fh_size);
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400422}
423
Jeff Layton89876f82013-04-02 09:01:59 -0400424static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700425
Jeff Layton12659652014-07-10 14:07:28 -0400426static void
427__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400428{
Jeff Layton7214e862014-07-10 14:07:33 -0400429 lockdep_assert_held(&fp->fi_lock);
430
Jeff Layton12659652014-07-10 14:07:28 -0400431 if (access & NFS4_SHARE_ACCESS_WRITE)
432 atomic_inc(&fp->fi_access[O_WRONLY]);
433 if (access & NFS4_SHARE_ACCESS_READ)
434 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400435}
436
Jeff Layton12659652014-07-10 14:07:28 -0400437static __be32
438nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400439{
Jeff Layton7214e862014-07-10 14:07:33 -0400440 lockdep_assert_held(&fp->fi_lock);
441
Jeff Layton12659652014-07-10 14:07:28 -0400442 /* Does this access mode make sense? */
443 if (access & ~NFS4_SHARE_ACCESS_BOTH)
444 return nfserr_inval;
445
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400446 /* Does it conflict with a deny mode already set? */
447 if ((access & fp->fi_share_deny) != 0)
448 return nfserr_share_denied;
449
Jeff Layton12659652014-07-10 14:07:28 -0400450 __nfs4_file_get_access(fp, access);
451 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400452}
453
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400454static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
455{
456 /* Common case is that there is no deny mode. */
457 if (deny) {
458 /* Does this deny mode make sense? */
459 if (deny & ~NFS4_SHARE_DENY_BOTH)
460 return nfserr_inval;
461
462 if ((deny & NFS4_SHARE_DENY_READ) &&
463 atomic_read(&fp->fi_access[O_RDONLY]))
464 return nfserr_share_denied;
465
466 if ((deny & NFS4_SHARE_DENY_WRITE) &&
467 atomic_read(&fp->fi_access[O_WRONLY]))
468 return nfserr_share_denied;
469 }
470 return nfs_ok;
471}
472
J. Bruce Fields998db522010-08-07 09:21:41 -0400473static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400474{
Trond Myklebustde186432014-07-10 14:07:26 -0400475 might_lock(&fp->fi_lock);
476
477 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
478 struct file *f1 = NULL;
479 struct file *f2 = NULL;
480
Jeff Layton6d338b52014-07-10 14:07:29 -0400481 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400482 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400483 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400484 spin_unlock(&fp->fi_lock);
485 if (f1)
486 fput(f1);
487 if (f2)
488 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400489 }
490}
491
Jeff Layton12659652014-07-10 14:07:28 -0400492static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400493{
Jeff Layton12659652014-07-10 14:07:28 -0400494 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
495
496 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400497 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400498 if (access & NFS4_SHARE_ACCESS_READ)
499 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400500}
501
Trond Myklebust60116952014-07-29 21:34:06 -0400502static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
503 struct kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400504{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500505 struct nfs4_stid *stid;
506 int new_id;
507
Trond Myklebustf8338832014-07-25 07:34:19 -0400508 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500509 if (!stid)
510 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400511
Jeff Layton4770d722014-07-29 21:34:10 -0400512 idr_preload(GFP_KERNEL);
513 spin_lock(&cl->cl_lock);
514 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
515 spin_unlock(&cl->cl_lock);
516 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700517 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500518 goto out_free;
519 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500520 stid->sc_stateid.si_opaque.so_id = new_id;
521 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
522 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400523 atomic_set(&stid->sc_count, 1);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500524
J. Bruce Fields996e0932011-10-17 11:14:48 -0400525 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500526 * It shouldn't be a problem to reuse an opaque stateid value.
527 * I don't think it is for 4.1. But with 4.0 I worry that, for
528 * example, a stray write retransmission could be accepted by
529 * the server when it should have been rejected. Therefore,
530 * adopt a trick from the sctp code to attempt to maximize the
531 * amount of time until an id is reused, by ensuring they always
532 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400533 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500534 return stid;
535out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800536 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500537 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400538}
539
Jeff Laytonb49e0842014-07-29 21:34:11 -0400540static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400541{
Trond Myklebust60116952014-07-29 21:34:06 -0400542 struct nfs4_stid *stid;
543 struct nfs4_ol_stateid *stp;
544
545 stid = nfs4_alloc_stid(clp, stateid_slab);
546 if (!stid)
547 return NULL;
548
549 stp = openlockstateid(stid);
550 stp->st_stid.sc_free = nfs4_free_ol_stateid;
551 return stp;
552}
553
554static void nfs4_free_deleg(struct nfs4_stid *stid)
555{
Trond Myklebust60116952014-07-29 21:34:06 -0400556 kmem_cache_free(deleg_slab, stid);
557 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400558}
559
NeilBrown6282cd52014-06-04 17:39:26 +1000560/*
561 * When we recall a delegation, we should be careful not to hand it
562 * out again straight away.
563 * To ensure this we keep a pair of bloom filters ('new' and 'old')
564 * in which the filehandles of recalled delegations are "stored".
565 * If a filehandle appear in either filter, a delegation is blocked.
566 * When a delegation is recalled, the filehandle is stored in the "new"
567 * filter.
568 * Every 30 seconds we swap the filters and clear the "new" one,
569 * unless both are empty of course.
570 *
571 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
572 * low 3 bytes as hash-table indices.
573 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400574 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000575 * is used to manage concurrent access. Testing does not need the lock
576 * except when swapping the two filters.
577 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400578static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000579static struct bloom_pair {
580 int entries, old_entries;
581 time_t swap_time;
582 int new; /* index into 'set' */
583 DECLARE_BITMAP(set[2], 256);
584} blocked_delegations;
585
586static int delegation_blocked(struct knfsd_fh *fh)
587{
588 u32 hash;
589 struct bloom_pair *bd = &blocked_delegations;
590
591 if (bd->entries == 0)
592 return 0;
593 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400594 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000595 if (seconds_since_boot() - bd->swap_time > 30) {
596 bd->entries -= bd->old_entries;
597 bd->old_entries = bd->entries;
598 memset(bd->set[bd->new], 0,
599 sizeof(bd->set[0]));
600 bd->new = 1-bd->new;
601 bd->swap_time = seconds_since_boot();
602 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400603 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000604 }
605 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
606 if (test_bit(hash&255, bd->set[0]) &&
607 test_bit((hash>>8)&255, bd->set[0]) &&
608 test_bit((hash>>16)&255, bd->set[0]))
609 return 1;
610
611 if (test_bit(hash&255, bd->set[1]) &&
612 test_bit((hash>>8)&255, bd->set[1]) &&
613 test_bit((hash>>16)&255, bd->set[1]))
614 return 1;
615
616 return 0;
617}
618
619static void block_delegations(struct knfsd_fh *fh)
620{
621 u32 hash;
622 struct bloom_pair *bd = &blocked_delegations;
623
624 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
625
Jeff Laytonf54fe962014-07-25 07:34:26 -0400626 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000627 __set_bit(hash&255, bd->set[bd->new]);
628 __set_bit((hash>>8)&255, bd->set[bd->new]);
629 __set_bit((hash>>16)&255, bd->set[bd->new]);
630 if (bd->entries == 0)
631 bd->swap_time = seconds_since_boot();
632 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400633 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000634}
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636static struct nfs4_delegation *
Jeff Laytonf9416e22014-07-25 07:34:23 -0400637alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400640 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400643 n = atomic_long_inc_return(&num_delegations);
644 if (n < 0 || n > max_delegations)
645 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000646 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400647 goto out_dec;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400648 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700649 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400650 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400651
652 dp->dl_stid.sc_free = nfs4_free_deleg;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400653 /*
654 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400655 * meaning of seqid 0 isn't meaningful, really, but let's avoid
656 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400657 */
658 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700659 INIT_LIST_HEAD(&dp->dl_perfile);
660 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 INIT_LIST_HEAD(&dp->dl_recall_lru);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400662 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Jeff Layton02e12152014-07-16 10:31:57 -0400663 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400665out_dec:
666 atomic_long_dec(&num_delegations);
667 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
670void
Trond Myklebust60116952014-07-29 21:34:06 -0400671nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Trond Myklebust11b91642014-07-29 21:34:08 -0400673 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400674 struct nfs4_client *clp = s->sc_client;
675
Jeff Layton4770d722014-07-29 21:34:10 -0400676 might_lock(&clp->cl_lock);
677
Jeff Laytonb401be222014-07-29 21:34:33 -0400678 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
679 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400680 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400681 }
Trond Myklebust60116952014-07-29 21:34:06 -0400682 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400683 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400684 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400685 if (fp)
686 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500689static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Jeff Layton417c6622014-07-21 09:34:57 -0400691 lockdep_assert_held(&state_lock);
692
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -0500693 if (!fp->fi_lease)
694 return;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500695 if (atomic_dec_and_test(&fp->fi_delegees)) {
696 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
697 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400698 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500699 fp->fi_deleg_file = NULL;
700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400703static void unhash_stid(struct nfs4_stid *s)
704{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500705 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400706}
707
Benny Halevy931ee562014-05-30 09:09:27 -0400708static void
709hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
710{
Benny Halevycdc975052014-05-30 09:09:30 -0400711 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400712 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400713
Trond Myklebust67cb1272014-07-29 21:34:17 -0400714 atomic_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400715 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400716 list_add(&dp->dl_perfile, &fp->fi_delegations);
717 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
718}
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720static void
Jeff Layton42690672014-07-25 07:34:20 -0400721unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Trond Myklebust11b91642014-07-29 21:34:08 -0400723 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400724
Jeff Layton42690672014-07-25 07:34:20 -0400725 lockdep_assert_held(&state_lock);
726
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400727 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400728 /* Ensure that deleg break won't try to requeue it */
729 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400730 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400731 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400733 list_del_init(&dp->dl_perfile);
734 spin_unlock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400735 if (fp)
Trond Myklebustf8338832014-07-25 07:34:19 -0400736 nfs4_put_deleg_lease(fp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400737}
738
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400739static void destroy_delegation(struct nfs4_delegation *dp)
740{
Jeff Layton42690672014-07-25 07:34:20 -0400741 spin_lock(&state_lock);
742 unhash_delegation_locked(dp);
743 spin_unlock(&state_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400744 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400745}
746
747static void revoke_delegation(struct nfs4_delegation *dp)
748{
749 struct nfs4_client *clp = dp->dl_stid.sc_client;
750
Jeff Layton2d4a5322014-07-25 07:34:21 -0400751 WARN_ON(!list_empty(&dp->dl_recall_lru));
752
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400753 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400754 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400755 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400756 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400757 spin_lock(&clp->cl_lock);
758 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
759 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400760 }
761}
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763/*
764 * SETCLIENTID state
765 */
766
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400767static unsigned int clientid_hashval(u32 id)
768{
769 return id & CLIENT_HASH_MASK;
770}
771
772static unsigned int clientstr_hashval(const char *name)
773{
774 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
775}
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400778 * We store the NONE, READ, WRITE, and BOTH bits separately in the
779 * st_{access,deny}_bmap field of the stateid, in order to track not
780 * only what share bits are currently in force, but also what
781 * combinations of share bits previous opens have used. This allows us
782 * to enforce the recommendation of rfc 3530 14.2.19 that the server
783 * return an error if the client attempt to downgrade to a combination
784 * of share bits not explicable by closing some of its previous opens.
785 *
786 * XXX: This enforcement is actually incomplete, since we don't keep
787 * track of access/deny bit combinations; so, e.g., we allow:
788 *
789 * OPEN allow read, deny write
790 * OPEN allow both, deny none
791 * DOWNGRADE allow read, deny none
792 *
793 * which we should reject.
794 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400795static unsigned int
796bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400797 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400798 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400799
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400800 for (i = 1; i < 4; i++) {
801 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400802 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400803 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400804 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400805}
806
Jeff Layton82c5ff12012-05-11 09:45:13 -0400807/* set share access for a given stateid */
808static inline void
809set_access(u32 access, struct nfs4_ol_stateid *stp)
810{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400811 unsigned char mask = 1 << access;
812
813 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
814 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400815}
816
817/* clear share access for a given stateid */
818static inline void
819clear_access(u32 access, struct nfs4_ol_stateid *stp)
820{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400821 unsigned char mask = 1 << access;
822
823 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
824 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400825}
826
827/* test whether a given stateid has access */
828static inline bool
829test_access(u32 access, struct nfs4_ol_stateid *stp)
830{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400831 unsigned char mask = 1 << access;
832
833 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400834}
835
Jeff Laytonce0fc432012-05-11 09:45:14 -0400836/* set share deny for a given stateid */
837static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400838set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400839{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400840 unsigned char mask = 1 << deny;
841
842 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
843 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400844}
845
846/* clear share deny for a given stateid */
847static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400848clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400849{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400850 unsigned char mask = 1 << deny;
851
852 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
853 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400854}
855
856/* test whether a given stateid is denying specific access */
857static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400858test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400859{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400860 unsigned char mask = 1 << deny;
861
862 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400863}
864
865static int nfs4_access_to_omode(u32 access)
866{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400867 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400868 case NFS4_SHARE_ACCESS_READ:
869 return O_RDONLY;
870 case NFS4_SHARE_ACCESS_WRITE:
871 return O_WRONLY;
872 case NFS4_SHARE_ACCESS_BOTH:
873 return O_RDWR;
874 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500875 WARN_ON_ONCE(1);
876 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400877}
878
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400879/*
880 * A stateid that had a deny mode associated with it is being released
881 * or downgraded. Recalculate the deny mode on the file.
882 */
883static void
884recalculate_deny_mode(struct nfs4_file *fp)
885{
886 struct nfs4_ol_stateid *stp;
887
888 spin_lock(&fp->fi_lock);
889 fp->fi_share_deny = 0;
890 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
891 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
892 spin_unlock(&fp->fi_lock);
893}
894
895static void
896reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
897{
898 int i;
899 bool change = false;
900
901 for (i = 1; i < 4; i++) {
902 if ((i & deny) != i) {
903 change = true;
904 clear_deny(i, stp);
905 }
906 }
907
908 /* Recalculate per-file deny mode if there was a change */
909 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -0400910 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400911}
912
Jeff Layton82c5ff12012-05-11 09:45:13 -0400913/* release all access and file references for a given stateid */
914static void
915release_all_access(struct nfs4_ol_stateid *stp)
916{
917 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -0400918 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400919
920 if (fp && stp->st_deny_bmap != 0)
921 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400922
923 for (i = 1; i < 4; i++) {
924 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -0400925 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400926 clear_access(i, stp);
927 }
928}
929
Jeff Layton6b180f02014-07-29 21:34:26 -0400930static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
931{
Jeff Laytona819ecc2014-07-29 21:34:38 -0400932 struct nfs4_client *clp = sop->so_client;
933
934 might_lock(&clp->cl_lock);
935
936 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -0400937 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -0400938 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -0400939 spin_unlock(&clp->cl_lock);
Jeff Layton6b180f02014-07-29 21:34:26 -0400940 kfree(sop->so_owner.data);
941 sop->so_ops->so_free(sop);
942}
943
Jeff Layton4ae098d2014-07-29 21:34:43 -0400944static void unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500945{
Trond Myklebust11b91642014-07-29 21:34:08 -0400946 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -0400947
Jeff Layton1c755dc2014-07-29 21:34:12 -0400948 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
949
Trond Myklebust1d31a252014-07-10 14:07:25 -0400950 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500951 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400952 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500953 list_del(&stp->st_perstateowner);
954}
955
Trond Myklebust60116952014-07-29 21:34:06 -0400956static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500957{
Trond Myklebust60116952014-07-29 21:34:06 -0400958 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400959
Trond Myklebust60116952014-07-29 21:34:06 -0400960 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -0400961 if (stp->st_stateowner)
962 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -0400963 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500964}
965
Jeff Laytonb49e0842014-07-29 21:34:11 -0400966static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500967{
Jeff Laytonb49e0842014-07-29 21:34:11 -0400968 struct nfs4_ol_stateid *stp = openlockstateid(stid);
969 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500970 struct file *file;
971
Jeff Laytonb49e0842014-07-29 21:34:11 -0400972 file = find_any_file(stp->st_stid.sc_file);
973 if (file)
974 filp_close(file, (fl_owner_t)lo);
975 nfs4_free_ol_stateid(stid);
976}
977
Jeff Layton2c41beb2014-07-29 21:34:41 -0400978/*
979 * Put the persistent reference to an already unhashed generic stateid, while
980 * holding the cl_lock. If it's the last reference, then put it onto the
981 * reaplist for later destruction.
982 */
983static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
984 struct list_head *reaplist)
985{
986 struct nfs4_stid *s = &stp->st_stid;
987 struct nfs4_client *clp = s->sc_client;
988
989 lockdep_assert_held(&clp->cl_lock);
990
991 WARN_ON_ONCE(!list_empty(&stp->st_locks));
992
993 if (!atomic_dec_and_test(&s->sc_count)) {
994 wake_up_all(&close_wq);
995 return;
996 }
997
998 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
999 list_add(&stp->st_locks, reaplist);
1000}
1001
Jeff Layton3c1c9952014-07-29 21:34:39 -04001002static void unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1003{
1004 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1005
1006 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
1007
1008 list_del_init(&stp->st_locks);
Jeff Layton4ae098d2014-07-29 21:34:43 -04001009 unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001010 unhash_stid(&stp->st_stid);
1011}
1012
Jeff Layton5adfd882014-07-29 21:34:31 -04001013static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001014{
Jeff Layton1c755dc2014-07-29 21:34:12 -04001015 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1016
1017 spin_lock(&oo->oo_owner.so_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001018 unhash_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001019 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04001020 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001021}
1022
Trond Myklebustc58c6612014-07-29 21:34:35 -04001023static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001024{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001025 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001026
Trond Myklebustd4f04892014-07-29 21:34:36 -04001027 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001028
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001029 list_del_init(&lo->lo_owner.so_strhash);
1030}
1031
Jeff Layton2c41beb2014-07-29 21:34:41 -04001032/*
1033 * Free a list of generic stateids that were collected earlier after being
1034 * fully unhashed.
1035 */
1036static void
1037free_ol_stateid_reaplist(struct list_head *reaplist)
1038{
1039 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001040 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001041
1042 might_sleep();
1043
1044 while (!list_empty(reaplist)) {
1045 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1046 st_locks);
1047 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001048 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001049 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001050 if (fp)
1051 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001052 }
1053}
1054
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001055static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001056{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001057 struct nfs4_client *clp = lo->lo_owner.so_client;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001058 struct nfs4_ol_stateid *stp;
1059 struct list_head reaplist;
1060
1061 INIT_LIST_HEAD(&reaplist);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001062
Trond Myklebustd4f04892014-07-29 21:34:36 -04001063 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001064 unhash_lockowner_locked(lo);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001065 while (!list_empty(&lo->lo_owner.so_stateids)) {
1066 stp = list_first_entry(&lo->lo_owner.so_stateids,
1067 struct nfs4_ol_stateid, st_perstateowner);
1068 unhash_lock_stateid(stp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001069 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001070 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001071 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001072 free_ol_stateid_reaplist(&reaplist);
Jeff Layton6b180f02014-07-29 21:34:26 -04001073 nfs4_put_stateowner(&lo->lo_owner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001074}
1075
Jeff Laytond83017f2014-07-29 21:34:42 -04001076static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1077 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001078{
1079 struct nfs4_ol_stateid *stp;
1080
1081 while (!list_empty(&open_stp->st_locks)) {
1082 stp = list_entry(open_stp->st_locks.next,
1083 struct nfs4_ol_stateid, st_locks);
Jeff Laytond83017f2014-07-29 21:34:42 -04001084 unhash_lock_stateid(stp);
1085 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001086 }
1087}
1088
Jeff Laytond83017f2014-07-29 21:34:42 -04001089static void unhash_open_stateid(struct nfs4_ol_stateid *stp,
1090 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001091{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001092 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1093
Jeff Layton4ae098d2014-07-29 21:34:43 -04001094 unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001095 release_open_stateid_locks(stp, reaplist);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001096}
1097
1098static void release_open_stateid(struct nfs4_ol_stateid *stp)
1099{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001100 LIST_HEAD(reaplist);
1101
1102 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04001103 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001104 put_ol_stateid_locked(stp, &reaplist);
1105 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1106 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001107}
1108
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001109static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001110{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001111 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001112
Trond Myklebustd4f04892014-07-29 21:34:36 -04001113 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001114
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001115 list_del_init(&oo->oo_owner.so_strhash);
1116 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001117}
1118
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001119static void release_last_closed_stateid(struct nfs4_openowner *oo)
1120{
Jeff Layton217526e2014-07-30 08:27:11 -04001121 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1122 nfsd_net_id);
1123 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001124
Jeff Layton217526e2014-07-30 08:27:11 -04001125 spin_lock(&nn->client_lock);
1126 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001127 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001128 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001129 oo->oo_last_closed_stid = NULL;
1130 }
Jeff Layton217526e2014-07-30 08:27:11 -04001131 spin_unlock(&nn->client_lock);
1132 if (s)
1133 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001134}
1135
Jeff Layton2c41beb2014-07-29 21:34:41 -04001136static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001137{
1138 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001139 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001140 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001141
Jeff Layton2c41beb2014-07-29 21:34:41 -04001142 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001143
Trond Myklebustd4f04892014-07-29 21:34:36 -04001144 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001145 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001146 while (!list_empty(&oo->oo_owner.so_stateids)) {
1147 stp = list_first_entry(&oo->oo_owner.so_stateids,
1148 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytond83017f2014-07-29 21:34:42 -04001149 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001150 put_ol_stateid_locked(stp, &reaplist);
1151 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001152 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001153 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001154 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001155 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001156}
1157
Marc Eshel5282fd72009-04-03 08:27:52 +03001158static inline int
1159hash_sessionid(struct nfs4_sessionid *sessionid)
1160{
1161 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1162
1163 return sid->sequence % SESSION_HASH_SIZE;
1164}
1165
Trond Myklebust8f199b82012-03-20 15:11:17 -04001166#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001167static inline void
1168dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1169{
1170 u32 *ptr = (u32 *)(&sessionid->data[0]);
1171 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1172}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001173#else
1174static inline void
1175dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1176{
1177}
1178#endif
1179
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001180/*
1181 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1182 * won't be used for replay.
1183 */
1184void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1185{
1186 struct nfs4_stateowner *so = cstate->replay_owner;
1187
1188 if (nfserr == nfserr_replay_me)
1189 return;
1190
1191 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001192 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001193 return;
1194 }
1195 if (!so)
1196 return;
1197 if (so->so_is_open_owner)
1198 release_last_closed_stateid(openowner(so));
1199 so->so_seqid++;
1200 return;
1201}
Marc Eshel5282fd72009-04-03 08:27:52 +03001202
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001203static void
1204gen_sessionid(struct nfsd4_session *ses)
1205{
1206 struct nfs4_client *clp = ses->se_client;
1207 struct nfsd4_sessionid *sid;
1208
1209 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1210 sid->clientid = clp->cl_clientid;
1211 sid->sequence = current_sessionid++;
1212 sid->reserved = 0;
1213}
1214
1215/*
Andy Adamsona649637c72009-08-28 08:45:01 -04001216 * The protocol defines ca_maxresponssize_cached to include the size of
1217 * the rpc header, but all we need to cache is the data starting after
1218 * the end of the initial SEQUENCE operation--the rest we regenerate
1219 * each time. Therefore we can advertise a ca_maxresponssize_cached
1220 * value that is the number of bytes in our cache plus a few additional
1221 * bytes. In order to stay on the safe side, and not promise more than
1222 * we can cache, those additional bytes must be the minimum possible: 24
1223 * bytes of rpc header (xid through accept state, with AUTH_NULL
1224 * verifier), 12 for the compound header (with zero-length tag), and 44
1225 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001226 */
Andy Adamsona649637c72009-08-28 08:45:01 -04001227#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1228
Andy Adamson557ce262009-08-28 08:45:04 -04001229static void
1230free_session_slots(struct nfsd4_session *ses)
1231{
1232 int i;
1233
1234 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1235 kfree(ses->se_slots[i]);
1236}
1237
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001238/*
1239 * We don't actually need to cache the rpc and session headers, so we
1240 * can allocate a little less for each slot:
1241 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001242static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001243{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001244 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001245
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001246 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1247 size = 0;
1248 else
1249 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1250 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001251}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001252
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001253/*
1254 * XXX: If we run out of reserved DRC memory we could (up to a point)
1255 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001256 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001257 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001258static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001259{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001260 u32 slotsize = slot_bytes(ca);
1261 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001262 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001263
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001264 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001265 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1266 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001267 num = min_t(int, num, avail / slotsize);
1268 nfsd_drc_mem_used += num * slotsize;
1269 spin_unlock(&nfsd_drc_lock);
1270
1271 return num;
1272}
1273
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001274static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001275{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001276 int slotsize = slot_bytes(ca);
1277
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001278 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001279 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001280 spin_unlock(&nfsd_drc_lock);
1281}
1282
Kinglong Mee60810e52014-01-01 00:35:47 +08001283static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1284 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001285{
Kinglong Mee60810e52014-01-01 00:35:47 +08001286 int numslots = fattrs->maxreqs;
1287 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001288 struct nfsd4_session *new;
1289 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001290
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001291 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001292 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1293 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001294
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001295 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001296 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001297 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001298 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001299 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001300 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001301 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001302 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001303 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001304
1305 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1306 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1307
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001308 return new;
1309out_free:
1310 while (i--)
1311 kfree(new->se_slots[i]);
1312 kfree(new);
1313 return NULL;
1314}
1315
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001316static void free_conn(struct nfsd4_conn *c)
1317{
1318 svc_xprt_put(c->cn_xprt);
1319 kfree(c);
1320}
1321
1322static void nfsd4_conn_lost(struct svc_xpt_user *u)
1323{
1324 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1325 struct nfs4_client *clp = c->cn_session->se_client;
1326
1327 spin_lock(&clp->cl_lock);
1328 if (!list_empty(&c->cn_persession)) {
1329 list_del(&c->cn_persession);
1330 free_conn(c);
1331 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001332 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001333 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001334}
1335
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001336static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001337{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001338 struct nfsd4_conn *conn;
1339
1340 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1341 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001342 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001343 svc_xprt_get(rqstp->rq_xprt);
1344 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001345 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001346 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1347 return conn;
1348}
1349
J. Bruce Fields328ead22010-09-29 16:11:06 -04001350static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1351{
1352 conn->cn_session = ses;
1353 list_add(&conn->cn_persession, &ses->se_conns);
1354}
1355
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001356static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1357{
1358 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001359
1360 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001361 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001362 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001363}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001364
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001365static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001366{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001367 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001368 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001369}
1370
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001371static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001372{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001373 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001374
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001375 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001376 ret = nfsd4_register_conn(conn);
1377 if (ret)
1378 /* oops; xprt is already down: */
1379 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001380 /* We may have gained or lost a callback channel: */
1381 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001382}
1383
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001384static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001385{
1386 u32 dir = NFS4_CDFC4_FORE;
1387
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001388 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001389 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001390 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001391}
1392
1393/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001394static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001395{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001396 struct nfs4_client *clp = s->se_client;
1397 struct nfsd4_conn *c;
1398
1399 spin_lock(&clp->cl_lock);
1400 while (!list_empty(&s->se_conns)) {
1401 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1402 list_del_init(&c->cn_persession);
1403 spin_unlock(&clp->cl_lock);
1404
1405 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1406 free_conn(c);
1407
1408 spin_lock(&clp->cl_lock);
1409 }
1410 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001411}
1412
J. Bruce Fields1377b692012-09-11 21:42:40 -04001413static void __free_session(struct nfsd4_session *ses)
1414{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001415 free_session_slots(ses);
1416 kfree(ses);
1417}
1418
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001419static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001420{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001421 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001422 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001423 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001424}
Andy Adamson557ce262009-08-28 08:45:04 -04001425
Fengguang Wu135ae822012-11-10 07:20:25 -05001426static 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 -04001427{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001428 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001429 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001430
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001431 new->se_client = clp;
1432 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001433
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001434 INIT_LIST_HEAD(&new->se_conns);
1435
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001436 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001437 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001438 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001439 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001440 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001441 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001442 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001443 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001444 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001445 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001446
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001447 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001448 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001449 /*
1450 * This is a little silly; with sessions there's no real
1451 * use for the callback address. Use the peer address
1452 * as a reasonable default for now, but consider fixing
1453 * the rpc client not to require an address in the
1454 * future:
1455 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001456 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1457 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001458 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001459}
1460
Benny Halevy9089f1b2010-05-12 00:12:26 +03001461/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001462static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001463__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001464{
1465 struct nfsd4_session *elem;
1466 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001467 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001468
Trond Myklebust0a880a22014-07-30 08:27:10 -04001469 lockdep_assert_held(&nn->client_lock);
1470
Marc Eshel5282fd72009-04-03 08:27:52 +03001471 dump_sessionid(__func__, sessionid);
1472 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001473 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001474 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001475 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1476 NFS4_MAX_SESSIONID_LEN)) {
1477 return elem;
1478 }
1479 }
1480
1481 dprintk("%s: session not found\n", __func__);
1482 return NULL;
1483}
1484
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001485static struct nfsd4_session *
1486find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1487 __be32 *ret)
1488{
1489 struct nfsd4_session *session;
1490 __be32 status = nfserr_badsession;
1491
1492 session = __find_in_sessionid_hashtbl(sessionid, net);
1493 if (!session)
1494 goto out;
1495 status = nfsd4_get_session_locked(session);
1496 if (status)
1497 session = NULL;
1498out:
1499 *ret = status;
1500 return session;
1501}
1502
Benny Halevy9089f1b2010-05-12 00:12:26 +03001503/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001504static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001505unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001506{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001507 struct nfs4_client *clp = ses->se_client;
1508 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1509
1510 lockdep_assert_held(&nn->client_lock);
1511
Andy Adamson7116ed62009-04-03 08:27:43 +03001512 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001513 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001514 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001515 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001516}
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1519static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001520STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001522 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001524 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001525 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 return 1;
1527}
1528
1529/*
1530 * XXX Should we use a slab cache ?
1531 * This type of memory management is somewhat inefficient, but we use it
1532 * anyway since SETCLIENTID is not a common operation.
1533 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001534static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
1536 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001537 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001539 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1540 if (clp == NULL)
1541 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001542 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001543 if (clp->cl_name.data == NULL)
1544 goto err_no_name;
1545 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1546 OWNER_HASH_SIZE, GFP_KERNEL);
1547 if (!clp->cl_ownerstr_hashtbl)
1548 goto err_no_hashtbl;
1549 for (i = 0; i < OWNER_HASH_SIZE; i++)
1550 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001551 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001552 INIT_LIST_HEAD(&clp->cl_sessions);
1553 idr_init(&clp->cl_stateids);
1554 atomic_set(&clp->cl_refcount, 0);
1555 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1556 INIT_LIST_HEAD(&clp->cl_idhash);
1557 INIT_LIST_HEAD(&clp->cl_openowners);
1558 INIT_LIST_HEAD(&clp->cl_delegations);
1559 INIT_LIST_HEAD(&clp->cl_lru);
1560 INIT_LIST_HEAD(&clp->cl_callbacks);
1561 INIT_LIST_HEAD(&clp->cl_revoked);
1562 spin_lock_init(&clp->cl_lock);
1563 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001565err_no_hashtbl:
1566 kfree(clp->cl_name.data);
1567err_no_name:
1568 kfree(clp);
1569 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570}
1571
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001572static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573free_client(struct nfs4_client *clp)
1574{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001575 while (!list_empty(&clp->cl_sessions)) {
1576 struct nfsd4_session *ses;
1577 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1578 se_perclnt);
1579 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001580 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1581 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001582 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001583 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001584 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001585 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001587 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 kfree(clp);
1589}
1590
Benny Halevy84d38ac2010-05-12 00:13:16 +03001591/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001592static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001593unhash_client_locked(struct nfs4_client *clp)
1594{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001595 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001596 struct nfsd4_session *ses;
1597
Trond Myklebust0a880a22014-07-30 08:27:10 -04001598 lockdep_assert_held(&nn->client_lock);
1599
Trond Myklebust4beb3452014-07-30 08:27:02 -04001600 /* Mark the client as expired! */
1601 clp->cl_time = 0;
1602 /* Make it invisible */
1603 if (!list_empty(&clp->cl_idhash)) {
1604 list_del_init(&clp->cl_idhash);
1605 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1606 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1607 else
1608 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1609 }
1610 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001611 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001612 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1613 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001614 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001615}
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001618unhash_client(struct nfs4_client *clp)
1619{
1620 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1621
1622 spin_lock(&nn->client_lock);
1623 unhash_client_locked(clp);
1624 spin_unlock(&nn->client_lock);
1625}
1626
Jeff Layton97403d92014-07-30 08:27:12 -04001627static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1628{
1629 if (atomic_read(&clp->cl_refcount))
1630 return nfserr_jukebox;
1631 unhash_client_locked(clp);
1632 return nfs_ok;
1633}
1634
Trond Myklebust4beb3452014-07-30 08:27:02 -04001635static void
1636__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001638 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 struct list_head reaplist;
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04001643 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001644 while (!list_empty(&clp->cl_delegations)) {
1645 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001646 unhash_delegation_locked(dp);
1647 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
Benny Halevycdc975052014-05-30 09:09:30 -04001649 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 while (!list_empty(&reaplist)) {
1651 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001652 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001653 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001655 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001656 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001657 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001658 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001659 }
NeilBrownea1da632005-06-23 22:04:17 -07001660 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001661 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Jeff Laytond3134b12014-07-29 21:34:32 -04001662 atomic_inc(&oo->oo_owner.so_count);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001663 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001665 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001666 if (clp->cl_cb_conn.cb_xprt)
1667 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001668 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
1670
Trond Myklebust4beb3452014-07-30 08:27:02 -04001671static void
1672destroy_client(struct nfs4_client *clp)
1673{
1674 unhash_client(clp);
1675 __destroy_client(clp);
1676}
1677
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001678static void expire_client(struct nfs4_client *clp)
1679{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001680 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001681 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001682 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001683}
1684
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001685static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1686{
1687 memcpy(target->cl_verifier.data, source->data,
1688 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
1690
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001691static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1692{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1694 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1695}
1696
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001697static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001698{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001699 if (source->cr_principal) {
1700 target->cr_principal =
1701 kstrdup(source->cr_principal, GFP_KERNEL);
1702 if (target->cr_principal == NULL)
1703 return -ENOMEM;
1704 } else
1705 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001706 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 target->cr_uid = source->cr_uid;
1708 target->cr_gid = source->cr_gid;
1709 target->cr_group_info = source->cr_group_info;
1710 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001711 target->cr_gss_mech = source->cr_gss_mech;
1712 if (source->cr_gss_mech)
1713 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001714 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715}
1716
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001717static long long
1718compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1719{
1720 long long res;
1721
1722 res = o1->len - o2->len;
1723 if (res)
1724 return res;
1725 return (long long)memcmp(o1->data, o2->data, o1->len);
1726}
1727
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001728static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001729{
NeilBrowna55370a2005-06-23 22:03:52 -07001730 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
1733static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001734same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1735{
1736 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737}
1738
1739static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001740same_clid(clientid_t *cl1, clientid_t *cl2)
1741{
1742 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743}
1744
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001745static bool groups_equal(struct group_info *g1, struct group_info *g2)
1746{
1747 int i;
1748
1749 if (g1->ngroups != g2->ngroups)
1750 return false;
1751 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001752 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001753 return false;
1754 return true;
1755}
1756
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001757/*
1758 * RFC 3530 language requires clid_inuse be returned when the
1759 * "principal" associated with a requests differs from that previously
1760 * used. We use uid, gid's, and gss principal string as our best
1761 * approximation. We also don't want to allow non-gss use of a client
1762 * established using gss: in theory cr_principal should catch that
1763 * change, but in practice cr_principal can be null even in the gss case
1764 * since gssd doesn't always pass down a principal string.
1765 */
1766static bool is_gss_cred(struct svc_cred *cr)
1767{
1768 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1769 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1770}
1771
1772
Vivek Trivedi5559b502012-07-24 21:18:20 +05301773static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001774same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1775{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001776 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001777 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1778 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001779 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1780 return false;
1781 if (cr1->cr_principal == cr2->cr_principal)
1782 return true;
1783 if (!cr1->cr_principal || !cr2->cr_principal)
1784 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301785 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
1787
J. Bruce Fields57266a62013-04-13 14:27:29 -04001788static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1789{
1790 struct svc_cred *cr = &rqstp->rq_cred;
1791 u32 service;
1792
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001793 if (!cr->cr_gss_mech)
1794 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001795 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1796 return service == RPC_GSS_SVC_INTEGRITY ||
1797 service == RPC_GSS_SVC_PRIVACY;
1798}
1799
1800static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1801{
1802 struct svc_cred *cr = &rqstp->rq_cred;
1803
1804 if (!cl->cl_mach_cred)
1805 return true;
1806 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1807 return false;
1808 if (!svc_rqst_integrity_protected(rqstp))
1809 return false;
1810 if (!cr->cr_principal)
1811 return false;
1812 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1813}
1814
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001815static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001816{
1817 static u32 current_clientid = 1;
1818
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001819 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 clp->cl_clientid.cl_id = current_clientid++;
1821}
1822
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001823static void gen_confirm(struct nfs4_client *clp)
1824{
Chuck Leverab4684d2012-03-02 17:13:50 -05001825 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001826 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Jeff Laytonf4199922014-06-17 07:44:11 -04001828 /*
1829 * This is opaque to client, so no need to byte-swap. Use
1830 * __force to keep sparse happy
1831 */
1832 verf[0] = (__force __be32)get_seconds();
1833 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001834 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
Jeff Layton4770d722014-07-29 21:34:10 -04001837static struct nfs4_stid *
1838find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001839{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001840 struct nfs4_stid *ret;
1841
1842 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1843 if (!ret || !ret->sc_type)
1844 return NULL;
1845 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001846}
1847
Jeff Layton4770d722014-07-29 21:34:10 -04001848static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001849find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001850{
1851 struct nfs4_stid *s;
1852
Jeff Layton4770d722014-07-29 21:34:10 -04001853 spin_lock(&cl->cl_lock);
1854 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001855 if (s != NULL) {
1856 if (typemask & s->sc_type)
1857 atomic_inc(&s->sc_count);
1858 else
1859 s = NULL;
1860 }
Jeff Layton4770d722014-07-29 21:34:10 -04001861 spin_unlock(&cl->cl_lock);
1862 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001863}
1864
Jeff Layton2216d442012-11-12 15:00:57 -05001865static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001866 struct svc_rqst *rqstp, nfs4_verifier *verf)
1867{
1868 struct nfs4_client *clp;
1869 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001870 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001871 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001872
1873 clp = alloc_client(name);
1874 if (clp == NULL)
1875 return NULL;
1876
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001877 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1878 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001879 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001880 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001881 }
Jeff Layton02e12152014-07-16 10:31:57 -04001882 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001883 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001884 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001885 copy_verf(clp, verf);
1886 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001887 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001888 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001889 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001890 return clp;
1891}
1892
NeilBrownfd39ca92005-06-23 22:04:03 -07001893static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001894add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1895{
1896 struct rb_node **new = &(root->rb_node), *parent = NULL;
1897 struct nfs4_client *clp;
1898
1899 while (*new) {
1900 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1901 parent = *new;
1902
1903 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1904 new = &((*new)->rb_left);
1905 else
1906 new = &((*new)->rb_right);
1907 }
1908
1909 rb_link_node(&new_clp->cl_namenode, parent, new);
1910 rb_insert_color(&new_clp->cl_namenode, root);
1911}
1912
1913static struct nfs4_client *
1914find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1915{
1916 long long cmp;
1917 struct rb_node *node = root->rb_node;
1918 struct nfs4_client *clp;
1919
1920 while (node) {
1921 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1922 cmp = compare_blob(&clp->cl_name, name);
1923 if (cmp > 0)
1924 node = node->rb_left;
1925 else if (cmp < 0)
1926 node = node->rb_right;
1927 else
1928 return clp;
1929 }
1930 return NULL;
1931}
1932
1933static void
1934add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
1936 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001937 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Trond Myklebust0a880a22014-07-30 08:27:10 -04001939 lockdep_assert_held(&nn->client_lock);
1940
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001941 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001942 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001944 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001945 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946}
1947
NeilBrownfd39ca92005-06-23 22:04:03 -07001948static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949move_to_confirmed(struct nfs4_client *clp)
1950{
1951 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001952 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Trond Myklebust0a880a22014-07-30 08:27:10 -04001954 lockdep_assert_held(&nn->client_lock);
1955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001957 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001958 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001959 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001960 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001961 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962}
1963
1964static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001965find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
1967 struct nfs4_client *clp;
1968 unsigned int idhashval = clientid_hashval(clid->cl_id);
1969
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001970 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001971 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001972 if ((bool)clp->cl_minorversion != sessions)
1973 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001974 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
1978 return NULL;
1979}
1980
1981static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001982find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1983{
1984 struct list_head *tbl = nn->conf_id_hashtbl;
1985
Trond Myklebust0a880a22014-07-30 08:27:10 -04001986 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001987 return find_client_in_id_table(tbl, clid, sessions);
1988}
1989
1990static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001991find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001993 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Trond Myklebust0a880a22014-07-30 08:27:10 -04001995 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001996 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001999static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002000{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002001 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002002}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002003
NeilBrown28ce6052005-06-23 22:03:56 -07002004static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002005find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002006{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002007 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002008 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002009}
2010
2011static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002012find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002013{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002014 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002015 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002016}
2017
NeilBrownfd39ca92005-06-23 22:04:03 -07002018static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002019gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002021 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002022 struct sockaddr *sa = svc_addr(rqstp);
2023 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002024 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Jeff Layton7077ecb2009-08-14 12:57:58 -04002026 /* Currently, we only support tcp and tcp6 for the callback channel */
2027 if (se->se_callback_netid_len == 3 &&
2028 !memcmp(se->se_callback_netid_val, "tcp", 3))
2029 expected_family = AF_INET;
2030 else if (se->se_callback_netid_len == 4 &&
2031 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2032 expected_family = AF_INET6;
2033 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 goto out_err;
2035
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002036 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002037 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002038 (struct sockaddr *)&conn->cb_addr,
2039 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002040
J. Bruce Fields07263f12010-05-31 19:09:40 -04002041 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002043
J. Bruce Fields07263f12010-05-31 19:09:40 -04002044 if (conn->cb_addr.ss_family == AF_INET6)
2045 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002046
J. Bruce Fields07263f12010-05-31 19:09:40 -04002047 conn->cb_prog = se->se_callback_prog;
2048 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002049 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 return;
2051out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002052 conn->cb_addr.ss_family = AF_UNSPEC;
2053 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002054 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 "will not receive delegations\n",
2056 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 return;
2059}
2060
Andy Adamson074fe892009-04-03 08:28:15 +03002061/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002062 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002063 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002064static void
Andy Adamson074fe892009-04-03 08:28:15 +03002065nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2066{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002067 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002068 struct nfsd4_slot *slot = resp->cstate.slot;
2069 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002070
Andy Adamson557ce262009-08-28 08:45:04 -04002071 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002072
Andy Adamson557ce262009-08-28 08:45:04 -04002073 slot->sl_opcnt = resp->opcnt;
2074 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002075
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002076 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002077 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002078 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002079 return;
2080 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002081 base = resp->cstate.data_offset;
2082 slot->sl_datalen = buf->len - base;
2083 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04002084 WARN("%s: sessions DRC could not cache compound\n", __func__);
2085 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002086}
2087
2088/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002089 * Encode the replay sequence operation from the slot values.
2090 * If cachethis is FALSE encode the uncached rep error on the next
2091 * operation which sets resp->p and increments resp->opcnt for
2092 * nfs4svc_encode_compoundres.
2093 *
Andy Adamson074fe892009-04-03 08:28:15 +03002094 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002095static __be32
2096nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2097 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002098{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002099 struct nfsd4_op *op;
2100 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002101
Andy Adamsonabfabf82009-07-23 19:02:18 -04002102 /* Encode the replayed sequence operation */
2103 op = &args->ops[resp->opcnt - 1];
2104 nfsd4_encode_operation(resp, op);
2105
2106 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002107 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002108 op = &args->ops[resp->opcnt++];
2109 op->status = nfserr_retry_uncached_rep;
2110 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002111 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002112 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002113}
2114
2115/*
Andy Adamson557ce262009-08-28 08:45:04 -04002116 * The sequence operation is not cached because we can use the slot and
2117 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002118 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002119static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002120nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2121 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002122{
Andy Adamson557ce262009-08-28 08:45:04 -04002123 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002124 struct xdr_stream *xdr = &resp->xdr;
2125 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002126 __be32 status;
2127
Andy Adamson557ce262009-08-28 08:45:04 -04002128 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002129
Andy Adamsonabfabf82009-07-23 19:02:18 -04002130 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002131 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002132 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002133
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002134 p = xdr_reserve_space(xdr, slot->sl_datalen);
2135 if (!p) {
2136 WARN_ON_ONCE(1);
2137 return nfserr_serverfault;
2138 }
2139 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2140 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002141
Andy Adamson557ce262009-08-28 08:45:04 -04002142 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002143 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002144}
2145
Andy Adamson0733d212009-04-03 08:28:01 +03002146/*
2147 * Set the exchange_id flags returned by the server.
2148 */
2149static void
2150nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2151{
2152 /* pNFS is not supported */
2153 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2154
2155 /* Referrals are supported, Migration is not. */
2156 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2157
2158 /* set the wire flags to return to client. */
2159 clid->flags = new->cl_exchange_flags;
2160}
2161
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002162static bool client_has_state(struct nfs4_client *clp)
2163{
2164 /*
2165 * Note clp->cl_openowners check isn't quite right: there's no
2166 * need to count owners without stateid's.
2167 *
2168 * Also note we should probably be using this in 4.0 case too.
2169 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002170 return !list_empty(&clp->cl_openowners)
2171 || !list_empty(&clp->cl_delegations)
2172 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002173}
2174
Al Virob37ad282006-10-19 23:28:59 -07002175__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002176nfsd4_exchange_id(struct svc_rqst *rqstp,
2177 struct nfsd4_compound_state *cstate,
2178 struct nfsd4_exchange_id *exid)
2179{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002180 struct nfs4_client *conf, *new;
2181 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002182 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002183 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002184 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002185 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002186 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002187 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002188
Jeff Layton363168b2009-08-14 12:57:56 -04002189 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002190 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002191 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002192 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002193 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002194
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002195 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002196 return nfserr_inval;
2197
Andy Adamson0733d212009-04-03 08:28:01 +03002198 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002199 case SP4_MACH_CRED:
2200 if (!svc_rqst_integrity_protected(rqstp))
2201 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002202 case SP4_NONE:
2203 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002204 default: /* checked by xdr code */
2205 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002206 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002207 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002208 }
2209
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002210 new = create_client(exid->clname, rqstp, &verf);
2211 if (new == NULL)
2212 return nfserr_jukebox;
2213
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002214 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03002215 nfs4_lock_state();
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002216 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002217 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002218 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002219 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2220 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2221
J. Bruce Fields136e6582012-05-12 20:37:23 -04002222 if (update) {
2223 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002224 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002225 goto out;
2226 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002227 if (!mach_creds_match(conf, rqstp)) {
2228 status = nfserr_wrong_cred;
2229 goto out;
2230 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002231 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002232 status = nfserr_perm;
2233 goto out;
2234 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002235 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002236 status = nfserr_not_same;
2237 goto out;
2238 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002239 /* case 6 */
2240 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002241 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002242 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002243 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002244 if (client_has_state(conf)) {
2245 status = nfserr_clid_inuse;
2246 goto out;
2247 }
Andy Adamson0733d212009-04-03 08:28:01 +03002248 goto out_new;
2249 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002250 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002251 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002252 goto out_copy;
2253 }
2254 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002255 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002256 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002257 }
2258
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002259 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002260 status = nfserr_noent;
2261 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002262 }
2263
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002264 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002265 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002266 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002267
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002268 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002269out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002270 if (conf) {
2271 status = mark_client_expired_locked(conf);
2272 if (status)
2273 goto out;
2274 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002275 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002276 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002277
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002278 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002279 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002280 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002281out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002282 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2283 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002284
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002285 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2286 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002287
2288 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002289 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002290 status = nfs_ok;
2291
2292out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002293 spin_unlock(&nn->client_lock);
Andy Adamson0733d212009-04-03 08:28:01 +03002294 nfs4_unlock_state();
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002295 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002296 expire_client(new);
2297 if (unconf)
2298 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002299 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002300}
2301
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002302static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002303check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002304{
Andy Adamson88e588d2009-07-23 19:02:15 -04002305 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2306 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002307
2308 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002309 if (slot_inuse) {
2310 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002311 return nfserr_jukebox;
2312 else
2313 return nfserr_seq_misordered;
2314 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002315 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002316 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002317 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002318 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002319 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002320 return nfserr_seq_misordered;
2321}
2322
Andy Adamson49557cc2009-07-23 19:02:16 -04002323/*
2324 * Cache the create session result into the create session single DRC
2325 * slot cache by saving the xdr structure. sl_seqid has been set.
2326 * Do this for solo or embedded create session operations.
2327 */
2328static void
2329nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002330 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002331{
2332 slot->sl_status = nfserr;
2333 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2334}
2335
2336static __be32
2337nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2338 struct nfsd4_clid_slot *slot)
2339{
2340 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2341 return slot->sl_status;
2342}
2343
Mi Jinlong1b74c252011-07-14 14:50:17 +08002344#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2345 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2346 1 + /* MIN tag is length with zero, only length */ \
2347 3 + /* version, opcount, opcode */ \
2348 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2349 /* seqid, slotID, slotID, cache */ \
2350 4 ) * sizeof(__be32))
2351
2352#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2353 2 + /* verifier: AUTH_NULL, length 0 */\
2354 1 + /* status */ \
2355 1 + /* MIN tag is length with zero, only length */ \
2356 3 + /* opcount, opcode, opstatus*/ \
2357 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2358 /* seqid, slotID, slotID, slotID, status */ \
2359 5 ) * sizeof(__be32))
2360
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002361static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002362{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002363 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2364
J. Bruce Fields373cd402013-04-08 15:42:12 -04002365 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2366 return nfserr_toosmall;
2367 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2368 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002369 ca->headerpadsz = 0;
2370 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2371 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2372 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2373 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2374 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2375 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2376 /*
2377 * Note decreasing slot size below client's request may make it
2378 * difficult for client to function correctly, whereas
2379 * decreasing the number of slots will (just?) affect
2380 * performance. When short on memory we therefore prefer to
2381 * decrease number of slots instead of their size. Clients that
2382 * request larger slots than they need will get poor results:
2383 */
2384 ca->maxreqs = nfsd4_get_drc_mem(ca);
2385 if (!ca->maxreqs)
2386 return nfserr_jukebox;
2387
J. Bruce Fields373cd402013-04-08 15:42:12 -04002388 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002389}
2390
Kinglong Mee8a891632013-12-23 18:11:02 +08002391#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2392 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2393#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2394 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2395
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002396static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2397{
2398 ca->headerpadsz = 0;
2399
2400 /*
2401 * These RPC_MAX_HEADER macros are overkill, especially since we
2402 * don't even do gss on the backchannel yet. But this is still
2403 * less than 1k. Tighten up this estimate in the unlikely event
2404 * it turns out to be a problem for some client:
2405 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002406 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002407 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002408 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002409 return nfserr_toosmall;
2410 ca->maxresp_cached = 0;
2411 if (ca->maxops < 2)
2412 return nfserr_toosmall;
2413
2414 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002415}
2416
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002417static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2418{
2419 switch (cbs->flavor) {
2420 case RPC_AUTH_NULL:
2421 case RPC_AUTH_UNIX:
2422 return nfs_ok;
2423 default:
2424 /*
2425 * GSS case: the spec doesn't allow us to return this
2426 * error. But it also doesn't allow us not to support
2427 * GSS.
2428 * I'd rather this fail hard than return some error the
2429 * client might think it can already handle:
2430 */
2431 return nfserr_encr_alg_unsupp;
2432 }
2433}
2434
Andy Adamson069b6ad2009-04-03 08:27:58 +03002435__be32
2436nfsd4_create_session(struct svc_rqst *rqstp,
2437 struct nfsd4_compound_state *cstate,
2438 struct nfsd4_create_session *cr_ses)
2439{
Jeff Layton363168b2009-08-14 12:57:56 -04002440 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002441 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002442 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04002443 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002444 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002445 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002446 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002447 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002448
Mi Jinlonga62573d2011-03-23 17:57:07 +08002449 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2450 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002451 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2452 if (status)
2453 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002454 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002455 if (status)
2456 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002457 status = check_backchannel_attrs(&cr_ses->back_channel);
2458 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002459 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002460 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002461 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002462 if (!new)
2463 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002464 conn = alloc_conn_from_crses(rqstp, cr_ses);
2465 if (!conn)
2466 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002467
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002468 nfs4_lock_state();
Jeff Laytond20c11d2014-07-30 08:27:07 -04002469 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002470 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002471 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002472 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002473
2474 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002475 status = nfserr_wrong_cred;
2476 if (!mach_creds_match(conf, rqstp))
2477 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002478 cs_slot = &conf->cl_cs_slot;
2479 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002480 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002481 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002482 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002483 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002484 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002485 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002486 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002487 } else if (unconf) {
2488 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002489 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002490 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002491 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002492 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002493 status = nfserr_wrong_cred;
2494 if (!mach_creds_match(unconf, rqstp))
2495 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002496 cs_slot = &unconf->cl_cs_slot;
2497 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002498 if (status) {
2499 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002500 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002501 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002502 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002503 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002504 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002505 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002506 if (status) {
2507 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002508 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002509 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002510 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002511 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002512 conf = unconf;
2513 } else {
2514 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002515 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002516 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002517 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002518 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002519 * We do not support RDMA or persistent sessions
2520 */
2521 cr_ses->flags &= ~SESSION4_PERSIST;
2522 cr_ses->flags &= ~SESSION4_RDMA;
2523
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002524 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002525 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002526
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04002527 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002528 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002529 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002530 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002531
Jeff Laytond20c11d2014-07-30 08:27:07 -04002532 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002533 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002534 spin_unlock(&nn->client_lock);
2535 /* init connection and backchannel */
2536 nfsd4_init_conn(rqstp, conn, new);
2537 nfsd4_put_session(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002538 nfs4_unlock_state();
Jeff Laytond20c11d2014-07-30 08:27:07 -04002539 if (old)
2540 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002541 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002542out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002543 spin_unlock(&nn->client_lock);
Yanchuan Nian266533c2012-12-24 18:11:45 +08002544 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002545 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002546 if (old)
2547 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002548out_free_session:
2549 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002550out_release_drc_mem:
2551 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002552 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002553}
2554
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002555static __be32 nfsd4_map_bcts_dir(u32 *dir)
2556{
2557 switch (*dir) {
2558 case NFS4_CDFC4_FORE:
2559 case NFS4_CDFC4_BACK:
2560 return nfs_ok;
2561 case NFS4_CDFC4_FORE_OR_BOTH:
2562 case NFS4_CDFC4_BACK_OR_BOTH:
2563 *dir = NFS4_CDFC4_BOTH;
2564 return nfs_ok;
2565 };
2566 return nfserr_inval;
2567}
2568
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002569__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2570{
2571 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002572 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002573 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002574
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002575 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2576 if (status)
2577 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002578 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002579 session->se_cb_prog = bc->bc_cb_program;
2580 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002581 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002582
2583 nfsd4_probe_callback(session->se_client);
2584
2585 return nfs_ok;
2586}
2587
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002588__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2589 struct nfsd4_compound_state *cstate,
2590 struct nfsd4_bind_conn_to_session *bcts)
2591{
2592 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002593 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002594 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002595 struct net *net = SVC_NET(rqstp);
2596 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002597
2598 if (!nfsd4_last_compound_op(rqstp))
2599 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002600 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002601 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002602 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002603 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002604 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002605 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002606 status = nfserr_wrong_cred;
2607 if (!mach_creds_match(session->se_client, rqstp))
2608 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002609 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002610 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002611 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002612 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002613 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002614 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002615 goto out;
2616 nfsd4_init_conn(rqstp, conn, session);
2617 status = nfs_ok;
2618out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002619 nfsd4_put_session(session);
2620out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002621 nfs4_unlock_state();
2622 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002623}
2624
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002625static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2626{
2627 if (!session)
2628 return 0;
2629 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2630}
2631
Andy Adamson069b6ad2009-04-03 08:27:58 +03002632__be32
2633nfsd4_destroy_session(struct svc_rqst *r,
2634 struct nfsd4_compound_state *cstate,
2635 struct nfsd4_destroy_session *sessionid)
2636{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002637 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002638 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002639 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002640 struct net *net = SVC_NET(r);
2641 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002642
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002643 nfs4_lock_state();
2644 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002645 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002646 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002647 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002648 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002649 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002650 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002651 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002652 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002653 if (!ses)
2654 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002655 status = nfserr_wrong_cred;
2656 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002657 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002658 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002659 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002660 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002661 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002662 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002663
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002664 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002665
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002666 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002667 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002668out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002669 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002670out_client_lock:
2671 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002672out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002673 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002674 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002675}
2676
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002677static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002678{
2679 struct nfsd4_conn *c;
2680
2681 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002682 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002683 return c;
2684 }
2685 }
2686 return NULL;
2687}
2688
J. Bruce Fields57266a62013-04-13 14:27:29 -04002689static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002690{
2691 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002692 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002693 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002694 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002695
2696 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002697 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002698 if (c)
2699 goto out_free;
2700 status = nfserr_conn_not_bound_to_session;
2701 if (clp->cl_mach_cred)
2702 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002703 __nfsd4_hash_conn(new, ses);
2704 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002705 ret = nfsd4_register_conn(new);
2706 if (ret)
2707 /* oops; xprt is already down: */
2708 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002709 return nfs_ok;
2710out_free:
2711 spin_unlock(&clp->cl_lock);
2712 free_conn(new);
2713 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002714}
2715
Mi Jinlong868b89c2011-04-27 09:09:58 +08002716static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2717{
2718 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2719
2720 return args->opcnt > session->se_fchannel.maxops;
2721}
2722
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002723static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2724 struct nfsd4_session *session)
2725{
2726 struct xdr_buf *xb = &rqstp->rq_arg;
2727
2728 return xb->len > session->se_fchannel.maxreq_sz;
2729}
2730
Andy Adamson069b6ad2009-04-03 08:27:58 +03002731__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002732nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002733 struct nfsd4_compound_state *cstate,
2734 struct nfsd4_sequence *seq)
2735{
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002736 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002737 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002738 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002739 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002740 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002741 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002742 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002743 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002744 struct net *net = SVC_NET(rqstp);
2745 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002746
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002747 if (resp->opcnt != 1)
2748 return nfserr_sequence_pos;
2749
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002750 /*
2751 * Will be either used or freed by nfsd4_sequence_check_conn
2752 * below.
2753 */
2754 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2755 if (!conn)
2756 return nfserr_jukebox;
2757
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002758 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002759 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002760 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002761 goto out_no_session;
2762 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002763
Mi Jinlong868b89c2011-04-27 09:09:58 +08002764 status = nfserr_too_many_ops;
2765 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002766 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002767
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002768 status = nfserr_req_too_big;
2769 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002770 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002771
Benny Halevyb85d4c02009-04-03 08:28:08 +03002772 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002773 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002774 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002775
Andy Adamson557ce262009-08-28 08:45:04 -04002776 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002777 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2778
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002779 /* We do not negotiate the number of slots yet, so set the
2780 * maxslots to the session maxreqs which is used to encode
2781 * sr_highest_slotid and the sr_target_slot id to maxslots */
2782 seq->maxslots = session->se_fchannel.maxreqs;
2783
J. Bruce Fields73e79482012-02-13 16:39:00 -05002784 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2785 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002786 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002787 status = nfserr_seq_misordered;
2788 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002789 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002790 cstate->slot = slot;
2791 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002792 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002793 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002794 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002795 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002796 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002797 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002798 }
2799 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002800 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002801
J. Bruce Fields57266a62013-04-13 14:27:29 -04002802 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002803 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002804 if (status)
2805 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002806
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002807 buflen = (seq->cachethis) ?
2808 session->se_fchannel.maxresp_cached :
2809 session->se_fchannel.maxresp_sz;
2810 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2811 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002812 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002813 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002814 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002815
2816 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002817 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002818 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002819 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002820 if (seq->cachethis)
2821 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002822 else
2823 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002824
2825 cstate->slot = slot;
2826 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002827 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002828
Benny Halevyb85d4c02009-04-03 08:28:08 +03002829out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002830 switch (clp->cl_cb_state) {
2831 case NFSD4_CB_DOWN:
2832 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2833 break;
2834 case NFSD4_CB_FAULT:
2835 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2836 break;
2837 default:
2838 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002839 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002840 if (!list_empty(&clp->cl_revoked))
2841 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002842out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002843 if (conn)
2844 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002845 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002846 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002847out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002848 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002849 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002850}
2851
Trond Myklebustb6076642014-06-30 11:48:35 -04002852void
2853nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2854{
2855 struct nfsd4_compound_state *cs = &resp->cstate;
2856
2857 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002858 if (cs->status != nfserr_replay_cache) {
2859 nfsd4_store_cache_entry(resp);
2860 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2861 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002862 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002863 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002864 } else if (cs->clp)
2865 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002866}
2867
Mi Jinlong345c2842011-10-20 17:51:39 +08002868__be32
2869nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2870{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002871 struct nfs4_client *conf, *unconf;
2872 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002873 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002874 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002875
2876 nfs4_lock_state();
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002877 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002878 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002879 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002880 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002881
2882 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002883 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002884 status = nfserr_clientid_busy;
2885 goto out;
2886 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04002887 status = mark_client_expired_locked(conf);
2888 if (status)
2889 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002890 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08002891 } else if (unconf)
2892 clp = unconf;
2893 else {
2894 status = nfserr_stale_clientid;
2895 goto out;
2896 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002897 if (!mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002898 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002899 status = nfserr_wrong_cred;
2900 goto out;
2901 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002902 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002903out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002904 spin_unlock(&nn->client_lock);
Mi Jinlong345c2842011-10-20 17:51:39 +08002905 nfs4_unlock_state();
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002906 if (clp)
2907 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002908 return status;
2909}
2910
Andy Adamson069b6ad2009-04-03 08:27:58 +03002911__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002912nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2913{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002914 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002915
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002916 if (rc->rca_one_fs) {
2917 if (!cstate->current_fh.fh_dentry)
2918 return nfserr_nofilehandle;
2919 /*
2920 * We don't take advantage of the rca_one_fs case.
2921 * That's OK, it's optional, we can safely ignore it.
2922 */
2923 return nfs_ok;
2924 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002925
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002926 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002927 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002928 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2929 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002930 goto out;
2931
2932 status = nfserr_stale_clientid;
2933 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002934 /*
2935 * The following error isn't really legal.
2936 * But we only get here if the client just explicitly
2937 * destroyed the client. Surely it no longer cares what
2938 * error it gets back on an operation for the dead
2939 * client.
2940 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002941 goto out;
2942
2943 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002944 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002945out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002946 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002947 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002948}
2949
2950__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002951nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2952 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002954 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002956 struct nfs4_client *conf, *new;
2957 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002958 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002959 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2960
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002961 new = create_client(clname, rqstp, &clverifier);
2962 if (new == NULL)
2963 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002964 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 nfs4_lock_state();
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002966 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002967 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002968 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002969 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002971 if (clp_used_exchangeid(conf))
2972 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002973 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002974 char addr_str[INET6_ADDRSTRLEN];
2975 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2976 sizeof(addr_str));
2977 dprintk("NFSD: setclientid: string in use by client "
2978 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 goto out;
2980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002982 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002983 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002984 unhash_client_locked(unconf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002985 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002986 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002988 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002989 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002990 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002991 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002992 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2994 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2995 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002996 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 status = nfs_ok;
2998out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002999 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 nfs4_unlock_state();
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003001 if (new)
3002 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003003 if (unconf)
3004 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 return status;
3006}
3007
3008
Al Virob37ad282006-10-19 23:28:59 -07003009__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003010nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3011 struct nfsd4_compound_state *cstate,
3012 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013{
NeilBrown21ab45a2005-06-23 22:04:14 -07003014 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003015 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3017 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003018 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003019 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003021 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07003024
Jeff Laytond20c11d2014-07-30 08:27:07 -04003025 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003026 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003027 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003028 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003029 * We try hard to give out unique clientid's, so if we get an
3030 * attempt to confirm the same clientid with a different cred,
3031 * there's a bug somewhere. Let's charitably assume it's our
3032 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003033 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04003034 status = nfserr_serverfault;
3035 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3036 goto out;
3037 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3038 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003039 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003040 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3041 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003043 else /* case 4: client hasn't noticed we rebooted yet? */
3044 status = nfserr_stale_clientid;
3045 goto out;
3046 }
3047 status = nfs_ok;
3048 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003049 old = unconf;
3050 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003051 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003052 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003053 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3054 if (old) {
3055 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003056 if (status) {
3057 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003058 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003059 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003060 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003061 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003062 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003063 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003064 get_client_locked(conf);
3065 spin_unlock(&nn->client_lock);
3066 nfsd4_probe_callback(conf);
3067 spin_lock(&nn->client_lock);
3068 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003070 spin_unlock(&nn->client_lock);
3071 if (old)
3072 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 nfs4_unlock_state();
3074 return status;
3075}
3076
J. Bruce Fields32513b42011-10-13 16:00:16 -04003077static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003079 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3080}
3081
3082/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003083static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003084{
Trond Myklebustca943212014-07-23 16:17:39 -04003085 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
Trond Myklebust950e0112014-06-30 11:48:31 -04003087 lockdep_assert_held(&state_lock);
3088
J. Bruce Fields32513b42011-10-13 16:00:16 -04003089 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003090 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003091 INIT_LIST_HEAD(&fp->fi_stateids);
3092 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003093 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003094 fp->fi_had_conflict = false;
3095 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003096 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003097 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3098 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04003099 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100}
3101
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003102void
NeilBrowne60d4392005-06-23 22:03:01 -07003103nfsd4_free_slabs(void)
3104{
Christoph Hellwigabf11352014-05-21 07:43:03 -07003105 kmem_cache_destroy(openowner_slab);
3106 kmem_cache_destroy(lockowner_slab);
3107 kmem_cache_destroy(file_slab);
3108 kmem_cache_destroy(stateid_slab);
3109 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003110}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
Bryan Schumaker72083392011-11-01 15:24:59 -04003112int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113nfsd4_init_slabs(void)
3114{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003115 openowner_slab = kmem_cache_create("nfsd4_openowners",
3116 sizeof(struct nfs4_openowner), 0, 0, NULL);
3117 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003118 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003119 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003120 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003121 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003122 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003123 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003124 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003125 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003126 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003127 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003128 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003129 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003130 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003131 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003132 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003133 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003134 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003136
3137out_free_stateid_slab:
3138 kmem_cache_destroy(stateid_slab);
3139out_free_file_slab:
3140 kmem_cache_destroy(file_slab);
3141out_free_lockowner_slab:
3142 kmem_cache_destroy(lockowner_slab);
3143out_free_openowner_slab:
3144 kmem_cache_destroy(openowner_slab);
3145out:
NeilBrowne60d4392005-06-23 22:03:01 -07003146 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3147 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148}
3149
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003150static void init_nfs4_replay(struct nfs4_replay *rp)
3151{
3152 rp->rp_status = nfserr_serverfault;
3153 rp->rp_buflen = 0;
3154 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003155 mutex_init(&rp->rp_mutex);
3156}
3157
3158static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3159 struct nfs4_stateowner *so)
3160{
3161 if (!nfsd4_has_session(cstate)) {
3162 mutex_lock(&so->so_replay.rp_mutex);
3163 cstate->replay_owner = so;
3164 atomic_inc(&so->so_count);
3165 }
3166}
3167
3168void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3169{
3170 struct nfs4_stateowner *so = cstate->replay_owner;
3171
3172 if (so != NULL) {
3173 cstate->replay_owner = NULL;
3174 mutex_unlock(&so->so_replay.rp_mutex);
3175 nfs4_put_stateowner(so);
3176 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003177}
3178
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003179static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180{
3181 struct nfs4_stateowner *sop;
3182
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003183 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003184 if (!sop)
3185 return NULL;
3186
3187 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3188 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003189 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003190 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003192 sop->so_owner.len = owner->len;
3193
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003194 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003195 sop->so_client = clp;
3196 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003197 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003198 return sop;
3199}
3200
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003201static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003202{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003203 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003204
Trond Myklebustd4f04892014-07-29 21:34:36 -04003205 list_add(&oo->oo_owner.so_strhash,
3206 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003207 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208}
3209
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003210static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3211{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003212 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003213}
3214
Jeff Layton6b180f02014-07-29 21:34:26 -04003215static void nfs4_free_openowner(struct nfs4_stateowner *so)
3216{
3217 struct nfs4_openowner *oo = openowner(so);
3218
3219 kmem_cache_free(openowner_slab, oo);
3220}
3221
3222static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003223 .so_unhash = nfs4_unhash_openowner,
3224 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003225};
3226
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003227static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003228alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003229 struct nfsd4_compound_state *cstate)
3230{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003231 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003232 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003234 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3235 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003237 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003238 oo->oo_owner.so_is_open_owner = 1;
3239 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003240 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003241 if (nfsd4_has_session(cstate))
3242 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003243 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003244 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003245 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003246 spin_lock(&clp->cl_lock);
3247 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003248 if (ret == NULL) {
3249 hash_openowner(oo, clp, strhashval);
3250 ret = oo;
3251 } else
3252 nfs4_free_openowner(&oo->oo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003253 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003254 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255}
3256
J. Bruce Fields996e0932011-10-17 11:14:48 -04003257static 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 -04003258 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003260 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003261 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003262 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003263 stp->st_stateowner = &oo->oo_owner;
Jeff Laytond3134b12014-07-29 21:34:32 -04003264 atomic_inc(&stp->st_stateowner->so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07003265 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003266 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 stp->st_access_bmap = 0;
3268 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003269 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003270 spin_lock(&oo->oo_owner.so_client->cl_lock);
3271 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003272 spin_lock(&fp->fi_lock);
3273 list_add(&stp->st_perfile, &fp->fi_stateids);
3274 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003275 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276}
3277
Jeff Laytond3134b12014-07-29 21:34:32 -04003278/*
3279 * In the 4.0 case we need to keep the owners around a little while to handle
3280 * CLOSE replay. We still do need to release any file access that is held by
3281 * them before returning however.
3282 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003284move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285{
Jeff Layton217526e2014-07-30 08:27:11 -04003286 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003287 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3288 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3289 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003290
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003291 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
Jeff Laytonb401be222014-07-29 21:34:33 -04003293 /*
3294 * We know that we hold one reference via nfsd4_close, and another
3295 * "persistent" reference for the client. If the refcount is higher
3296 * than 2, then there are still calls in progress that are using this
3297 * stateid. We can't put the sc_file reference until they are finished.
3298 * Wait for the refcount to drop to 2. Since it has been unhashed,
3299 * there should be no danger of the refcount going back up again at
3300 * this point.
3301 */
3302 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3303
Jeff Laytond3134b12014-07-29 21:34:32 -04003304 release_all_access(s);
3305 if (s->st_stid.sc_file) {
3306 put_nfs4_file(s->st_stid.sc_file);
3307 s->st_stid.sc_file = NULL;
3308 }
Jeff Layton217526e2014-07-30 08:27:11 -04003309
3310 spin_lock(&nn->client_lock);
3311 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003312 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003313 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003314 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003315 spin_unlock(&nn->client_lock);
3316 if (last)
3317 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318}
3319
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320/* search file_hashtbl[] for file */
3321static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003322find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323{
Trond Myklebustca943212014-07-23 16:17:39 -04003324 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 struct nfs4_file *fp;
3326
Trond Myklebust950e0112014-06-30 11:48:31 -04003327 lockdep_assert_held(&state_lock);
3328
Jeff Layton89876f82013-04-02 09:01:59 -04003329 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003330 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003331 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 }
3335 return NULL;
3336}
3337
Trond Myklebust950e0112014-06-30 11:48:31 -04003338static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003339find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003340{
3341 struct nfs4_file *fp;
3342
3343 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003344 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003345 spin_unlock(&state_lock);
3346 return fp;
3347}
3348
3349static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003350find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003351{
3352 struct nfs4_file *fp;
3353
3354 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003355 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003356 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003357 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003358 fp = new;
3359 }
3360 spin_unlock(&state_lock);
3361
3362 return fp;
3363}
3364
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003365/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 * Called to check deny when READ with all zero stateid or
3367 * WRITE with all zero or all one stateid
3368 */
Al Virob37ad282006-10-19 23:28:59 -07003369static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3371{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003373 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
Trond Myklebustca943212014-07-23 16:17:39 -04003375 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003376 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003377 return ret;
3378 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003379 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003380 if (fp->fi_share_deny & deny_type)
3381 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003382 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003383 put_nfs4_file(fp);
3384 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385}
3386
Jeff Layton02e12152014-07-16 10:31:57 -04003387void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388{
Trond Myklebust11b91642014-07-29 21:34:08 -04003389 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3390 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003391
Trond Myklebust11b91642014-07-29 21:34:08 -04003392 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003393
Jeff Layton02e12152014-07-16 10:31:57 -04003394 /*
3395 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003396 * already holding inode->i_lock.
3397 *
Jeff Laytondff13992014-07-08 14:02:49 -04003398 * If the dl_time != 0, then we know that it has already been
3399 * queued for a lease break. Don't queue it again.
3400 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003401 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003402 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003403 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003404 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003405 }
Jeff Layton02e12152014-07-16 10:31:57 -04003406 spin_unlock(&state_lock);
3407}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
Jeff Layton02e12152014-07-16 10:31:57 -04003409static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3410{
3411 /*
3412 * We're assuming the state code never drops its reference
3413 * without first removing the lease. Since we're in this lease
3414 * callback (and since the lease code is serialized by the kernel
3415 * lock) we know the server hasn't removed the lease yet, we know
3416 * it's safe to take a reference.
3417 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003418 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003419 nfsd4_cb_recall(dp);
3420}
3421
Jeff Layton1c8c6012013-06-21 08:58:15 -04003422/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003423static void nfsd_break_deleg_cb(struct file_lock *fl)
3424{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003425 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3426 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003427
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003428 if (!fp) {
3429 WARN(1, "(%p)->fl_owner NULL\n", fl);
3430 return;
3431 }
3432 if (fp->fi_had_conflict) {
3433 WARN(1, "duplicate break on %p\n", fp);
3434 return;
3435 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003436 /*
3437 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003438 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003439 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003440 */
3441 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
Jeff Layton02e12152014-07-16 10:31:57 -04003443 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003444 fp->fi_had_conflict = true;
3445 /*
3446 * If there are no delegations on the list, then we can't count on this
3447 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3448 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3449 * true should keep any new delegations from being hashed.
3450 */
3451 if (list_empty(&fp->fi_delegations))
3452 fl->fl_break_time = jiffies;
3453 else
3454 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3455 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003456 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457}
3458
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459static
3460int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3461{
3462 if (arg & F_UNLCK)
3463 return lease_modify(onlist, arg);
3464 else
3465 return -EAGAIN;
3466}
3467
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003468static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003469 .lm_break = nfsd_break_deleg_cb,
3470 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471};
3472
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003473static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3474{
3475 if (nfsd4_has_session(cstate))
3476 return nfs_ok;
3477 if (seqid == so->so_seqid - 1)
3478 return nfserr_replay_me;
3479 if (seqid == so->so_seqid)
3480 return nfs_ok;
3481 return nfserr_bad_seqid;
3482}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
Jeff Layton4b24ca72014-06-30 11:48:44 -04003484static __be32 lookup_clientid(clientid_t *clid,
3485 struct nfsd4_compound_state *cstate,
3486 struct nfsd_net *nn)
3487{
3488 struct nfs4_client *found;
3489
3490 if (cstate->clp) {
3491 found = cstate->clp;
3492 if (!same_clid(&found->cl_clientid, clid))
3493 return nfserr_stale_clientid;
3494 return nfs_ok;
3495 }
3496
3497 if (STALE_CLIENTID(clid, nn))
3498 return nfserr_stale_clientid;
3499
3500 /*
3501 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3502 * cached already then we know this is for is for v4.0 and "sessions"
3503 * will be false.
3504 */
3505 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003506 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003507 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003508 if (!found) {
3509 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003510 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003511 }
3512 atomic_inc(&found->cl_refcount);
3513 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003514
3515 /* Cache the nfs4_client in cstate! */
3516 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003517 return nfs_ok;
3518}
3519
Al Virob37ad282006-10-19 23:28:59 -07003520__be32
Andy Adamson66689582009-04-03 08:28:45 +03003521nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003522 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 clientid_t *clientid = &open->op_clientid;
3525 struct nfs4_client *clp = NULL;
3526 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003527 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003528 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003530 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003532 /*
3533 * In case we need it later, after we've already created the
3534 * file and don't want to risk a further failure:
3535 */
3536 open->op_file = nfsd4_alloc_file();
3537 if (open->op_file == NULL)
3538 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
Trond Myklebust2d91e892014-06-30 11:48:46 -04003540 status = lookup_clientid(clientid, cstate, nn);
3541 if (status)
3542 return status;
3543 clp = cstate->clp;
3544
Trond Myklebustd4f04892014-07-29 21:34:36 -04003545 strhashval = ownerstr_hashval(&open->op_owner);
3546 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003547 open->op_openowner = oo;
3548 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003549 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003551 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003552 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003553 release_openowner(oo);
3554 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003555 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003556 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003557 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3558 if (status)
3559 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003560 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003561new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003562 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003563 if (oo == NULL)
3564 return nfserr_jukebox;
3565 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003566alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003567 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003568 if (!open->op_stp)
3569 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003570 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571}
3572
Al Virob37ad282006-10-19 23:28:59 -07003573static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003574nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3575{
3576 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3577 return nfserr_openmode;
3578 else
3579 return nfs_ok;
3580}
3581
Daniel Mackc47d8322011-05-16 16:38:14 +02003582static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003583{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003584 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3585}
3586
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003587static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003588{
3589 struct nfs4_stid *ret;
3590
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003591 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003592 if (!ret)
3593 return NULL;
3594 return delegstateid(ret);
3595}
3596
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003597static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3598{
3599 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3600 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3601}
3602
Al Virob37ad282006-10-19 23:28:59 -07003603static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003604nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003605 struct nfs4_delegation **dp)
3606{
3607 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003608 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003609 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003610
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003611 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3612 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003613 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003614 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003615 status = nfs4_check_delegmode(deleg, flags);
3616 if (status) {
3617 nfs4_put_stid(&deleg->dl_stid);
3618 goto out;
3619 }
3620 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003621out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003622 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003623 return nfs_ok;
3624 if (status)
3625 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003626 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003627 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003628}
3629
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003630static struct nfs4_ol_stateid *
3631nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003633 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003634 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635
Trond Myklebust1d31a252014-07-10 14:07:25 -04003636 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003637 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 /* ignore lock owners */
3639 if (local->st_stateowner->so_is_open_owner == 0)
3640 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003641 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003642 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003643 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003644 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003647 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003648 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649}
3650
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003651static inline int nfs4_access_to_access(u32 nfs4_access)
3652{
3653 int flags = 0;
3654
3655 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3656 flags |= NFSD_MAY_READ;
3657 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3658 flags |= NFSD_MAY_WRITE;
3659 return flags;
3660}
3661
Al Virob37ad282006-10-19 23:28:59 -07003662static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3664 struct nfsd4_open *open)
3665{
3666 struct iattr iattr = {
3667 .ia_valid = ATTR_SIZE,
3668 .ia_size = 0,
3669 };
3670 if (!open->op_truncate)
3671 return 0;
3672 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003673 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3675}
3676
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003677static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003678 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3679 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003680{
Trond Myklebustde186432014-07-10 14:07:26 -04003681 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003682 __be32 status;
3683 int oflag = nfs4_access_to_omode(open->op_share_access);
3684 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003685 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003686
Trond Myklebustde186432014-07-10 14:07:26 -04003687 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003688
3689 /*
3690 * Are we trying to set a deny mode that would conflict with
3691 * current access?
3692 */
3693 status = nfs4_file_check_deny(fp, open->op_share_deny);
3694 if (status != nfs_ok) {
3695 spin_unlock(&fp->fi_lock);
3696 goto out;
3697 }
3698
3699 /* set access to the file */
3700 status = nfs4_file_get_access(fp, open->op_share_access);
3701 if (status != nfs_ok) {
3702 spin_unlock(&fp->fi_lock);
3703 goto out;
3704 }
3705
3706 /* Set access bits in stateid */
3707 old_access_bmap = stp->st_access_bmap;
3708 set_access(open->op_share_access, stp);
3709
3710 /* Set new deny mask */
3711 old_deny_bmap = stp->st_deny_bmap;
3712 set_deny(open->op_share_deny, stp);
3713 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3714
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003715 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003716 spin_unlock(&fp->fi_lock);
3717 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003718 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003719 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003720 spin_lock(&fp->fi_lock);
3721 if (!fp->fi_fds[oflag]) {
3722 fp->fi_fds[oflag] = filp;
3723 filp = NULL;
3724 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003725 }
Trond Myklebustde186432014-07-10 14:07:26 -04003726 spin_unlock(&fp->fi_lock);
3727 if (filp)
3728 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003729
3730 status = nfsd4_truncate(rqstp, cur_fh, open);
3731 if (status)
3732 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003733out:
3734 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003735out_put_access:
3736 stp->st_access_bmap = old_access_bmap;
3737 nfs4_file_put_access(fp, open->op_share_access);
3738 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3739 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003740}
3741
Al Virob37ad282006-10-19 23:28:59 -07003742static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003743nfs4_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 -07003744{
Al Virob37ad282006-10-19 23:28:59 -07003745 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003746 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003748 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003749 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003750
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003751 /* test and set deny mode */
3752 spin_lock(&fp->fi_lock);
3753 status = nfs4_file_check_deny(fp, open->op_share_deny);
3754 if (status == nfs_ok) {
3755 old_deny_bmap = stp->st_deny_bmap;
3756 set_deny(open->op_share_deny, stp);
3757 fp->fi_share_deny |=
3758 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3759 }
3760 spin_unlock(&fp->fi_lock);
3761
3762 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003765 status = nfsd4_truncate(rqstp, cur_fh, open);
3766 if (status != nfs_ok)
3767 reset_union_bmap_deny(old_deny_bmap, stp);
3768 return status;
3769}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003772nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003774 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775}
3776
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003777/* Should we give out recallable state?: */
3778static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3779{
3780 if (clp->cl_cb_state == NFSD4_CB_UP)
3781 return true;
3782 /*
3783 * In the sessions case, since we don't have to establish a
3784 * separate connection for callbacks, we assume it's OK
3785 * until we hear otherwise:
3786 */
3787 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3788}
3789
Jeff Laytond564fbe2014-07-16 10:31:58 -04003790static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003791{
3792 struct file_lock *fl;
3793
3794 fl = locks_alloc_lock();
3795 if (!fl)
3796 return NULL;
3797 locks_init_lock(fl);
3798 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003799 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003800 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3801 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003802 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003803 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003804 return fl;
3805}
3806
J. Bruce Fields99c41512013-05-21 16:21:25 -04003807static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003808{
Trond Myklebust11b91642014-07-29 21:34:08 -04003809 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003810 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003811 struct file *filp;
3812 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003813
Jeff Laytond564fbe2014-07-16 10:31:58 -04003814 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003815 if (!fl)
3816 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003817 filp = find_readable_file(fp);
3818 if (!filp) {
3819 /* We should always have a readable file here */
3820 WARN_ON_ONCE(1);
3821 return -EBADF;
3822 }
3823 fl->fl_file = filp;
3824 status = vfs_setlease(filp, fl->fl_type, &fl);
3825 if (status) {
3826 locks_free_lock(fl);
3827 goto out_fput;
3828 }
Benny Halevycdc975052014-05-30 09:09:30 -04003829 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003830 spin_lock(&fp->fi_lock);
3831 /* Did the lease get broken before we took the lock? */
3832 status = -EAGAIN;
3833 if (fp->fi_had_conflict)
3834 goto out_unlock;
3835 /* Race breaker */
3836 if (fp->fi_lease) {
3837 status = 0;
3838 atomic_inc(&fp->fi_delegees);
3839 hash_delegation_locked(dp, fp);
3840 goto out_unlock;
3841 }
3842 fp->fi_lease = fl;
3843 fp->fi_deleg_file = filp;
3844 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003845 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003846 spin_unlock(&fp->fi_lock);
Benny Halevycdc975052014-05-30 09:09:30 -04003847 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003848 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003849out_unlock:
3850 spin_unlock(&fp->fi_lock);
3851 spin_unlock(&state_lock);
3852out_fput:
3853 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003854 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003855}
3856
Jeff Layton0b266932014-07-25 07:34:25 -04003857static struct nfs4_delegation *
3858nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3859 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003860{
Jeff Layton0b266932014-07-25 07:34:25 -04003861 int status;
3862 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003863
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003864 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003865 return ERR_PTR(-EAGAIN);
3866
3867 dp = alloc_init_deleg(clp, fh);
3868 if (!dp)
3869 return ERR_PTR(-ENOMEM);
3870
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003871 get_nfs4_file(fp);
Benny Halevycdc975052014-05-30 09:09:30 -04003872 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003873 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003874 dp->dl_stid.sc_file = fp;
Jeff Layton417c6622014-07-21 09:34:57 -04003875 if (!fp->fi_lease) {
3876 spin_unlock(&fp->fi_lock);
3877 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003878 status = nfs4_setlease(dp);
3879 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003880 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003881 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003882 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003883 status = -EAGAIN;
3884 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003885 }
Benny Halevy931ee562014-05-30 09:09:27 -04003886 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003887 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003888out_unlock:
3889 spin_unlock(&fp->fi_lock);
Benny Halevycdc975052014-05-30 09:09:30 -04003890 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003891out:
3892 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003893 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003894 return ERR_PTR(status);
3895 }
3896 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003897}
3898
Benny Halevy4aa89132012-02-21 14:16:44 -08003899static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3900{
3901 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3902 if (status == -EAGAIN)
3903 open->op_why_no_deleg = WND4_CONTENTION;
3904 else {
3905 open->op_why_no_deleg = WND4_RESOURCE;
3906 switch (open->op_deleg_want) {
3907 case NFS4_SHARE_WANT_READ_DELEG:
3908 case NFS4_SHARE_WANT_WRITE_DELEG:
3909 case NFS4_SHARE_WANT_ANY_DELEG:
3910 break;
3911 case NFS4_SHARE_WANT_CANCEL:
3912 open->op_why_no_deleg = WND4_CANCELLED;
3913 break;
3914 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003915 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003916 }
3917 }
3918}
3919
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920/*
3921 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003922 *
3923 * Note we don't support write delegations, and won't until the vfs has
3924 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 */
3926static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003927nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3928 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929{
3930 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003931 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3932 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003933 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003934 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003936 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003937 open->op_recall = 0;
3938 switch (open->op_claim_type) {
3939 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003940 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003941 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003942 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3943 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003944 break;
3945 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003946 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003947 /*
3948 * Let's not give out any delegations till everyone's
3949 * had the chance to reclaim theirs....
3950 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003951 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003952 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003953 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003954 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003955 /*
3956 * Also, if the file was opened for write or
3957 * create, there's a good chance the client's
3958 * about to write to it, resulting in an
3959 * immediate recall (since we don't support
3960 * write delegations):
3961 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003962 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003963 goto out_no_deleg;
3964 if (open->op_create == NFS4_OPEN_CREATE)
3965 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003966 break;
3967 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003968 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003969 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003970 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003971 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003972 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003974 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003976 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003977 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003978 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003979 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003980 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003981out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003982 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3983 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003984 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003985 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003986 open->op_recall = 1;
3987 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003988
3989 /* 4.1 client asking for a delegation? */
3990 if (open->op_deleg_want)
3991 nfsd4_open_deleg_none_ext(open, status);
3992 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993}
3994
Benny Halevye27f49c2012-02-21 14:16:54 -08003995static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3996 struct nfs4_delegation *dp)
3997{
3998 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3999 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4000 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4001 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4002 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4003 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4004 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4005 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4006 }
4007 /* Otherwise the client must be confused wanting a delegation
4008 * it already has, therefore we don't return
4009 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4010 */
4011}
4012
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013/*
4014 * called with nfs4_lock_state() held.
4015 */
Al Virob37ad282006-10-19 23:28:59 -07004016__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4018{
Andy Adamson66689582009-04-03 08:28:45 +03004019 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004020 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004022 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004023 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004024 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 /*
4027 * Lookup file; if found, lookup stateid and check open request,
4028 * and check for delegations in the process of being recalled.
4029 * If not found, create the nfs4_file struct
4030 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004031 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004032 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004033 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004034 if (status)
4035 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04004036 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004038 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004039 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004040 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004041 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004042 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 }
4044
4045 /*
4046 * OPEN the file, or upgrade an existing OPEN.
4047 * If truncate fails, the OPEN fails.
4048 */
4049 if (stp) {
4050 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004051 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 if (status)
4053 goto out;
4054 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004055 stp = open->op_stp;
4056 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04004057 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004058 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4059 if (status) {
4060 release_open_stateid(stp);
4061 goto out;
4062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004064 update_stateid(&stp->st_stid.sc_stateid);
4065 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066
Benny Halevyd24433c2012-02-16 20:57:17 +02004067 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004068 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4069 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4070 open->op_why_no_deleg = WND4_NOT_WANTED;
4071 goto nodeleg;
4072 }
4073 }
4074
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 /*
4076 * Attempt to hand out a delegation. No error return, because the
4077 * OPEN succeeds even if we fail.
4078 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004079 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004080nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 status = nfs_ok;
4082
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004083 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004084 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004086 /* 4.1 client trying to upgrade/downgrade delegation? */
4087 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004088 open->op_deleg_want)
4089 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004090
NeilBrown13cd2182005-06-23 22:03:10 -07004091 if (fp)
4092 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004093 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05004094 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 /*
4096 * To finish the open response, we just need to set the rflags.
4097 */
4098 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004099 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004100 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004102 if (dp)
4103 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004104 if (stp)
4105 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106
4107 return status;
4108}
4109
Jeff Layton58fb12e2014-07-29 21:34:27 -04004110void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4111 struct nfsd4_open *open, __be32 status)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004112{
4113 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004114 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004115
Jeff Laytond3134b12014-07-29 21:34:32 -04004116 nfsd4_cstate_assign_replay(cstate, so);
4117 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004118 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004119 if (open->op_file)
4120 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004121 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004122 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004123}
4124
Al Virob37ad282006-10-19 23:28:59 -07004125__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004126nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4127 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128{
4129 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004130 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004131 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132
4133 nfs4_lock_state();
4134 dprintk("process_renew(%08x/%08x): starting\n",
4135 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004136 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004137 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004139 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004141 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004142 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 goto out;
4144 status = nfs_ok;
4145out:
4146 nfs4_unlock_state();
4147 return status;
4148}
4149
NeilBrowna76b4312005-06-23 22:04:01 -07004150static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004151nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004152{
Jeff Layton33dcc482012-04-10 11:08:48 -04004153 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004154 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004155 return;
4156
NeilBrowna76b4312005-06-23 22:04:01 -07004157 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004158 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004159 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004160 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004161 /*
4162 * Now that every NFSv4 client has had the chance to recover and
4163 * to see the (possibly new, possibly shorter) lease time, we
4164 * can safely set the next grace time to the current lease time:
4165 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03004166 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07004167}
4168
NeilBrownfd39ca92005-06-23 22:04:03 -07004169static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004170nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171{
4172 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004173 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004175 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004177 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004178 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179
4180 nfs4_lock_state();
4181
4182 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004183 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004184 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004185 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004186 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 clp = list_entry(pos, struct nfs4_client, cl_lru);
4188 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4189 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004190 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 break;
4192 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004193 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004194 dprintk("NFSD: client in use (clientid %08x)\n",
4195 clp->cl_clientid.cl_id);
4196 continue;
4197 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004198 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004199 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004200 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004201 list_for_each_safe(pos, next, &reaplist) {
4202 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 dprintk("NFSD: purging unused client (clientid %08x)\n",
4204 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004205 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 expire_client(clp);
4207 }
Benny Halevycdc975052014-05-30 09:09:30 -04004208 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004209 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004211 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4212 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004214 t = dp->dl_time - cutoff;
4215 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 break;
4217 }
Jeff Layton42690672014-07-25 07:34:20 -04004218 unhash_delegation_locked(dp);
4219 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 }
Benny Halevycdc975052014-05-30 09:09:30 -04004221 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004222 while (!list_empty(&reaplist)) {
4223 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4224 dl_recall_lru);
4225 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004226 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 }
Jeff Layton217526e2014-07-30 08:27:11 -04004228
4229 spin_lock(&nn->client_lock);
4230 while (!list_empty(&nn->close_lru)) {
4231 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4232 oo_close_lru);
4233 if (time_after((unsigned long)oo->oo_time,
4234 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004235 t = oo->oo_time - cutoff;
4236 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 break;
4238 }
Jeff Layton217526e2014-07-30 08:27:11 -04004239 list_del_init(&oo->oo_close_lru);
4240 stp = oo->oo_last_closed_stid;
4241 oo->oo_last_closed_stid = NULL;
4242 spin_unlock(&nn->client_lock);
4243 nfs4_put_stid(&stp->st_stid);
4244 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 }
Jeff Layton217526e2014-07-30 08:27:11 -04004246 spin_unlock(&nn->client_lock);
4247
Jeff Laytona832e7a2014-05-30 09:09:26 -04004248 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04004250 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251}
4252
Harvey Harrisona254b242008-02-20 12:49:00 -08004253static struct workqueue_struct *laundry_wq;
4254static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004255
4256static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004257laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258{
4259 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004260 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4261 work);
4262 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4263 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004265 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004267 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268}
4269
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004270static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004271{
Trond Myklebust11b91642014-07-29 21:34:08 -04004272 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004273 return nfserr_bad_stateid;
4274 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275}
4276
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004278access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004280 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4281 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4282 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283}
4284
4285static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004286access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004288 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4289 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290}
4291
4292static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004293__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294{
Al Virob37ad282006-10-19 23:28:59 -07004295 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296
J. Bruce Fields02921912010-07-29 15:16:59 -04004297 /* For lock stateid's, we test the parent open, not the lock: */
4298 if (stp->st_openstp)
4299 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004300 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004302 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 goto out;
4304 status = nfs_ok;
4305out:
4306 return status;
4307}
4308
Al Virob37ad282006-10-19 23:28:59 -07004309static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004310check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004312 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004314 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004315 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 * conflicting state; so we must wait out the grace period. */
4317 return nfserr_grace;
4318 } else if (flags & WR_STATE)
4319 return nfs4_share_conflict(current_fh,
4320 NFS4_SHARE_DENY_WRITE);
4321 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4322 return nfs4_share_conflict(current_fh,
4323 NFS4_SHARE_DENY_READ);
4324}
4325
4326/*
4327 * Allow READ/WRITE during grace period on recovered state only for files
4328 * that are not able to provide mandatory locking.
4329 */
4330static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004331grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004333 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334}
4335
J. Bruce Fields81b82962011-08-23 11:03:29 -04004336/* Returns true iff a is later than b: */
4337static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4338{
Jim Rees1a9357f2013-05-17 17:33:00 -04004339 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004340}
4341
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004342static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004343{
Andy Adamson66689582009-04-03 08:28:45 +03004344 /*
4345 * When sessions are used the stateid generation number is ignored
4346 * when it is zero.
4347 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004348 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004349 return nfs_ok;
4350
4351 if (in->si_generation == ref->si_generation)
4352 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004353
J. Bruce Fields0836f582008-01-26 19:08:12 -05004354 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004355 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004356 return nfserr_bad_stateid;
4357 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004358 * However, we could see a stateid from the past, even from a
4359 * non-buggy client. For example, if the client sends a lock
4360 * while some IO is outstanding, the lock may bump si_generation
4361 * while the IO is still in flight. The client could avoid that
4362 * situation by waiting for responses on all the IO requests,
4363 * but better performance may result in retrying IO that
4364 * receives an old_stateid error if requests are rarely
4365 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004366 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004367 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004368}
4369
Chuck Lever7df302f2012-05-29 13:56:37 -04004370static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004371{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004372 struct nfs4_stid *s;
4373 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004374 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004375
Chuck Lever7df302f2012-05-29 13:56:37 -04004376 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004377 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004378 /* Client debugging aid. */
4379 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4380 char addr_str[INET6_ADDRSTRLEN];
4381 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4382 sizeof(addr_str));
4383 pr_warn_ratelimited("NFSD: client %s testing state ID "
4384 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004385 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004386 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004387 spin_lock(&cl->cl_lock);
4388 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004389 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004390 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004391 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004392 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004393 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004394 switch (s->sc_type) {
4395 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004396 status = nfs_ok;
4397 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004398 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004399 status = nfserr_deleg_revoked;
4400 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004401 case NFS4_OPEN_STID:
4402 case NFS4_LOCK_STID:
4403 ols = openlockstateid(s);
4404 if (ols->st_stateowner->so_is_open_owner
4405 && !(openowner(ols->st_stateowner)->oo_flags
4406 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004407 status = nfserr_bad_stateid;
4408 else
4409 status = nfs_ok;
4410 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004411 default:
4412 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004413 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004414 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004415 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004416 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004417 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004418out_unlock:
4419 spin_unlock(&cl->cl_lock);
4420 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004421}
4422
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004423static __be32
4424nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4425 stateid_t *stateid, unsigned char typemask,
4426 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004427{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004428 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004429
4430 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4431 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004432 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004433 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004434 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004435 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004436 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004437 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004438 if (status)
4439 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004440 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004441 if (!*s)
4442 return nfserr_bad_stateid;
4443 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004444}
4445
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446/*
4447* Checks for stateid operations
4448*/
Al Virob37ad282006-10-19 23:28:59 -07004449__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004450nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004451 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004453 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004454 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004456 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004458 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004459 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004460 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 if (filpp)
4463 *filpp = NULL;
4464
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004465 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 return nfserr_grace;
4467
4468 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004469 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470
Trond Myklebust14bcab12014-04-18 14:44:07 -04004471 nfs4_lock_state();
4472
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004473 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004474 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004475 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004476 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004477 goto unlock_state;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004478 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4479 if (status)
4480 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004481 switch (s->sc_type) {
4482 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004483 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004484 status = nfs4_check_delegmode(dp, flags);
4485 if (status)
4486 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004487 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004488 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004489 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004490 WARN_ON_ONCE(1);
4491 status = nfserr_serverfault;
4492 goto out;
4493 }
Trond Myklebustde186432014-07-10 14:07:26 -04004494 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004495 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004496 break;
4497 case NFS4_OPEN_STID:
4498 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004499 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004500 status = nfs4_check_fh(current_fh, stp);
4501 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004503 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004504 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004506 status = nfs4_check_openmode(stp, flags);
4507 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004509 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004510 struct nfs4_file *fp = stp->st_stid.sc_file;
4511
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004512 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004513 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004514 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004515 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004516 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004517 break;
4518 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004519 status = nfserr_bad_stateid;
4520 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 }
4522 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004523 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004524 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004526 nfs4_put_stid(s);
4527unlock_state:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004528 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 return status;
4530}
4531
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004532/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004533 * Test if the stateid is valid
4534 */
4535__be32
4536nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4537 struct nfsd4_test_stateid *test_stateid)
4538{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004539 struct nfsd4_test_stateid_id *stateid;
4540 struct nfs4_client *cl = cstate->session->se_client;
4541
4542 nfs4_lock_state();
4543 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004544 stateid->ts_id_status =
4545 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004546 nfs4_unlock_state();
4547
Bryan Schumaker17456802011-07-13 10:50:48 -04004548 return nfs_ok;
4549}
4550
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004551__be32
4552nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4553 struct nfsd4_free_stateid *free_stateid)
4554{
4555 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004556 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004557 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004558 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004559 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004560 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004561
4562 nfs4_lock_state();
Jeff Layton1af71cc2014-07-29 21:34:14 -04004563 spin_lock(&cl->cl_lock);
4564 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004565 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004566 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004567 switch (s->sc_type) {
4568 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004569 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004570 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004571 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004572 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4573 if (ret)
4574 break;
4575 ret = nfserr_locks_held;
4576 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004577 case NFS4_LOCK_STID:
4578 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4579 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004580 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004581 stp = openlockstateid(s);
4582 ret = nfserr_locks_held;
4583 if (check_for_locks(stp->st_stid.sc_file,
4584 lockowner(stp->st_stateowner)))
4585 break;
4586 unhash_lock_stateid(stp);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004587 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004588 nfs4_put_stid(s);
4589 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004590 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004591 case NFS4_REVOKED_DELEG_STID:
4592 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004593 list_del_init(&dp->dl_recall_lru);
4594 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004595 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004596 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004597 goto out;
4598 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004599 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004600out_unlock:
4601 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004602out:
4603 nfs4_unlock_state();
4604 return ret;
4605}
4606
NeilBrown4c4cd222005-07-07 17:59:27 -07004607static inline int
4608setlkflg (int type)
4609{
4610 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4611 RD_STATE : WR_STATE;
4612}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004614static __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 -04004615{
4616 struct svc_fh *current_fh = &cstate->current_fh;
4617 struct nfs4_stateowner *sop = stp->st_stateowner;
4618 __be32 status;
4619
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004620 status = nfsd4_check_seqid(cstate, sop, seqid);
4621 if (status)
4622 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004623 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4624 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004625 /*
4626 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004627 * nfserr_replay_me from the previous step, and
4628 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004629 */
4630 return nfserr_bad_stateid;
4631 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4632 if (status)
4633 return status;
4634 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004635}
4636
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637/*
4638 * Checks for sequence id mutating operations.
4639 */
Al Virob37ad282006-10-19 23:28:59 -07004640static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004641nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004642 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004643 struct nfs4_ol_stateid **stpp,
4644 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004646 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004647 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004648 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004650 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4651 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004652
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004654 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004655 if (status)
4656 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004657 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004658 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004660 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004661 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004662 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004663 else
4664 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004665 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004666}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004667
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004668static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4669 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004670{
4671 __be32 status;
4672 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004673 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004675 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004676 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004677 if (status)
4678 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004679 oo = openowner(stp->st_stateowner);
4680 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4681 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004682 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004683 }
4684 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004685 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686}
4687
Al Virob37ad282006-10-19 23:28:59 -07004688__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004689nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004690 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691{
Al Virob37ad282006-10-19 23:28:59 -07004692 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004693 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004694 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004695 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696
Al Viroa6a9f182013-09-16 10:57:01 -04004697 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4698 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004700 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004701 if (status)
4702 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
4704 nfs4_lock_state();
4705
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004706 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004707 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004708 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004709 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004710 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004711 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004712 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004713 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004714 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004715 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004716 update_stateid(&stp->st_stid.sc_stateid);
4717 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004718 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004719 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004720
Jeff Layton2a4317c2012-03-21 16:42:43 -04004721 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004722 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004723put_stateid:
4724 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004726 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004727 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 return status;
4729}
4730
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004731static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004733 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004734 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004735 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004736 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004737}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004738
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004739static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4740{
4741 switch (to_access) {
4742 case NFS4_SHARE_ACCESS_READ:
4743 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4744 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4745 break;
4746 case NFS4_SHARE_ACCESS_WRITE:
4747 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4748 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4749 break;
4750 case NFS4_SHARE_ACCESS_BOTH:
4751 break;
4752 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004753 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 }
4755}
4756
Al Virob37ad282006-10-19 23:28:59 -07004757__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004758nfsd4_open_downgrade(struct svc_rqst *rqstp,
4759 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004760 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761{
Al Virob37ad282006-10-19 23:28:59 -07004762 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004763 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004764 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765
Al Viroa6a9f182013-09-16 10:57:01 -04004766 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4767 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004769 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004770 if (od->od_deleg_want)
4771 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4772 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773
4774 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004775 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004776 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004777 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004780 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004781 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004783 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004785 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004786 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004788 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004790 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791
Jeff Laytonce0fc432012-05-11 09:45:14 -04004792 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004794 update_stateid(&stp->st_stid.sc_stateid);
4795 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004797put_stateid:
4798 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004800 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004801 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 return status;
4803}
4804
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004805static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4806{
Trond Myklebustacf92952014-06-30 11:48:37 -04004807 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytond83017f2014-07-29 21:34:42 -04004808 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004809
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004810 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04004811 spin_lock(&clp->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04004812 unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004813
Jeff Laytond83017f2014-07-29 21:34:42 -04004814 if (clp->cl_minorversion) {
4815 put_ol_stateid_locked(s, &reaplist);
4816 spin_unlock(&clp->cl_lock);
4817 free_ol_stateid_reaplist(&reaplist);
4818 } else {
4819 spin_unlock(&clp->cl_lock);
4820 free_ol_stateid_reaplist(&reaplist);
Jeff Laytond3134b12014-07-29 21:34:32 -04004821 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04004822 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004823}
4824
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825/*
4826 * nfs4_unlock_state() called after encode
4827 */
Al Virob37ad282006-10-19 23:28:59 -07004828__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004829nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004830 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831{
Al Virob37ad282006-10-19 23:28:59 -07004832 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004833 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004834 struct net *net = SVC_NET(rqstp);
4835 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
Al Viroa6a9f182013-09-16 10:57:01 -04004837 dprintk("NFSD: nfsd4_close on file %pd\n",
4838 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839
4840 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004841 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4842 &close->cl_stateid,
4843 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004844 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004845 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004846 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004848 update_stateid(&stp->st_stid.sc_stateid);
4849 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004851 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004852
4853 /* put reference from nfs4_preprocess_seqid_op */
4854 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855out:
Jeff Layton58fb12e2014-07-29 21:34:27 -04004856 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 return status;
4858}
4859
Al Virob37ad282006-10-19 23:28:59 -07004860__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004861nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4862 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004864 struct nfs4_delegation *dp;
4865 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004866 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004867 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004868 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004870 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004871 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
4873 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004874 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004875 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004876 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004877 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004878 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004879 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004880 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004881
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004882 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004883put_stateid:
4884 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004886 nfs4_unlock_state();
4887
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 return status;
4889}
4890
4891
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893
Benny Halevy87df4de2008-12-15 19:42:03 +02004894static inline u64
4895end_offset(u64 start, u64 len)
4896{
4897 u64 end;
4898
4899 end = start + len;
4900 return end >= start ? end: NFS4_MAX_UINT64;
4901}
4902
4903/* last octet in a range */
4904static inline u64
4905last_byte_offset(u64 start, u64 len)
4906{
4907 u64 end;
4908
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004909 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004910 end = start + len;
4911 return end > start ? end - 1: NFS4_MAX_UINT64;
4912}
4913
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914/*
4915 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4916 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4917 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4918 * locking, this prevents us from being completely protocol-compliant. The
4919 * real solution to this problem is to start using unsigned file offsets in
4920 * the VFS, but this is a very deep change!
4921 */
4922static inline void
4923nfs4_transform_lock_offset(struct file_lock *lock)
4924{
4925 if (lock->fl_start < 0)
4926 lock->fl_start = OFFSET_MAX;
4927 if (lock->fl_end < 0)
4928 lock->fl_end = OFFSET_MAX;
4929}
4930
NeilBrownd5b90262006-04-10 22:55:22 -07004931/* Hack!: For now, we're defining this just so we can use a pointer to it
4932 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004933static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004934};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935
4936static inline void
4937nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4938{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004939 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940
NeilBrownd5b90262006-04-10 22:55:22 -07004941 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004942 lo = (struct nfs4_lockowner *) fl->fl_owner;
4943 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4944 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004945 if (!deny->ld_owner.data)
4946 /* We just don't care that much */
4947 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004948 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4949 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004950 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004951nevermind:
4952 deny->ld_owner.len = 0;
4953 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004954 deny->ld_clientid.cl_boot = 0;
4955 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 }
4957 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004958 deny->ld_length = NFS4_MAX_UINT64;
4959 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4961 deny->ld_type = NFS4_READ_LT;
4962 if (fl->fl_type != F_RDLCK)
4963 deny->ld_type = NFS4_WRITE_LT;
4964}
4965
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004966static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004967find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004968 struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004970 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004971 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972
Trond Myklebust0a880a22014-07-30 08:27:10 -04004973 lockdep_assert_held(&clp->cl_lock);
4974
Trond Myklebustd4f04892014-07-29 21:34:36 -04004975 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4976 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004977 if (so->so_is_open_owner)
4978 continue;
Trond Myklebustd4f04892014-07-29 21:34:36 -04004979 if (!same_owner_str(so, owner))
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004980 continue;
Jeff Layton5db1c032014-07-29 21:34:28 -04004981 atomic_inc(&so->so_count);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004982 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 }
4984 return NULL;
4985}
4986
Trond Myklebustc58c6612014-07-29 21:34:35 -04004987static struct nfs4_lockowner *
4988find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004989 struct nfs4_client *clp)
Trond Myklebustc58c6612014-07-29 21:34:35 -04004990{
4991 struct nfs4_lockowner *lo;
4992
Trond Myklebustd4f04892014-07-29 21:34:36 -04004993 spin_lock(&clp->cl_lock);
4994 lo = find_lockowner_str_locked(clid, owner, clp);
4995 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004996 return lo;
4997}
4998
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004999static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5000{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005001 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005002}
5003
Jeff Layton6b180f02014-07-29 21:34:26 -04005004static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5005{
5006 struct nfs4_lockowner *lo = lockowner(sop);
5007
5008 kmem_cache_free(lockowner_slab, lo);
5009}
5010
5011static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005012 .so_unhash = nfs4_unhash_lockowner,
5013 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005014};
5015
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016/*
5017 * Alloc a lock owner structure.
5018 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005019 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005021 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005023static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005024alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5025 struct nfs4_ol_stateid *open_stp,
5026 struct nfsd4_lock *lock)
5027{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005028 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005030 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5031 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005033 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5034 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005035 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005036 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005037 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005038 ret = find_lockowner_str_locked(&clp->cl_clientid,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005039 &lock->lk_new_owner, clp);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005040 if (ret == NULL) {
5041 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005042 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005043 ret = lo;
5044 } else
5045 nfs4_free_lockowner(&lo->lo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005046 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005047 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048}
5049
Jeff Layton356a95e2014-07-29 21:34:13 -04005050static void
5051init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5052 struct nfs4_file *fp, struct inode *inode,
5053 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005055 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056
Jeff Layton356a95e2014-07-29 21:34:13 -04005057 lockdep_assert_held(&clp->cl_lock);
5058
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005059 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005060 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005061 stp->st_stateowner = &lo->lo_owner;
Trond Myklebuste4f1dd72014-07-29 21:34:30 -04005062 atomic_inc(&lo->lo_owner.so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07005063 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005064 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04005065 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005066 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005068 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005069 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005070 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005071 spin_lock(&fp->fi_lock);
5072 list_add(&stp->st_perfile, &fp->fi_stateids);
5073 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074}
5075
Jeff Laytonc53530d2014-06-30 11:48:39 -04005076static struct nfs4_ol_stateid *
5077find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5078{
5079 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005080 struct nfs4_client *clp = lo->lo_owner.so_client;
5081
5082 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005083
5084 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005085 if (lst->st_stid.sc_file == fp) {
5086 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005087 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005088 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005089 }
5090 return NULL;
5091}
5092
Jeff Layton356a95e2014-07-29 21:34:13 -04005093static struct nfs4_ol_stateid *
5094find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5095 struct inode *inode, struct nfs4_ol_stateid *ost,
5096 bool *new)
5097{
5098 struct nfs4_stid *ns = NULL;
5099 struct nfs4_ol_stateid *lst;
5100 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5101 struct nfs4_client *clp = oo->oo_owner.so_client;
5102
5103 spin_lock(&clp->cl_lock);
5104 lst = find_lock_stateid(lo, fi);
5105 if (lst == NULL) {
5106 spin_unlock(&clp->cl_lock);
5107 ns = nfs4_alloc_stid(clp, stateid_slab);
5108 if (ns == NULL)
5109 return NULL;
5110
5111 spin_lock(&clp->cl_lock);
5112 lst = find_lock_stateid(lo, fi);
5113 if (likely(!lst)) {
5114 lst = openlockstateid(ns);
5115 init_lock_stateid(lst, lo, fi, inode, ost);
5116 ns = NULL;
5117 *new = true;
5118 }
5119 }
5120 spin_unlock(&clp->cl_lock);
5121 if (ns)
5122 nfs4_put_stid(ns);
5123 return lst;
5124}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005125
NeilBrownfd39ca92005-06-23 22:04:03 -07005126static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127check_lock_length(u64 offset, u64 length)
5128{
Benny Halevy87df4de2008-12-15 19:42:03 +02005129 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 LOFF_OVERFLOW(offset, length)));
5131}
5132
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005133static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005134{
Trond Myklebust11b91642014-07-29 21:34:08 -04005135 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005136
Jeff Layton7214e862014-07-10 14:07:33 -04005137 lockdep_assert_held(&fp->fi_lock);
5138
Jeff Layton82c5ff12012-05-11 09:45:13 -04005139 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005140 return;
Jeff Layton12659652014-07-10 14:07:28 -04005141 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005142 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005143}
5144
Jeff Layton356a95e2014-07-29 21:34:13 -04005145static __be32
5146lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5147 struct nfs4_ol_stateid *ost,
5148 struct nfsd4_lock *lock,
5149 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005150{
Jeff Layton5db1c032014-07-29 21:34:28 -04005151 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005152 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005153 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5154 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005155 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005156 struct nfs4_lockowner *lo;
5157 unsigned int strhashval;
5158
Trond Myklebustd4f04892014-07-29 21:34:36 -04005159 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005160 if (!lo) {
Trond Myklebustd4f04892014-07-29 21:34:36 -04005161 strhashval = ownerstr_hashval(&lock->v.new.owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005162 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5163 if (lo == NULL)
5164 return nfserr_jukebox;
5165 } else {
5166 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005167 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005168 if (!cstate->minorversion &&
5169 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005170 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005171 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005172
Jeff Layton356a95e2014-07-29 21:34:13 -04005173 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005174 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005175 status = nfserr_jukebox;
5176 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005177 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005178 status = nfs_ok;
5179out:
5180 nfs4_put_stateowner(&lo->lo_owner);
5181 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005182}
5183
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184/*
5185 * LOCK operation
5186 */
Al Virob37ad282006-10-19 23:28:59 -07005187__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005188nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005189 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005191 struct nfs4_openowner *open_sop = NULL;
5192 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005193 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005194 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005195 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005196 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005197 struct file_lock *file_lock = NULL;
5198 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005199 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005200 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005201 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005202 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005203 struct net *net = SVC_NET(rqstp);
5204 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205
5206 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5207 (long long) lock->lk_offset,
5208 (long long) lock->lk_length);
5209
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 if (check_lock_length(lock->lk_offset, lock->lk_length))
5211 return nfserr_inval;
5212
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005213 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005214 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005215 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5216 return status;
5217 }
5218
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 nfs4_lock_state();
5220
5221 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005222 if (nfsd4_has_session(cstate))
5223 /* See rfc 5661 18.10.3: given clientid is ignored: */
5224 memcpy(&lock->v.new.clientid,
5225 &cstate->session->se_client->cl_clientid,
5226 sizeof(clientid_t));
5227
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005229 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005233 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 lock->lk_new_open_seqid,
5235 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005236 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005237 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005239 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005240 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005241 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005242 &lock->v.new.clientid))
5243 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005244 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005245 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005246 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005247 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005248 lock->lk_old_lock_seqid,
5249 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005250 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005251 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005252 if (status)
5253 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005254 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005256 lkflg = setlkflg(lock->lk_type);
5257 status = nfs4_check_openmode(lock_stp, lkflg);
5258 if (status)
5259 goto out;
5260
NeilBrown0dd395d2005-07-07 17:59:15 -07005261 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005262 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005263 goto out;
5264 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005265 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005266 goto out;
5267
Jeff Layton21179d82012-08-21 08:03:32 -04005268 file_lock = locks_alloc_lock();
5269 if (!file_lock) {
5270 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5271 status = nfserr_jukebox;
5272 goto out;
5273 }
5274
Trond Myklebust11b91642014-07-29 21:34:08 -04005275 fp = lock_stp->st_stid.sc_file;
Jeff Layton21179d82012-08-21 08:03:32 -04005276 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 switch (lock->lk_type) {
5278 case NFS4_READ_LT:
5279 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005280 spin_lock(&fp->fi_lock);
5281 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005282 if (filp)
5283 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005284 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005285 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005286 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 case NFS4_WRITE_LT:
5288 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005289 spin_lock(&fp->fi_lock);
5290 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005291 if (filp)
5292 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005293 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005294 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005295 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 default:
5297 status = nfserr_inval;
5298 goto out;
5299 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005300 if (!filp) {
5301 status = nfserr_openmode;
5302 goto out;
5303 }
Jeff Layton21179d82012-08-21 08:03:32 -04005304 file_lock->fl_owner = (fl_owner_t)lock_sop;
5305 file_lock->fl_pid = current->tgid;
5306 file_lock->fl_file = filp;
5307 file_lock->fl_flags = FL_POSIX;
5308 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5309 file_lock->fl_start = lock->lk_offset;
5310 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5311 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312
Jeff Layton21179d82012-08-21 08:03:32 -04005313 conflock = locks_alloc_lock();
5314 if (!conflock) {
5315 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5316 status = nfserr_jukebox;
5317 goto out;
5318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319
Jeff Layton21179d82012-08-21 08:03:32 -04005320 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005321 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005323 update_stateid(&lock_stp->st_stid.sc_stateid);
5324 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005326 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005327 break;
5328 case (EAGAIN): /* conflock holds conflicting lock */
5329 status = nfserr_denied;
5330 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005331 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005332 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 case (EDEADLK):
5334 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005335 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005336 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005337 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005338 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005339 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341out:
Trond Myklebustde186432014-07-10 14:07:26 -04005342 if (filp)
5343 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005344 if (lock_stp) {
5345 /* Bump seqid manually if the 4.0 replay owner is openowner */
5346 if (cstate->replay_owner &&
5347 cstate->replay_owner != &lock_sop->lo_owner &&
5348 seqid_mutating_err(ntohl(status)))
5349 lock_sop->lo_owner.so_seqid++;
5350
5351 /*
5352 * If this is a new, never-before-used stateid, and we are
5353 * returning an error, then just go ahead and release it.
5354 */
5355 if (status && new)
5356 release_lock_stateid(lock_stp);
5357
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005358 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005359 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005360 if (open_stp)
5361 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005362 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005363 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005364 if (file_lock)
5365 locks_free_lock(file_lock);
5366 if (conflock)
5367 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 return status;
5369}
5370
5371/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005372 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5373 * so we do a temporary open here just to get an open file to pass to
5374 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5375 * inode operation.)
5376 */
Al Viro04da6e92012-04-13 00:00:04 -04005377static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005378{
5379 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005380 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5381 if (!err) {
5382 err = nfserrno(vfs_test_lock(file, lock));
5383 nfsd_close(file);
5384 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005385 return err;
5386}
5387
5388/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 * LOCKT operation
5390 */
Al Virob37ad282006-10-19 23:28:59 -07005391__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005392nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5393 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394{
Jeff Layton21179d82012-08-21 08:03:32 -04005395 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005396 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005397 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005398 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005400 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 return nfserr_grace;
5402
5403 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5404 return nfserr_inval;
5405
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 nfs4_lock_state();
5407
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005408 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005409 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005410 if (status)
5411 goto out;
5412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005414 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416
Jeff Layton21179d82012-08-21 08:03:32 -04005417 file_lock = locks_alloc_lock();
5418 if (!file_lock) {
5419 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5420 status = nfserr_jukebox;
5421 goto out;
5422 }
5423 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 switch (lockt->lt_type) {
5425 case NFS4_READ_LT:
5426 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005427 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 break;
5429 case NFS4_WRITE_LT:
5430 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005431 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 break;
5433 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005434 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 status = nfserr_inval;
5436 goto out;
5437 }
5438
Trond Myklebustd4f04892014-07-29 21:34:36 -04005439 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5440 cstate->clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005441 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005442 file_lock->fl_owner = (fl_owner_t)lo;
5443 file_lock->fl_pid = current->tgid;
5444 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445
Jeff Layton21179d82012-08-21 08:03:32 -04005446 file_lock->fl_start = lockt->lt_offset;
5447 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448
Jeff Layton21179d82012-08-21 08:03:32 -04005449 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450
Jeff Layton21179d82012-08-21 08:03:32 -04005451 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005452 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005453 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005454
Jeff Layton21179d82012-08-21 08:03:32 -04005455 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005457 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 }
5459out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005460 if (lo)
5461 nfs4_put_stateowner(&lo->lo_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005463 if (file_lock)
5464 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 return status;
5466}
5467
Al Virob37ad282006-10-19 23:28:59 -07005468__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005469nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005470 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005472 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005474 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005475 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005476 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005477 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5478
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5480 (long long) locku->lu_offset,
5481 (long long) locku->lu_length);
5482
5483 if (check_lock_length(locku->lu_offset, locku->lu_length))
5484 return nfserr_inval;
5485
5486 nfs4_lock_state();
5487
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005488 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005489 &locku->lu_stateid, NFS4_LOCK_STID,
5490 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005491 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005493 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005494 if (!filp) {
5495 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005496 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005497 }
Jeff Layton21179d82012-08-21 08:03:32 -04005498 file_lock = locks_alloc_lock();
5499 if (!file_lock) {
5500 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5501 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005502 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005503 }
5504 locks_init_lock(file_lock);
5505 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005506 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005507 file_lock->fl_pid = current->tgid;
5508 file_lock->fl_file = filp;
5509 file_lock->fl_flags = FL_POSIX;
5510 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5511 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512
Jeff Layton21179d82012-08-21 08:03:32 -04005513 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5514 locku->lu_length);
5515 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516
Jeff Layton21179d82012-08-21 08:03:32 -04005517 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005518 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005519 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 goto out_nfserr;
5521 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005522 update_stateid(&stp->st_stid.sc_stateid);
5523 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005524fput:
5525 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005526put_stateid:
5527 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005529 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005530 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005531 if (file_lock)
5532 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 return status;
5534
5535out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005536 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005537 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538}
5539
5540/*
5541 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005542 * true: locks held by lockowner
5543 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005545static bool
5546check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547{
5548 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005549 int status = false;
5550 struct file *filp = find_any_file(fp);
5551 struct inode *inode;
5552
5553 if (!filp) {
5554 /* Any valid lock stateid should have some sort of access */
5555 WARN_ON_ONCE(1);
5556 return status;
5557 }
5558
5559 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560
Jeff Layton1c8c6012013-06-21 08:58:15 -04005561 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005563 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005564 status = true;
5565 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005568 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005569 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 return status;
5571}
5572
Al Virob37ad282006-10-19 23:28:59 -07005573__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005574nfsd4_release_lockowner(struct svc_rqst *rqstp,
5575 struct nfsd4_compound_state *cstate,
5576 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577{
5578 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005579 struct nfs4_stateowner *sop;
5580 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005581 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005583 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005584 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005585 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005586 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587
5588 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5589 clid->cl_boot, clid->cl_id);
5590
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 nfs4_lock_state();
5592
Jeff Layton4b24ca72014-06-30 11:48:44 -04005593 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005594 if (status)
5595 goto out;
5596
Trond Myklebustd4f04892014-07-29 21:34:36 -04005597 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005598 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005599 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005600 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005601 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005602
5603 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005604 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005605
Jeff Layton882e9d22014-07-29 21:34:37 -04005606 /* see if there are still any locks associated with it */
5607 lo = lockowner(sop);
5608 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5609 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5610 status = nfserr_locks_held;
5611 spin_unlock(&clp->cl_lock);
5612 goto out;
5613 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005614 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005615
5616 atomic_inc(&sop->so_count);
5617 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005618 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005619 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005620 if (lo)
5621 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622out:
5623 nfs4_unlock_state();
5624 return status;
5625}
5626
5627static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005628alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629{
NeilBrowna55370a2005-06-23 22:03:52 -07005630 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631}
5632
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005633bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005634nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005635{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005636 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005637
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005638 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005639 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005640}
5641
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642/*
5643 * failure => all reset bets are off, nfserr_no_grace...
5644 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005645struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005646nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647{
5648 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005649 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650
NeilBrowna55370a2005-06-23 22:03:52 -07005651 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5652 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005653 if (crp) {
5654 strhashval = clientstr_hashval(name);
5655 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005656 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005657 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005658 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005659 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005660 }
5661 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662}
5663
Jeff Layton2a4317c2012-03-21 16:42:43 -04005664void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005665nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005666{
5667 list_del(&crp->cr_strhash);
5668 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005669 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005670}
5671
5672void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005673nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674{
5675 struct nfs4_client_reclaim *crp = NULL;
5676 int i;
5677
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005679 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5680 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005682 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 }
5684 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005685 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686}
5687
5688/*
5689 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005690struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005691nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692{
5693 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 struct nfs4_client_reclaim *crp = NULL;
5695
Jeff Layton278c9312012-11-12 15:00:52 -05005696 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697
Jeff Layton278c9312012-11-12 15:00:52 -05005698 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005699 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005700 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 return crp;
5702 }
5703 }
5704 return NULL;
5705}
5706
5707/*
5708* Called from OPEN. Look for clientid in reclaim list.
5709*/
Al Virob37ad282006-10-19 23:28:59 -07005710__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005711nfs4_check_open_reclaim(clientid_t *clid,
5712 struct nfsd4_compound_state *cstate,
5713 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005715 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005716
5717 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005718 status = lookup_clientid(clid, cstate, nn);
5719 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005720 return nfserr_reclaim_bad;
5721
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005722 if (nfsd4_client_record_check(cstate->clp))
5723 return nfserr_reclaim_bad;
5724
5725 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726}
5727
Bryan Schumaker65178db2011-11-01 13:35:21 -04005728#ifdef CONFIG_NFSD_FAULT_INJECTION
5729
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005730u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5731{
Jeff Laytond20c11d2014-07-30 08:27:07 -04005732 __be32 ret;
5733 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
5734
5735 spin_lock(&nn->client_lock);
5736 ret = mark_client_expired_locked(clp);
5737 spin_unlock(&nn->client_lock);
5738 if (ret != nfs_ok)
J. Bruce Fields221a6872013-04-01 22:23:49 -04005739 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005740 expire_client(clp);
5741 return 1;
5742}
5743
Bryan Schumaker184c1842012-11-29 11:40:44 -05005744u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5745{
5746 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005747 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005748 printk(KERN_INFO "NFS Client: %s\n", buf);
5749 return 1;
5750}
5751
5752static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5753 const char *type)
5754{
5755 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005756 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005757 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5758}
5759
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005760static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5761 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005762{
5763 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005764 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005765 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005766 u64 count = 0;
5767
5768 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005769 list_for_each_entry_safe(stp, st_next,
5770 &oop->oo_owner.so_stateids, st_perstateowner) {
5771 list_for_each_entry_safe(lst, lst_next,
5772 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005773 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005774 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005775 if (++count == max)
5776 return count;
5777 }
5778 }
5779 }
5780
5781 return count;
5782}
5783
5784u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5785{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005786 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005787}
5788
Bryan Schumaker184c1842012-11-29 11:40:44 -05005789u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5790{
5791 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5792 nfsd_print_count(clp, count, "locked files");
5793 return count;
5794}
5795
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005796static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5797{
5798 struct nfs4_openowner *oop, *next;
5799 u64 count = 0;
5800
5801 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5802 if (func)
5803 func(oop);
5804 if (++count == max)
5805 break;
5806 }
5807
5808 return count;
5809}
5810
5811u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5812{
5813 return nfsd_foreach_client_open(clp, max, release_openowner);
5814}
5815
Bryan Schumaker184c1842012-11-29 11:40:44 -05005816u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5817{
5818 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5819 nfsd_print_count(clp, count, "open files");
5820 return count;
5821}
5822
Bryan Schumaker269de302012-11-29 11:40:42 -05005823static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5824 struct list_head *victims)
5825{
5826 struct nfs4_delegation *dp, *next;
5827 u64 count = 0;
5828
Benny Halevycdc975052014-05-30 09:09:30 -04005829 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005830 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005831 if (victims) {
5832 /*
5833 * It's not safe to mess with delegations that have a
5834 * non-zero dl_time. They might have already been broken
5835 * and could be processed by the laundromat outside of
5836 * the state_lock. Just leave them be.
5837 */
5838 if (dp->dl_time != 0)
5839 continue;
5840
Jeff Layton42690672014-07-25 07:34:20 -04005841 unhash_delegation_locked(dp);
5842 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005843 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005844 if (++count == max)
5845 break;
5846 }
5847 return count;
5848}
5849
5850u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5851{
5852 struct nfs4_delegation *dp, *next;
5853 LIST_HEAD(victims);
5854 u64 count;
5855
Benny Halevycdc975052014-05-30 09:09:30 -04005856 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005857 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc975052014-05-30 09:09:30 -04005858 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005859
Jeff Layton2d4a5322014-07-25 07:34:21 -04005860 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5861 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005862 revoke_delegation(dp);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005863 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005864
5865 return count;
5866}
5867
5868u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5869{
Jeff Laytondff13992014-07-08 14:02:49 -04005870 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005871 LIST_HEAD(victims);
5872 u64 count;
5873
Benny Halevycdc975052014-05-30 09:09:30 -04005874 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005875 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005876 while (!list_empty(&victims)) {
5877 dp = list_first_entry(&victims, struct nfs4_delegation,
5878 dl_recall_lru);
5879 list_del_init(&dp->dl_recall_lru);
5880 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005881 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005882 }
Benny Halevycdc975052014-05-30 09:09:30 -04005883 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005884
5885 return count;
5886}
5887
Bryan Schumaker184c1842012-11-29 11:40:44 -05005888u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5889{
5890 u64 count = 0;
5891
Benny Halevycdc975052014-05-30 09:09:30 -04005892 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005893 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc975052014-05-30 09:09:30 -04005894 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005895
5896 nfsd_print_count(clp, count, "delegations");
5897 return count;
5898}
5899
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005900u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005901{
5902 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005903 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005904 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005905
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005906 if (!nfsd_netns_ready(nn))
5907 return 0;
5908
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005909 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005910 count += func(clp, max - count);
5911 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005912 break;
5913 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005914
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005915 return count;
5916}
5917
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005918struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5919{
5920 struct nfs4_client *clp;
5921 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5922
5923 if (!nfsd_netns_ready(nn))
5924 return NULL;
5925
5926 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5927 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5928 return clp;
5929 }
5930 return NULL;
5931}
5932
Bryan Schumaker65178db2011-11-01 13:35:21 -04005933#endif /* CONFIG_NFSD_FAULT_INJECTION */
5934
Meelap Shahc2f1a552007-07-17 04:04:39 -07005935/*
5936 * Since the lifetime of a delegation isn't limited to that of an open, a
5937 * client may quite reasonably hang on to a delegation as long as it has
5938 * the inode cached. This becomes an obvious problem the first time a
5939 * client's inode cache approaches the size of the server's total memory.
5940 *
5941 * For now we avoid this problem by imposing a hard limit on the number
5942 * of delegations, which varies according to the server's memory size.
5943 */
5944static void
5945set_max_delegations(void)
5946{
5947 /*
5948 * Allow at most 4 delegations per megabyte of RAM. Quick
5949 * estimates suggest that in the worst case (where every delegation
5950 * is for a different inode), a delegation could take about 1.5K,
5951 * giving a worst case usage of about 6% of memory.
5952 */
5953 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5954}
5955
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005956static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005957{
5958 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5959 int i;
5960
5961 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5962 CLIENT_HASH_SIZE, GFP_KERNEL);
5963 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005964 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005965 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5966 CLIENT_HASH_SIZE, GFP_KERNEL);
5967 if (!nn->unconf_id_hashtbl)
5968 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005969 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5970 SESSION_HASH_SIZE, GFP_KERNEL);
5971 if (!nn->sessionid_hashtbl)
5972 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005973
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005974 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005975 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005976 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005977 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005978 for (i = 0; i < SESSION_HASH_SIZE; i++)
5979 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005980 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005981 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005982 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005983 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005984 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005985 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005986
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005987 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005988 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005989
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005990 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005991
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005992err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005993 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005994err_unconf_id:
5995 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005996err:
5997 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005998}
5999
6000static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006001nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006002{
6003 int i;
6004 struct nfs4_client *clp = NULL;
6005 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6006
6007 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6008 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6009 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6010 destroy_client(clp);
6011 }
6012 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006013
Kinglong Mee2b905632014-03-26 22:09:30 +08006014 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6015 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6016 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6017 destroy_client(clp);
6018 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006019 }
6020
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006021 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006022 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006023 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006024 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006025}
6026
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006027int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006028nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07006029{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006030 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006031 int ret;
6032
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006033 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006034 if (ret)
6035 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006036 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006037 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006038 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04006039 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006040 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03006041 nn->nfsd4_grace, net);
6042 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006043 return 0;
6044}
6045
6046/* initialization to perform when the nfsd service is started: */
6047
6048int
6049nfs4_state_start(void)
6050{
6051 int ret;
6052
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006053 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006054 if (ret)
6055 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07006056 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05006057 if (laundry_wq == NULL) {
6058 ret = -ENOMEM;
6059 goto out_recovery;
6060 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006061 ret = nfsd4_create_callback_queue();
6062 if (ret)
6063 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006064
Meelap Shahc2f1a552007-07-17 04:04:39 -07006065 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006066
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006067 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006068
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006069out_free_laundry:
6070 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05006071out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006072 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073}
6074
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006075void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006076nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006080 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006082 cancel_delayed_work_sync(&nn->laundromat_work);
6083 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05006084
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04006085 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04006087 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006088 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006090 unhash_delegation_locked(dp);
6091 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 }
Benny Halevycdc975052014-05-30 09:09:30 -04006093 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 list_for_each_safe(pos, next, &reaplist) {
6095 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006096 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04006097 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 }
6099
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006100 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006101 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04006102 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103}
6104
6105void
6106nfs4_state_shutdown(void)
6107{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006108 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006109 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006111
6112static void
6113get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6114{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006115 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6116 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006117}
6118
6119static void
6120put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6121{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006122 if (cstate->minorversion) {
6123 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6124 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6125 }
6126}
6127
6128void
6129clear_current_stateid(struct nfsd4_compound_state *cstate)
6130{
6131 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006132}
6133
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006134/*
6135 * functions to set current state id
6136 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006137void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006138nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6139{
6140 put_stateid(cstate, &odp->od_stateid);
6141}
6142
6143void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006144nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6145{
6146 put_stateid(cstate, &open->op_stateid);
6147}
6148
6149void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006150nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6151{
6152 put_stateid(cstate, &close->cl_stateid);
6153}
6154
6155void
6156nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6157{
6158 put_stateid(cstate, &lock->lk_resp_stateid);
6159}
6160
6161/*
6162 * functions to consume current state id
6163 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006164
6165void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006166nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6167{
6168 get_stateid(cstate, &odp->od_stateid);
6169}
6170
6171void
6172nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6173{
6174 get_stateid(cstate, &drp->dr_stateid);
6175}
6176
6177void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006178nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6179{
6180 get_stateid(cstate, &fsp->fr_stateid);
6181}
6182
6183void
6184nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6185{
6186 get_stateid(cstate, &setattr->sa_stateid);
6187}
6188
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006189void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006190nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6191{
6192 get_stateid(cstate, &close->cl_stateid);
6193}
6194
6195void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006196nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006197{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006198 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006199}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006200
6201void
6202nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6203{
6204 get_stateid(cstate, &read->rd_stateid);
6205}
6206
6207void
6208nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6209{
6210 get_stateid(cstate, &write->wr_stateid);
6211}