blob: 43e66fc1b90dc6ae4a963784faa0a967805c2ac0 [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 Halevycdc97502014-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 Halevycdc97502014-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 Halevycdc97502014-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 Halevycdc97502014-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;
1040
1041 might_sleep();
1042
1043 while (!list_empty(reaplist)) {
1044 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1045 st_locks);
1046 list_del(&stp->st_locks);
1047 stp->st_stid.sc_free(&stp->st_stid);
1048 }
1049}
1050
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001051static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001052{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001053 struct nfs4_client *clp = lo->lo_owner.so_client;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001054 struct nfs4_ol_stateid *stp;
1055 struct list_head reaplist;
1056
1057 INIT_LIST_HEAD(&reaplist);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001058
Trond Myklebustd4f04892014-07-29 21:34:36 -04001059 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001060 unhash_lockowner_locked(lo);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001061 while (!list_empty(&lo->lo_owner.so_stateids)) {
1062 stp = list_first_entry(&lo->lo_owner.so_stateids,
1063 struct nfs4_ol_stateid, st_perstateowner);
1064 unhash_lock_stateid(stp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001065 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001066 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001067 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001068 free_ol_stateid_reaplist(&reaplist);
Jeff Layton6b180f02014-07-29 21:34:26 -04001069 nfs4_put_stateowner(&lo->lo_owner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001070}
1071
Jeff Laytond83017f2014-07-29 21:34:42 -04001072static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1073 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001074{
1075 struct nfs4_ol_stateid *stp;
1076
1077 while (!list_empty(&open_stp->st_locks)) {
1078 stp = list_entry(open_stp->st_locks.next,
1079 struct nfs4_ol_stateid, st_locks);
Jeff Laytond83017f2014-07-29 21:34:42 -04001080 unhash_lock_stateid(stp);
1081 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001082 }
1083}
1084
Jeff Laytond83017f2014-07-29 21:34:42 -04001085static void unhash_open_stateid(struct nfs4_ol_stateid *stp,
1086 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001087{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001088 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1089
Jeff Layton4ae098d2014-07-29 21:34:43 -04001090 unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001091 release_open_stateid_locks(stp, reaplist);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001092}
1093
1094static void release_open_stateid(struct nfs4_ol_stateid *stp)
1095{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001096 LIST_HEAD(reaplist);
1097
1098 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04001099 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001100 put_ol_stateid_locked(stp, &reaplist);
1101 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1102 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001103}
1104
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001105static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001106{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001107 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001108
Trond Myklebustd4f04892014-07-29 21:34:36 -04001109 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001110
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001111 list_del_init(&oo->oo_owner.so_strhash);
1112 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001113}
1114
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001115static void release_last_closed_stateid(struct nfs4_openowner *oo)
1116{
Jeff Layton217526e2014-07-30 08:27:11 -04001117 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1118 nfsd_net_id);
1119 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001120
Jeff Layton217526e2014-07-30 08:27:11 -04001121 spin_lock(&nn->client_lock);
1122 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001123 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001124 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001125 oo->oo_last_closed_stid = NULL;
1126 }
Jeff Layton217526e2014-07-30 08:27:11 -04001127 spin_unlock(&nn->client_lock);
1128 if (s)
1129 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001130}
1131
Jeff Layton2c41beb2014-07-29 21:34:41 -04001132static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001133{
1134 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001135 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001136 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001137
Jeff Layton2c41beb2014-07-29 21:34:41 -04001138 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001139
Trond Myklebustd4f04892014-07-29 21:34:36 -04001140 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001141 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001142 while (!list_empty(&oo->oo_owner.so_stateids)) {
1143 stp = list_first_entry(&oo->oo_owner.so_stateids,
1144 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytond83017f2014-07-29 21:34:42 -04001145 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001146 put_ol_stateid_locked(stp, &reaplist);
1147 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001148 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001149 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001150 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001151 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001152}
1153
Marc Eshel5282fd72009-04-03 08:27:52 +03001154static inline int
1155hash_sessionid(struct nfs4_sessionid *sessionid)
1156{
1157 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1158
1159 return sid->sequence % SESSION_HASH_SIZE;
1160}
1161
Trond Myklebust8f199b82012-03-20 15:11:17 -04001162#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001163static inline void
1164dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1165{
1166 u32 *ptr = (u32 *)(&sessionid->data[0]);
1167 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1168}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001169#else
1170static inline void
1171dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1172{
1173}
1174#endif
1175
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001176/*
1177 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1178 * won't be used for replay.
1179 */
1180void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1181{
1182 struct nfs4_stateowner *so = cstate->replay_owner;
1183
1184 if (nfserr == nfserr_replay_me)
1185 return;
1186
1187 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001188 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001189 return;
1190 }
1191 if (!so)
1192 return;
1193 if (so->so_is_open_owner)
1194 release_last_closed_stateid(openowner(so));
1195 so->so_seqid++;
1196 return;
1197}
Marc Eshel5282fd72009-04-03 08:27:52 +03001198
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001199static void
1200gen_sessionid(struct nfsd4_session *ses)
1201{
1202 struct nfs4_client *clp = ses->se_client;
1203 struct nfsd4_sessionid *sid;
1204
1205 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1206 sid->clientid = clp->cl_clientid;
1207 sid->sequence = current_sessionid++;
1208 sid->reserved = 0;
1209}
1210
1211/*
Andy Adamsona6496372009-08-28 08:45:01 -04001212 * The protocol defines ca_maxresponssize_cached to include the size of
1213 * the rpc header, but all we need to cache is the data starting after
1214 * the end of the initial SEQUENCE operation--the rest we regenerate
1215 * each time. Therefore we can advertise a ca_maxresponssize_cached
1216 * value that is the number of bytes in our cache plus a few additional
1217 * bytes. In order to stay on the safe side, and not promise more than
1218 * we can cache, those additional bytes must be the minimum possible: 24
1219 * bytes of rpc header (xid through accept state, with AUTH_NULL
1220 * verifier), 12 for the compound header (with zero-length tag), and 44
1221 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001222 */
Andy Adamsona6496372009-08-28 08:45:01 -04001223#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1224
Andy Adamson557ce262009-08-28 08:45:04 -04001225static void
1226free_session_slots(struct nfsd4_session *ses)
1227{
1228 int i;
1229
1230 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1231 kfree(ses->se_slots[i]);
1232}
1233
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001234/*
1235 * We don't actually need to cache the rpc and session headers, so we
1236 * can allocate a little less for each slot:
1237 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001238static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001239{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001240 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001241
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001242 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1243 size = 0;
1244 else
1245 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1246 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001247}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001248
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001249/*
1250 * XXX: If we run out of reserved DRC memory we could (up to a point)
1251 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001252 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001253 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001254static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001255{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001256 u32 slotsize = slot_bytes(ca);
1257 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001258 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001259
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001260 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001261 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1262 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001263 num = min_t(int, num, avail / slotsize);
1264 nfsd_drc_mem_used += num * slotsize;
1265 spin_unlock(&nfsd_drc_lock);
1266
1267 return num;
1268}
1269
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001270static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001271{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001272 int slotsize = slot_bytes(ca);
1273
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001274 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001275 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001276 spin_unlock(&nfsd_drc_lock);
1277}
1278
Kinglong Mee60810e52014-01-01 00:35:47 +08001279static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1280 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001281{
Kinglong Mee60810e52014-01-01 00:35:47 +08001282 int numslots = fattrs->maxreqs;
1283 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001284 struct nfsd4_session *new;
1285 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001286
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001287 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001288 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1289 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001290
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001291 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001292 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001293 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001294 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001295 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001296 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001297 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001298 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001299 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001300
1301 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1302 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1303
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001304 return new;
1305out_free:
1306 while (i--)
1307 kfree(new->se_slots[i]);
1308 kfree(new);
1309 return NULL;
1310}
1311
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001312static void free_conn(struct nfsd4_conn *c)
1313{
1314 svc_xprt_put(c->cn_xprt);
1315 kfree(c);
1316}
1317
1318static void nfsd4_conn_lost(struct svc_xpt_user *u)
1319{
1320 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1321 struct nfs4_client *clp = c->cn_session->se_client;
1322
1323 spin_lock(&clp->cl_lock);
1324 if (!list_empty(&c->cn_persession)) {
1325 list_del(&c->cn_persession);
1326 free_conn(c);
1327 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001328 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001329 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001330}
1331
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001332static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001333{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001334 struct nfsd4_conn *conn;
1335
1336 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1337 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001338 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001339 svc_xprt_get(rqstp->rq_xprt);
1340 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001341 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001342 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1343 return conn;
1344}
1345
J. Bruce Fields328ead22010-09-29 16:11:06 -04001346static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1347{
1348 conn->cn_session = ses;
1349 list_add(&conn->cn_persession, &ses->se_conns);
1350}
1351
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001352static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1353{
1354 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001355
1356 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001357 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001358 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001359}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001360
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001361static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001362{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001363 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001364 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001365}
1366
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001367static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001368{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001369 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001370
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001371 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001372 ret = nfsd4_register_conn(conn);
1373 if (ret)
1374 /* oops; xprt is already down: */
1375 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001376 /* We may have gained or lost a callback channel: */
1377 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001378}
1379
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001380static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001381{
1382 u32 dir = NFS4_CDFC4_FORE;
1383
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001384 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001385 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001386 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001387}
1388
1389/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001390static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001391{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001392 struct nfs4_client *clp = s->se_client;
1393 struct nfsd4_conn *c;
1394
1395 spin_lock(&clp->cl_lock);
1396 while (!list_empty(&s->se_conns)) {
1397 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1398 list_del_init(&c->cn_persession);
1399 spin_unlock(&clp->cl_lock);
1400
1401 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1402 free_conn(c);
1403
1404 spin_lock(&clp->cl_lock);
1405 }
1406 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001407}
1408
J. Bruce Fields1377b692012-09-11 21:42:40 -04001409static void __free_session(struct nfsd4_session *ses)
1410{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001411 free_session_slots(ses);
1412 kfree(ses);
1413}
1414
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001415static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001416{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001417 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001418 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001419 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001420}
Andy Adamson557ce262009-08-28 08:45:04 -04001421
Fengguang Wu135ae822012-11-10 07:20:25 -05001422static 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 -04001423{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001424 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001425 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001426
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001427 new->se_client = clp;
1428 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001429
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001430 INIT_LIST_HEAD(&new->se_conns);
1431
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001432 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001433 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001434 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001435 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001436 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001437 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001438 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001439 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001440 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001441 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001442
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001443 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001444 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001445 /*
1446 * This is a little silly; with sessions there's no real
1447 * use for the callback address. Use the peer address
1448 * as a reasonable default for now, but consider fixing
1449 * the rpc client not to require an address in the
1450 * future:
1451 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001452 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1453 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001454 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001455}
1456
Benny Halevy9089f1b2010-05-12 00:12:26 +03001457/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001458static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001459__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001460{
1461 struct nfsd4_session *elem;
1462 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001463 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001464
Trond Myklebust0a880a22014-07-30 08:27:10 -04001465 lockdep_assert_held(&nn->client_lock);
1466
Marc Eshel5282fd72009-04-03 08:27:52 +03001467 dump_sessionid(__func__, sessionid);
1468 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001469 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001470 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001471 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1472 NFS4_MAX_SESSIONID_LEN)) {
1473 return elem;
1474 }
1475 }
1476
1477 dprintk("%s: session not found\n", __func__);
1478 return NULL;
1479}
1480
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001481static struct nfsd4_session *
1482find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1483 __be32 *ret)
1484{
1485 struct nfsd4_session *session;
1486 __be32 status = nfserr_badsession;
1487
1488 session = __find_in_sessionid_hashtbl(sessionid, net);
1489 if (!session)
1490 goto out;
1491 status = nfsd4_get_session_locked(session);
1492 if (status)
1493 session = NULL;
1494out:
1495 *ret = status;
1496 return session;
1497}
1498
Benny Halevy9089f1b2010-05-12 00:12:26 +03001499/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001500static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001501unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001502{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001503 struct nfs4_client *clp = ses->se_client;
1504 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1505
1506 lockdep_assert_held(&nn->client_lock);
1507
Andy Adamson7116ed62009-04-03 08:27:43 +03001508 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001509 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001510 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001511 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001512}
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1515static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001516STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001518 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001520 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001521 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return 1;
1523}
1524
1525/*
1526 * XXX Should we use a slab cache ?
1527 * This type of memory management is somewhat inefficient, but we use it
1528 * anyway since SETCLIENTID is not a common operation.
1529 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001530static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
1532 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001533 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001535 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1536 if (clp == NULL)
1537 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001538 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001539 if (clp->cl_name.data == NULL)
1540 goto err_no_name;
1541 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1542 OWNER_HASH_SIZE, GFP_KERNEL);
1543 if (!clp->cl_ownerstr_hashtbl)
1544 goto err_no_hashtbl;
1545 for (i = 0; i < OWNER_HASH_SIZE; i++)
1546 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001547 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001548 INIT_LIST_HEAD(&clp->cl_sessions);
1549 idr_init(&clp->cl_stateids);
1550 atomic_set(&clp->cl_refcount, 0);
1551 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1552 INIT_LIST_HEAD(&clp->cl_idhash);
1553 INIT_LIST_HEAD(&clp->cl_openowners);
1554 INIT_LIST_HEAD(&clp->cl_delegations);
1555 INIT_LIST_HEAD(&clp->cl_lru);
1556 INIT_LIST_HEAD(&clp->cl_callbacks);
1557 INIT_LIST_HEAD(&clp->cl_revoked);
1558 spin_lock_init(&clp->cl_lock);
1559 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001561err_no_hashtbl:
1562 kfree(clp->cl_name.data);
1563err_no_name:
1564 kfree(clp);
1565 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001568static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569free_client(struct nfs4_client *clp)
1570{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001571 while (!list_empty(&clp->cl_sessions)) {
1572 struct nfsd4_session *ses;
1573 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1574 se_perclnt);
1575 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001576 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1577 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001578 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001579 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001580 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001581 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001583 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 kfree(clp);
1585}
1586
Benny Halevy84d38ac2010-05-12 00:13:16 +03001587/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001588static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001589unhash_client_locked(struct nfs4_client *clp)
1590{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001591 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001592 struct nfsd4_session *ses;
1593
Trond Myklebust0a880a22014-07-30 08:27:10 -04001594 lockdep_assert_held(&nn->client_lock);
1595
Trond Myklebust4beb3452014-07-30 08:27:02 -04001596 /* Mark the client as expired! */
1597 clp->cl_time = 0;
1598 /* Make it invisible */
1599 if (!list_empty(&clp->cl_idhash)) {
1600 list_del_init(&clp->cl_idhash);
1601 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1602 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1603 else
1604 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1605 }
1606 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001607 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001608 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1609 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001610 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001611}
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001614unhash_client(struct nfs4_client *clp)
1615{
1616 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1617
1618 spin_lock(&nn->client_lock);
1619 unhash_client_locked(clp);
1620 spin_unlock(&nn->client_lock);
1621}
1622
Jeff Layton97403d92014-07-30 08:27:12 -04001623static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1624{
1625 if (atomic_read(&clp->cl_refcount))
1626 return nfserr_jukebox;
1627 unhash_client_locked(clp);
1628 return nfs_ok;
1629}
1630
Trond Myklebust4beb3452014-07-30 08:27:02 -04001631static void
1632__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001634 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 struct list_head reaplist;
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001639 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001640 while (!list_empty(&clp->cl_delegations)) {
1641 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001642 unhash_delegation_locked(dp);
1643 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 }
Benny Halevycdc97502014-05-30 09:09:30 -04001645 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 while (!list_empty(&reaplist)) {
1647 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001648 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001649 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001651 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001652 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001653 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001654 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001655 }
NeilBrownea1da632005-06-23 22:04:17 -07001656 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001657 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Jeff Laytond3134b12014-07-29 21:34:32 -04001658 atomic_inc(&oo->oo_owner.so_count);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001659 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001661 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001662 if (clp->cl_cb_conn.cb_xprt)
1663 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001664 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
1666
Trond Myklebust4beb3452014-07-30 08:27:02 -04001667static void
1668destroy_client(struct nfs4_client *clp)
1669{
1670 unhash_client(clp);
1671 __destroy_client(clp);
1672}
1673
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001674static void expire_client(struct nfs4_client *clp)
1675{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001676 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001677 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001678 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001679}
1680
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001681static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1682{
1683 memcpy(target->cl_verifier.data, source->data,
1684 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001687static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1688{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1690 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1691}
1692
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001693static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001694{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001695 if (source->cr_principal) {
1696 target->cr_principal =
1697 kstrdup(source->cr_principal, GFP_KERNEL);
1698 if (target->cr_principal == NULL)
1699 return -ENOMEM;
1700 } else
1701 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001702 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 target->cr_uid = source->cr_uid;
1704 target->cr_gid = source->cr_gid;
1705 target->cr_group_info = source->cr_group_info;
1706 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001707 target->cr_gss_mech = source->cr_gss_mech;
1708 if (source->cr_gss_mech)
1709 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001710 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711}
1712
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001713static long long
1714compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1715{
1716 long long res;
1717
1718 res = o1->len - o2->len;
1719 if (res)
1720 return res;
1721 return (long long)memcmp(o1->data, o2->data, o1->len);
1722}
1723
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001724static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001725{
NeilBrowna55370a2005-06-23 22:03:52 -07001726 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728
1729static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001730same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1731{
1732 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
1734
1735static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001736same_clid(clientid_t *cl1, clientid_t *cl2)
1737{
1738 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739}
1740
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001741static bool groups_equal(struct group_info *g1, struct group_info *g2)
1742{
1743 int i;
1744
1745 if (g1->ngroups != g2->ngroups)
1746 return false;
1747 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001748 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001749 return false;
1750 return true;
1751}
1752
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001753/*
1754 * RFC 3530 language requires clid_inuse be returned when the
1755 * "principal" associated with a requests differs from that previously
1756 * used. We use uid, gid's, and gss principal string as our best
1757 * approximation. We also don't want to allow non-gss use of a client
1758 * established using gss: in theory cr_principal should catch that
1759 * change, but in practice cr_principal can be null even in the gss case
1760 * since gssd doesn't always pass down a principal string.
1761 */
1762static bool is_gss_cred(struct svc_cred *cr)
1763{
1764 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1765 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1766}
1767
1768
Vivek Trivedi5559b502012-07-24 21:18:20 +05301769static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001770same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1771{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001772 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001773 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1774 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001775 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1776 return false;
1777 if (cr1->cr_principal == cr2->cr_principal)
1778 return true;
1779 if (!cr1->cr_principal || !cr2->cr_principal)
1780 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301781 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782}
1783
J. Bruce Fields57266a62013-04-13 14:27:29 -04001784static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1785{
1786 struct svc_cred *cr = &rqstp->rq_cred;
1787 u32 service;
1788
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001789 if (!cr->cr_gss_mech)
1790 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001791 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1792 return service == RPC_GSS_SVC_INTEGRITY ||
1793 service == RPC_GSS_SVC_PRIVACY;
1794}
1795
1796static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1797{
1798 struct svc_cred *cr = &rqstp->rq_cred;
1799
1800 if (!cl->cl_mach_cred)
1801 return true;
1802 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1803 return false;
1804 if (!svc_rqst_integrity_protected(rqstp))
1805 return false;
1806 if (!cr->cr_principal)
1807 return false;
1808 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1809}
1810
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001811static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001812{
1813 static u32 current_clientid = 1;
1814
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001815 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 clp->cl_clientid.cl_id = current_clientid++;
1817}
1818
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001819static void gen_confirm(struct nfs4_client *clp)
1820{
Chuck Leverab4684d2012-03-02 17:13:50 -05001821 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001822 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Jeff Laytonf4199922014-06-17 07:44:11 -04001824 /*
1825 * This is opaque to client, so no need to byte-swap. Use
1826 * __force to keep sparse happy
1827 */
1828 verf[0] = (__force __be32)get_seconds();
1829 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001830 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831}
1832
Jeff Layton4770d722014-07-29 21:34:10 -04001833static struct nfs4_stid *
1834find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001835{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001836 struct nfs4_stid *ret;
1837
1838 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1839 if (!ret || !ret->sc_type)
1840 return NULL;
1841 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001842}
1843
Jeff Layton4770d722014-07-29 21:34:10 -04001844static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001845find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001846{
1847 struct nfs4_stid *s;
1848
Jeff Layton4770d722014-07-29 21:34:10 -04001849 spin_lock(&cl->cl_lock);
1850 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001851 if (s != NULL) {
1852 if (typemask & s->sc_type)
1853 atomic_inc(&s->sc_count);
1854 else
1855 s = NULL;
1856 }
Jeff Layton4770d722014-07-29 21:34:10 -04001857 spin_unlock(&cl->cl_lock);
1858 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001859}
1860
Jeff Layton2216d442012-11-12 15:00:57 -05001861static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001862 struct svc_rqst *rqstp, nfs4_verifier *verf)
1863{
1864 struct nfs4_client *clp;
1865 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001866 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001867 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001868
1869 clp = alloc_client(name);
1870 if (clp == NULL)
1871 return NULL;
1872
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001873 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1874 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001875 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001876 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001877 }
Jeff Layton02e12152014-07-16 10:31:57 -04001878 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001879 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001880 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001881 copy_verf(clp, verf);
1882 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001883 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001884 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001885 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001886 return clp;
1887}
1888
NeilBrownfd39ca92005-06-23 22:04:03 -07001889static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001890add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1891{
1892 struct rb_node **new = &(root->rb_node), *parent = NULL;
1893 struct nfs4_client *clp;
1894
1895 while (*new) {
1896 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1897 parent = *new;
1898
1899 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1900 new = &((*new)->rb_left);
1901 else
1902 new = &((*new)->rb_right);
1903 }
1904
1905 rb_link_node(&new_clp->cl_namenode, parent, new);
1906 rb_insert_color(&new_clp->cl_namenode, root);
1907}
1908
1909static struct nfs4_client *
1910find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1911{
1912 long long cmp;
1913 struct rb_node *node = root->rb_node;
1914 struct nfs4_client *clp;
1915
1916 while (node) {
1917 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1918 cmp = compare_blob(&clp->cl_name, name);
1919 if (cmp > 0)
1920 node = node->rb_left;
1921 else if (cmp < 0)
1922 node = node->rb_right;
1923 else
1924 return clp;
1925 }
1926 return NULL;
1927}
1928
1929static void
1930add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
1932 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001933 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Trond Myklebust0a880a22014-07-30 08:27:10 -04001935 lockdep_assert_held(&nn->client_lock);
1936
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001937 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001938 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001940 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001941 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942}
1943
NeilBrownfd39ca92005-06-23 22:04:03 -07001944static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945move_to_confirmed(struct nfs4_client *clp)
1946{
1947 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001948 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Trond Myklebust0a880a22014-07-30 08:27:10 -04001950 lockdep_assert_held(&nn->client_lock);
1951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001953 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001954 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001955 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001956 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001957 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958}
1959
1960static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001961find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
1963 struct nfs4_client *clp;
1964 unsigned int idhashval = clientid_hashval(clid->cl_id);
1965
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001966 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001967 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001968 if ((bool)clp->cl_minorversion != sessions)
1969 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001970 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 }
1974 return NULL;
1975}
1976
1977static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001978find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1979{
1980 struct list_head *tbl = nn->conf_id_hashtbl;
1981
Trond Myklebust0a880a22014-07-30 08:27:10 -04001982 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001983 return find_client_in_id_table(tbl, clid, sessions);
1984}
1985
1986static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001987find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001989 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Trond Myklebust0a880a22014-07-30 08:27:10 -04001991 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001992 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001995static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001996{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001997 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001998}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001999
NeilBrown28ce6052005-06-23 22:03:56 -07002000static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002001find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002002{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002003 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002004 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002005}
2006
2007static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002008find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002009{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002010 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002011 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002012}
2013
NeilBrownfd39ca92005-06-23 22:04:03 -07002014static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002015gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002017 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002018 struct sockaddr *sa = svc_addr(rqstp);
2019 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002020 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Jeff Layton7077ecb2009-08-14 12:57:58 -04002022 /* Currently, we only support tcp and tcp6 for the callback channel */
2023 if (se->se_callback_netid_len == 3 &&
2024 !memcmp(se->se_callback_netid_val, "tcp", 3))
2025 expected_family = AF_INET;
2026 else if (se->se_callback_netid_len == 4 &&
2027 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2028 expected_family = AF_INET6;
2029 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 goto out_err;
2031
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002032 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002033 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002034 (struct sockaddr *)&conn->cb_addr,
2035 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002036
J. Bruce Fields07263f12010-05-31 19:09:40 -04002037 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002039
J. Bruce Fields07263f12010-05-31 19:09:40 -04002040 if (conn->cb_addr.ss_family == AF_INET6)
2041 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002042
J. Bruce Fields07263f12010-05-31 19:09:40 -04002043 conn->cb_prog = se->se_callback_prog;
2044 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002045 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 return;
2047out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002048 conn->cb_addr.ss_family = AF_UNSPEC;
2049 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002050 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 "will not receive delegations\n",
2052 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 return;
2055}
2056
Andy Adamson074fe892009-04-03 08:28:15 +03002057/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002058 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002059 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002060static void
Andy Adamson074fe892009-04-03 08:28:15 +03002061nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2062{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002063 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002064 struct nfsd4_slot *slot = resp->cstate.slot;
2065 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002066
Andy Adamson557ce262009-08-28 08:45:04 -04002067 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002068
Andy Adamson557ce262009-08-28 08:45:04 -04002069 slot->sl_opcnt = resp->opcnt;
2070 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002071
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002072 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002073 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002074 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002075 return;
2076 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002077 base = resp->cstate.data_offset;
2078 slot->sl_datalen = buf->len - base;
2079 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04002080 WARN("%s: sessions DRC could not cache compound\n", __func__);
2081 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002082}
2083
2084/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002085 * Encode the replay sequence operation from the slot values.
2086 * If cachethis is FALSE encode the uncached rep error on the next
2087 * operation which sets resp->p and increments resp->opcnt for
2088 * nfs4svc_encode_compoundres.
2089 *
Andy Adamson074fe892009-04-03 08:28:15 +03002090 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002091static __be32
2092nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2093 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002094{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002095 struct nfsd4_op *op;
2096 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002097
Andy Adamsonabfabf82009-07-23 19:02:18 -04002098 /* Encode the replayed sequence operation */
2099 op = &args->ops[resp->opcnt - 1];
2100 nfsd4_encode_operation(resp, op);
2101
2102 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002103 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002104 op = &args->ops[resp->opcnt++];
2105 op->status = nfserr_retry_uncached_rep;
2106 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002107 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002108 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002109}
2110
2111/*
Andy Adamson557ce262009-08-28 08:45:04 -04002112 * The sequence operation is not cached because we can use the slot and
2113 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002114 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002115static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002116nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2117 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002118{
Andy Adamson557ce262009-08-28 08:45:04 -04002119 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002120 struct xdr_stream *xdr = &resp->xdr;
2121 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002122 __be32 status;
2123
Andy Adamson557ce262009-08-28 08:45:04 -04002124 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002125
Andy Adamsonabfabf82009-07-23 19:02:18 -04002126 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002127 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002128 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002129
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002130 p = xdr_reserve_space(xdr, slot->sl_datalen);
2131 if (!p) {
2132 WARN_ON_ONCE(1);
2133 return nfserr_serverfault;
2134 }
2135 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2136 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002137
Andy Adamson557ce262009-08-28 08:45:04 -04002138 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002139 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002140}
2141
Andy Adamson0733d212009-04-03 08:28:01 +03002142/*
2143 * Set the exchange_id flags returned by the server.
2144 */
2145static void
2146nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2147{
2148 /* pNFS is not supported */
2149 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2150
2151 /* Referrals are supported, Migration is not. */
2152 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2153
2154 /* set the wire flags to return to client. */
2155 clid->flags = new->cl_exchange_flags;
2156}
2157
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002158static bool client_has_state(struct nfs4_client *clp)
2159{
2160 /*
2161 * Note clp->cl_openowners check isn't quite right: there's no
2162 * need to count owners without stateid's.
2163 *
2164 * Also note we should probably be using this in 4.0 case too.
2165 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002166 return !list_empty(&clp->cl_openowners)
2167 || !list_empty(&clp->cl_delegations)
2168 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002169}
2170
Al Virob37ad282006-10-19 23:28:59 -07002171__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002172nfsd4_exchange_id(struct svc_rqst *rqstp,
2173 struct nfsd4_compound_state *cstate,
2174 struct nfsd4_exchange_id *exid)
2175{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002176 struct nfs4_client *conf, *new;
2177 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002178 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002179 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002180 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002181 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002182 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002183 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002184
Jeff Layton363168b2009-08-14 12:57:56 -04002185 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002186 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002187 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002188 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002189 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002190
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002191 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002192 return nfserr_inval;
2193
Andy Adamson0733d212009-04-03 08:28:01 +03002194 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002195 case SP4_MACH_CRED:
2196 if (!svc_rqst_integrity_protected(rqstp))
2197 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002198 case SP4_NONE:
2199 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002200 default: /* checked by xdr code */
2201 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002202 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002203 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002204 }
2205
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002206 new = create_client(exid->clname, rqstp, &verf);
2207 if (new == NULL)
2208 return nfserr_jukebox;
2209
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002210 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03002211 nfs4_lock_state();
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002212 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002213 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002214 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002215 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2216 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2217
J. Bruce Fields136e6582012-05-12 20:37:23 -04002218 if (update) {
2219 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002220 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002221 goto out;
2222 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002223 if (!mach_creds_match(conf, rqstp)) {
2224 status = nfserr_wrong_cred;
2225 goto out;
2226 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002227 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002228 status = nfserr_perm;
2229 goto out;
2230 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002231 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002232 status = nfserr_not_same;
2233 goto out;
2234 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002235 /* case 6 */
2236 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002237 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002238 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002239 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002240 if (client_has_state(conf)) {
2241 status = nfserr_clid_inuse;
2242 goto out;
2243 }
Andy Adamson0733d212009-04-03 08:28:01 +03002244 goto out_new;
2245 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002246 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002247 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002248 goto out_copy;
2249 }
2250 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002251 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002252 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002253 }
2254
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002255 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002256 status = nfserr_noent;
2257 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002258 }
2259
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002260 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002261 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002262 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002263
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002264 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002265out_new:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002266 if (conf)
2267 unhash_client_locked(conf);
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002268 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002269 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002270
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002271 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002272 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002273 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002274out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002275 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2276 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002277
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002278 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2279 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002280
2281 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002282 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002283 status = nfs_ok;
2284
2285out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002286 spin_unlock(&nn->client_lock);
Andy Adamson0733d212009-04-03 08:28:01 +03002287 nfs4_unlock_state();
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002288 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002289 expire_client(new);
2290 if (unconf)
2291 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002292 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002293}
2294
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002295static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002296check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002297{
Andy Adamson88e588d2009-07-23 19:02:15 -04002298 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2299 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002300
2301 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002302 if (slot_inuse) {
2303 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002304 return nfserr_jukebox;
2305 else
2306 return nfserr_seq_misordered;
2307 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002308 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002309 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002310 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002311 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002312 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002313 return nfserr_seq_misordered;
2314}
2315
Andy Adamson49557cc2009-07-23 19:02:16 -04002316/*
2317 * Cache the create session result into the create session single DRC
2318 * slot cache by saving the xdr structure. sl_seqid has been set.
2319 * Do this for solo or embedded create session operations.
2320 */
2321static void
2322nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002323 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002324{
2325 slot->sl_status = nfserr;
2326 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2327}
2328
2329static __be32
2330nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2331 struct nfsd4_clid_slot *slot)
2332{
2333 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2334 return slot->sl_status;
2335}
2336
Mi Jinlong1b74c252011-07-14 14:50:17 +08002337#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2338 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2339 1 + /* MIN tag is length with zero, only length */ \
2340 3 + /* version, opcount, opcode */ \
2341 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2342 /* seqid, slotID, slotID, cache */ \
2343 4 ) * sizeof(__be32))
2344
2345#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2346 2 + /* verifier: AUTH_NULL, length 0 */\
2347 1 + /* status */ \
2348 1 + /* MIN tag is length with zero, only length */ \
2349 3 + /* opcount, opcode, opstatus*/ \
2350 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2351 /* seqid, slotID, slotID, slotID, status */ \
2352 5 ) * sizeof(__be32))
2353
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002354static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002355{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002356 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2357
J. Bruce Fields373cd402013-04-08 15:42:12 -04002358 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2359 return nfserr_toosmall;
2360 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2361 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002362 ca->headerpadsz = 0;
2363 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2364 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2365 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2366 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2367 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2368 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2369 /*
2370 * Note decreasing slot size below client's request may make it
2371 * difficult for client to function correctly, whereas
2372 * decreasing the number of slots will (just?) affect
2373 * performance. When short on memory we therefore prefer to
2374 * decrease number of slots instead of their size. Clients that
2375 * request larger slots than they need will get poor results:
2376 */
2377 ca->maxreqs = nfsd4_get_drc_mem(ca);
2378 if (!ca->maxreqs)
2379 return nfserr_jukebox;
2380
J. Bruce Fields373cd402013-04-08 15:42:12 -04002381 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002382}
2383
Kinglong Mee8a891632013-12-23 18:11:02 +08002384#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2385 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2386#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2387 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2388
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002389static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2390{
2391 ca->headerpadsz = 0;
2392
2393 /*
2394 * These RPC_MAX_HEADER macros are overkill, especially since we
2395 * don't even do gss on the backchannel yet. But this is still
2396 * less than 1k. Tighten up this estimate in the unlikely event
2397 * it turns out to be a problem for some client:
2398 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002399 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002400 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002401 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002402 return nfserr_toosmall;
2403 ca->maxresp_cached = 0;
2404 if (ca->maxops < 2)
2405 return nfserr_toosmall;
2406
2407 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002408}
2409
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002410static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2411{
2412 switch (cbs->flavor) {
2413 case RPC_AUTH_NULL:
2414 case RPC_AUTH_UNIX:
2415 return nfs_ok;
2416 default:
2417 /*
2418 * GSS case: the spec doesn't allow us to return this
2419 * error. But it also doesn't allow us not to support
2420 * GSS.
2421 * I'd rather this fail hard than return some error the
2422 * client might think it can already handle:
2423 */
2424 return nfserr_encr_alg_unsupp;
2425 }
2426}
2427
Andy Adamson069b6ad2009-04-03 08:27:58 +03002428__be32
2429nfsd4_create_session(struct svc_rqst *rqstp,
2430 struct nfsd4_compound_state *cstate,
2431 struct nfsd4_create_session *cr_ses)
2432{
Jeff Layton363168b2009-08-14 12:57:56 -04002433 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002434 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002435 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002436 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002437 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002438 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002439 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002440 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002441
Mi Jinlonga62573d2011-03-23 17:57:07 +08002442 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2443 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002444 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2445 if (status)
2446 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002447 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002448 if (status)
2449 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002450 status = check_backchannel_attrs(&cr_ses->back_channel);
2451 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002452 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002453 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002454 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002455 if (!new)
2456 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002457 conn = alloc_conn_from_crses(rqstp, cr_ses);
2458 if (!conn)
2459 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002460
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002461 nfs4_lock_state();
Jeff Laytond20c11d2014-07-30 08:27:07 -04002462 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002463 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002464 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002465 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002466
2467 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002468 status = nfserr_wrong_cred;
2469 if (!mach_creds_match(conf, rqstp))
2470 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002471 cs_slot = &conf->cl_cs_slot;
2472 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002473 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002474 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002475 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002476 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002477 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002478 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002479 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002480 } else if (unconf) {
2481 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002482 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002483 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002484 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002485 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002486 status = nfserr_wrong_cred;
2487 if (!mach_creds_match(unconf, rqstp))
2488 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002489 cs_slot = &unconf->cl_cs_slot;
2490 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002491 if (status) {
2492 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002493 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002494 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002495 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002496 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002497 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002498 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002499 if (status) {
2500 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002501 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002502 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002503 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002504 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002505 conf = unconf;
2506 } else {
2507 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002508 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002509 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002510 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002511 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002512 * We do not support RDMA or persistent sessions
2513 */
2514 cr_ses->flags &= ~SESSION4_PERSIST;
2515 cr_ses->flags &= ~SESSION4_RDMA;
2516
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002517 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002518 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002519
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002520 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002521 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002522 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002523 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002524
Jeff Laytond20c11d2014-07-30 08:27:07 -04002525 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002526 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002527 spin_unlock(&nn->client_lock);
2528 /* init connection and backchannel */
2529 nfsd4_init_conn(rqstp, conn, new);
2530 nfsd4_put_session(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002531 nfs4_unlock_state();
Jeff Laytond20c11d2014-07-30 08:27:07 -04002532 if (old)
2533 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002534 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002535out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002536 spin_unlock(&nn->client_lock);
Yanchuan Nian266533c2012-12-24 18:11:45 +08002537 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002538 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002539 if (old)
2540 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002541out_free_session:
2542 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002543out_release_drc_mem:
2544 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002545 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002546}
2547
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002548static __be32 nfsd4_map_bcts_dir(u32 *dir)
2549{
2550 switch (*dir) {
2551 case NFS4_CDFC4_FORE:
2552 case NFS4_CDFC4_BACK:
2553 return nfs_ok;
2554 case NFS4_CDFC4_FORE_OR_BOTH:
2555 case NFS4_CDFC4_BACK_OR_BOTH:
2556 *dir = NFS4_CDFC4_BOTH;
2557 return nfs_ok;
2558 };
2559 return nfserr_inval;
2560}
2561
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002562__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2563{
2564 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002565 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002566 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002567
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002568 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2569 if (status)
2570 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002571 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002572 session->se_cb_prog = bc->bc_cb_program;
2573 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002574 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002575
2576 nfsd4_probe_callback(session->se_client);
2577
2578 return nfs_ok;
2579}
2580
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002581__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2582 struct nfsd4_compound_state *cstate,
2583 struct nfsd4_bind_conn_to_session *bcts)
2584{
2585 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002586 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002587 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002588 struct net *net = SVC_NET(rqstp);
2589 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002590
2591 if (!nfsd4_last_compound_op(rqstp))
2592 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002593 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002594 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002595 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002596 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002597 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002598 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002599 status = nfserr_wrong_cred;
2600 if (!mach_creds_match(session->se_client, rqstp))
2601 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002602 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002603 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002604 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002605 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002606 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002607 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002608 goto out;
2609 nfsd4_init_conn(rqstp, conn, session);
2610 status = nfs_ok;
2611out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002612 nfsd4_put_session(session);
2613out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002614 nfs4_unlock_state();
2615 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002616}
2617
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002618static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2619{
2620 if (!session)
2621 return 0;
2622 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2623}
2624
Andy Adamson069b6ad2009-04-03 08:27:58 +03002625__be32
2626nfsd4_destroy_session(struct svc_rqst *r,
2627 struct nfsd4_compound_state *cstate,
2628 struct nfsd4_destroy_session *sessionid)
2629{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002630 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002631 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002632 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002633 struct net *net = SVC_NET(r);
2634 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002635
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002636 nfs4_lock_state();
2637 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002638 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002639 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002640 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002641 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002642 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002643 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002644 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002645 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002646 if (!ses)
2647 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002648 status = nfserr_wrong_cred;
2649 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002650 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002651 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002652 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002653 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002654 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002655 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002656
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002657 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002658
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002659 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002660 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002661out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002662 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002663out_client_lock:
2664 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002665out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002666 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002667 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002668}
2669
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002670static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002671{
2672 struct nfsd4_conn *c;
2673
2674 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002675 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002676 return c;
2677 }
2678 }
2679 return NULL;
2680}
2681
J. Bruce Fields57266a62013-04-13 14:27:29 -04002682static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002683{
2684 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002685 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002686 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002687 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002688
2689 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002690 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002691 if (c)
2692 goto out_free;
2693 status = nfserr_conn_not_bound_to_session;
2694 if (clp->cl_mach_cred)
2695 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002696 __nfsd4_hash_conn(new, ses);
2697 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002698 ret = nfsd4_register_conn(new);
2699 if (ret)
2700 /* oops; xprt is already down: */
2701 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002702 return nfs_ok;
2703out_free:
2704 spin_unlock(&clp->cl_lock);
2705 free_conn(new);
2706 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002707}
2708
Mi Jinlong868b89c2011-04-27 09:09:58 +08002709static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2710{
2711 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2712
2713 return args->opcnt > session->se_fchannel.maxops;
2714}
2715
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002716static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2717 struct nfsd4_session *session)
2718{
2719 struct xdr_buf *xb = &rqstp->rq_arg;
2720
2721 return xb->len > session->se_fchannel.maxreq_sz;
2722}
2723
Andy Adamson069b6ad2009-04-03 08:27:58 +03002724__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002725nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002726 struct nfsd4_compound_state *cstate,
2727 struct nfsd4_sequence *seq)
2728{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002729 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002730 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002731 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002732 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002733 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002734 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002735 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002736 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002737 struct net *net = SVC_NET(rqstp);
2738 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002739
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002740 if (resp->opcnt != 1)
2741 return nfserr_sequence_pos;
2742
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002743 /*
2744 * Will be either used or freed by nfsd4_sequence_check_conn
2745 * below.
2746 */
2747 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2748 if (!conn)
2749 return nfserr_jukebox;
2750
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002751 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002752 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002753 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002754 goto out_no_session;
2755 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002756
Mi Jinlong868b89c2011-04-27 09:09:58 +08002757 status = nfserr_too_many_ops;
2758 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002759 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002760
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002761 status = nfserr_req_too_big;
2762 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002763 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002764
Benny Halevyb85d4c02009-04-03 08:28:08 +03002765 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002766 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002767 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002768
Andy Adamson557ce262009-08-28 08:45:04 -04002769 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002770 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2771
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002772 /* We do not negotiate the number of slots yet, so set the
2773 * maxslots to the session maxreqs which is used to encode
2774 * sr_highest_slotid and the sr_target_slot id to maxslots */
2775 seq->maxslots = session->se_fchannel.maxreqs;
2776
J. Bruce Fields73e79482012-02-13 16:39:00 -05002777 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2778 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002779 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002780 status = nfserr_seq_misordered;
2781 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002782 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002783 cstate->slot = slot;
2784 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002785 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002786 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002787 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002788 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002789 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002790 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002791 }
2792 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002793 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002794
J. Bruce Fields57266a62013-04-13 14:27:29 -04002795 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002796 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002797 if (status)
2798 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002799
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002800 buflen = (seq->cachethis) ?
2801 session->se_fchannel.maxresp_cached :
2802 session->se_fchannel.maxresp_sz;
2803 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2804 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002805 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002806 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002807 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002808
2809 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002810 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002811 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002812 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002813 if (seq->cachethis)
2814 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002815 else
2816 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002817
2818 cstate->slot = slot;
2819 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002820 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002821
Benny Halevyb85d4c02009-04-03 08:28:08 +03002822out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002823 switch (clp->cl_cb_state) {
2824 case NFSD4_CB_DOWN:
2825 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2826 break;
2827 case NFSD4_CB_FAULT:
2828 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2829 break;
2830 default:
2831 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002832 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002833 if (!list_empty(&clp->cl_revoked))
2834 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002835out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002836 if (conn)
2837 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002838 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002839 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002840out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002841 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002842 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002843}
2844
Trond Myklebustb6076642014-06-30 11:48:35 -04002845void
2846nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2847{
2848 struct nfsd4_compound_state *cs = &resp->cstate;
2849
2850 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002851 if (cs->status != nfserr_replay_cache) {
2852 nfsd4_store_cache_entry(resp);
2853 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2854 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002855 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002856 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002857 } else if (cs->clp)
2858 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002859}
2860
Mi Jinlong345c2842011-10-20 17:51:39 +08002861__be32
2862nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2863{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002864 struct nfs4_client *conf, *unconf;
2865 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002866 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002867 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002868
2869 nfs4_lock_state();
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002870 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002871 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002872 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002873 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002874
2875 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002876 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002877 status = nfserr_clientid_busy;
2878 goto out;
2879 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002880 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08002881 } else if (unconf)
2882 clp = unconf;
2883 else {
2884 status = nfserr_stale_clientid;
2885 goto out;
2886 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002887 if (!mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002888 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002889 status = nfserr_wrong_cred;
2890 goto out;
2891 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002892 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002893out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002894 spin_unlock(&nn->client_lock);
Mi Jinlong345c2842011-10-20 17:51:39 +08002895 nfs4_unlock_state();
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002896 if (clp)
2897 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002898 return status;
2899}
2900
Andy Adamson069b6ad2009-04-03 08:27:58 +03002901__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002902nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2903{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002904 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002905
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002906 if (rc->rca_one_fs) {
2907 if (!cstate->current_fh.fh_dentry)
2908 return nfserr_nofilehandle;
2909 /*
2910 * We don't take advantage of the rca_one_fs case.
2911 * That's OK, it's optional, we can safely ignore it.
2912 */
2913 return nfs_ok;
2914 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002915
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002916 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002917 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002918 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2919 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002920 goto out;
2921
2922 status = nfserr_stale_clientid;
2923 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002924 /*
2925 * The following error isn't really legal.
2926 * But we only get here if the client just explicitly
2927 * destroyed the client. Surely it no longer cares what
2928 * error it gets back on an operation for the dead
2929 * client.
2930 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002931 goto out;
2932
2933 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002934 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002935out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002936 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002937 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002938}
2939
2940__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002941nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2942 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002944 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002946 struct nfs4_client *conf, *new;
2947 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002948 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002949 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2950
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002951 new = create_client(clname, rqstp, &clverifier);
2952 if (new == NULL)
2953 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002954 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 nfs4_lock_state();
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002956 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002957 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002958 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002959 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002961 if (clp_used_exchangeid(conf))
2962 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002963 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002964 char addr_str[INET6_ADDRSTRLEN];
2965 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2966 sizeof(addr_str));
2967 dprintk("NFSD: setclientid: string in use by client "
2968 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 goto out;
2970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002972 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002973 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002974 unhash_client_locked(unconf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002975 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002976 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002978 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002979 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002980 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002981 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002982 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2984 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2985 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002986 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 status = nfs_ok;
2988out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002989 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 nfs4_unlock_state();
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002991 if (new)
2992 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002993 if (unconf)
2994 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 return status;
2996}
2997
2998
Al Virob37ad282006-10-19 23:28:59 -07002999__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003000nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3001 struct nfsd4_compound_state *cstate,
3002 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003{
NeilBrown21ab45a2005-06-23 22:04:14 -07003004 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003005 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3007 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003008 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003009 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003011 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07003014
Jeff Laytond20c11d2014-07-30 08:27:07 -04003015 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003016 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003017 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003018 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003019 * We try hard to give out unique clientid's, so if we get an
3020 * attempt to confirm the same clientid with a different cred,
3021 * there's a bug somewhere. Let's charitably assume it's our
3022 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003023 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04003024 status = nfserr_serverfault;
3025 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3026 goto out;
3027 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3028 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003029 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003030 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3031 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003033 else /* case 4: client hasn't noticed we rebooted yet? */
3034 status = nfserr_stale_clientid;
3035 goto out;
3036 }
3037 status = nfs_ok;
3038 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003039 old = unconf;
3040 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003041 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003042 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003043 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3044 if (old) {
3045 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003046 if (status) {
3047 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003048 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003049 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003050 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003051 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003052 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003053 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003054 get_client_locked(conf);
3055 spin_unlock(&nn->client_lock);
3056 nfsd4_probe_callback(conf);
3057 spin_lock(&nn->client_lock);
3058 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003060 spin_unlock(&nn->client_lock);
3061 if (old)
3062 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 nfs4_unlock_state();
3064 return status;
3065}
3066
J. Bruce Fields32513b42011-10-13 16:00:16 -04003067static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003069 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3070}
3071
3072/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003073static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003074{
Trond Myklebustca943212014-07-23 16:17:39 -04003075 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Trond Myklebust950e0112014-06-30 11:48:31 -04003077 lockdep_assert_held(&state_lock);
3078
J. Bruce Fields32513b42011-10-13 16:00:16 -04003079 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003080 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003081 INIT_LIST_HEAD(&fp->fi_stateids);
3082 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003083 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003084 fp->fi_had_conflict = false;
3085 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003086 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003087 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3088 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04003089 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090}
3091
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003092void
NeilBrowne60d4392005-06-23 22:03:01 -07003093nfsd4_free_slabs(void)
3094{
Christoph Hellwigabf11352014-05-21 07:43:03 -07003095 kmem_cache_destroy(openowner_slab);
3096 kmem_cache_destroy(lockowner_slab);
3097 kmem_cache_destroy(file_slab);
3098 kmem_cache_destroy(stateid_slab);
3099 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003100}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Bryan Schumaker72083392011-11-01 15:24:59 -04003102int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103nfsd4_init_slabs(void)
3104{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003105 openowner_slab = kmem_cache_create("nfsd4_openowners",
3106 sizeof(struct nfs4_openowner), 0, 0, NULL);
3107 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003108 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003109 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003110 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003111 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003112 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003113 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003114 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003115 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003116 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003117 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003118 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003119 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003120 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003121 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003122 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003123 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003124 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003126
3127out_free_stateid_slab:
3128 kmem_cache_destroy(stateid_slab);
3129out_free_file_slab:
3130 kmem_cache_destroy(file_slab);
3131out_free_lockowner_slab:
3132 kmem_cache_destroy(lockowner_slab);
3133out_free_openowner_slab:
3134 kmem_cache_destroy(openowner_slab);
3135out:
NeilBrowne60d4392005-06-23 22:03:01 -07003136 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3137 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138}
3139
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003140static void init_nfs4_replay(struct nfs4_replay *rp)
3141{
3142 rp->rp_status = nfserr_serverfault;
3143 rp->rp_buflen = 0;
3144 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003145 mutex_init(&rp->rp_mutex);
3146}
3147
3148static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3149 struct nfs4_stateowner *so)
3150{
3151 if (!nfsd4_has_session(cstate)) {
3152 mutex_lock(&so->so_replay.rp_mutex);
3153 cstate->replay_owner = so;
3154 atomic_inc(&so->so_count);
3155 }
3156}
3157
3158void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3159{
3160 struct nfs4_stateowner *so = cstate->replay_owner;
3161
3162 if (so != NULL) {
3163 cstate->replay_owner = NULL;
3164 mutex_unlock(&so->so_replay.rp_mutex);
3165 nfs4_put_stateowner(so);
3166 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003167}
3168
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003169static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170{
3171 struct nfs4_stateowner *sop;
3172
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003173 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003174 if (!sop)
3175 return NULL;
3176
3177 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3178 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003179 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003180 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003182 sop->so_owner.len = owner->len;
3183
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003184 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003185 sop->so_client = clp;
3186 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003187 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003188 return sop;
3189}
3190
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003191static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003192{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003193 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003194
Trond Myklebustd4f04892014-07-29 21:34:36 -04003195 list_add(&oo->oo_owner.so_strhash,
3196 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003197 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198}
3199
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003200static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3201{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003202 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003203}
3204
Jeff Layton6b180f02014-07-29 21:34:26 -04003205static void nfs4_free_openowner(struct nfs4_stateowner *so)
3206{
3207 struct nfs4_openowner *oo = openowner(so);
3208
3209 kmem_cache_free(openowner_slab, oo);
3210}
3211
3212static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003213 .so_unhash = nfs4_unhash_openowner,
3214 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003215};
3216
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003217static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003218alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003219 struct nfsd4_compound_state *cstate)
3220{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003221 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003222 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003224 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3225 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003227 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003228 oo->oo_owner.so_is_open_owner = 1;
3229 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003230 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003231 if (nfsd4_has_session(cstate))
3232 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003233 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003234 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003235 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003236 spin_lock(&clp->cl_lock);
3237 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003238 if (ret == NULL) {
3239 hash_openowner(oo, clp, strhashval);
3240 ret = oo;
3241 } else
3242 nfs4_free_openowner(&oo->oo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003243 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003244 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245}
3246
J. Bruce Fields996e0932011-10-17 11:14:48 -04003247static 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 -04003248 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003250 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003251 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003252 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003253 stp->st_stateowner = &oo->oo_owner;
Jeff Laytond3134b12014-07-29 21:34:32 -04003254 atomic_inc(&stp->st_stateowner->so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07003255 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003256 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 stp->st_access_bmap = 0;
3258 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003259 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003260 spin_lock(&oo->oo_owner.so_client->cl_lock);
3261 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003262 spin_lock(&fp->fi_lock);
3263 list_add(&stp->st_perfile, &fp->fi_stateids);
3264 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003265 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266}
3267
Jeff Laytond3134b12014-07-29 21:34:32 -04003268/*
3269 * In the 4.0 case we need to keep the owners around a little while to handle
3270 * CLOSE replay. We still do need to release any file access that is held by
3271 * them before returning however.
3272 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003274move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275{
Jeff Layton217526e2014-07-30 08:27:11 -04003276 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003277 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3278 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3279 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003280
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003281 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Jeff Laytonb401be222014-07-29 21:34:33 -04003283 /*
3284 * We know that we hold one reference via nfsd4_close, and another
3285 * "persistent" reference for the client. If the refcount is higher
3286 * than 2, then there are still calls in progress that are using this
3287 * stateid. We can't put the sc_file reference until they are finished.
3288 * Wait for the refcount to drop to 2. Since it has been unhashed,
3289 * there should be no danger of the refcount going back up again at
3290 * this point.
3291 */
3292 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3293
Jeff Laytond3134b12014-07-29 21:34:32 -04003294 release_all_access(s);
3295 if (s->st_stid.sc_file) {
3296 put_nfs4_file(s->st_stid.sc_file);
3297 s->st_stid.sc_file = NULL;
3298 }
Jeff Layton217526e2014-07-30 08:27:11 -04003299
3300 spin_lock(&nn->client_lock);
3301 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003302 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003303 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003304 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003305 spin_unlock(&nn->client_lock);
3306 if (last)
3307 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308}
3309
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310/* search file_hashtbl[] for file */
3311static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003312find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313{
Trond Myklebustca943212014-07-23 16:17:39 -04003314 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 struct nfs4_file *fp;
3316
Trond Myklebust950e0112014-06-30 11:48:31 -04003317 lockdep_assert_held(&state_lock);
3318
Jeff Layton89876f82013-04-02 09:01:59 -04003319 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003320 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003321 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 }
3325 return NULL;
3326}
3327
Trond Myklebust950e0112014-06-30 11:48:31 -04003328static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003329find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003330{
3331 struct nfs4_file *fp;
3332
3333 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003334 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003335 spin_unlock(&state_lock);
3336 return fp;
3337}
3338
3339static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003340find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003341{
3342 struct nfs4_file *fp;
3343
3344 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003345 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003346 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003347 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003348 fp = new;
3349 }
3350 spin_unlock(&state_lock);
3351
3352 return fp;
3353}
3354
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003355/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 * Called to check deny when READ with all zero stateid or
3357 * WRITE with all zero or all one stateid
3358 */
Al Virob37ad282006-10-19 23:28:59 -07003359static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3361{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003363 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364
Trond Myklebustca943212014-07-23 16:17:39 -04003365 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003366 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003367 return ret;
3368 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003369 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003370 if (fp->fi_share_deny & deny_type)
3371 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003372 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003373 put_nfs4_file(fp);
3374 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375}
3376
Jeff Layton02e12152014-07-16 10:31:57 -04003377void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378{
Trond Myklebust11b91642014-07-29 21:34:08 -04003379 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3380 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003381
Trond Myklebust11b91642014-07-29 21:34:08 -04003382 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003383
Jeff Layton02e12152014-07-16 10:31:57 -04003384 /*
3385 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003386 * already holding inode->i_lock.
3387 *
Jeff Laytondff13992014-07-08 14:02:49 -04003388 * If the dl_time != 0, then we know that it has already been
3389 * queued for a lease break. Don't queue it again.
3390 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003391 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003392 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003393 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003394 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003395 }
Jeff Layton02e12152014-07-16 10:31:57 -04003396 spin_unlock(&state_lock);
3397}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398
Jeff Layton02e12152014-07-16 10:31:57 -04003399static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3400{
3401 /*
3402 * We're assuming the state code never drops its reference
3403 * without first removing the lease. Since we're in this lease
3404 * callback (and since the lease code is serialized by the kernel
3405 * lock) we know the server hasn't removed the lease yet, we know
3406 * it's safe to take a reference.
3407 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003408 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003409 nfsd4_cb_recall(dp);
3410}
3411
Jeff Layton1c8c6012013-06-21 08:58:15 -04003412/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003413static void nfsd_break_deleg_cb(struct file_lock *fl)
3414{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003415 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3416 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003417
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003418 if (!fp) {
3419 WARN(1, "(%p)->fl_owner NULL\n", fl);
3420 return;
3421 }
3422 if (fp->fi_had_conflict) {
3423 WARN(1, "duplicate break on %p\n", fp);
3424 return;
3425 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003426 /*
3427 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003428 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003429 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003430 */
3431 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432
Jeff Layton02e12152014-07-16 10:31:57 -04003433 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003434 fp->fi_had_conflict = true;
3435 /*
3436 * If there are no delegations on the list, then we can't count on this
3437 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3438 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3439 * true should keep any new delegations from being hashed.
3440 */
3441 if (list_empty(&fp->fi_delegations))
3442 fl->fl_break_time = jiffies;
3443 else
3444 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3445 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003446 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447}
3448
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449static
3450int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3451{
3452 if (arg & F_UNLCK)
3453 return lease_modify(onlist, arg);
3454 else
3455 return -EAGAIN;
3456}
3457
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003458static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003459 .lm_break = nfsd_break_deleg_cb,
3460 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461};
3462
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003463static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3464{
3465 if (nfsd4_has_session(cstate))
3466 return nfs_ok;
3467 if (seqid == so->so_seqid - 1)
3468 return nfserr_replay_me;
3469 if (seqid == so->so_seqid)
3470 return nfs_ok;
3471 return nfserr_bad_seqid;
3472}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
Jeff Layton4b24ca72014-06-30 11:48:44 -04003474static __be32 lookup_clientid(clientid_t *clid,
3475 struct nfsd4_compound_state *cstate,
3476 struct nfsd_net *nn)
3477{
3478 struct nfs4_client *found;
3479
3480 if (cstate->clp) {
3481 found = cstate->clp;
3482 if (!same_clid(&found->cl_clientid, clid))
3483 return nfserr_stale_clientid;
3484 return nfs_ok;
3485 }
3486
3487 if (STALE_CLIENTID(clid, nn))
3488 return nfserr_stale_clientid;
3489
3490 /*
3491 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3492 * cached already then we know this is for is for v4.0 and "sessions"
3493 * will be false.
3494 */
3495 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003496 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003497 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003498 if (!found) {
3499 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003500 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003501 }
3502 atomic_inc(&found->cl_refcount);
3503 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003504
3505 /* Cache the nfs4_client in cstate! */
3506 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003507 return nfs_ok;
3508}
3509
Al Virob37ad282006-10-19 23:28:59 -07003510__be32
Andy Adamson66689582009-04-03 08:28:45 +03003511nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003512 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 clientid_t *clientid = &open->op_clientid;
3515 struct nfs4_client *clp = NULL;
3516 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003517 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003518 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003520 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003522 /*
3523 * In case we need it later, after we've already created the
3524 * file and don't want to risk a further failure:
3525 */
3526 open->op_file = nfsd4_alloc_file();
3527 if (open->op_file == NULL)
3528 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529
Trond Myklebust2d91e892014-06-30 11:48:46 -04003530 status = lookup_clientid(clientid, cstate, nn);
3531 if (status)
3532 return status;
3533 clp = cstate->clp;
3534
Trond Myklebustd4f04892014-07-29 21:34:36 -04003535 strhashval = ownerstr_hashval(&open->op_owner);
3536 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003537 open->op_openowner = oo;
3538 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003539 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003541 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003542 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003543 release_openowner(oo);
3544 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003545 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003546 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003547 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3548 if (status)
3549 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003550 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003551new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003552 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003553 if (oo == NULL)
3554 return nfserr_jukebox;
3555 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003556alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003557 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003558 if (!open->op_stp)
3559 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003560 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561}
3562
Al Virob37ad282006-10-19 23:28:59 -07003563static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003564nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3565{
3566 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3567 return nfserr_openmode;
3568 else
3569 return nfs_ok;
3570}
3571
Daniel Mackc47d8322011-05-16 16:38:14 +02003572static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003573{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003574 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3575}
3576
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003577static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003578{
3579 struct nfs4_stid *ret;
3580
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003581 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003582 if (!ret)
3583 return NULL;
3584 return delegstateid(ret);
3585}
3586
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003587static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3588{
3589 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3590 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3591}
3592
Al Virob37ad282006-10-19 23:28:59 -07003593static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003594nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003595 struct nfs4_delegation **dp)
3596{
3597 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003598 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003599 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003600
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003601 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3602 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003603 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003604 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003605 status = nfs4_check_delegmode(deleg, flags);
3606 if (status) {
3607 nfs4_put_stid(&deleg->dl_stid);
3608 goto out;
3609 }
3610 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003611out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003612 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003613 return nfs_ok;
3614 if (status)
3615 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003616 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003617 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003618}
3619
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003620static struct nfs4_ol_stateid *
3621nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003623 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003624 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625
Trond Myklebust1d31a252014-07-10 14:07:25 -04003626 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003627 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 /* ignore lock owners */
3629 if (local->st_stateowner->so_is_open_owner == 0)
3630 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003631 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003632 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003633 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003634 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003637 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003638 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639}
3640
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003641static inline int nfs4_access_to_access(u32 nfs4_access)
3642{
3643 int flags = 0;
3644
3645 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3646 flags |= NFSD_MAY_READ;
3647 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3648 flags |= NFSD_MAY_WRITE;
3649 return flags;
3650}
3651
Al Virob37ad282006-10-19 23:28:59 -07003652static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3654 struct nfsd4_open *open)
3655{
3656 struct iattr iattr = {
3657 .ia_valid = ATTR_SIZE,
3658 .ia_size = 0,
3659 };
3660 if (!open->op_truncate)
3661 return 0;
3662 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003663 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3665}
3666
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003667static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003668 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3669 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003670{
Trond Myklebustde186432014-07-10 14:07:26 -04003671 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003672 __be32 status;
3673 int oflag = nfs4_access_to_omode(open->op_share_access);
3674 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003675 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003676
Trond Myklebustde186432014-07-10 14:07:26 -04003677 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003678
3679 /*
3680 * Are we trying to set a deny mode that would conflict with
3681 * current access?
3682 */
3683 status = nfs4_file_check_deny(fp, open->op_share_deny);
3684 if (status != nfs_ok) {
3685 spin_unlock(&fp->fi_lock);
3686 goto out;
3687 }
3688
3689 /* set access to the file */
3690 status = nfs4_file_get_access(fp, open->op_share_access);
3691 if (status != nfs_ok) {
3692 spin_unlock(&fp->fi_lock);
3693 goto out;
3694 }
3695
3696 /* Set access bits in stateid */
3697 old_access_bmap = stp->st_access_bmap;
3698 set_access(open->op_share_access, stp);
3699
3700 /* Set new deny mask */
3701 old_deny_bmap = stp->st_deny_bmap;
3702 set_deny(open->op_share_deny, stp);
3703 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3704
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003705 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003706 spin_unlock(&fp->fi_lock);
3707 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003708 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003709 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003710 spin_lock(&fp->fi_lock);
3711 if (!fp->fi_fds[oflag]) {
3712 fp->fi_fds[oflag] = filp;
3713 filp = NULL;
3714 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003715 }
Trond Myklebustde186432014-07-10 14:07:26 -04003716 spin_unlock(&fp->fi_lock);
3717 if (filp)
3718 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003719
3720 status = nfsd4_truncate(rqstp, cur_fh, open);
3721 if (status)
3722 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003723out:
3724 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003725out_put_access:
3726 stp->st_access_bmap = old_access_bmap;
3727 nfs4_file_put_access(fp, open->op_share_access);
3728 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3729 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003730}
3731
Al Virob37ad282006-10-19 23:28:59 -07003732static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003733nfs4_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 -07003734{
Al Virob37ad282006-10-19 23:28:59 -07003735 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003736 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003738 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003739 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003740
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003741 /* test and set deny mode */
3742 spin_lock(&fp->fi_lock);
3743 status = nfs4_file_check_deny(fp, open->op_share_deny);
3744 if (status == nfs_ok) {
3745 old_deny_bmap = stp->st_deny_bmap;
3746 set_deny(open->op_share_deny, stp);
3747 fp->fi_share_deny |=
3748 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3749 }
3750 spin_unlock(&fp->fi_lock);
3751
3752 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003755 status = nfsd4_truncate(rqstp, cur_fh, open);
3756 if (status != nfs_ok)
3757 reset_union_bmap_deny(old_deny_bmap, stp);
3758 return status;
3759}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003762nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003764 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765}
3766
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003767/* Should we give out recallable state?: */
3768static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3769{
3770 if (clp->cl_cb_state == NFSD4_CB_UP)
3771 return true;
3772 /*
3773 * In the sessions case, since we don't have to establish a
3774 * separate connection for callbacks, we assume it's OK
3775 * until we hear otherwise:
3776 */
3777 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3778}
3779
Jeff Laytond564fbe2014-07-16 10:31:58 -04003780static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003781{
3782 struct file_lock *fl;
3783
3784 fl = locks_alloc_lock();
3785 if (!fl)
3786 return NULL;
3787 locks_init_lock(fl);
3788 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003789 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003790 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3791 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003792 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003793 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003794 return fl;
3795}
3796
J. Bruce Fields99c41512013-05-21 16:21:25 -04003797static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003798{
Trond Myklebust11b91642014-07-29 21:34:08 -04003799 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003800 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003801 struct file *filp;
3802 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003803
Jeff Laytond564fbe2014-07-16 10:31:58 -04003804 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003805 if (!fl)
3806 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003807 filp = find_readable_file(fp);
3808 if (!filp) {
3809 /* We should always have a readable file here */
3810 WARN_ON_ONCE(1);
3811 return -EBADF;
3812 }
3813 fl->fl_file = filp;
3814 status = vfs_setlease(filp, fl->fl_type, &fl);
3815 if (status) {
3816 locks_free_lock(fl);
3817 goto out_fput;
3818 }
Benny Halevycdc97502014-05-30 09:09:30 -04003819 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003820 spin_lock(&fp->fi_lock);
3821 /* Did the lease get broken before we took the lock? */
3822 status = -EAGAIN;
3823 if (fp->fi_had_conflict)
3824 goto out_unlock;
3825 /* Race breaker */
3826 if (fp->fi_lease) {
3827 status = 0;
3828 atomic_inc(&fp->fi_delegees);
3829 hash_delegation_locked(dp, fp);
3830 goto out_unlock;
3831 }
3832 fp->fi_lease = fl;
3833 fp->fi_deleg_file = filp;
3834 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003835 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003836 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003837 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003838 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003839out_unlock:
3840 spin_unlock(&fp->fi_lock);
3841 spin_unlock(&state_lock);
3842out_fput:
3843 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003844 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003845}
3846
Jeff Layton0b266932014-07-25 07:34:25 -04003847static struct nfs4_delegation *
3848nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3849 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003850{
Jeff Layton0b266932014-07-25 07:34:25 -04003851 int status;
3852 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003853
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003854 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003855 return ERR_PTR(-EAGAIN);
3856
3857 dp = alloc_init_deleg(clp, fh);
3858 if (!dp)
3859 return ERR_PTR(-ENOMEM);
3860
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003861 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003862 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003863 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003864 dp->dl_stid.sc_file = fp;
Jeff Layton417c6622014-07-21 09:34:57 -04003865 if (!fp->fi_lease) {
3866 spin_unlock(&fp->fi_lock);
3867 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003868 status = nfs4_setlease(dp);
3869 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003870 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003871 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003872 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003873 status = -EAGAIN;
3874 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003875 }
Benny Halevy931ee562014-05-30 09:09:27 -04003876 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003877 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003878out_unlock:
3879 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003880 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003881out:
3882 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003883 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003884 return ERR_PTR(status);
3885 }
3886 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003887}
3888
Benny Halevy4aa89132012-02-21 14:16:44 -08003889static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3890{
3891 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3892 if (status == -EAGAIN)
3893 open->op_why_no_deleg = WND4_CONTENTION;
3894 else {
3895 open->op_why_no_deleg = WND4_RESOURCE;
3896 switch (open->op_deleg_want) {
3897 case NFS4_SHARE_WANT_READ_DELEG:
3898 case NFS4_SHARE_WANT_WRITE_DELEG:
3899 case NFS4_SHARE_WANT_ANY_DELEG:
3900 break;
3901 case NFS4_SHARE_WANT_CANCEL:
3902 open->op_why_no_deleg = WND4_CANCELLED;
3903 break;
3904 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003905 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003906 }
3907 }
3908}
3909
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910/*
3911 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003912 *
3913 * Note we don't support write delegations, and won't until the vfs has
3914 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 */
3916static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003917nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3918 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919{
3920 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003921 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3922 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003923 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003924 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003926 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003927 open->op_recall = 0;
3928 switch (open->op_claim_type) {
3929 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003930 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003931 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003932 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3933 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003934 break;
3935 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003936 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003937 /*
3938 * Let's not give out any delegations till everyone's
3939 * had the chance to reclaim theirs....
3940 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003941 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003942 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003943 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003944 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003945 /*
3946 * Also, if the file was opened for write or
3947 * create, there's a good chance the client's
3948 * about to write to it, resulting in an
3949 * immediate recall (since we don't support
3950 * write delegations):
3951 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003952 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003953 goto out_no_deleg;
3954 if (open->op_create == NFS4_OPEN_CREATE)
3955 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003956 break;
3957 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003958 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003959 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003960 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003961 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003962 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003964 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003966 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003967 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003968 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003969 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003970 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003971out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003972 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3973 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003974 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003975 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003976 open->op_recall = 1;
3977 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003978
3979 /* 4.1 client asking for a delegation? */
3980 if (open->op_deleg_want)
3981 nfsd4_open_deleg_none_ext(open, status);
3982 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983}
3984
Benny Halevye27f49c2012-02-21 14:16:54 -08003985static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3986 struct nfs4_delegation *dp)
3987{
3988 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3989 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3990 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3991 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3992 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3993 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3994 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3995 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3996 }
3997 /* Otherwise the client must be confused wanting a delegation
3998 * it already has, therefore we don't return
3999 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4000 */
4001}
4002
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003/*
4004 * called with nfs4_lock_state() held.
4005 */
Al Virob37ad282006-10-19 23:28:59 -07004006__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4008{
Andy Adamson66689582009-04-03 08:28:45 +03004009 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004010 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004012 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004013 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004014 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 /*
4017 * Lookup file; if found, lookup stateid and check open request,
4018 * and check for delegations in the process of being recalled.
4019 * If not found, create the nfs4_file struct
4020 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004021 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004022 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004023 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004024 if (status)
4025 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04004026 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004028 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004029 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004030 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004031 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004032 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 }
4034
4035 /*
4036 * OPEN the file, or upgrade an existing OPEN.
4037 * If truncate fails, the OPEN fails.
4038 */
4039 if (stp) {
4040 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004041 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 if (status)
4043 goto out;
4044 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004045 stp = open->op_stp;
4046 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04004047 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004048 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4049 if (status) {
4050 release_open_stateid(stp);
4051 goto out;
4052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004054 update_stateid(&stp->st_stid.sc_stateid);
4055 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056
Benny Halevyd24433c2012-02-16 20:57:17 +02004057 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004058 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4059 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4060 open->op_why_no_deleg = WND4_NOT_WANTED;
4061 goto nodeleg;
4062 }
4063 }
4064
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 /*
4066 * Attempt to hand out a delegation. No error return, because the
4067 * OPEN succeeds even if we fail.
4068 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004069 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004070nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 status = nfs_ok;
4072
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004073 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004074 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004076 /* 4.1 client trying to upgrade/downgrade delegation? */
4077 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004078 open->op_deleg_want)
4079 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004080
NeilBrown13cd2182005-06-23 22:03:10 -07004081 if (fp)
4082 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004083 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05004084 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 /*
4086 * To finish the open response, we just need to set the rflags.
4087 */
4088 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004089 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004090 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004092 if (dp)
4093 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004094 if (stp)
4095 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096
4097 return status;
4098}
4099
Jeff Layton58fb12e2014-07-29 21:34:27 -04004100void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4101 struct nfsd4_open *open, __be32 status)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004102{
4103 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004104 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004105
Jeff Laytond3134b12014-07-29 21:34:32 -04004106 nfsd4_cstate_assign_replay(cstate, so);
4107 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004108 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004109 if (open->op_file)
4110 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004111 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004112 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004113}
4114
Al Virob37ad282006-10-19 23:28:59 -07004115__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004116nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4117 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118{
4119 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004120 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004121 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122
4123 nfs4_lock_state();
4124 dprintk("process_renew(%08x/%08x): starting\n",
4125 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004126 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004127 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004129 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004131 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004132 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 goto out;
4134 status = nfs_ok;
4135out:
4136 nfs4_unlock_state();
4137 return status;
4138}
4139
NeilBrowna76b4312005-06-23 22:04:01 -07004140static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004141nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004142{
Jeff Layton33dcc482012-04-10 11:08:48 -04004143 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004144 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004145 return;
4146
NeilBrowna76b4312005-06-23 22:04:01 -07004147 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004148 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004149 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004150 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004151 /*
4152 * Now that every NFSv4 client has had the chance to recover and
4153 * to see the (possibly new, possibly shorter) lease time, we
4154 * can safely set the next grace time to the current lease time:
4155 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03004156 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07004157}
4158
NeilBrownfd39ca92005-06-23 22:04:03 -07004159static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004160nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161{
4162 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004163 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004165 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004167 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004168 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169
4170 nfs4_lock_state();
4171
4172 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004173 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004174 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004175 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004176 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 clp = list_entry(pos, struct nfs4_client, cl_lru);
4178 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4179 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004180 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 break;
4182 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004183 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004184 dprintk("NFSD: client in use (clientid %08x)\n",
4185 clp->cl_clientid.cl_id);
4186 continue;
4187 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004188 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004189 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004190 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004191 list_for_each_safe(pos, next, &reaplist) {
4192 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 dprintk("NFSD: purging unused client (clientid %08x)\n",
4194 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004195 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 expire_client(clp);
4197 }
Benny Halevycdc97502014-05-30 09:09:30 -04004198 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004199 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004201 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4202 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004204 t = dp->dl_time - cutoff;
4205 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 break;
4207 }
Jeff Layton42690672014-07-25 07:34:20 -04004208 unhash_delegation_locked(dp);
4209 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 }
Benny Halevycdc97502014-05-30 09:09:30 -04004211 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004212 while (!list_empty(&reaplist)) {
4213 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4214 dl_recall_lru);
4215 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004216 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 }
Jeff Layton217526e2014-07-30 08:27:11 -04004218
4219 spin_lock(&nn->client_lock);
4220 while (!list_empty(&nn->close_lru)) {
4221 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4222 oo_close_lru);
4223 if (time_after((unsigned long)oo->oo_time,
4224 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004225 t = oo->oo_time - cutoff;
4226 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 break;
4228 }
Jeff Layton217526e2014-07-30 08:27:11 -04004229 list_del_init(&oo->oo_close_lru);
4230 stp = oo->oo_last_closed_stid;
4231 oo->oo_last_closed_stid = NULL;
4232 spin_unlock(&nn->client_lock);
4233 nfs4_put_stid(&stp->st_stid);
4234 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 }
Jeff Layton217526e2014-07-30 08:27:11 -04004236 spin_unlock(&nn->client_lock);
4237
Jeff Laytona832e7a2014-05-30 09:09:26 -04004238 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04004240 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241}
4242
Harvey Harrisona254b242008-02-20 12:49:00 -08004243static struct workqueue_struct *laundry_wq;
4244static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004245
4246static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004247laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248{
4249 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004250 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4251 work);
4252 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4253 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004255 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004257 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258}
4259
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004260static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004261{
Trond Myklebust11b91642014-07-29 21:34:08 -04004262 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004263 return nfserr_bad_stateid;
4264 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265}
4266
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004268access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004270 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4271 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4272 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273}
4274
4275static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004276access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004278 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4279 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280}
4281
4282static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004283__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284{
Al Virob37ad282006-10-19 23:28:59 -07004285 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286
J. Bruce Fields02921912010-07-29 15:16:59 -04004287 /* For lock stateid's, we test the parent open, not the lock: */
4288 if (stp->st_openstp)
4289 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004290 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004292 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 goto out;
4294 status = nfs_ok;
4295out:
4296 return status;
4297}
4298
Al Virob37ad282006-10-19 23:28:59 -07004299static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004300check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004302 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004304 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004305 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 * conflicting state; so we must wait out the grace period. */
4307 return nfserr_grace;
4308 } else if (flags & WR_STATE)
4309 return nfs4_share_conflict(current_fh,
4310 NFS4_SHARE_DENY_WRITE);
4311 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4312 return nfs4_share_conflict(current_fh,
4313 NFS4_SHARE_DENY_READ);
4314}
4315
4316/*
4317 * Allow READ/WRITE during grace period on recovered state only for files
4318 * that are not able to provide mandatory locking.
4319 */
4320static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004321grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004323 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324}
4325
J. Bruce Fields81b82962011-08-23 11:03:29 -04004326/* Returns true iff a is later than b: */
4327static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4328{
Jim Rees1a9357f2013-05-17 17:33:00 -04004329 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004330}
4331
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004332static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004333{
Andy Adamson66689582009-04-03 08:28:45 +03004334 /*
4335 * When sessions are used the stateid generation number is ignored
4336 * when it is zero.
4337 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004338 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004339 return nfs_ok;
4340
4341 if (in->si_generation == ref->si_generation)
4342 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004343
J. Bruce Fields0836f582008-01-26 19:08:12 -05004344 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004345 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004346 return nfserr_bad_stateid;
4347 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004348 * However, we could see a stateid from the past, even from a
4349 * non-buggy client. For example, if the client sends a lock
4350 * while some IO is outstanding, the lock may bump si_generation
4351 * while the IO is still in flight. The client could avoid that
4352 * situation by waiting for responses on all the IO requests,
4353 * but better performance may result in retrying IO that
4354 * receives an old_stateid error if requests are rarely
4355 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004356 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004357 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004358}
4359
Chuck Lever7df302f2012-05-29 13:56:37 -04004360static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004361{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004362 struct nfs4_stid *s;
4363 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004364 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004365
Chuck Lever7df302f2012-05-29 13:56:37 -04004366 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004367 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004368 /* Client debugging aid. */
4369 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4370 char addr_str[INET6_ADDRSTRLEN];
4371 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4372 sizeof(addr_str));
4373 pr_warn_ratelimited("NFSD: client %s testing state ID "
4374 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004375 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004376 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004377 spin_lock(&cl->cl_lock);
4378 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004379 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004380 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004381 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004382 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004383 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004384 switch (s->sc_type) {
4385 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004386 status = nfs_ok;
4387 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004388 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004389 status = nfserr_deleg_revoked;
4390 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004391 case NFS4_OPEN_STID:
4392 case NFS4_LOCK_STID:
4393 ols = openlockstateid(s);
4394 if (ols->st_stateowner->so_is_open_owner
4395 && !(openowner(ols->st_stateowner)->oo_flags
4396 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004397 status = nfserr_bad_stateid;
4398 else
4399 status = nfs_ok;
4400 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004401 default:
4402 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004403 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004404 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004405 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004406 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004407 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004408out_unlock:
4409 spin_unlock(&cl->cl_lock);
4410 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004411}
4412
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004413static __be32
4414nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4415 stateid_t *stateid, unsigned char typemask,
4416 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004417{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004418 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004419
4420 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4421 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004422 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004423 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004424 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004425 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004426 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004427 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004428 if (status)
4429 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004430 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004431 if (!*s)
4432 return nfserr_bad_stateid;
4433 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004434}
4435
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436/*
4437* Checks for stateid operations
4438*/
Al Virob37ad282006-10-19 23:28:59 -07004439__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004440nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004441 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004443 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004444 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004446 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004448 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004449 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004450 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 if (filpp)
4453 *filpp = NULL;
4454
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004455 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 return nfserr_grace;
4457
4458 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004459 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460
Trond Myklebust14bcab12014-04-18 14:44:07 -04004461 nfs4_lock_state();
4462
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004463 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004464 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004465 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004466 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004467 goto unlock_state;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004468 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4469 if (status)
4470 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004471 switch (s->sc_type) {
4472 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004473 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004474 status = nfs4_check_delegmode(dp, flags);
4475 if (status)
4476 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004477 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004478 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004479 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004480 WARN_ON_ONCE(1);
4481 status = nfserr_serverfault;
4482 goto out;
4483 }
Trond Myklebustde186432014-07-10 14:07:26 -04004484 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004485 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004486 break;
4487 case NFS4_OPEN_STID:
4488 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004489 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004490 status = nfs4_check_fh(current_fh, stp);
4491 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004493 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004494 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004496 status = nfs4_check_openmode(stp, flags);
4497 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004499 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004500 struct nfs4_file *fp = stp->st_stid.sc_file;
4501
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004502 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004503 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004504 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004505 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004506 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004507 break;
4508 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004509 status = nfserr_bad_stateid;
4510 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 }
4512 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004513 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004514 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004516 nfs4_put_stid(s);
4517unlock_state:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004518 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 return status;
4520}
4521
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004522/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004523 * Test if the stateid is valid
4524 */
4525__be32
4526nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4527 struct nfsd4_test_stateid *test_stateid)
4528{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004529 struct nfsd4_test_stateid_id *stateid;
4530 struct nfs4_client *cl = cstate->session->se_client;
4531
4532 nfs4_lock_state();
4533 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004534 stateid->ts_id_status =
4535 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004536 nfs4_unlock_state();
4537
Bryan Schumaker17456802011-07-13 10:50:48 -04004538 return nfs_ok;
4539}
4540
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004541__be32
4542nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4543 struct nfsd4_free_stateid *free_stateid)
4544{
4545 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004546 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004547 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004548 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004549 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004550 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004551
4552 nfs4_lock_state();
Jeff Layton1af71cc2014-07-29 21:34:14 -04004553 spin_lock(&cl->cl_lock);
4554 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004555 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004556 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004557 switch (s->sc_type) {
4558 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004559 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004560 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004561 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004562 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4563 if (ret)
4564 break;
4565 ret = nfserr_locks_held;
4566 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004567 case NFS4_LOCK_STID:
4568 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4569 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004570 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004571 stp = openlockstateid(s);
4572 ret = nfserr_locks_held;
4573 if (check_for_locks(stp->st_stid.sc_file,
4574 lockowner(stp->st_stateowner)))
4575 break;
4576 unhash_lock_stateid(stp);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004577 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004578 nfs4_put_stid(s);
4579 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004580 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004581 case NFS4_REVOKED_DELEG_STID:
4582 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004583 list_del_init(&dp->dl_recall_lru);
4584 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004585 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004586 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004587 goto out;
4588 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004589 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004590out_unlock:
4591 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004592out:
4593 nfs4_unlock_state();
4594 return ret;
4595}
4596
NeilBrown4c4cd222005-07-07 17:59:27 -07004597static inline int
4598setlkflg (int type)
4599{
4600 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4601 RD_STATE : WR_STATE;
4602}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004604static __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 -04004605{
4606 struct svc_fh *current_fh = &cstate->current_fh;
4607 struct nfs4_stateowner *sop = stp->st_stateowner;
4608 __be32 status;
4609
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004610 status = nfsd4_check_seqid(cstate, sop, seqid);
4611 if (status)
4612 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004613 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4614 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004615 /*
4616 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004617 * nfserr_replay_me from the previous step, and
4618 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004619 */
4620 return nfserr_bad_stateid;
4621 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4622 if (status)
4623 return status;
4624 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004625}
4626
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627/*
4628 * Checks for sequence id mutating operations.
4629 */
Al Virob37ad282006-10-19 23:28:59 -07004630static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004631nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004632 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004633 struct nfs4_ol_stateid **stpp,
4634 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004636 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004637 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004638 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004640 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4641 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004642
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004644 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004645 if (status)
4646 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004647 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004648 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004650 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004651 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004652 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004653 else
4654 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004655 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004656}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004657
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004658static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4659 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004660{
4661 __be32 status;
4662 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004663 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004665 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004666 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004667 if (status)
4668 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004669 oo = openowner(stp->st_stateowner);
4670 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4671 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004672 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004673 }
4674 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004675 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676}
4677
Al Virob37ad282006-10-19 23:28:59 -07004678__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004679nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004680 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681{
Al Virob37ad282006-10-19 23:28:59 -07004682 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004683 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004684 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004685 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686
Al Viroa6a9f182013-09-16 10:57:01 -04004687 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4688 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004690 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004691 if (status)
4692 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693
4694 nfs4_lock_state();
4695
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004696 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004697 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004698 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004699 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004700 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004701 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004702 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004703 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004704 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004705 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004706 update_stateid(&stp->st_stid.sc_stateid);
4707 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004708 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004709 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004710
Jeff Layton2a4317c2012-03-21 16:42:43 -04004711 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004712 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004713put_stateid:
4714 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004716 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004717 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 return status;
4719}
4720
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004721static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004723 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004724 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004725 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004726 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004727}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004728
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004729static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4730{
4731 switch (to_access) {
4732 case NFS4_SHARE_ACCESS_READ:
4733 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4734 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4735 break;
4736 case NFS4_SHARE_ACCESS_WRITE:
4737 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4738 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4739 break;
4740 case NFS4_SHARE_ACCESS_BOTH:
4741 break;
4742 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004743 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 }
4745}
4746
Al Virob37ad282006-10-19 23:28:59 -07004747__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004748nfsd4_open_downgrade(struct svc_rqst *rqstp,
4749 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004750 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751{
Al Virob37ad282006-10-19 23:28:59 -07004752 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004753 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004754 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755
Al Viroa6a9f182013-09-16 10:57:01 -04004756 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4757 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004759 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004760 if (od->od_deleg_want)
4761 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4762 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763
4764 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004765 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004766 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004767 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004770 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004771 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004773 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004775 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004776 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004778 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004780 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781
Jeff Laytonce0fc432012-05-11 09:45:14 -04004782 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004784 update_stateid(&stp->st_stid.sc_stateid);
4785 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004787put_stateid:
4788 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004790 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004791 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 return status;
4793}
4794
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004795static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4796{
Trond Myklebustacf92952014-06-30 11:48:37 -04004797 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytond83017f2014-07-29 21:34:42 -04004798 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004799
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004800 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04004801 spin_lock(&clp->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04004802 unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004803
Jeff Laytond83017f2014-07-29 21:34:42 -04004804 if (clp->cl_minorversion) {
4805 put_ol_stateid_locked(s, &reaplist);
4806 spin_unlock(&clp->cl_lock);
4807 free_ol_stateid_reaplist(&reaplist);
4808 } else {
4809 spin_unlock(&clp->cl_lock);
4810 free_ol_stateid_reaplist(&reaplist);
Jeff Laytond3134b12014-07-29 21:34:32 -04004811 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04004812 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004813}
4814
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815/*
4816 * nfs4_unlock_state() called after encode
4817 */
Al Virob37ad282006-10-19 23:28:59 -07004818__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004819nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004820 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821{
Al Virob37ad282006-10-19 23:28:59 -07004822 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004823 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004824 struct net *net = SVC_NET(rqstp);
4825 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826
Al Viroa6a9f182013-09-16 10:57:01 -04004827 dprintk("NFSD: nfsd4_close on file %pd\n",
4828 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829
4830 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004831 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4832 &close->cl_stateid,
4833 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004834 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004835 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004836 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004838 update_stateid(&stp->st_stid.sc_stateid);
4839 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004841 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004842
4843 /* put reference from nfs4_preprocess_seqid_op */
4844 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845out:
Jeff Layton58fb12e2014-07-29 21:34:27 -04004846 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 return status;
4848}
4849
Al Virob37ad282006-10-19 23:28:59 -07004850__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004851nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4852 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004854 struct nfs4_delegation *dp;
4855 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004856 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004857 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004858 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004860 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004861 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862
4863 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004864 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004865 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004866 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004867 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004868 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004869 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004870 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004871
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004872 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004873put_stateid:
4874 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004876 nfs4_unlock_state();
4877
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 return status;
4879}
4880
4881
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883
Benny Halevy87df4de2008-12-15 19:42:03 +02004884static inline u64
4885end_offset(u64 start, u64 len)
4886{
4887 u64 end;
4888
4889 end = start + len;
4890 return end >= start ? end: NFS4_MAX_UINT64;
4891}
4892
4893/* last octet in a range */
4894static inline u64
4895last_byte_offset(u64 start, u64 len)
4896{
4897 u64 end;
4898
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004899 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004900 end = start + len;
4901 return end > start ? end - 1: NFS4_MAX_UINT64;
4902}
4903
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904/*
4905 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4906 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4907 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4908 * locking, this prevents us from being completely protocol-compliant. The
4909 * real solution to this problem is to start using unsigned file offsets in
4910 * the VFS, but this is a very deep change!
4911 */
4912static inline void
4913nfs4_transform_lock_offset(struct file_lock *lock)
4914{
4915 if (lock->fl_start < 0)
4916 lock->fl_start = OFFSET_MAX;
4917 if (lock->fl_end < 0)
4918 lock->fl_end = OFFSET_MAX;
4919}
4920
NeilBrownd5b90262006-04-10 22:55:22 -07004921/* Hack!: For now, we're defining this just so we can use a pointer to it
4922 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004923static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004924};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925
4926static inline void
4927nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4928{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004929 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930
NeilBrownd5b90262006-04-10 22:55:22 -07004931 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004932 lo = (struct nfs4_lockowner *) fl->fl_owner;
4933 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4934 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004935 if (!deny->ld_owner.data)
4936 /* We just don't care that much */
4937 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004938 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4939 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004940 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004941nevermind:
4942 deny->ld_owner.len = 0;
4943 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004944 deny->ld_clientid.cl_boot = 0;
4945 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 }
4947 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004948 deny->ld_length = NFS4_MAX_UINT64;
4949 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4951 deny->ld_type = NFS4_READ_LT;
4952 if (fl->fl_type != F_RDLCK)
4953 deny->ld_type = NFS4_WRITE_LT;
4954}
4955
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004956static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004957find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004958 struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004960 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004961 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962
Trond Myklebust0a880a22014-07-30 08:27:10 -04004963 lockdep_assert_held(&clp->cl_lock);
4964
Trond Myklebustd4f04892014-07-29 21:34:36 -04004965 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4966 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004967 if (so->so_is_open_owner)
4968 continue;
Trond Myklebustd4f04892014-07-29 21:34:36 -04004969 if (!same_owner_str(so, owner))
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004970 continue;
Jeff Layton5db1c032014-07-29 21:34:28 -04004971 atomic_inc(&so->so_count);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004972 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 }
4974 return NULL;
4975}
4976
Trond Myklebustc58c6612014-07-29 21:34:35 -04004977static struct nfs4_lockowner *
4978find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004979 struct nfs4_client *clp)
Trond Myklebustc58c6612014-07-29 21:34:35 -04004980{
4981 struct nfs4_lockowner *lo;
4982
Trond Myklebustd4f04892014-07-29 21:34:36 -04004983 spin_lock(&clp->cl_lock);
4984 lo = find_lockowner_str_locked(clid, owner, clp);
4985 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004986 return lo;
4987}
4988
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004989static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4990{
Trond Myklebustc58c6612014-07-29 21:34:35 -04004991 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004992}
4993
Jeff Layton6b180f02014-07-29 21:34:26 -04004994static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4995{
4996 struct nfs4_lockowner *lo = lockowner(sop);
4997
4998 kmem_cache_free(lockowner_slab, lo);
4999}
5000
5001static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005002 .so_unhash = nfs4_unhash_lockowner,
5003 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005004};
5005
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006/*
5007 * Alloc a lock owner structure.
5008 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005009 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005011 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005013static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005014alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5015 struct nfs4_ol_stateid *open_stp,
5016 struct nfsd4_lock *lock)
5017{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005018 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005020 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5021 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005023 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5024 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005025 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005026 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005027 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005028 ret = find_lockowner_str_locked(&clp->cl_clientid,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005029 &lock->lk_new_owner, clp);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005030 if (ret == NULL) {
5031 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005032 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005033 ret = lo;
5034 } else
5035 nfs4_free_lockowner(&lo->lo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005036 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005037 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038}
5039
Jeff Layton356a95e2014-07-29 21:34:13 -04005040static void
5041init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5042 struct nfs4_file *fp, struct inode *inode,
5043 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005045 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046
Jeff Layton356a95e2014-07-29 21:34:13 -04005047 lockdep_assert_held(&clp->cl_lock);
5048
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005049 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005050 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005051 stp->st_stateowner = &lo->lo_owner;
Trond Myklebuste4f1dd72014-07-29 21:34:30 -04005052 atomic_inc(&lo->lo_owner.so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07005053 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005054 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04005055 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005056 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005058 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005059 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005060 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005061 spin_lock(&fp->fi_lock);
5062 list_add(&stp->st_perfile, &fp->fi_stateids);
5063 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064}
5065
Jeff Laytonc53530d2014-06-30 11:48:39 -04005066static struct nfs4_ol_stateid *
5067find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5068{
5069 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005070 struct nfs4_client *clp = lo->lo_owner.so_client;
5071
5072 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005073
5074 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005075 if (lst->st_stid.sc_file == fp) {
5076 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005077 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005078 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005079 }
5080 return NULL;
5081}
5082
Jeff Layton356a95e2014-07-29 21:34:13 -04005083static struct nfs4_ol_stateid *
5084find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5085 struct inode *inode, struct nfs4_ol_stateid *ost,
5086 bool *new)
5087{
5088 struct nfs4_stid *ns = NULL;
5089 struct nfs4_ol_stateid *lst;
5090 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5091 struct nfs4_client *clp = oo->oo_owner.so_client;
5092
5093 spin_lock(&clp->cl_lock);
5094 lst = find_lock_stateid(lo, fi);
5095 if (lst == NULL) {
5096 spin_unlock(&clp->cl_lock);
5097 ns = nfs4_alloc_stid(clp, stateid_slab);
5098 if (ns == NULL)
5099 return NULL;
5100
5101 spin_lock(&clp->cl_lock);
5102 lst = find_lock_stateid(lo, fi);
5103 if (likely(!lst)) {
5104 lst = openlockstateid(ns);
5105 init_lock_stateid(lst, lo, fi, inode, ost);
5106 ns = NULL;
5107 *new = true;
5108 }
5109 }
5110 spin_unlock(&clp->cl_lock);
5111 if (ns)
5112 nfs4_put_stid(ns);
5113 return lst;
5114}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005115
NeilBrownfd39ca92005-06-23 22:04:03 -07005116static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117check_lock_length(u64 offset, u64 length)
5118{
Benny Halevy87df4de2008-12-15 19:42:03 +02005119 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 LOFF_OVERFLOW(offset, length)));
5121}
5122
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005123static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005124{
Trond Myklebust11b91642014-07-29 21:34:08 -04005125 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005126
Jeff Layton7214e862014-07-10 14:07:33 -04005127 lockdep_assert_held(&fp->fi_lock);
5128
Jeff Layton82c5ff12012-05-11 09:45:13 -04005129 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005130 return;
Jeff Layton12659652014-07-10 14:07:28 -04005131 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005132 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005133}
5134
Jeff Layton356a95e2014-07-29 21:34:13 -04005135static __be32
5136lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5137 struct nfs4_ol_stateid *ost,
5138 struct nfsd4_lock *lock,
5139 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005140{
Jeff Layton5db1c032014-07-29 21:34:28 -04005141 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005142 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005143 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5144 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005145 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005146 struct nfs4_lockowner *lo;
5147 unsigned int strhashval;
5148
Trond Myklebustd4f04892014-07-29 21:34:36 -04005149 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005150 if (!lo) {
Trond Myklebustd4f04892014-07-29 21:34:36 -04005151 strhashval = ownerstr_hashval(&lock->v.new.owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005152 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5153 if (lo == NULL)
5154 return nfserr_jukebox;
5155 } else {
5156 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005157 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005158 if (!cstate->minorversion &&
5159 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005160 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005161 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005162
Jeff Layton356a95e2014-07-29 21:34:13 -04005163 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005164 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005165 status = nfserr_jukebox;
5166 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005167 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005168 status = nfs_ok;
5169out:
5170 nfs4_put_stateowner(&lo->lo_owner);
5171 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005172}
5173
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174/*
5175 * LOCK operation
5176 */
Al Virob37ad282006-10-19 23:28:59 -07005177__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005178nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005179 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005181 struct nfs4_openowner *open_sop = NULL;
5182 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005183 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005184 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005185 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005186 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005187 struct file_lock *file_lock = NULL;
5188 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005189 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005190 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005191 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005192 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005193 struct net *net = SVC_NET(rqstp);
5194 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195
5196 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5197 (long long) lock->lk_offset,
5198 (long long) lock->lk_length);
5199
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 if (check_lock_length(lock->lk_offset, lock->lk_length))
5201 return nfserr_inval;
5202
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005203 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005204 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005205 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5206 return status;
5207 }
5208
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 nfs4_lock_state();
5210
5211 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005212 if (nfsd4_has_session(cstate))
5213 /* See rfc 5661 18.10.3: given clientid is ignored: */
5214 memcpy(&lock->v.new.clientid,
5215 &cstate->session->se_client->cl_clientid,
5216 sizeof(clientid_t));
5217
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005219 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005223 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 lock->lk_new_open_seqid,
5225 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005226 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005227 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005229 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005230 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005231 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005232 &lock->v.new.clientid))
5233 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005234 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005235 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005236 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005237 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005238 lock->lk_old_lock_seqid,
5239 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005240 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005241 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005242 if (status)
5243 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005244 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005246 lkflg = setlkflg(lock->lk_type);
5247 status = nfs4_check_openmode(lock_stp, lkflg);
5248 if (status)
5249 goto out;
5250
NeilBrown0dd395d2005-07-07 17:59:15 -07005251 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005252 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005253 goto out;
5254 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005255 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005256 goto out;
5257
Jeff Layton21179d82012-08-21 08:03:32 -04005258 file_lock = locks_alloc_lock();
5259 if (!file_lock) {
5260 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5261 status = nfserr_jukebox;
5262 goto out;
5263 }
5264
Trond Myklebust11b91642014-07-29 21:34:08 -04005265 fp = lock_stp->st_stid.sc_file;
Jeff Layton21179d82012-08-21 08:03:32 -04005266 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 switch (lock->lk_type) {
5268 case NFS4_READ_LT:
5269 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005270 spin_lock(&fp->fi_lock);
5271 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005272 if (filp)
5273 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005274 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005275 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005276 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 case NFS4_WRITE_LT:
5278 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005279 spin_lock(&fp->fi_lock);
5280 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005281 if (filp)
5282 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005283 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005284 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005285 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 default:
5287 status = nfserr_inval;
5288 goto out;
5289 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005290 if (!filp) {
5291 status = nfserr_openmode;
5292 goto out;
5293 }
Jeff Layton21179d82012-08-21 08:03:32 -04005294 file_lock->fl_owner = (fl_owner_t)lock_sop;
5295 file_lock->fl_pid = current->tgid;
5296 file_lock->fl_file = filp;
5297 file_lock->fl_flags = FL_POSIX;
5298 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5299 file_lock->fl_start = lock->lk_offset;
5300 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5301 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302
Jeff Layton21179d82012-08-21 08:03:32 -04005303 conflock = locks_alloc_lock();
5304 if (!conflock) {
5305 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5306 status = nfserr_jukebox;
5307 goto out;
5308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309
Jeff Layton21179d82012-08-21 08:03:32 -04005310 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005311 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005313 update_stateid(&lock_stp->st_stid.sc_stateid);
5314 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005316 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005317 break;
5318 case (EAGAIN): /* conflock holds conflicting lock */
5319 status = nfserr_denied;
5320 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005321 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005322 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 case (EDEADLK):
5324 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005325 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005326 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005327 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005328 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005329 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331out:
Trond Myklebustde186432014-07-10 14:07:26 -04005332 if (filp)
5333 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005334 if (lock_stp) {
5335 /* Bump seqid manually if the 4.0 replay owner is openowner */
5336 if (cstate->replay_owner &&
5337 cstate->replay_owner != &lock_sop->lo_owner &&
5338 seqid_mutating_err(ntohl(status)))
5339 lock_sop->lo_owner.so_seqid++;
5340
5341 /*
5342 * If this is a new, never-before-used stateid, and we are
5343 * returning an error, then just go ahead and release it.
5344 */
5345 if (status && new)
5346 release_lock_stateid(lock_stp);
5347
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005348 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005349 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005350 if (open_stp)
5351 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005352 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005353 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005354 if (file_lock)
5355 locks_free_lock(file_lock);
5356 if (conflock)
5357 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 return status;
5359}
5360
5361/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005362 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5363 * so we do a temporary open here just to get an open file to pass to
5364 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5365 * inode operation.)
5366 */
Al Viro04da6e92012-04-13 00:00:04 -04005367static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005368{
5369 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005370 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5371 if (!err) {
5372 err = nfserrno(vfs_test_lock(file, lock));
5373 nfsd_close(file);
5374 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005375 return err;
5376}
5377
5378/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 * LOCKT operation
5380 */
Al Virob37ad282006-10-19 23:28:59 -07005381__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005382nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5383 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384{
Jeff Layton21179d82012-08-21 08:03:32 -04005385 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005386 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005387 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005388 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005390 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 return nfserr_grace;
5392
5393 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5394 return nfserr_inval;
5395
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 nfs4_lock_state();
5397
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005398 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005399 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005400 if (status)
5401 goto out;
5402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005404 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406
Jeff Layton21179d82012-08-21 08:03:32 -04005407 file_lock = locks_alloc_lock();
5408 if (!file_lock) {
5409 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5410 status = nfserr_jukebox;
5411 goto out;
5412 }
5413 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 switch (lockt->lt_type) {
5415 case NFS4_READ_LT:
5416 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005417 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 break;
5419 case NFS4_WRITE_LT:
5420 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005421 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 break;
5423 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005424 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 status = nfserr_inval;
5426 goto out;
5427 }
5428
Trond Myklebustd4f04892014-07-29 21:34:36 -04005429 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5430 cstate->clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005431 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005432 file_lock->fl_owner = (fl_owner_t)lo;
5433 file_lock->fl_pid = current->tgid;
5434 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435
Jeff Layton21179d82012-08-21 08:03:32 -04005436 file_lock->fl_start = lockt->lt_offset;
5437 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438
Jeff Layton21179d82012-08-21 08:03:32 -04005439 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440
Jeff Layton21179d82012-08-21 08:03:32 -04005441 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005442 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005443 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005444
Jeff Layton21179d82012-08-21 08:03:32 -04005445 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005447 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 }
5449out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005450 if (lo)
5451 nfs4_put_stateowner(&lo->lo_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005453 if (file_lock)
5454 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 return status;
5456}
5457
Al Virob37ad282006-10-19 23:28:59 -07005458__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005459nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005460 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005462 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005464 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005465 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005466 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005467 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5468
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5470 (long long) locku->lu_offset,
5471 (long long) locku->lu_length);
5472
5473 if (check_lock_length(locku->lu_offset, locku->lu_length))
5474 return nfserr_inval;
5475
5476 nfs4_lock_state();
5477
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005478 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005479 &locku->lu_stateid, NFS4_LOCK_STID,
5480 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005481 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005483 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005484 if (!filp) {
5485 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005486 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005487 }
Jeff Layton21179d82012-08-21 08:03:32 -04005488 file_lock = locks_alloc_lock();
5489 if (!file_lock) {
5490 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5491 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005492 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005493 }
5494 locks_init_lock(file_lock);
5495 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005496 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005497 file_lock->fl_pid = current->tgid;
5498 file_lock->fl_file = filp;
5499 file_lock->fl_flags = FL_POSIX;
5500 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5501 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502
Jeff Layton21179d82012-08-21 08:03:32 -04005503 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5504 locku->lu_length);
5505 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506
Jeff Layton21179d82012-08-21 08:03:32 -04005507 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005508 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005509 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 goto out_nfserr;
5511 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005512 update_stateid(&stp->st_stid.sc_stateid);
5513 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005514fput:
5515 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005516put_stateid:
5517 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005519 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005520 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005521 if (file_lock)
5522 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 return status;
5524
5525out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005526 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005527 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528}
5529
5530/*
5531 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005532 * true: locks held by lockowner
5533 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005535static bool
5536check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537{
5538 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005539 int status = false;
5540 struct file *filp = find_any_file(fp);
5541 struct inode *inode;
5542
5543 if (!filp) {
5544 /* Any valid lock stateid should have some sort of access */
5545 WARN_ON_ONCE(1);
5546 return status;
5547 }
5548
5549 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550
Jeff Layton1c8c6012013-06-21 08:58:15 -04005551 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005553 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005554 status = true;
5555 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005558 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005559 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 return status;
5561}
5562
Al Virob37ad282006-10-19 23:28:59 -07005563__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005564nfsd4_release_lockowner(struct svc_rqst *rqstp,
5565 struct nfsd4_compound_state *cstate,
5566 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567{
5568 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005569 struct nfs4_stateowner *sop;
5570 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005571 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005573 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005574 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005575 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005576 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577
5578 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5579 clid->cl_boot, clid->cl_id);
5580
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 nfs4_lock_state();
5582
Jeff Layton4b24ca72014-06-30 11:48:44 -04005583 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005584 if (status)
5585 goto out;
5586
Trond Myklebustd4f04892014-07-29 21:34:36 -04005587 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005588 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005589 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005590 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005591 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005592
5593 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005594 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005595
Jeff Layton882e9d22014-07-29 21:34:37 -04005596 /* see if there are still any locks associated with it */
5597 lo = lockowner(sop);
5598 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5599 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5600 status = nfserr_locks_held;
5601 spin_unlock(&clp->cl_lock);
5602 goto out;
5603 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005604 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005605
5606 atomic_inc(&sop->so_count);
5607 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005608 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005609 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005610 if (lo)
5611 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612out:
5613 nfs4_unlock_state();
5614 return status;
5615}
5616
5617static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005618alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619{
NeilBrowna55370a2005-06-23 22:03:52 -07005620 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621}
5622
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005623bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005624nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005625{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005626 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005627
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005628 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005629 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005630}
5631
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632/*
5633 * failure => all reset bets are off, nfserr_no_grace...
5634 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005635struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005636nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637{
5638 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005639 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640
NeilBrowna55370a2005-06-23 22:03:52 -07005641 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5642 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005643 if (crp) {
5644 strhashval = clientstr_hashval(name);
5645 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005646 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005647 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005648 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005649 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005650 }
5651 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652}
5653
Jeff Layton2a4317c2012-03-21 16:42:43 -04005654void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005655nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005656{
5657 list_del(&crp->cr_strhash);
5658 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005659 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005660}
5661
5662void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005663nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664{
5665 struct nfs4_client_reclaim *crp = NULL;
5666 int i;
5667
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005669 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5670 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005672 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 }
5674 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005675 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676}
5677
5678/*
5679 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005680struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005681nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682{
5683 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 struct nfs4_client_reclaim *crp = NULL;
5685
Jeff Layton278c9312012-11-12 15:00:52 -05005686 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687
Jeff Layton278c9312012-11-12 15:00:52 -05005688 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005689 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005690 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 return crp;
5692 }
5693 }
5694 return NULL;
5695}
5696
5697/*
5698* Called from OPEN. Look for clientid in reclaim list.
5699*/
Al Virob37ad282006-10-19 23:28:59 -07005700__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005701nfs4_check_open_reclaim(clientid_t *clid,
5702 struct nfsd4_compound_state *cstate,
5703 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005705 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005706
5707 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005708 status = lookup_clientid(clid, cstate, nn);
5709 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005710 return nfserr_reclaim_bad;
5711
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005712 if (nfsd4_client_record_check(cstate->clp))
5713 return nfserr_reclaim_bad;
5714
5715 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716}
5717
Bryan Schumaker65178db2011-11-01 13:35:21 -04005718#ifdef CONFIG_NFSD_FAULT_INJECTION
5719
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005720u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5721{
Jeff Laytond20c11d2014-07-30 08:27:07 -04005722 __be32 ret;
5723 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
5724
5725 spin_lock(&nn->client_lock);
5726 ret = mark_client_expired_locked(clp);
5727 spin_unlock(&nn->client_lock);
5728 if (ret != nfs_ok)
J. Bruce Fields221a6872013-04-01 22:23:49 -04005729 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005730 expire_client(clp);
5731 return 1;
5732}
5733
Bryan Schumaker184c1842012-11-29 11:40:44 -05005734u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5735{
5736 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005737 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005738 printk(KERN_INFO "NFS Client: %s\n", buf);
5739 return 1;
5740}
5741
5742static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5743 const char *type)
5744{
5745 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005746 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005747 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5748}
5749
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005750static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5751 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005752{
5753 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005754 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005755 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005756 u64 count = 0;
5757
5758 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005759 list_for_each_entry_safe(stp, st_next,
5760 &oop->oo_owner.so_stateids, st_perstateowner) {
5761 list_for_each_entry_safe(lst, lst_next,
5762 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005763 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005764 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005765 if (++count == max)
5766 return count;
5767 }
5768 }
5769 }
5770
5771 return count;
5772}
5773
5774u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5775{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005776 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005777}
5778
Bryan Schumaker184c1842012-11-29 11:40:44 -05005779u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5780{
5781 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5782 nfsd_print_count(clp, count, "locked files");
5783 return count;
5784}
5785
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005786static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5787{
5788 struct nfs4_openowner *oop, *next;
5789 u64 count = 0;
5790
5791 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5792 if (func)
5793 func(oop);
5794 if (++count == max)
5795 break;
5796 }
5797
5798 return count;
5799}
5800
5801u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5802{
5803 return nfsd_foreach_client_open(clp, max, release_openowner);
5804}
5805
Bryan Schumaker184c1842012-11-29 11:40:44 -05005806u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5807{
5808 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5809 nfsd_print_count(clp, count, "open files");
5810 return count;
5811}
5812
Bryan Schumaker269de302012-11-29 11:40:42 -05005813static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5814 struct list_head *victims)
5815{
5816 struct nfs4_delegation *dp, *next;
5817 u64 count = 0;
5818
Benny Halevycdc97502014-05-30 09:09:30 -04005819 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005820 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005821 if (victims) {
5822 /*
5823 * It's not safe to mess with delegations that have a
5824 * non-zero dl_time. They might have already been broken
5825 * and could be processed by the laundromat outside of
5826 * the state_lock. Just leave them be.
5827 */
5828 if (dp->dl_time != 0)
5829 continue;
5830
Jeff Layton42690672014-07-25 07:34:20 -04005831 unhash_delegation_locked(dp);
5832 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005833 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005834 if (++count == max)
5835 break;
5836 }
5837 return count;
5838}
5839
5840u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5841{
5842 struct nfs4_delegation *dp, *next;
5843 LIST_HEAD(victims);
5844 u64 count;
5845
Benny Halevycdc97502014-05-30 09:09:30 -04005846 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005847 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005848 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005849
Jeff Layton2d4a5322014-07-25 07:34:21 -04005850 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5851 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005852 revoke_delegation(dp);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005853 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005854
5855 return count;
5856}
5857
5858u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5859{
Jeff Laytondff13992014-07-08 14:02:49 -04005860 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005861 LIST_HEAD(victims);
5862 u64 count;
5863
Benny Halevycdc97502014-05-30 09:09:30 -04005864 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005865 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005866 while (!list_empty(&victims)) {
5867 dp = list_first_entry(&victims, struct nfs4_delegation,
5868 dl_recall_lru);
5869 list_del_init(&dp->dl_recall_lru);
5870 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005871 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005872 }
Benny Halevycdc97502014-05-30 09:09:30 -04005873 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005874
5875 return count;
5876}
5877
Bryan Schumaker184c1842012-11-29 11:40:44 -05005878u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5879{
5880 u64 count = 0;
5881
Benny Halevycdc97502014-05-30 09:09:30 -04005882 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005883 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005884 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005885
5886 nfsd_print_count(clp, count, "delegations");
5887 return count;
5888}
5889
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005890u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005891{
5892 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005893 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005894 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005895
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005896 if (!nfsd_netns_ready(nn))
5897 return 0;
5898
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005899 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005900 count += func(clp, max - count);
5901 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005902 break;
5903 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005904
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005905 return count;
5906}
5907
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005908struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5909{
5910 struct nfs4_client *clp;
5911 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5912
5913 if (!nfsd_netns_ready(nn))
5914 return NULL;
5915
5916 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5917 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5918 return clp;
5919 }
5920 return NULL;
5921}
5922
Bryan Schumaker65178db2011-11-01 13:35:21 -04005923#endif /* CONFIG_NFSD_FAULT_INJECTION */
5924
Meelap Shahc2f1a552007-07-17 04:04:39 -07005925/*
5926 * Since the lifetime of a delegation isn't limited to that of an open, a
5927 * client may quite reasonably hang on to a delegation as long as it has
5928 * the inode cached. This becomes an obvious problem the first time a
5929 * client's inode cache approaches the size of the server's total memory.
5930 *
5931 * For now we avoid this problem by imposing a hard limit on the number
5932 * of delegations, which varies according to the server's memory size.
5933 */
5934static void
5935set_max_delegations(void)
5936{
5937 /*
5938 * Allow at most 4 delegations per megabyte of RAM. Quick
5939 * estimates suggest that in the worst case (where every delegation
5940 * is for a different inode), a delegation could take about 1.5K,
5941 * giving a worst case usage of about 6% of memory.
5942 */
5943 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5944}
5945
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005946static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005947{
5948 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5949 int i;
5950
5951 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5952 CLIENT_HASH_SIZE, GFP_KERNEL);
5953 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005954 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005955 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5956 CLIENT_HASH_SIZE, GFP_KERNEL);
5957 if (!nn->unconf_id_hashtbl)
5958 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005959 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5960 SESSION_HASH_SIZE, GFP_KERNEL);
5961 if (!nn->sessionid_hashtbl)
5962 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005963
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005964 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005965 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005966 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005967 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005968 for (i = 0; i < SESSION_HASH_SIZE; i++)
5969 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005970 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005971 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005972 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005973 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005974 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005975 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005976
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005977 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005978 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005979
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005980 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005981
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005982err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005983 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005984err_unconf_id:
5985 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005986err:
5987 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005988}
5989
5990static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005991nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005992{
5993 int i;
5994 struct nfs4_client *clp = NULL;
5995 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5996
5997 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5998 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5999 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6000 destroy_client(clp);
6001 }
6002 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006003
Kinglong Mee2b905632014-03-26 22:09:30 +08006004 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6005 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6006 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6007 destroy_client(clp);
6008 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006009 }
6010
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006011 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006012 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006013 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006014 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006015}
6016
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006017int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006018nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07006019{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006020 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006021 int ret;
6022
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006023 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006024 if (ret)
6025 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006026 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006027 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006028 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04006029 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006030 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03006031 nn->nfsd4_grace, net);
6032 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006033 return 0;
6034}
6035
6036/* initialization to perform when the nfsd service is started: */
6037
6038int
6039nfs4_state_start(void)
6040{
6041 int ret;
6042
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006043 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006044 if (ret)
6045 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07006046 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05006047 if (laundry_wq == NULL) {
6048 ret = -ENOMEM;
6049 goto out_recovery;
6050 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006051 ret = nfsd4_create_callback_queue();
6052 if (ret)
6053 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006054
Meelap Shahc2f1a552007-07-17 04:04:39 -07006055 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006056
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006057 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006058
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006059out_free_laundry:
6060 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05006061out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006062 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063}
6064
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006065void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006066nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006070 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006072 cancel_delayed_work_sync(&nn->laundromat_work);
6073 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05006074
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04006075 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04006077 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006078 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006080 unhash_delegation_locked(dp);
6081 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 }
Benny Halevycdc97502014-05-30 09:09:30 -04006083 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 list_for_each_safe(pos, next, &reaplist) {
6085 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006086 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04006087 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 }
6089
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006090 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006091 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04006092 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093}
6094
6095void
6096nfs4_state_shutdown(void)
6097{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006098 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006099 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006101
6102static void
6103get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6104{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006105 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6106 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006107}
6108
6109static void
6110put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6111{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006112 if (cstate->minorversion) {
6113 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6114 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6115 }
6116}
6117
6118void
6119clear_current_stateid(struct nfsd4_compound_state *cstate)
6120{
6121 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006122}
6123
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006124/*
6125 * functions to set current state id
6126 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006127void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006128nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6129{
6130 put_stateid(cstate, &odp->od_stateid);
6131}
6132
6133void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006134nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6135{
6136 put_stateid(cstate, &open->op_stateid);
6137}
6138
6139void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006140nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6141{
6142 put_stateid(cstate, &close->cl_stateid);
6143}
6144
6145void
6146nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6147{
6148 put_stateid(cstate, &lock->lk_resp_stateid);
6149}
6150
6151/*
6152 * functions to consume current state id
6153 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006154
6155void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006156nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6157{
6158 get_stateid(cstate, &odp->od_stateid);
6159}
6160
6161void
6162nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6163{
6164 get_stateid(cstate, &drp->dr_stateid);
6165}
6166
6167void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006168nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6169{
6170 get_stateid(cstate, &fsp->fr_stateid);
6171}
6172
6173void
6174nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6175{
6176 get_stateid(cstate, &setattr->sa_stateid);
6177}
6178
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006179void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006180nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6181{
6182 get_stateid(cstate, &close->cl_stateid);
6183}
6184
6185void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006186nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006187{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006188 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006189}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006190
6191void
6192nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6193{
6194 get_stateid(cstate, &read->rd_stateid);
6195}
6196
6197void
6198nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6199{
6200 get_stateid(cstate, &write->wr_stateid);
6201}