blob: b661294144ba0f8fed5dacf8162f90814003be97 [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;
Kinglong Meefb94d762014-08-05 21:20:27 +08001040 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001041
1042 might_sleep();
1043
1044 while (!list_empty(reaplist)) {
1045 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1046 st_locks);
1047 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001048 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001049 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001050 if (fp)
1051 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001052 }
1053}
1054
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001055static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001056{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001057 struct nfs4_client *clp = lo->lo_owner.so_client;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001058 struct nfs4_ol_stateid *stp;
1059 struct list_head reaplist;
1060
1061 INIT_LIST_HEAD(&reaplist);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001062
Trond Myklebustd4f04892014-07-29 21:34:36 -04001063 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001064 unhash_lockowner_locked(lo);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001065 while (!list_empty(&lo->lo_owner.so_stateids)) {
1066 stp = list_first_entry(&lo->lo_owner.so_stateids,
1067 struct nfs4_ol_stateid, st_perstateowner);
1068 unhash_lock_stateid(stp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001069 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001070 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001071 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001072 free_ol_stateid_reaplist(&reaplist);
Jeff Layton6b180f02014-07-29 21:34:26 -04001073 nfs4_put_stateowner(&lo->lo_owner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001074}
1075
Jeff Laytond83017f2014-07-29 21:34:42 -04001076static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1077 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001078{
1079 struct nfs4_ol_stateid *stp;
1080
1081 while (!list_empty(&open_stp->st_locks)) {
1082 stp = list_entry(open_stp->st_locks.next,
1083 struct nfs4_ol_stateid, st_locks);
Jeff Laytond83017f2014-07-29 21:34:42 -04001084 unhash_lock_stateid(stp);
1085 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001086 }
1087}
1088
Jeff Laytond83017f2014-07-29 21:34:42 -04001089static void unhash_open_stateid(struct nfs4_ol_stateid *stp,
1090 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001091{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001092 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1093
Jeff Layton4ae098d2014-07-29 21:34:43 -04001094 unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001095 release_open_stateid_locks(stp, reaplist);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001096}
1097
1098static void release_open_stateid(struct nfs4_ol_stateid *stp)
1099{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001100 LIST_HEAD(reaplist);
1101
1102 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04001103 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001104 put_ol_stateid_locked(stp, &reaplist);
1105 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1106 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001107}
1108
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001109static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001110{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001111 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001112
Trond Myklebustd4f04892014-07-29 21:34:36 -04001113 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001114
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001115 list_del_init(&oo->oo_owner.so_strhash);
1116 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001117}
1118
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001119static void release_last_closed_stateid(struct nfs4_openowner *oo)
1120{
Jeff Layton217526e2014-07-30 08:27:11 -04001121 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1122 nfsd_net_id);
1123 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001124
Jeff Layton217526e2014-07-30 08:27:11 -04001125 spin_lock(&nn->client_lock);
1126 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001127 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001128 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001129 oo->oo_last_closed_stid = NULL;
1130 }
Jeff Layton217526e2014-07-30 08:27:11 -04001131 spin_unlock(&nn->client_lock);
1132 if (s)
1133 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001134}
1135
Jeff Layton2c41beb2014-07-29 21:34:41 -04001136static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001137{
1138 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001139 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001140 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001141
Jeff Layton2c41beb2014-07-29 21:34:41 -04001142 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001143
Trond Myklebustd4f04892014-07-29 21:34:36 -04001144 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001145 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001146 while (!list_empty(&oo->oo_owner.so_stateids)) {
1147 stp = list_first_entry(&oo->oo_owner.so_stateids,
1148 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytond83017f2014-07-29 21:34:42 -04001149 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001150 put_ol_stateid_locked(stp, &reaplist);
1151 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001152 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001153 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001154 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001155 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001156}
1157
Marc Eshel5282fd72009-04-03 08:27:52 +03001158static inline int
1159hash_sessionid(struct nfs4_sessionid *sessionid)
1160{
1161 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1162
1163 return sid->sequence % SESSION_HASH_SIZE;
1164}
1165
Trond Myklebust8f199b82012-03-20 15:11:17 -04001166#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001167static inline void
1168dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1169{
1170 u32 *ptr = (u32 *)(&sessionid->data[0]);
1171 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1172}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001173#else
1174static inline void
1175dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1176{
1177}
1178#endif
1179
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001180/*
1181 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1182 * won't be used for replay.
1183 */
1184void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1185{
1186 struct nfs4_stateowner *so = cstate->replay_owner;
1187
1188 if (nfserr == nfserr_replay_me)
1189 return;
1190
1191 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001192 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001193 return;
1194 }
1195 if (!so)
1196 return;
1197 if (so->so_is_open_owner)
1198 release_last_closed_stateid(openowner(so));
1199 so->so_seqid++;
1200 return;
1201}
Marc Eshel5282fd72009-04-03 08:27:52 +03001202
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001203static void
1204gen_sessionid(struct nfsd4_session *ses)
1205{
1206 struct nfs4_client *clp = ses->se_client;
1207 struct nfsd4_sessionid *sid;
1208
1209 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1210 sid->clientid = clp->cl_clientid;
1211 sid->sequence = current_sessionid++;
1212 sid->reserved = 0;
1213}
1214
1215/*
Andy Adamsona6496372009-08-28 08:45:01 -04001216 * The protocol defines ca_maxresponssize_cached to include the size of
1217 * the rpc header, but all we need to cache is the data starting after
1218 * the end of the initial SEQUENCE operation--the rest we regenerate
1219 * each time. Therefore we can advertise a ca_maxresponssize_cached
1220 * value that is the number of bytes in our cache plus a few additional
1221 * bytes. In order to stay on the safe side, and not promise more than
1222 * we can cache, those additional bytes must be the minimum possible: 24
1223 * bytes of rpc header (xid through accept state, with AUTH_NULL
1224 * verifier), 12 for the compound header (with zero-length tag), and 44
1225 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001226 */
Andy Adamsona6496372009-08-28 08:45:01 -04001227#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1228
Andy Adamson557ce262009-08-28 08:45:04 -04001229static void
1230free_session_slots(struct nfsd4_session *ses)
1231{
1232 int i;
1233
1234 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1235 kfree(ses->se_slots[i]);
1236}
1237
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001238/*
1239 * We don't actually need to cache the rpc and session headers, so we
1240 * can allocate a little less for each slot:
1241 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001242static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001243{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001244 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001245
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001246 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1247 size = 0;
1248 else
1249 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1250 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001251}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001252
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001253/*
1254 * XXX: If we run out of reserved DRC memory we could (up to a point)
1255 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001256 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001257 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001258static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001259{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001260 u32 slotsize = slot_bytes(ca);
1261 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001262 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001263
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001264 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001265 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1266 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001267 num = min_t(int, num, avail / slotsize);
1268 nfsd_drc_mem_used += num * slotsize;
1269 spin_unlock(&nfsd_drc_lock);
1270
1271 return num;
1272}
1273
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001274static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001275{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001276 int slotsize = slot_bytes(ca);
1277
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001278 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001279 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001280 spin_unlock(&nfsd_drc_lock);
1281}
1282
Kinglong Mee60810e52014-01-01 00:35:47 +08001283static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1284 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001285{
Kinglong Mee60810e52014-01-01 00:35:47 +08001286 int numslots = fattrs->maxreqs;
1287 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001288 struct nfsd4_session *new;
1289 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001290
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001291 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001292 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1293 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001294
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001295 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001296 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001297 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001298 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001299 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001300 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001301 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001302 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001303 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001304
1305 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1306 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1307
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001308 return new;
1309out_free:
1310 while (i--)
1311 kfree(new->se_slots[i]);
1312 kfree(new);
1313 return NULL;
1314}
1315
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001316static void free_conn(struct nfsd4_conn *c)
1317{
1318 svc_xprt_put(c->cn_xprt);
1319 kfree(c);
1320}
1321
1322static void nfsd4_conn_lost(struct svc_xpt_user *u)
1323{
1324 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1325 struct nfs4_client *clp = c->cn_session->se_client;
1326
1327 spin_lock(&clp->cl_lock);
1328 if (!list_empty(&c->cn_persession)) {
1329 list_del(&c->cn_persession);
1330 free_conn(c);
1331 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001332 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001333 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001334}
1335
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001336static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001337{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001338 struct nfsd4_conn *conn;
1339
1340 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1341 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001342 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001343 svc_xprt_get(rqstp->rq_xprt);
1344 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001345 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001346 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1347 return conn;
1348}
1349
J. Bruce Fields328ead22010-09-29 16:11:06 -04001350static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1351{
1352 conn->cn_session = ses;
1353 list_add(&conn->cn_persession, &ses->se_conns);
1354}
1355
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001356static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1357{
1358 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001359
1360 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001361 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001362 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001363}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001364
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001365static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001366{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001367 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001368 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001369}
1370
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001371static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001372{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001373 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001374
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001375 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001376 ret = nfsd4_register_conn(conn);
1377 if (ret)
1378 /* oops; xprt is already down: */
1379 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001380 /* We may have gained or lost a callback channel: */
1381 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001382}
1383
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001384static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001385{
1386 u32 dir = NFS4_CDFC4_FORE;
1387
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001388 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001389 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001390 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001391}
1392
1393/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001394static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001395{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001396 struct nfs4_client *clp = s->se_client;
1397 struct nfsd4_conn *c;
1398
1399 spin_lock(&clp->cl_lock);
1400 while (!list_empty(&s->se_conns)) {
1401 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1402 list_del_init(&c->cn_persession);
1403 spin_unlock(&clp->cl_lock);
1404
1405 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1406 free_conn(c);
1407
1408 spin_lock(&clp->cl_lock);
1409 }
1410 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001411}
1412
J. Bruce Fields1377b692012-09-11 21:42:40 -04001413static void __free_session(struct nfsd4_session *ses)
1414{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001415 free_session_slots(ses);
1416 kfree(ses);
1417}
1418
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001419static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001420{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001421 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001422 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001423 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001424}
Andy Adamson557ce262009-08-28 08:45:04 -04001425
Fengguang Wu135ae822012-11-10 07:20:25 -05001426static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001427{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001428 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001429 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001430
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001431 new->se_client = clp;
1432 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001433
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001434 INIT_LIST_HEAD(&new->se_conns);
1435
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001436 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001437 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001438 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001439 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001440 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001441 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001442 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001443 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001444 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001445 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001446
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001447 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001448 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001449 /*
1450 * This is a little silly; with sessions there's no real
1451 * use for the callback address. Use the peer address
1452 * as a reasonable default for now, but consider fixing
1453 * the rpc client not to require an address in the
1454 * future:
1455 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001456 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1457 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001458 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001459}
1460
Benny Halevy9089f1b2010-05-12 00:12:26 +03001461/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001462static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001463__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001464{
1465 struct nfsd4_session *elem;
1466 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001467 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001468
Trond Myklebust0a880a22014-07-30 08:27:10 -04001469 lockdep_assert_held(&nn->client_lock);
1470
Marc Eshel5282fd72009-04-03 08:27:52 +03001471 dump_sessionid(__func__, sessionid);
1472 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001473 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001474 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001475 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1476 NFS4_MAX_SESSIONID_LEN)) {
1477 return elem;
1478 }
1479 }
1480
1481 dprintk("%s: session not found\n", __func__);
1482 return NULL;
1483}
1484
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001485static struct nfsd4_session *
1486find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1487 __be32 *ret)
1488{
1489 struct nfsd4_session *session;
1490 __be32 status = nfserr_badsession;
1491
1492 session = __find_in_sessionid_hashtbl(sessionid, net);
1493 if (!session)
1494 goto out;
1495 status = nfsd4_get_session_locked(session);
1496 if (status)
1497 session = NULL;
1498out:
1499 *ret = status;
1500 return session;
1501}
1502
Benny Halevy9089f1b2010-05-12 00:12:26 +03001503/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001504static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001505unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001506{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001507 struct nfs4_client *clp = ses->se_client;
1508 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1509
1510 lockdep_assert_held(&nn->client_lock);
1511
Andy Adamson7116ed62009-04-03 08:27:43 +03001512 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001513 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001514 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001515 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001516}
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1519static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001520STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001522 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001524 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001525 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 return 1;
1527}
1528
1529/*
1530 * XXX Should we use a slab cache ?
1531 * This type of memory management is somewhat inefficient, but we use it
1532 * anyway since SETCLIENTID is not a common operation.
1533 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001534static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
1536 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001537 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001539 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1540 if (clp == NULL)
1541 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001542 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001543 if (clp->cl_name.data == NULL)
1544 goto err_no_name;
1545 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1546 OWNER_HASH_SIZE, GFP_KERNEL);
1547 if (!clp->cl_ownerstr_hashtbl)
1548 goto err_no_hashtbl;
1549 for (i = 0; i < OWNER_HASH_SIZE; i++)
1550 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001551 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001552 INIT_LIST_HEAD(&clp->cl_sessions);
1553 idr_init(&clp->cl_stateids);
1554 atomic_set(&clp->cl_refcount, 0);
1555 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1556 INIT_LIST_HEAD(&clp->cl_idhash);
1557 INIT_LIST_HEAD(&clp->cl_openowners);
1558 INIT_LIST_HEAD(&clp->cl_delegations);
1559 INIT_LIST_HEAD(&clp->cl_lru);
1560 INIT_LIST_HEAD(&clp->cl_callbacks);
1561 INIT_LIST_HEAD(&clp->cl_revoked);
1562 spin_lock_init(&clp->cl_lock);
1563 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001565err_no_hashtbl:
1566 kfree(clp->cl_name.data);
1567err_no_name:
1568 kfree(clp);
1569 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570}
1571
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001572static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573free_client(struct nfs4_client *clp)
1574{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001575 while (!list_empty(&clp->cl_sessions)) {
1576 struct nfsd4_session *ses;
1577 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1578 se_perclnt);
1579 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001580 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1581 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001582 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001583 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001584 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001585 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001587 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 kfree(clp);
1589}
1590
Benny Halevy84d38ac2010-05-12 00:13:16 +03001591/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001592static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001593unhash_client_locked(struct nfs4_client *clp)
1594{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001595 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001596 struct nfsd4_session *ses;
1597
Trond Myklebust0a880a22014-07-30 08:27:10 -04001598 lockdep_assert_held(&nn->client_lock);
1599
Trond Myklebust4beb3452014-07-30 08:27:02 -04001600 /* Mark the client as expired! */
1601 clp->cl_time = 0;
1602 /* Make it invisible */
1603 if (!list_empty(&clp->cl_idhash)) {
1604 list_del_init(&clp->cl_idhash);
1605 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1606 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1607 else
1608 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1609 }
1610 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001611 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001612 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1613 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001614 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001615}
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001618unhash_client(struct nfs4_client *clp)
1619{
1620 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1621
1622 spin_lock(&nn->client_lock);
1623 unhash_client_locked(clp);
1624 spin_unlock(&nn->client_lock);
1625}
1626
Jeff Layton97403d92014-07-30 08:27:12 -04001627static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1628{
1629 if (atomic_read(&clp->cl_refcount))
1630 return nfserr_jukebox;
1631 unhash_client_locked(clp);
1632 return nfs_ok;
1633}
1634
Trond Myklebust4beb3452014-07-30 08:27:02 -04001635static void
1636__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001638 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 struct list_head reaplist;
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001643 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001644 while (!list_empty(&clp->cl_delegations)) {
1645 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001646 unhash_delegation_locked(dp);
1647 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
Benny Halevycdc97502014-05-30 09:09:30 -04001649 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 while (!list_empty(&reaplist)) {
1651 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001652 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001653 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001655 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001656 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001657 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001658 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001659 }
NeilBrownea1da632005-06-23 22:04:17 -07001660 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001661 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Jeff Laytond3134b12014-07-29 21:34:32 -04001662 atomic_inc(&oo->oo_owner.so_count);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001663 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001665 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001666 if (clp->cl_cb_conn.cb_xprt)
1667 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001668 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
1670
Trond Myklebust4beb3452014-07-30 08:27:02 -04001671static void
1672destroy_client(struct nfs4_client *clp)
1673{
1674 unhash_client(clp);
1675 __destroy_client(clp);
1676}
1677
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001678static void expire_client(struct nfs4_client *clp)
1679{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001680 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001681 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001682 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001683}
1684
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001685static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1686{
1687 memcpy(target->cl_verifier.data, source->data,
1688 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
1690
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001691static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1692{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1694 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1695}
1696
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001697static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001698{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001699 if (source->cr_principal) {
1700 target->cr_principal =
1701 kstrdup(source->cr_principal, GFP_KERNEL);
1702 if (target->cr_principal == NULL)
1703 return -ENOMEM;
1704 } else
1705 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001706 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 target->cr_uid = source->cr_uid;
1708 target->cr_gid = source->cr_gid;
1709 target->cr_group_info = source->cr_group_info;
1710 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001711 target->cr_gss_mech = source->cr_gss_mech;
1712 if (source->cr_gss_mech)
1713 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001714 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715}
1716
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001717static long long
1718compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1719{
1720 long long res;
1721
1722 res = o1->len - o2->len;
1723 if (res)
1724 return res;
1725 return (long long)memcmp(o1->data, o2->data, o1->len);
1726}
1727
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001728static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001729{
NeilBrowna55370a2005-06-23 22:03:52 -07001730 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
1733static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001734same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1735{
1736 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737}
1738
1739static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001740same_clid(clientid_t *cl1, clientid_t *cl2)
1741{
1742 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743}
1744
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001745static bool groups_equal(struct group_info *g1, struct group_info *g2)
1746{
1747 int i;
1748
1749 if (g1->ngroups != g2->ngroups)
1750 return false;
1751 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001752 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001753 return false;
1754 return true;
1755}
1756
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001757/*
1758 * RFC 3530 language requires clid_inuse be returned when the
1759 * "principal" associated with a requests differs from that previously
1760 * used. We use uid, gid's, and gss principal string as our best
1761 * approximation. We also don't want to allow non-gss use of a client
1762 * established using gss: in theory cr_principal should catch that
1763 * change, but in practice cr_principal can be null even in the gss case
1764 * since gssd doesn't always pass down a principal string.
1765 */
1766static bool is_gss_cred(struct svc_cred *cr)
1767{
1768 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1769 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1770}
1771
1772
Vivek Trivedi5559b502012-07-24 21:18:20 +05301773static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001774same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1775{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001776 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001777 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1778 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001779 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1780 return false;
1781 if (cr1->cr_principal == cr2->cr_principal)
1782 return true;
1783 if (!cr1->cr_principal || !cr2->cr_principal)
1784 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301785 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
1787
J. Bruce Fields57266a62013-04-13 14:27:29 -04001788static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1789{
1790 struct svc_cred *cr = &rqstp->rq_cred;
1791 u32 service;
1792
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001793 if (!cr->cr_gss_mech)
1794 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001795 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1796 return service == RPC_GSS_SVC_INTEGRITY ||
1797 service == RPC_GSS_SVC_PRIVACY;
1798}
1799
1800static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1801{
1802 struct svc_cred *cr = &rqstp->rq_cred;
1803
1804 if (!cl->cl_mach_cred)
1805 return true;
1806 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1807 return false;
1808 if (!svc_rqst_integrity_protected(rqstp))
1809 return false;
1810 if (!cr->cr_principal)
1811 return false;
1812 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1813}
1814
Jeff Layton294ac322014-07-30 08:27:15 -04001815static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001816{
Chuck Leverab4684d2012-03-02 17:13:50 -05001817 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Jeff Laytonf4199922014-06-17 07:44:11 -04001819 /*
1820 * This is opaque to client, so no need to byte-swap. Use
1821 * __force to keep sparse happy
1822 */
1823 verf[0] = (__force __be32)get_seconds();
Jeff Layton294ac322014-07-30 08:27:15 -04001824 verf[1] = (__force __be32)nn->clientid_counter;
Chuck Leverab4684d2012-03-02 17:13:50 -05001825 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826}
1827
Jeff Layton294ac322014-07-30 08:27:15 -04001828static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1829{
1830 clp->cl_clientid.cl_boot = nn->boot_time;
1831 clp->cl_clientid.cl_id = nn->clientid_counter++;
1832 gen_confirm(clp, nn);
1833}
1834
Jeff Layton4770d722014-07-29 21:34:10 -04001835static struct nfs4_stid *
1836find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001837{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001838 struct nfs4_stid *ret;
1839
1840 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1841 if (!ret || !ret->sc_type)
1842 return NULL;
1843 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001844}
1845
Jeff Layton4770d722014-07-29 21:34:10 -04001846static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001847find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001848{
1849 struct nfs4_stid *s;
1850
Jeff Layton4770d722014-07-29 21:34:10 -04001851 spin_lock(&cl->cl_lock);
1852 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001853 if (s != NULL) {
1854 if (typemask & s->sc_type)
1855 atomic_inc(&s->sc_count);
1856 else
1857 s = NULL;
1858 }
Jeff Layton4770d722014-07-29 21:34:10 -04001859 spin_unlock(&cl->cl_lock);
1860 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001861}
1862
Jeff Layton2216d442012-11-12 15:00:57 -05001863static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001864 struct svc_rqst *rqstp, nfs4_verifier *verf)
1865{
1866 struct nfs4_client *clp;
1867 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001868 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001869 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001870
1871 clp = alloc_client(name);
1872 if (clp == NULL)
1873 return NULL;
1874
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001875 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1876 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001877 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001878 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001879 }
Jeff Layton02e12152014-07-16 10:31:57 -04001880 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001881 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001882 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001883 copy_verf(clp, verf);
1884 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001885 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001886 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001887 return clp;
1888}
1889
NeilBrownfd39ca92005-06-23 22:04:03 -07001890static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001891add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1892{
1893 struct rb_node **new = &(root->rb_node), *parent = NULL;
1894 struct nfs4_client *clp;
1895
1896 while (*new) {
1897 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1898 parent = *new;
1899
1900 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1901 new = &((*new)->rb_left);
1902 else
1903 new = &((*new)->rb_right);
1904 }
1905
1906 rb_link_node(&new_clp->cl_namenode, parent, new);
1907 rb_insert_color(&new_clp->cl_namenode, root);
1908}
1909
1910static struct nfs4_client *
1911find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1912{
1913 long long cmp;
1914 struct rb_node *node = root->rb_node;
1915 struct nfs4_client *clp;
1916
1917 while (node) {
1918 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1919 cmp = compare_blob(&clp->cl_name, name);
1920 if (cmp > 0)
1921 node = node->rb_left;
1922 else if (cmp < 0)
1923 node = node->rb_right;
1924 else
1925 return clp;
1926 }
1927 return NULL;
1928}
1929
1930static void
1931add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
1933 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001934 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Trond Myklebust0a880a22014-07-30 08:27:10 -04001936 lockdep_assert_held(&nn->client_lock);
1937
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001938 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001939 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001941 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001942 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
NeilBrownfd39ca92005-06-23 22:04:03 -07001945static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946move_to_confirmed(struct nfs4_client *clp)
1947{
1948 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001949 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Trond Myklebust0a880a22014-07-30 08:27:10 -04001951 lockdep_assert_held(&nn->client_lock);
1952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001954 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001955 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001956 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001957 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001958 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959}
1960
1961static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001962find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
1964 struct nfs4_client *clp;
1965 unsigned int idhashval = clientid_hashval(clid->cl_id);
1966
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001967 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001968 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001969 if ((bool)clp->cl_minorversion != sessions)
1970 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001971 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 }
1975 return NULL;
1976}
1977
1978static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001979find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1980{
1981 struct list_head *tbl = nn->conf_id_hashtbl;
1982
Trond Myklebust0a880a22014-07-30 08:27:10 -04001983 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001984 return find_client_in_id_table(tbl, clid, sessions);
1985}
1986
1987static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001988find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001990 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Trond Myklebust0a880a22014-07-30 08:27:10 -04001992 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001993 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994}
1995
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001996static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001997{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001998 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001999}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002000
NeilBrown28ce6052005-06-23 22:03:56 -07002001static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002002find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002003{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002004 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002005 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002006}
2007
2008static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002009find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002010{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002011 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002012 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002013}
2014
NeilBrownfd39ca92005-06-23 22:04:03 -07002015static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002016gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002018 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002019 struct sockaddr *sa = svc_addr(rqstp);
2020 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002021 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Jeff Layton7077ecb2009-08-14 12:57:58 -04002023 /* Currently, we only support tcp and tcp6 for the callback channel */
2024 if (se->se_callback_netid_len == 3 &&
2025 !memcmp(se->se_callback_netid_val, "tcp", 3))
2026 expected_family = AF_INET;
2027 else if (se->se_callback_netid_len == 4 &&
2028 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2029 expected_family = AF_INET6;
2030 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 goto out_err;
2032
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002033 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002034 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002035 (struct sockaddr *)&conn->cb_addr,
2036 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002037
J. Bruce Fields07263f12010-05-31 19:09:40 -04002038 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002040
J. Bruce Fields07263f12010-05-31 19:09:40 -04002041 if (conn->cb_addr.ss_family == AF_INET6)
2042 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002043
J. Bruce Fields07263f12010-05-31 19:09:40 -04002044 conn->cb_prog = se->se_callback_prog;
2045 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002046 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 return;
2048out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002049 conn->cb_addr.ss_family = AF_UNSPEC;
2050 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002051 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 "will not receive delegations\n",
2053 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 return;
2056}
2057
Andy Adamson074fe892009-04-03 08:28:15 +03002058/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002059 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002060 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002061static void
Andy Adamson074fe892009-04-03 08:28:15 +03002062nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2063{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002064 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002065 struct nfsd4_slot *slot = resp->cstate.slot;
2066 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002067
Andy Adamson557ce262009-08-28 08:45:04 -04002068 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002069
Andy Adamson557ce262009-08-28 08:45:04 -04002070 slot->sl_opcnt = resp->opcnt;
2071 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002072
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002073 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002074 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002075 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002076 return;
2077 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002078 base = resp->cstate.data_offset;
2079 slot->sl_datalen = buf->len - base;
2080 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04002081 WARN("%s: sessions DRC could not cache compound\n", __func__);
2082 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002083}
2084
2085/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002086 * Encode the replay sequence operation from the slot values.
2087 * If cachethis is FALSE encode the uncached rep error on the next
2088 * operation which sets resp->p and increments resp->opcnt for
2089 * nfs4svc_encode_compoundres.
2090 *
Andy Adamson074fe892009-04-03 08:28:15 +03002091 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002092static __be32
2093nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2094 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002095{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002096 struct nfsd4_op *op;
2097 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002098
Andy Adamsonabfabf82009-07-23 19:02:18 -04002099 /* Encode the replayed sequence operation */
2100 op = &args->ops[resp->opcnt - 1];
2101 nfsd4_encode_operation(resp, op);
2102
2103 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002104 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002105 op = &args->ops[resp->opcnt++];
2106 op->status = nfserr_retry_uncached_rep;
2107 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002108 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002109 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002110}
2111
2112/*
Andy Adamson557ce262009-08-28 08:45:04 -04002113 * The sequence operation is not cached because we can use the slot and
2114 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002115 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002116static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002117nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2118 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002119{
Andy Adamson557ce262009-08-28 08:45:04 -04002120 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002121 struct xdr_stream *xdr = &resp->xdr;
2122 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002123 __be32 status;
2124
Andy Adamson557ce262009-08-28 08:45:04 -04002125 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002126
Andy Adamsonabfabf82009-07-23 19:02:18 -04002127 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002128 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002129 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002130
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002131 p = xdr_reserve_space(xdr, slot->sl_datalen);
2132 if (!p) {
2133 WARN_ON_ONCE(1);
2134 return nfserr_serverfault;
2135 }
2136 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2137 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002138
Andy Adamson557ce262009-08-28 08:45:04 -04002139 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002140 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002141}
2142
Andy Adamson0733d212009-04-03 08:28:01 +03002143/*
2144 * Set the exchange_id flags returned by the server.
2145 */
2146static void
2147nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2148{
2149 /* pNFS is not supported */
2150 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2151
2152 /* Referrals are supported, Migration is not. */
2153 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2154
2155 /* set the wire flags to return to client. */
2156 clid->flags = new->cl_exchange_flags;
2157}
2158
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002159static bool client_has_state(struct nfs4_client *clp)
2160{
2161 /*
2162 * Note clp->cl_openowners check isn't quite right: there's no
2163 * need to count owners without stateid's.
2164 *
2165 * Also note we should probably be using this in 4.0 case too.
2166 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002167 return !list_empty(&clp->cl_openowners)
2168 || !list_empty(&clp->cl_delegations)
2169 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002170}
2171
Al Virob37ad282006-10-19 23:28:59 -07002172__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002173nfsd4_exchange_id(struct svc_rqst *rqstp,
2174 struct nfsd4_compound_state *cstate,
2175 struct nfsd4_exchange_id *exid)
2176{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002177 struct nfs4_client *conf, *new;
2178 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002179 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002180 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002181 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002182 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002183 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002184 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002185
Jeff Layton363168b2009-08-14 12:57:56 -04002186 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002187 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002188 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002189 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002190 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002191
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002192 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002193 return nfserr_inval;
2194
Andy Adamson0733d212009-04-03 08:28:01 +03002195 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002196 case SP4_MACH_CRED:
2197 if (!svc_rqst_integrity_protected(rqstp))
2198 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002199 case SP4_NONE:
2200 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002201 default: /* checked by xdr code */
2202 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002203 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002204 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002205 }
2206
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002207 new = create_client(exid->clname, rqstp, &verf);
2208 if (new == NULL)
2209 return nfserr_jukebox;
2210
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002211 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03002212 nfs4_lock_state();
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002213 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002214 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002215 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002216 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2217 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2218
J. Bruce Fields136e6582012-05-12 20:37:23 -04002219 if (update) {
2220 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002221 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002222 goto out;
2223 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002224 if (!mach_creds_match(conf, rqstp)) {
2225 status = nfserr_wrong_cred;
2226 goto out;
2227 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002228 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002229 status = nfserr_perm;
2230 goto out;
2231 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002232 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002233 status = nfserr_not_same;
2234 goto out;
2235 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002236 /* case 6 */
2237 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002238 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002239 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002240 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002241 if (client_has_state(conf)) {
2242 status = nfserr_clid_inuse;
2243 goto out;
2244 }
Andy Adamson0733d212009-04-03 08:28:01 +03002245 goto out_new;
2246 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002247 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002248 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002249 goto out_copy;
2250 }
2251 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002252 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002253 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002254 }
2255
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002256 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002257 status = nfserr_noent;
2258 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002259 }
2260
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002261 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002262 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002263 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002264
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002265 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002266out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002267 if (conf) {
2268 status = mark_client_expired_locked(conf);
2269 if (status)
2270 goto out;
2271 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002272 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002273 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002274
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002275 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002276 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002277 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002278out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002279 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2280 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002281
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002282 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2283 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002284
2285 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002286 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002287 status = nfs_ok;
2288
2289out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002290 spin_unlock(&nn->client_lock);
Andy Adamson0733d212009-04-03 08:28:01 +03002291 nfs4_unlock_state();
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002292 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002293 expire_client(new);
2294 if (unconf)
2295 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002296 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002297}
2298
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002299static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002300check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002301{
Andy Adamson88e588d2009-07-23 19:02:15 -04002302 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2303 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002304
2305 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002306 if (slot_inuse) {
2307 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002308 return nfserr_jukebox;
2309 else
2310 return nfserr_seq_misordered;
2311 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002312 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002313 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002314 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002315 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002316 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002317 return nfserr_seq_misordered;
2318}
2319
Andy Adamson49557cc2009-07-23 19:02:16 -04002320/*
2321 * Cache the create session result into the create session single DRC
2322 * slot cache by saving the xdr structure. sl_seqid has been set.
2323 * Do this for solo or embedded create session operations.
2324 */
2325static void
2326nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002327 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002328{
2329 slot->sl_status = nfserr;
2330 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2331}
2332
2333static __be32
2334nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2335 struct nfsd4_clid_slot *slot)
2336{
2337 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2338 return slot->sl_status;
2339}
2340
Mi Jinlong1b74c252011-07-14 14:50:17 +08002341#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2342 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2343 1 + /* MIN tag is length with zero, only length */ \
2344 3 + /* version, opcount, opcode */ \
2345 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2346 /* seqid, slotID, slotID, cache */ \
2347 4 ) * sizeof(__be32))
2348
2349#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2350 2 + /* verifier: AUTH_NULL, length 0 */\
2351 1 + /* status */ \
2352 1 + /* MIN tag is length with zero, only length */ \
2353 3 + /* opcount, opcode, opstatus*/ \
2354 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2355 /* seqid, slotID, slotID, slotID, status */ \
2356 5 ) * sizeof(__be32))
2357
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002358static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002359{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002360 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2361
J. Bruce Fields373cd402013-04-08 15:42:12 -04002362 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2363 return nfserr_toosmall;
2364 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2365 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002366 ca->headerpadsz = 0;
2367 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2368 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2369 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2370 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2371 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2372 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2373 /*
2374 * Note decreasing slot size below client's request may make it
2375 * difficult for client to function correctly, whereas
2376 * decreasing the number of slots will (just?) affect
2377 * performance. When short on memory we therefore prefer to
2378 * decrease number of slots instead of their size. Clients that
2379 * request larger slots than they need will get poor results:
2380 */
2381 ca->maxreqs = nfsd4_get_drc_mem(ca);
2382 if (!ca->maxreqs)
2383 return nfserr_jukebox;
2384
J. Bruce Fields373cd402013-04-08 15:42:12 -04002385 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002386}
2387
Kinglong Mee8a891632013-12-23 18:11:02 +08002388#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2389 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2390#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2391 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2392
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002393static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2394{
2395 ca->headerpadsz = 0;
2396
2397 /*
2398 * These RPC_MAX_HEADER macros are overkill, especially since we
2399 * don't even do gss on the backchannel yet. But this is still
2400 * less than 1k. Tighten up this estimate in the unlikely event
2401 * it turns out to be a problem for some client:
2402 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002403 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002404 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002405 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002406 return nfserr_toosmall;
2407 ca->maxresp_cached = 0;
2408 if (ca->maxops < 2)
2409 return nfserr_toosmall;
2410
2411 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002412}
2413
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002414static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2415{
2416 switch (cbs->flavor) {
2417 case RPC_AUTH_NULL:
2418 case RPC_AUTH_UNIX:
2419 return nfs_ok;
2420 default:
2421 /*
2422 * GSS case: the spec doesn't allow us to return this
2423 * error. But it also doesn't allow us not to support
2424 * GSS.
2425 * I'd rather this fail hard than return some error the
2426 * client might think it can already handle:
2427 */
2428 return nfserr_encr_alg_unsupp;
2429 }
2430}
2431
Andy Adamson069b6ad2009-04-03 08:27:58 +03002432__be32
2433nfsd4_create_session(struct svc_rqst *rqstp,
2434 struct nfsd4_compound_state *cstate,
2435 struct nfsd4_create_session *cr_ses)
2436{
Jeff Layton363168b2009-08-14 12:57:56 -04002437 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002438 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002439 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002440 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002441 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002442 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002443 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002444 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002445
Mi Jinlonga62573d2011-03-23 17:57:07 +08002446 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2447 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002448 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2449 if (status)
2450 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002451 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002452 if (status)
2453 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002454 status = check_backchannel_attrs(&cr_ses->back_channel);
2455 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002456 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002457 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002458 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002459 if (!new)
2460 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002461 conn = alloc_conn_from_crses(rqstp, cr_ses);
2462 if (!conn)
2463 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002464
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002465 nfs4_lock_state();
Jeff Laytond20c11d2014-07-30 08:27:07 -04002466 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002467 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002468 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002469 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002470
2471 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002472 status = nfserr_wrong_cred;
2473 if (!mach_creds_match(conf, rqstp))
2474 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002475 cs_slot = &conf->cl_cs_slot;
2476 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002477 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002478 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002479 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002480 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002481 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002482 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002483 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002484 } else if (unconf) {
2485 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002486 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002487 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002488 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002489 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002490 status = nfserr_wrong_cred;
2491 if (!mach_creds_match(unconf, rqstp))
2492 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002493 cs_slot = &unconf->cl_cs_slot;
2494 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002495 if (status) {
2496 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002497 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002498 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002499 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002500 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002501 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002502 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002503 if (status) {
2504 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002505 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002506 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002507 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002508 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002509 conf = unconf;
2510 } else {
2511 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002512 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002513 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002514 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002515 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002516 * We do not support RDMA or persistent sessions
2517 */
2518 cr_ses->flags &= ~SESSION4_PERSIST;
2519 cr_ses->flags &= ~SESSION4_RDMA;
2520
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002521 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002522 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002523
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002524 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002525 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002526 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002527 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002528
Jeff Laytond20c11d2014-07-30 08:27:07 -04002529 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002530 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002531 spin_unlock(&nn->client_lock);
2532 /* init connection and backchannel */
2533 nfsd4_init_conn(rqstp, conn, new);
2534 nfsd4_put_session(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002535 nfs4_unlock_state();
Jeff Laytond20c11d2014-07-30 08:27:07 -04002536 if (old)
2537 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002538 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002539out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002540 spin_unlock(&nn->client_lock);
Yanchuan Nian266533c2012-12-24 18:11:45 +08002541 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002542 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002543 if (old)
2544 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002545out_free_session:
2546 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002547out_release_drc_mem:
2548 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002549 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002550}
2551
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002552static __be32 nfsd4_map_bcts_dir(u32 *dir)
2553{
2554 switch (*dir) {
2555 case NFS4_CDFC4_FORE:
2556 case NFS4_CDFC4_BACK:
2557 return nfs_ok;
2558 case NFS4_CDFC4_FORE_OR_BOTH:
2559 case NFS4_CDFC4_BACK_OR_BOTH:
2560 *dir = NFS4_CDFC4_BOTH;
2561 return nfs_ok;
2562 };
2563 return nfserr_inval;
2564}
2565
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002566__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2567{
2568 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002569 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002570 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002571
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002572 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2573 if (status)
2574 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002575 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002576 session->se_cb_prog = bc->bc_cb_program;
2577 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002578 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002579
2580 nfsd4_probe_callback(session->se_client);
2581
2582 return nfs_ok;
2583}
2584
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002585__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2586 struct nfsd4_compound_state *cstate,
2587 struct nfsd4_bind_conn_to_session *bcts)
2588{
2589 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002590 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002591 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002592 struct net *net = SVC_NET(rqstp);
2593 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002594
2595 if (!nfsd4_last_compound_op(rqstp))
2596 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002597 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002598 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002599 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002600 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002601 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002602 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002603 status = nfserr_wrong_cred;
2604 if (!mach_creds_match(session->se_client, rqstp))
2605 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002606 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002607 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002608 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002609 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002610 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002611 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002612 goto out;
2613 nfsd4_init_conn(rqstp, conn, session);
2614 status = nfs_ok;
2615out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002616 nfsd4_put_session(session);
2617out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002618 nfs4_unlock_state();
2619 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002620}
2621
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002622static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2623{
2624 if (!session)
2625 return 0;
2626 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2627}
2628
Andy Adamson069b6ad2009-04-03 08:27:58 +03002629__be32
2630nfsd4_destroy_session(struct svc_rqst *r,
2631 struct nfsd4_compound_state *cstate,
2632 struct nfsd4_destroy_session *sessionid)
2633{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002634 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002635 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002636 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002637 struct net *net = SVC_NET(r);
2638 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002639
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002640 nfs4_lock_state();
2641 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002642 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002643 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002644 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002645 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002646 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002647 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002648 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002649 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002650 if (!ses)
2651 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002652 status = nfserr_wrong_cred;
2653 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002654 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002655 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002656 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002657 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002658 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002659 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002660
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002661 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002662
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002663 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002664 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002665out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002666 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002667out_client_lock:
2668 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002669out:
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002670 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002671 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002672}
2673
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002674static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002675{
2676 struct nfsd4_conn *c;
2677
2678 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002679 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002680 return c;
2681 }
2682 }
2683 return NULL;
2684}
2685
J. Bruce Fields57266a62013-04-13 14:27:29 -04002686static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002687{
2688 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002689 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002690 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002691 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002692
2693 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002694 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002695 if (c)
2696 goto out_free;
2697 status = nfserr_conn_not_bound_to_session;
2698 if (clp->cl_mach_cred)
2699 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002700 __nfsd4_hash_conn(new, ses);
2701 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002702 ret = nfsd4_register_conn(new);
2703 if (ret)
2704 /* oops; xprt is already down: */
2705 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002706 return nfs_ok;
2707out_free:
2708 spin_unlock(&clp->cl_lock);
2709 free_conn(new);
2710 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002711}
2712
Mi Jinlong868b89c2011-04-27 09:09:58 +08002713static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2714{
2715 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2716
2717 return args->opcnt > session->se_fchannel.maxops;
2718}
2719
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002720static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2721 struct nfsd4_session *session)
2722{
2723 struct xdr_buf *xb = &rqstp->rq_arg;
2724
2725 return xb->len > session->se_fchannel.maxreq_sz;
2726}
2727
Andy Adamson069b6ad2009-04-03 08:27:58 +03002728__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002729nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002730 struct nfsd4_compound_state *cstate,
2731 struct nfsd4_sequence *seq)
2732{
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002733 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002734 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002735 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002736 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002737 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002738 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002739 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002740 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002741 struct net *net = SVC_NET(rqstp);
2742 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002743
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002744 if (resp->opcnt != 1)
2745 return nfserr_sequence_pos;
2746
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002747 /*
2748 * Will be either used or freed by nfsd4_sequence_check_conn
2749 * below.
2750 */
2751 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2752 if (!conn)
2753 return nfserr_jukebox;
2754
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002755 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002756 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002757 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002758 goto out_no_session;
2759 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002760
Mi Jinlong868b89c2011-04-27 09:09:58 +08002761 status = nfserr_too_many_ops;
2762 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002763 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002764
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002765 status = nfserr_req_too_big;
2766 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002767 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002768
Benny Halevyb85d4c02009-04-03 08:28:08 +03002769 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002770 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002771 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002772
Andy Adamson557ce262009-08-28 08:45:04 -04002773 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002774 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2775
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002776 /* We do not negotiate the number of slots yet, so set the
2777 * maxslots to the session maxreqs which is used to encode
2778 * sr_highest_slotid and the sr_target_slot id to maxslots */
2779 seq->maxslots = session->se_fchannel.maxreqs;
2780
J. Bruce Fields73e79482012-02-13 16:39:00 -05002781 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2782 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002783 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002784 status = nfserr_seq_misordered;
2785 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002786 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002787 cstate->slot = slot;
2788 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002789 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002790 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002791 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002792 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002793 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002794 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002795 }
2796 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002797 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002798
J. Bruce Fields57266a62013-04-13 14:27:29 -04002799 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002800 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002801 if (status)
2802 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002803
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002804 buflen = (seq->cachethis) ?
2805 session->se_fchannel.maxresp_cached :
2806 session->se_fchannel.maxresp_sz;
2807 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2808 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002809 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002810 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002811 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002812
2813 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002814 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002815 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002816 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002817 if (seq->cachethis)
2818 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002819 else
2820 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002821
2822 cstate->slot = slot;
2823 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002824 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002825
Benny Halevyb85d4c02009-04-03 08:28:08 +03002826out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002827 switch (clp->cl_cb_state) {
2828 case NFSD4_CB_DOWN:
2829 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2830 break;
2831 case NFSD4_CB_FAULT:
2832 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2833 break;
2834 default:
2835 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002836 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002837 if (!list_empty(&clp->cl_revoked))
2838 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002839out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002840 if (conn)
2841 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002842 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002843 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002844out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002845 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002846 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002847}
2848
Trond Myklebustb6076642014-06-30 11:48:35 -04002849void
2850nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2851{
2852 struct nfsd4_compound_state *cs = &resp->cstate;
2853
2854 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002855 if (cs->status != nfserr_replay_cache) {
2856 nfsd4_store_cache_entry(resp);
2857 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2858 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002859 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002860 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002861 } else if (cs->clp)
2862 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002863}
2864
Mi Jinlong345c2842011-10-20 17:51:39 +08002865__be32
2866nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2867{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002868 struct nfs4_client *conf, *unconf;
2869 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002870 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002871 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002872
2873 nfs4_lock_state();
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002874 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002875 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002876 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002877 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002878
2879 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002880 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002881 status = nfserr_clientid_busy;
2882 goto out;
2883 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04002884 status = mark_client_expired_locked(conf);
2885 if (status)
2886 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002887 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08002888 } else if (unconf)
2889 clp = unconf;
2890 else {
2891 status = nfserr_stale_clientid;
2892 goto out;
2893 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002894 if (!mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002895 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002896 status = nfserr_wrong_cred;
2897 goto out;
2898 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002899 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002900out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002901 spin_unlock(&nn->client_lock);
Mi Jinlong345c2842011-10-20 17:51:39 +08002902 nfs4_unlock_state();
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002903 if (clp)
2904 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002905 return status;
2906}
2907
Andy Adamson069b6ad2009-04-03 08:27:58 +03002908__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002909nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2910{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002911 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002912
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002913 if (rc->rca_one_fs) {
2914 if (!cstate->current_fh.fh_dentry)
2915 return nfserr_nofilehandle;
2916 /*
2917 * We don't take advantage of the rca_one_fs case.
2918 * That's OK, it's optional, we can safely ignore it.
2919 */
2920 return nfs_ok;
2921 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002922
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002923 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002924 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002925 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2926 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002927 goto out;
2928
2929 status = nfserr_stale_clientid;
2930 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002931 /*
2932 * The following error isn't really legal.
2933 * But we only get here if the client just explicitly
2934 * destroyed the client. Surely it no longer cares what
2935 * error it gets back on an operation for the dead
2936 * client.
2937 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002938 goto out;
2939
2940 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002941 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002942out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002943 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002944 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002945}
2946
2947__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002948nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2949 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002951 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002953 struct nfs4_client *conf, *new;
2954 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002955 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002956 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2957
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002958 new = create_client(clname, rqstp, &clverifier);
2959 if (new == NULL)
2960 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002961 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 nfs4_lock_state();
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002963 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002964 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002965 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002966 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002968 if (clp_used_exchangeid(conf))
2969 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002970 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002971 char addr_str[INET6_ADDRSTRLEN];
2972 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2973 sizeof(addr_str));
2974 dprintk("NFSD: setclientid: string in use by client "
2975 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 goto out;
2977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002979 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002980 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002981 unhash_client_locked(unconf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002982 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002983 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002985 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002986 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002987 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002988 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002989 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2991 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2992 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002993 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 status = nfs_ok;
2995out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002996 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 nfs4_unlock_state();
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002998 if (new)
2999 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003000 if (unconf)
3001 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 return status;
3003}
3004
3005
Al Virob37ad282006-10-19 23:28:59 -07003006__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003007nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3008 struct nfsd4_compound_state *cstate,
3009 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010{
NeilBrown21ab45a2005-06-23 22:04:14 -07003011 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003012 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3014 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003015 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003016 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003018 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07003021
Jeff Laytond20c11d2014-07-30 08:27:07 -04003022 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003023 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003024 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003025 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003026 * We try hard to give out unique clientid's, so if we get an
3027 * attempt to confirm the same clientid with a different cred,
3028 * there's a bug somewhere. Let's charitably assume it's our
3029 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003030 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04003031 status = nfserr_serverfault;
3032 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3033 goto out;
3034 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3035 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003036 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003037 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3038 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003040 else /* case 4: client hasn't noticed we rebooted yet? */
3041 status = nfserr_stale_clientid;
3042 goto out;
3043 }
3044 status = nfs_ok;
3045 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003046 old = unconf;
3047 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003048 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003049 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003050 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3051 if (old) {
3052 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003053 if (status) {
3054 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003055 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003056 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003057 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003058 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003059 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003060 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003061 get_client_locked(conf);
3062 spin_unlock(&nn->client_lock);
3063 nfsd4_probe_callback(conf);
3064 spin_lock(&nn->client_lock);
3065 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003067 spin_unlock(&nn->client_lock);
3068 if (old)
3069 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 nfs4_unlock_state();
3071 return status;
3072}
3073
J. Bruce Fields32513b42011-10-13 16:00:16 -04003074static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003076 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3077}
3078
3079/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003080static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003081{
Trond Myklebustca943212014-07-23 16:17:39 -04003082 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
Trond Myklebust950e0112014-06-30 11:48:31 -04003084 lockdep_assert_held(&state_lock);
3085
J. Bruce Fields32513b42011-10-13 16:00:16 -04003086 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003087 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003088 INIT_LIST_HEAD(&fp->fi_stateids);
3089 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003090 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003091 fp->fi_had_conflict = false;
3092 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003093 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003094 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3095 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04003096 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097}
3098
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003099void
NeilBrowne60d4392005-06-23 22:03:01 -07003100nfsd4_free_slabs(void)
3101{
Christoph Hellwigabf11352014-05-21 07:43:03 -07003102 kmem_cache_destroy(openowner_slab);
3103 kmem_cache_destroy(lockowner_slab);
3104 kmem_cache_destroy(file_slab);
3105 kmem_cache_destroy(stateid_slab);
3106 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003107}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Bryan Schumaker72083392011-11-01 15:24:59 -04003109int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110nfsd4_init_slabs(void)
3111{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003112 openowner_slab = kmem_cache_create("nfsd4_openowners",
3113 sizeof(struct nfs4_openowner), 0, 0, NULL);
3114 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003115 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003116 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003117 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003118 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003119 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003120 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003121 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003122 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003123 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003124 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003125 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003126 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003127 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003128 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003129 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003130 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003131 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003133
3134out_free_stateid_slab:
3135 kmem_cache_destroy(stateid_slab);
3136out_free_file_slab:
3137 kmem_cache_destroy(file_slab);
3138out_free_lockowner_slab:
3139 kmem_cache_destroy(lockowner_slab);
3140out_free_openowner_slab:
3141 kmem_cache_destroy(openowner_slab);
3142out:
NeilBrowne60d4392005-06-23 22:03:01 -07003143 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3144 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145}
3146
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003147static void init_nfs4_replay(struct nfs4_replay *rp)
3148{
3149 rp->rp_status = nfserr_serverfault;
3150 rp->rp_buflen = 0;
3151 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003152 mutex_init(&rp->rp_mutex);
3153}
3154
3155static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3156 struct nfs4_stateowner *so)
3157{
3158 if (!nfsd4_has_session(cstate)) {
3159 mutex_lock(&so->so_replay.rp_mutex);
3160 cstate->replay_owner = so;
3161 atomic_inc(&so->so_count);
3162 }
3163}
3164
3165void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3166{
3167 struct nfs4_stateowner *so = cstate->replay_owner;
3168
3169 if (so != NULL) {
3170 cstate->replay_owner = NULL;
3171 mutex_unlock(&so->so_replay.rp_mutex);
3172 nfs4_put_stateowner(so);
3173 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003174}
3175
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003176static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177{
3178 struct nfs4_stateowner *sop;
3179
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003180 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003181 if (!sop)
3182 return NULL;
3183
3184 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3185 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003186 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003187 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003189 sop->so_owner.len = owner->len;
3190
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003191 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003192 sop->so_client = clp;
3193 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003194 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003195 return sop;
3196}
3197
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003198static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003199{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003200 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003201
Trond Myklebustd4f04892014-07-29 21:34:36 -04003202 list_add(&oo->oo_owner.so_strhash,
3203 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003204 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205}
3206
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003207static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3208{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003209 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003210}
3211
Jeff Layton6b180f02014-07-29 21:34:26 -04003212static void nfs4_free_openowner(struct nfs4_stateowner *so)
3213{
3214 struct nfs4_openowner *oo = openowner(so);
3215
3216 kmem_cache_free(openowner_slab, oo);
3217}
3218
3219static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003220 .so_unhash = nfs4_unhash_openowner,
3221 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003222};
3223
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003224static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003225alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003226 struct nfsd4_compound_state *cstate)
3227{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003228 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003229 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003231 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3232 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003234 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003235 oo->oo_owner.so_is_open_owner = 1;
3236 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003237 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003238 if (nfsd4_has_session(cstate))
3239 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003240 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003241 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003242 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003243 spin_lock(&clp->cl_lock);
3244 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003245 if (ret == NULL) {
3246 hash_openowner(oo, clp, strhashval);
3247 ret = oo;
3248 } else
3249 nfs4_free_openowner(&oo->oo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003250 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003251 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252}
3253
J. Bruce Fields996e0932011-10-17 11:14:48 -04003254static 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 -04003255 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003257 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003258 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003259 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003260 stp->st_stateowner = &oo->oo_owner;
Jeff Laytond3134b12014-07-29 21:34:32 -04003261 atomic_inc(&stp->st_stateowner->so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07003262 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003263 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 stp->st_access_bmap = 0;
3265 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003266 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003267 spin_lock(&oo->oo_owner.so_client->cl_lock);
3268 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003269 spin_lock(&fp->fi_lock);
3270 list_add(&stp->st_perfile, &fp->fi_stateids);
3271 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003272 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273}
3274
Jeff Laytond3134b12014-07-29 21:34:32 -04003275/*
3276 * In the 4.0 case we need to keep the owners around a little while to handle
3277 * CLOSE replay. We still do need to release any file access that is held by
3278 * them before returning however.
3279 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003281move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282{
Jeff Layton217526e2014-07-30 08:27:11 -04003283 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003284 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3285 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3286 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003287
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003288 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
Jeff Laytonb401be222014-07-29 21:34:33 -04003290 /*
3291 * We know that we hold one reference via nfsd4_close, and another
3292 * "persistent" reference for the client. If the refcount is higher
3293 * than 2, then there are still calls in progress that are using this
3294 * stateid. We can't put the sc_file reference until they are finished.
3295 * Wait for the refcount to drop to 2. Since it has been unhashed,
3296 * there should be no danger of the refcount going back up again at
3297 * this point.
3298 */
3299 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3300
Jeff Laytond3134b12014-07-29 21:34:32 -04003301 release_all_access(s);
3302 if (s->st_stid.sc_file) {
3303 put_nfs4_file(s->st_stid.sc_file);
3304 s->st_stid.sc_file = NULL;
3305 }
Jeff Layton217526e2014-07-30 08:27:11 -04003306
3307 spin_lock(&nn->client_lock);
3308 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003309 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003310 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003311 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003312 spin_unlock(&nn->client_lock);
3313 if (last)
3314 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315}
3316
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317/* search file_hashtbl[] for file */
3318static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003319find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320{
Trond Myklebustca943212014-07-23 16:17:39 -04003321 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 struct nfs4_file *fp;
3323
Trond Myklebust950e0112014-06-30 11:48:31 -04003324 lockdep_assert_held(&state_lock);
3325
Jeff Layton89876f82013-04-02 09:01:59 -04003326 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003327 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003328 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 }
3332 return NULL;
3333}
3334
Trond Myklebust950e0112014-06-30 11:48:31 -04003335static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003336find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003337{
3338 struct nfs4_file *fp;
3339
3340 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003341 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003342 spin_unlock(&state_lock);
3343 return fp;
3344}
3345
3346static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003347find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003348{
3349 struct nfs4_file *fp;
3350
3351 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003352 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003353 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003354 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003355 fp = new;
3356 }
3357 spin_unlock(&state_lock);
3358
3359 return fp;
3360}
3361
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003362/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 * Called to check deny when READ with all zero stateid or
3364 * WRITE with all zero or all one stateid
3365 */
Al Virob37ad282006-10-19 23:28:59 -07003366static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3368{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003370 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
Trond Myklebustca943212014-07-23 16:17:39 -04003372 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003373 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003374 return ret;
3375 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003376 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003377 if (fp->fi_share_deny & deny_type)
3378 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003379 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003380 put_nfs4_file(fp);
3381 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382}
3383
Jeff Layton02e12152014-07-16 10:31:57 -04003384void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385{
Trond Myklebust11b91642014-07-29 21:34:08 -04003386 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3387 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003388
Trond Myklebust11b91642014-07-29 21:34:08 -04003389 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003390
Jeff Layton02e12152014-07-16 10:31:57 -04003391 /*
3392 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003393 * already holding inode->i_lock.
3394 *
Jeff Laytondff13992014-07-08 14:02:49 -04003395 * If the dl_time != 0, then we know that it has already been
3396 * queued for a lease break. Don't queue it again.
3397 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003398 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003399 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003400 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003401 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003402 }
Jeff Layton02e12152014-07-16 10:31:57 -04003403 spin_unlock(&state_lock);
3404}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405
Jeff Layton02e12152014-07-16 10:31:57 -04003406static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3407{
3408 /*
3409 * We're assuming the state code never drops its reference
3410 * without first removing the lease. Since we're in this lease
3411 * callback (and since the lease code is serialized by the kernel
3412 * lock) we know the server hasn't removed the lease yet, we know
3413 * it's safe to take a reference.
3414 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003415 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003416 nfsd4_cb_recall(dp);
3417}
3418
Jeff Layton1c8c6012013-06-21 08:58:15 -04003419/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003420static void nfsd_break_deleg_cb(struct file_lock *fl)
3421{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003422 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3423 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003424
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003425 if (!fp) {
3426 WARN(1, "(%p)->fl_owner NULL\n", fl);
3427 return;
3428 }
3429 if (fp->fi_had_conflict) {
3430 WARN(1, "duplicate break on %p\n", fp);
3431 return;
3432 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003433 /*
3434 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003435 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003436 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003437 */
3438 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
Jeff Layton02e12152014-07-16 10:31:57 -04003440 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003441 fp->fi_had_conflict = true;
3442 /*
3443 * If there are no delegations on the list, then we can't count on this
3444 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3445 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3446 * true should keep any new delegations from being hashed.
3447 */
3448 if (list_empty(&fp->fi_delegations))
3449 fl->fl_break_time = jiffies;
3450 else
3451 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3452 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003453 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454}
3455
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456static
3457int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3458{
3459 if (arg & F_UNLCK)
3460 return lease_modify(onlist, arg);
3461 else
3462 return -EAGAIN;
3463}
3464
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003465static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003466 .lm_break = nfsd_break_deleg_cb,
3467 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468};
3469
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003470static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3471{
3472 if (nfsd4_has_session(cstate))
3473 return nfs_ok;
3474 if (seqid == so->so_seqid - 1)
3475 return nfserr_replay_me;
3476 if (seqid == so->so_seqid)
3477 return nfs_ok;
3478 return nfserr_bad_seqid;
3479}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
Jeff Layton4b24ca72014-06-30 11:48:44 -04003481static __be32 lookup_clientid(clientid_t *clid,
3482 struct nfsd4_compound_state *cstate,
3483 struct nfsd_net *nn)
3484{
3485 struct nfs4_client *found;
3486
3487 if (cstate->clp) {
3488 found = cstate->clp;
3489 if (!same_clid(&found->cl_clientid, clid))
3490 return nfserr_stale_clientid;
3491 return nfs_ok;
3492 }
3493
3494 if (STALE_CLIENTID(clid, nn))
3495 return nfserr_stale_clientid;
3496
3497 /*
3498 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3499 * cached already then we know this is for is for v4.0 and "sessions"
3500 * will be false.
3501 */
3502 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003503 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003504 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003505 if (!found) {
3506 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003507 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003508 }
3509 atomic_inc(&found->cl_refcount);
3510 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003511
3512 /* Cache the nfs4_client in cstate! */
3513 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003514 return nfs_ok;
3515}
3516
Al Virob37ad282006-10-19 23:28:59 -07003517__be32
Andy Adamson66689582009-04-03 08:28:45 +03003518nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003519 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 clientid_t *clientid = &open->op_clientid;
3522 struct nfs4_client *clp = NULL;
3523 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003524 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003525 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003527 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003529 /*
3530 * In case we need it later, after we've already created the
3531 * file and don't want to risk a further failure:
3532 */
3533 open->op_file = nfsd4_alloc_file();
3534 if (open->op_file == NULL)
3535 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
Trond Myklebust2d91e892014-06-30 11:48:46 -04003537 status = lookup_clientid(clientid, cstate, nn);
3538 if (status)
3539 return status;
3540 clp = cstate->clp;
3541
Trond Myklebustd4f04892014-07-29 21:34:36 -04003542 strhashval = ownerstr_hashval(&open->op_owner);
3543 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003544 open->op_openowner = oo;
3545 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003546 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003548 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003549 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003550 release_openowner(oo);
3551 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003552 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003553 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003554 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3555 if (status)
3556 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003557 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003558new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003559 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003560 if (oo == NULL)
3561 return nfserr_jukebox;
3562 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003563alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003564 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003565 if (!open->op_stp)
3566 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003567 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568}
3569
Al Virob37ad282006-10-19 23:28:59 -07003570static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003571nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3572{
3573 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3574 return nfserr_openmode;
3575 else
3576 return nfs_ok;
3577}
3578
Daniel Mackc47d8322011-05-16 16:38:14 +02003579static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003580{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003581 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3582}
3583
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003584static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003585{
3586 struct nfs4_stid *ret;
3587
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003588 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003589 if (!ret)
3590 return NULL;
3591 return delegstateid(ret);
3592}
3593
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003594static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3595{
3596 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3597 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3598}
3599
Al Virob37ad282006-10-19 23:28:59 -07003600static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003601nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003602 struct nfs4_delegation **dp)
3603{
3604 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003605 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003606 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003607
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003608 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3609 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003610 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003611 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003612 status = nfs4_check_delegmode(deleg, flags);
3613 if (status) {
3614 nfs4_put_stid(&deleg->dl_stid);
3615 goto out;
3616 }
3617 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003618out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003619 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003620 return nfs_ok;
3621 if (status)
3622 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003623 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003624 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003625}
3626
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003627static struct nfs4_ol_stateid *
3628nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003630 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003631 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
Trond Myklebust1d31a252014-07-10 14:07:25 -04003633 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003634 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 /* ignore lock owners */
3636 if (local->st_stateowner->so_is_open_owner == 0)
3637 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003638 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003639 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003640 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003641 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003644 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003645 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646}
3647
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003648static inline int nfs4_access_to_access(u32 nfs4_access)
3649{
3650 int flags = 0;
3651
3652 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3653 flags |= NFSD_MAY_READ;
3654 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3655 flags |= NFSD_MAY_WRITE;
3656 return flags;
3657}
3658
Al Virob37ad282006-10-19 23:28:59 -07003659static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3661 struct nfsd4_open *open)
3662{
3663 struct iattr iattr = {
3664 .ia_valid = ATTR_SIZE,
3665 .ia_size = 0,
3666 };
3667 if (!open->op_truncate)
3668 return 0;
3669 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003670 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3672}
3673
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003674static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003675 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3676 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003677{
Trond Myklebustde186432014-07-10 14:07:26 -04003678 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003679 __be32 status;
3680 int oflag = nfs4_access_to_omode(open->op_share_access);
3681 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003682 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003683
Trond Myklebustde186432014-07-10 14:07:26 -04003684 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003685
3686 /*
3687 * Are we trying to set a deny mode that would conflict with
3688 * current access?
3689 */
3690 status = nfs4_file_check_deny(fp, open->op_share_deny);
3691 if (status != nfs_ok) {
3692 spin_unlock(&fp->fi_lock);
3693 goto out;
3694 }
3695
3696 /* set access to the file */
3697 status = nfs4_file_get_access(fp, open->op_share_access);
3698 if (status != nfs_ok) {
3699 spin_unlock(&fp->fi_lock);
3700 goto out;
3701 }
3702
3703 /* Set access bits in stateid */
3704 old_access_bmap = stp->st_access_bmap;
3705 set_access(open->op_share_access, stp);
3706
3707 /* Set new deny mask */
3708 old_deny_bmap = stp->st_deny_bmap;
3709 set_deny(open->op_share_deny, stp);
3710 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3711
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003712 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003713 spin_unlock(&fp->fi_lock);
3714 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003715 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003716 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003717 spin_lock(&fp->fi_lock);
3718 if (!fp->fi_fds[oflag]) {
3719 fp->fi_fds[oflag] = filp;
3720 filp = NULL;
3721 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003722 }
Trond Myklebustde186432014-07-10 14:07:26 -04003723 spin_unlock(&fp->fi_lock);
3724 if (filp)
3725 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003726
3727 status = nfsd4_truncate(rqstp, cur_fh, open);
3728 if (status)
3729 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003730out:
3731 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003732out_put_access:
3733 stp->st_access_bmap = old_access_bmap;
3734 nfs4_file_put_access(fp, open->op_share_access);
3735 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3736 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003737}
3738
Al Virob37ad282006-10-19 23:28:59 -07003739static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003740nfs4_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 -07003741{
Al Virob37ad282006-10-19 23:28:59 -07003742 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003743 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003745 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003746 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003747
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003748 /* test and set deny mode */
3749 spin_lock(&fp->fi_lock);
3750 status = nfs4_file_check_deny(fp, open->op_share_deny);
3751 if (status == nfs_ok) {
3752 old_deny_bmap = stp->st_deny_bmap;
3753 set_deny(open->op_share_deny, stp);
3754 fp->fi_share_deny |=
3755 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3756 }
3757 spin_unlock(&fp->fi_lock);
3758
3759 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003762 status = nfsd4_truncate(rqstp, cur_fh, open);
3763 if (status != nfs_ok)
3764 reset_union_bmap_deny(old_deny_bmap, stp);
3765 return status;
3766}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003769nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003771 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772}
3773
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003774/* Should we give out recallable state?: */
3775static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3776{
3777 if (clp->cl_cb_state == NFSD4_CB_UP)
3778 return true;
3779 /*
3780 * In the sessions case, since we don't have to establish a
3781 * separate connection for callbacks, we assume it's OK
3782 * until we hear otherwise:
3783 */
3784 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3785}
3786
Jeff Laytond564fbe2014-07-16 10:31:58 -04003787static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003788{
3789 struct file_lock *fl;
3790
3791 fl = locks_alloc_lock();
3792 if (!fl)
3793 return NULL;
3794 locks_init_lock(fl);
3795 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003796 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003797 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3798 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003799 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003800 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003801 return fl;
3802}
3803
J. Bruce Fields99c41512013-05-21 16:21:25 -04003804static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003805{
Trond Myklebust11b91642014-07-29 21:34:08 -04003806 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003807 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003808 struct file *filp;
3809 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003810
Jeff Laytond564fbe2014-07-16 10:31:58 -04003811 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003812 if (!fl)
3813 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003814 filp = find_readable_file(fp);
3815 if (!filp) {
3816 /* We should always have a readable file here */
3817 WARN_ON_ONCE(1);
3818 return -EBADF;
3819 }
3820 fl->fl_file = filp;
3821 status = vfs_setlease(filp, fl->fl_type, &fl);
3822 if (status) {
3823 locks_free_lock(fl);
3824 goto out_fput;
3825 }
Benny Halevycdc97502014-05-30 09:09:30 -04003826 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003827 spin_lock(&fp->fi_lock);
3828 /* Did the lease get broken before we took the lock? */
3829 status = -EAGAIN;
3830 if (fp->fi_had_conflict)
3831 goto out_unlock;
3832 /* Race breaker */
3833 if (fp->fi_lease) {
3834 status = 0;
3835 atomic_inc(&fp->fi_delegees);
3836 hash_delegation_locked(dp, fp);
3837 goto out_unlock;
3838 }
3839 fp->fi_lease = fl;
3840 fp->fi_deleg_file = filp;
3841 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003842 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003843 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003844 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003845 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003846out_unlock:
3847 spin_unlock(&fp->fi_lock);
3848 spin_unlock(&state_lock);
3849out_fput:
3850 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003851 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003852}
3853
Jeff Layton0b266932014-07-25 07:34:25 -04003854static struct nfs4_delegation *
3855nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3856 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003857{
Jeff Layton0b266932014-07-25 07:34:25 -04003858 int status;
3859 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003860
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003861 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003862 return ERR_PTR(-EAGAIN);
3863
3864 dp = alloc_init_deleg(clp, fh);
3865 if (!dp)
3866 return ERR_PTR(-ENOMEM);
3867
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003868 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003869 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003870 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003871 dp->dl_stid.sc_file = fp;
Jeff Layton417c6622014-07-21 09:34:57 -04003872 if (!fp->fi_lease) {
3873 spin_unlock(&fp->fi_lock);
3874 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003875 status = nfs4_setlease(dp);
3876 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003877 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003878 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003879 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003880 status = -EAGAIN;
3881 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003882 }
Benny Halevy931ee562014-05-30 09:09:27 -04003883 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003884 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003885out_unlock:
3886 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003887 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003888out:
3889 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003890 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003891 return ERR_PTR(status);
3892 }
3893 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003894}
3895
Benny Halevy4aa89132012-02-21 14:16:44 -08003896static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3897{
3898 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3899 if (status == -EAGAIN)
3900 open->op_why_no_deleg = WND4_CONTENTION;
3901 else {
3902 open->op_why_no_deleg = WND4_RESOURCE;
3903 switch (open->op_deleg_want) {
3904 case NFS4_SHARE_WANT_READ_DELEG:
3905 case NFS4_SHARE_WANT_WRITE_DELEG:
3906 case NFS4_SHARE_WANT_ANY_DELEG:
3907 break;
3908 case NFS4_SHARE_WANT_CANCEL:
3909 open->op_why_no_deleg = WND4_CANCELLED;
3910 break;
3911 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003912 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003913 }
3914 }
3915}
3916
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917/*
3918 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003919 *
3920 * Note we don't support write delegations, and won't until the vfs has
3921 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 */
3923static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003924nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3925 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926{
3927 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003928 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3929 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003930 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003931 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003933 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003934 open->op_recall = 0;
3935 switch (open->op_claim_type) {
3936 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003937 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003938 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003939 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3940 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003941 break;
3942 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003943 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003944 /*
3945 * Let's not give out any delegations till everyone's
3946 * had the chance to reclaim theirs....
3947 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003948 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003949 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003950 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003951 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003952 /*
3953 * Also, if the file was opened for write or
3954 * create, there's a good chance the client's
3955 * about to write to it, resulting in an
3956 * immediate recall (since we don't support
3957 * write delegations):
3958 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003959 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003960 goto out_no_deleg;
3961 if (open->op_create == NFS4_OPEN_CREATE)
3962 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003963 break;
3964 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003965 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003966 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003967 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003968 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003969 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003971 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003973 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003974 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003975 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003976 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003977 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003978out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003979 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3980 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003981 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003982 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003983 open->op_recall = 1;
3984 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003985
3986 /* 4.1 client asking for a delegation? */
3987 if (open->op_deleg_want)
3988 nfsd4_open_deleg_none_ext(open, status);
3989 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990}
3991
Benny Halevye27f49c2012-02-21 14:16:54 -08003992static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3993 struct nfs4_delegation *dp)
3994{
3995 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3996 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3997 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3998 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3999 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4000 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4001 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4002 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4003 }
4004 /* Otherwise the client must be confused wanting a delegation
4005 * it already has, therefore we don't return
4006 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4007 */
4008}
4009
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010/*
4011 * called with nfs4_lock_state() held.
4012 */
Al Virob37ad282006-10-19 23:28:59 -07004013__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4015{
Andy Adamson66689582009-04-03 08:28:45 +03004016 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004017 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004019 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004020 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004021 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 /*
4024 * Lookup file; if found, lookup stateid and check open request,
4025 * and check for delegations in the process of being recalled.
4026 * If not found, create the nfs4_file struct
4027 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004028 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004029 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004030 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004031 if (status)
4032 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04004033 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004035 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004036 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004037 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004038 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004039 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 }
4041
4042 /*
4043 * OPEN the file, or upgrade an existing OPEN.
4044 * If truncate fails, the OPEN fails.
4045 */
4046 if (stp) {
4047 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004048 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 if (status)
4050 goto out;
4051 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004052 stp = open->op_stp;
4053 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04004054 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004055 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4056 if (status) {
4057 release_open_stateid(stp);
4058 goto out;
4059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004061 update_stateid(&stp->st_stid.sc_stateid);
4062 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
Benny Halevyd24433c2012-02-16 20:57:17 +02004064 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004065 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4066 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4067 open->op_why_no_deleg = WND4_NOT_WANTED;
4068 goto nodeleg;
4069 }
4070 }
4071
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 /*
4073 * Attempt to hand out a delegation. No error return, because the
4074 * OPEN succeeds even if we fail.
4075 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004076 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004077nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 status = nfs_ok;
4079
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004080 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004081 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004083 /* 4.1 client trying to upgrade/downgrade delegation? */
4084 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004085 open->op_deleg_want)
4086 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004087
NeilBrown13cd2182005-06-23 22:03:10 -07004088 if (fp)
4089 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004090 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05004091 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 /*
4093 * To finish the open response, we just need to set the rflags.
4094 */
4095 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004096 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004097 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004099 if (dp)
4100 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004101 if (stp)
4102 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103
4104 return status;
4105}
4106
Jeff Layton58fb12e2014-07-29 21:34:27 -04004107void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4108 struct nfsd4_open *open, __be32 status)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004109{
4110 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004111 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004112
Jeff Laytond3134b12014-07-29 21:34:32 -04004113 nfsd4_cstate_assign_replay(cstate, so);
4114 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004115 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004116 if (open->op_file)
4117 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004118 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004119 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004120}
4121
Al Virob37ad282006-10-19 23:28:59 -07004122__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004123nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4124 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125{
4126 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004127 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004128 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129
4130 nfs4_lock_state();
4131 dprintk("process_renew(%08x/%08x): starting\n",
4132 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004133 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004134 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004136 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004138 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004139 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 goto out;
4141 status = nfs_ok;
4142out:
4143 nfs4_unlock_state();
4144 return status;
4145}
4146
NeilBrowna76b4312005-06-23 22:04:01 -07004147static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004148nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004149{
Jeff Layton33dcc482012-04-10 11:08:48 -04004150 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004151 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004152 return;
4153
NeilBrowna76b4312005-06-23 22:04:01 -07004154 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004155 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004156 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004157 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004158 /*
4159 * Now that every NFSv4 client has had the chance to recover and
4160 * to see the (possibly new, possibly shorter) lease time, we
4161 * can safely set the next grace time to the current lease time:
4162 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03004163 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07004164}
4165
NeilBrownfd39ca92005-06-23 22:04:03 -07004166static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004167nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168{
4169 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004170 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004172 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004174 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004175 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176
4177 nfs4_lock_state();
4178
4179 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004180 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004181 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004182 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004183 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 clp = list_entry(pos, struct nfs4_client, cl_lru);
4185 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4186 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004187 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 break;
4189 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004190 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004191 dprintk("NFSD: client in use (clientid %08x)\n",
4192 clp->cl_clientid.cl_id);
4193 continue;
4194 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004195 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004196 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004197 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004198 list_for_each_safe(pos, next, &reaplist) {
4199 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 dprintk("NFSD: purging unused client (clientid %08x)\n",
4201 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004202 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 expire_client(clp);
4204 }
Benny Halevycdc97502014-05-30 09:09:30 -04004205 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004206 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004208 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4209 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004211 t = dp->dl_time - cutoff;
4212 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 break;
4214 }
Jeff Layton42690672014-07-25 07:34:20 -04004215 unhash_delegation_locked(dp);
4216 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 }
Benny Halevycdc97502014-05-30 09:09:30 -04004218 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004219 while (!list_empty(&reaplist)) {
4220 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4221 dl_recall_lru);
4222 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004223 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 }
Jeff Layton217526e2014-07-30 08:27:11 -04004225
4226 spin_lock(&nn->client_lock);
4227 while (!list_empty(&nn->close_lru)) {
4228 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4229 oo_close_lru);
4230 if (time_after((unsigned long)oo->oo_time,
4231 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004232 t = oo->oo_time - cutoff;
4233 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 break;
4235 }
Jeff Layton217526e2014-07-30 08:27:11 -04004236 list_del_init(&oo->oo_close_lru);
4237 stp = oo->oo_last_closed_stid;
4238 oo->oo_last_closed_stid = NULL;
4239 spin_unlock(&nn->client_lock);
4240 nfs4_put_stid(&stp->st_stid);
4241 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 }
Jeff Layton217526e2014-07-30 08:27:11 -04004243 spin_unlock(&nn->client_lock);
4244
Jeff Laytona832e7a2014-05-30 09:09:26 -04004245 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04004247 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248}
4249
Harvey Harrisona254b242008-02-20 12:49:00 -08004250static struct workqueue_struct *laundry_wq;
4251static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004252
4253static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004254laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255{
4256 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004257 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4258 work);
4259 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4260 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004262 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004264 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265}
4266
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004267static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004268{
Trond Myklebust11b91642014-07-29 21:34:08 -04004269 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004270 return nfserr_bad_stateid;
4271 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272}
4273
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004275access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004277 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4278 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4279 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280}
4281
4282static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004283access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004285 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4286 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287}
4288
4289static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004290__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291{
Al Virob37ad282006-10-19 23:28:59 -07004292 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293
J. Bruce Fields02921912010-07-29 15:16:59 -04004294 /* For lock stateid's, we test the parent open, not the lock: */
4295 if (stp->st_openstp)
4296 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004297 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004299 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 goto out;
4301 status = nfs_ok;
4302out:
4303 return status;
4304}
4305
Al Virob37ad282006-10-19 23:28:59 -07004306static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004307check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004309 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004311 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004312 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 * conflicting state; so we must wait out the grace period. */
4314 return nfserr_grace;
4315 } else if (flags & WR_STATE)
4316 return nfs4_share_conflict(current_fh,
4317 NFS4_SHARE_DENY_WRITE);
4318 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4319 return nfs4_share_conflict(current_fh,
4320 NFS4_SHARE_DENY_READ);
4321}
4322
4323/*
4324 * Allow READ/WRITE during grace period on recovered state only for files
4325 * that are not able to provide mandatory locking.
4326 */
4327static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004328grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004330 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331}
4332
J. Bruce Fields81b82962011-08-23 11:03:29 -04004333/* Returns true iff a is later than b: */
4334static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4335{
Jim Rees1a9357f2013-05-17 17:33:00 -04004336 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004337}
4338
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004339static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004340{
Andy Adamson66689582009-04-03 08:28:45 +03004341 /*
4342 * When sessions are used the stateid generation number is ignored
4343 * when it is zero.
4344 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004345 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004346 return nfs_ok;
4347
4348 if (in->si_generation == ref->si_generation)
4349 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004350
J. Bruce Fields0836f582008-01-26 19:08:12 -05004351 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004352 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004353 return nfserr_bad_stateid;
4354 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004355 * However, we could see a stateid from the past, even from a
4356 * non-buggy client. For example, if the client sends a lock
4357 * while some IO is outstanding, the lock may bump si_generation
4358 * while the IO is still in flight. The client could avoid that
4359 * situation by waiting for responses on all the IO requests,
4360 * but better performance may result in retrying IO that
4361 * receives an old_stateid error if requests are rarely
4362 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004363 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004364 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004365}
4366
Chuck Lever7df302f2012-05-29 13:56:37 -04004367static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004368{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004369 struct nfs4_stid *s;
4370 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004371 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004372
Chuck Lever7df302f2012-05-29 13:56:37 -04004373 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004374 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004375 /* Client debugging aid. */
4376 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4377 char addr_str[INET6_ADDRSTRLEN];
4378 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4379 sizeof(addr_str));
4380 pr_warn_ratelimited("NFSD: client %s testing state ID "
4381 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004382 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004383 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004384 spin_lock(&cl->cl_lock);
4385 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004386 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004387 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004388 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004389 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004390 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004391 switch (s->sc_type) {
4392 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004393 status = nfs_ok;
4394 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004395 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004396 status = nfserr_deleg_revoked;
4397 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004398 case NFS4_OPEN_STID:
4399 case NFS4_LOCK_STID:
4400 ols = openlockstateid(s);
4401 if (ols->st_stateowner->so_is_open_owner
4402 && !(openowner(ols->st_stateowner)->oo_flags
4403 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004404 status = nfserr_bad_stateid;
4405 else
4406 status = nfs_ok;
4407 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004408 default:
4409 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004410 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004411 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004412 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004413 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004414 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004415out_unlock:
4416 spin_unlock(&cl->cl_lock);
4417 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004418}
4419
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004420static __be32
4421nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4422 stateid_t *stateid, unsigned char typemask,
4423 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004424{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004425 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004426
4427 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4428 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004429 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004430 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004431 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004432 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004433 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004434 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004435 if (status)
4436 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004437 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004438 if (!*s)
4439 return nfserr_bad_stateid;
4440 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004441}
4442
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443/*
4444* Checks for stateid operations
4445*/
Al Virob37ad282006-10-19 23:28:59 -07004446__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004447nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004448 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004450 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004451 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004453 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004455 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004456 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004457 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 if (filpp)
4460 *filpp = NULL;
4461
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004462 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 return nfserr_grace;
4464
4465 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004466 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467
Trond Myklebust14bcab12014-04-18 14:44:07 -04004468 nfs4_lock_state();
4469
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004470 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004471 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004472 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004473 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004474 goto unlock_state;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004475 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4476 if (status)
4477 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004478 switch (s->sc_type) {
4479 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004480 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004481 status = nfs4_check_delegmode(dp, flags);
4482 if (status)
4483 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004484 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004485 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004486 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004487 WARN_ON_ONCE(1);
4488 status = nfserr_serverfault;
4489 goto out;
4490 }
Trond Myklebustde186432014-07-10 14:07:26 -04004491 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004492 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004493 break;
4494 case NFS4_OPEN_STID:
4495 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004496 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004497 status = nfs4_check_fh(current_fh, stp);
4498 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004500 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004501 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004503 status = nfs4_check_openmode(stp, flags);
4504 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004506 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004507 struct nfs4_file *fp = stp->st_stid.sc_file;
4508
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004509 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004510 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004511 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004512 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004513 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004514 break;
4515 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004516 status = nfserr_bad_stateid;
4517 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 }
4519 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004520 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004521 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004523 nfs4_put_stid(s);
4524unlock_state:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004525 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 return status;
4527}
4528
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004529/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004530 * Test if the stateid is valid
4531 */
4532__be32
4533nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4534 struct nfsd4_test_stateid *test_stateid)
4535{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004536 struct nfsd4_test_stateid_id *stateid;
4537 struct nfs4_client *cl = cstate->session->se_client;
4538
4539 nfs4_lock_state();
4540 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004541 stateid->ts_id_status =
4542 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004543 nfs4_unlock_state();
4544
Bryan Schumaker17456802011-07-13 10:50:48 -04004545 return nfs_ok;
4546}
4547
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004548__be32
4549nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4550 struct nfsd4_free_stateid *free_stateid)
4551{
4552 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004553 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004554 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004555 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004556 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004557 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004558
4559 nfs4_lock_state();
Jeff Layton1af71cc2014-07-29 21:34:14 -04004560 spin_lock(&cl->cl_lock);
4561 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004562 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004563 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004564 switch (s->sc_type) {
4565 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004566 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004567 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004568 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004569 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4570 if (ret)
4571 break;
4572 ret = nfserr_locks_held;
4573 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004574 case NFS4_LOCK_STID:
4575 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4576 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004577 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004578 stp = openlockstateid(s);
4579 ret = nfserr_locks_held;
4580 if (check_for_locks(stp->st_stid.sc_file,
4581 lockowner(stp->st_stateowner)))
4582 break;
4583 unhash_lock_stateid(stp);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004584 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004585 nfs4_put_stid(s);
4586 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004587 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004588 case NFS4_REVOKED_DELEG_STID:
4589 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004590 list_del_init(&dp->dl_recall_lru);
4591 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004592 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004593 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004594 goto out;
4595 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004596 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004597out_unlock:
4598 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004599out:
4600 nfs4_unlock_state();
4601 return ret;
4602}
4603
NeilBrown4c4cd222005-07-07 17:59:27 -07004604static inline int
4605setlkflg (int type)
4606{
4607 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4608 RD_STATE : WR_STATE;
4609}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004611static __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 -04004612{
4613 struct svc_fh *current_fh = &cstate->current_fh;
4614 struct nfs4_stateowner *sop = stp->st_stateowner;
4615 __be32 status;
4616
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004617 status = nfsd4_check_seqid(cstate, sop, seqid);
4618 if (status)
4619 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004620 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4621 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004622 /*
4623 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004624 * nfserr_replay_me from the previous step, and
4625 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004626 */
4627 return nfserr_bad_stateid;
4628 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4629 if (status)
4630 return status;
4631 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004632}
4633
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634/*
4635 * Checks for sequence id mutating operations.
4636 */
Al Virob37ad282006-10-19 23:28:59 -07004637static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004638nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004639 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004640 struct nfs4_ol_stateid **stpp,
4641 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004643 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004644 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004645 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004647 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4648 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004649
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004651 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004652 if (status)
4653 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004654 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004655 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004657 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004658 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004659 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004660 else
4661 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004662 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004663}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004664
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004665static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4666 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004667{
4668 __be32 status;
4669 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004670 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004672 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004673 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004674 if (status)
4675 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004676 oo = openowner(stp->st_stateowner);
4677 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4678 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004679 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004680 }
4681 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004682 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683}
4684
Al Virob37ad282006-10-19 23:28:59 -07004685__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004686nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004687 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688{
Al Virob37ad282006-10-19 23:28:59 -07004689 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004690 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004691 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004692 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693
Al Viroa6a9f182013-09-16 10:57:01 -04004694 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4695 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004697 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004698 if (status)
4699 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700
4701 nfs4_lock_state();
4702
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004703 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004704 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004705 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004706 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004707 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004708 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004709 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004710 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004711 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004712 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004713 update_stateid(&stp->st_stid.sc_stateid);
4714 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004715 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004716 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004717
Jeff Layton2a4317c2012-03-21 16:42:43 -04004718 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004719 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004720put_stateid:
4721 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004723 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004724 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 return status;
4726}
4727
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004728static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004730 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004731 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004732 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004733 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004734}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004735
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004736static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4737{
4738 switch (to_access) {
4739 case NFS4_SHARE_ACCESS_READ:
4740 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4741 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4742 break;
4743 case NFS4_SHARE_ACCESS_WRITE:
4744 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4745 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4746 break;
4747 case NFS4_SHARE_ACCESS_BOTH:
4748 break;
4749 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004750 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 }
4752}
4753
Al Virob37ad282006-10-19 23:28:59 -07004754__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004755nfsd4_open_downgrade(struct svc_rqst *rqstp,
4756 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004757 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758{
Al Virob37ad282006-10-19 23:28:59 -07004759 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004760 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004761 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762
Al Viroa6a9f182013-09-16 10:57:01 -04004763 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4764 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004766 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004767 if (od->od_deleg_want)
4768 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4769 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
4771 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004772 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004773 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004774 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004777 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004778 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004780 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004782 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004783 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004785 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004787 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788
Jeff Laytonce0fc432012-05-11 09:45:14 -04004789 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004791 update_stateid(&stp->st_stid.sc_stateid);
4792 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004794put_stateid:
4795 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004797 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004798 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 return status;
4800}
4801
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004802static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4803{
Trond Myklebustacf92952014-06-30 11:48:37 -04004804 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytond83017f2014-07-29 21:34:42 -04004805 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004806
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004807 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04004808 spin_lock(&clp->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04004809 unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004810
Jeff Laytond83017f2014-07-29 21:34:42 -04004811 if (clp->cl_minorversion) {
4812 put_ol_stateid_locked(s, &reaplist);
4813 spin_unlock(&clp->cl_lock);
4814 free_ol_stateid_reaplist(&reaplist);
4815 } else {
4816 spin_unlock(&clp->cl_lock);
4817 free_ol_stateid_reaplist(&reaplist);
Jeff Laytond3134b12014-07-29 21:34:32 -04004818 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04004819 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004820}
4821
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822/*
4823 * nfs4_unlock_state() called after encode
4824 */
Al Virob37ad282006-10-19 23:28:59 -07004825__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004826nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004827 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828{
Al Virob37ad282006-10-19 23:28:59 -07004829 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004830 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004831 struct net *net = SVC_NET(rqstp);
4832 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
Al Viroa6a9f182013-09-16 10:57:01 -04004834 dprintk("NFSD: nfsd4_close on file %pd\n",
4835 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
4837 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004838 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4839 &close->cl_stateid,
4840 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004841 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004842 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004843 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004845 update_stateid(&stp->st_stid.sc_stateid);
4846 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004848 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004849
4850 /* put reference from nfs4_preprocess_seqid_op */
4851 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852out:
Jeff Layton58fb12e2014-07-29 21:34:27 -04004853 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 return status;
4855}
4856
Al Virob37ad282006-10-19 23:28:59 -07004857__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004858nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4859 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004861 struct nfs4_delegation *dp;
4862 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004863 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004864 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004865 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004867 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004868 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
4870 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004871 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004872 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004873 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004874 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004875 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004876 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004877 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004878
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004879 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004880put_stateid:
4881 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004883 nfs4_unlock_state();
4884
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 return status;
4886}
4887
4888
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890
Benny Halevy87df4de2008-12-15 19:42:03 +02004891static inline u64
4892end_offset(u64 start, u64 len)
4893{
4894 u64 end;
4895
4896 end = start + len;
4897 return end >= start ? end: NFS4_MAX_UINT64;
4898}
4899
4900/* last octet in a range */
4901static inline u64
4902last_byte_offset(u64 start, u64 len)
4903{
4904 u64 end;
4905
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004906 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004907 end = start + len;
4908 return end > start ? end - 1: NFS4_MAX_UINT64;
4909}
4910
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911/*
4912 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4913 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4914 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4915 * locking, this prevents us from being completely protocol-compliant. The
4916 * real solution to this problem is to start using unsigned file offsets in
4917 * the VFS, but this is a very deep change!
4918 */
4919static inline void
4920nfs4_transform_lock_offset(struct file_lock *lock)
4921{
4922 if (lock->fl_start < 0)
4923 lock->fl_start = OFFSET_MAX;
4924 if (lock->fl_end < 0)
4925 lock->fl_end = OFFSET_MAX;
4926}
4927
NeilBrownd5b90262006-04-10 22:55:22 -07004928/* Hack!: For now, we're defining this just so we can use a pointer to it
4929 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004930static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004931};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932
4933static inline void
4934nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4935{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004936 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937
NeilBrownd5b90262006-04-10 22:55:22 -07004938 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004939 lo = (struct nfs4_lockowner *) fl->fl_owner;
4940 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4941 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004942 if (!deny->ld_owner.data)
4943 /* We just don't care that much */
4944 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004945 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4946 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004947 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004948nevermind:
4949 deny->ld_owner.len = 0;
4950 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004951 deny->ld_clientid.cl_boot = 0;
4952 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 }
4954 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004955 deny->ld_length = NFS4_MAX_UINT64;
4956 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4958 deny->ld_type = NFS4_READ_LT;
4959 if (fl->fl_type != F_RDLCK)
4960 deny->ld_type = NFS4_WRITE_LT;
4961}
4962
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004963static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004964find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004965 struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004967 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004968 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969
Trond Myklebust0a880a22014-07-30 08:27:10 -04004970 lockdep_assert_held(&clp->cl_lock);
4971
Trond Myklebustd4f04892014-07-29 21:34:36 -04004972 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4973 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004974 if (so->so_is_open_owner)
4975 continue;
Trond Myklebustd4f04892014-07-29 21:34:36 -04004976 if (!same_owner_str(so, owner))
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004977 continue;
Jeff Layton5db1c032014-07-29 21:34:28 -04004978 atomic_inc(&so->so_count);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004979 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 }
4981 return NULL;
4982}
4983
Trond Myklebustc58c6612014-07-29 21:34:35 -04004984static struct nfs4_lockowner *
4985find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004986 struct nfs4_client *clp)
Trond Myklebustc58c6612014-07-29 21:34:35 -04004987{
4988 struct nfs4_lockowner *lo;
4989
Trond Myklebustd4f04892014-07-29 21:34:36 -04004990 spin_lock(&clp->cl_lock);
4991 lo = find_lockowner_str_locked(clid, owner, clp);
4992 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004993 return lo;
4994}
4995
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004996static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4997{
Trond Myklebustc58c6612014-07-29 21:34:35 -04004998 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004999}
5000
Jeff Layton6b180f02014-07-29 21:34:26 -04005001static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5002{
5003 struct nfs4_lockowner *lo = lockowner(sop);
5004
5005 kmem_cache_free(lockowner_slab, lo);
5006}
5007
5008static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005009 .so_unhash = nfs4_unhash_lockowner,
5010 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005011};
5012
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013/*
5014 * Alloc a lock owner structure.
5015 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005016 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005018 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005020static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005021alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5022 struct nfs4_ol_stateid *open_stp,
5023 struct nfsd4_lock *lock)
5024{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005025 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005027 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5028 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005030 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5031 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005032 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005033 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005034 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005035 ret = find_lockowner_str_locked(&clp->cl_clientid,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005036 &lock->lk_new_owner, clp);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005037 if (ret == NULL) {
5038 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005039 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005040 ret = lo;
5041 } else
5042 nfs4_free_lockowner(&lo->lo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005043 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005044 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045}
5046
Jeff Layton356a95e2014-07-29 21:34:13 -04005047static void
5048init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5049 struct nfs4_file *fp, struct inode *inode,
5050 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005052 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053
Jeff Layton356a95e2014-07-29 21:34:13 -04005054 lockdep_assert_held(&clp->cl_lock);
5055
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005056 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005057 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005058 stp->st_stateowner = &lo->lo_owner;
Trond Myklebuste4f1dd72014-07-29 21:34:30 -04005059 atomic_inc(&lo->lo_owner.so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07005060 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005061 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04005062 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005063 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005065 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005066 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005067 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005068 spin_lock(&fp->fi_lock);
5069 list_add(&stp->st_perfile, &fp->fi_stateids);
5070 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071}
5072
Jeff Laytonc53530d2014-06-30 11:48:39 -04005073static struct nfs4_ol_stateid *
5074find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5075{
5076 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005077 struct nfs4_client *clp = lo->lo_owner.so_client;
5078
5079 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005080
5081 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005082 if (lst->st_stid.sc_file == fp) {
5083 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005084 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005085 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005086 }
5087 return NULL;
5088}
5089
Jeff Layton356a95e2014-07-29 21:34:13 -04005090static struct nfs4_ol_stateid *
5091find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5092 struct inode *inode, struct nfs4_ol_stateid *ost,
5093 bool *new)
5094{
5095 struct nfs4_stid *ns = NULL;
5096 struct nfs4_ol_stateid *lst;
5097 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5098 struct nfs4_client *clp = oo->oo_owner.so_client;
5099
5100 spin_lock(&clp->cl_lock);
5101 lst = find_lock_stateid(lo, fi);
5102 if (lst == NULL) {
5103 spin_unlock(&clp->cl_lock);
5104 ns = nfs4_alloc_stid(clp, stateid_slab);
5105 if (ns == NULL)
5106 return NULL;
5107
5108 spin_lock(&clp->cl_lock);
5109 lst = find_lock_stateid(lo, fi);
5110 if (likely(!lst)) {
5111 lst = openlockstateid(ns);
5112 init_lock_stateid(lst, lo, fi, inode, ost);
5113 ns = NULL;
5114 *new = true;
5115 }
5116 }
5117 spin_unlock(&clp->cl_lock);
5118 if (ns)
5119 nfs4_put_stid(ns);
5120 return lst;
5121}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005122
NeilBrownfd39ca92005-06-23 22:04:03 -07005123static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124check_lock_length(u64 offset, u64 length)
5125{
Benny Halevy87df4de2008-12-15 19:42:03 +02005126 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 LOFF_OVERFLOW(offset, length)));
5128}
5129
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005130static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005131{
Trond Myklebust11b91642014-07-29 21:34:08 -04005132 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005133
Jeff Layton7214e862014-07-10 14:07:33 -04005134 lockdep_assert_held(&fp->fi_lock);
5135
Jeff Layton82c5ff12012-05-11 09:45:13 -04005136 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005137 return;
Jeff Layton12659652014-07-10 14:07:28 -04005138 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005139 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005140}
5141
Jeff Layton356a95e2014-07-29 21:34:13 -04005142static __be32
5143lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5144 struct nfs4_ol_stateid *ost,
5145 struct nfsd4_lock *lock,
5146 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005147{
Jeff Layton5db1c032014-07-29 21:34:28 -04005148 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005149 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005150 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5151 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005152 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005153 struct nfs4_lockowner *lo;
5154 unsigned int strhashval;
5155
Trond Myklebustd4f04892014-07-29 21:34:36 -04005156 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005157 if (!lo) {
Trond Myklebustd4f04892014-07-29 21:34:36 -04005158 strhashval = ownerstr_hashval(&lock->v.new.owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005159 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5160 if (lo == NULL)
5161 return nfserr_jukebox;
5162 } else {
5163 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005164 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005165 if (!cstate->minorversion &&
5166 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005167 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005168 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005169
Jeff Layton356a95e2014-07-29 21:34:13 -04005170 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005171 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005172 status = nfserr_jukebox;
5173 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005174 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005175 status = nfs_ok;
5176out:
5177 nfs4_put_stateowner(&lo->lo_owner);
5178 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005179}
5180
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181/*
5182 * LOCK operation
5183 */
Al Virob37ad282006-10-19 23:28:59 -07005184__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005185nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005186 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005188 struct nfs4_openowner *open_sop = NULL;
5189 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005190 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005191 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005192 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005193 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005194 struct file_lock *file_lock = NULL;
5195 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005196 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005197 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005198 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005199 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005200 struct net *net = SVC_NET(rqstp);
5201 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202
5203 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5204 (long long) lock->lk_offset,
5205 (long long) lock->lk_length);
5206
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 if (check_lock_length(lock->lk_offset, lock->lk_length))
5208 return nfserr_inval;
5209
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005210 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005211 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005212 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5213 return status;
5214 }
5215
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 nfs4_lock_state();
5217
5218 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005219 if (nfsd4_has_session(cstate))
5220 /* See rfc 5661 18.10.3: given clientid is ignored: */
5221 memcpy(&lock->v.new.clientid,
5222 &cstate->session->se_client->cl_clientid,
5223 sizeof(clientid_t));
5224
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005226 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005230 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 lock->lk_new_open_seqid,
5232 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005233 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005234 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005236 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005237 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005238 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005239 &lock->v.new.clientid))
5240 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005241 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005242 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005243 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005244 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005245 lock->lk_old_lock_seqid,
5246 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005247 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005248 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005249 if (status)
5250 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005251 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005253 lkflg = setlkflg(lock->lk_type);
5254 status = nfs4_check_openmode(lock_stp, lkflg);
5255 if (status)
5256 goto out;
5257
NeilBrown0dd395d2005-07-07 17:59:15 -07005258 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005259 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005260 goto out;
5261 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005262 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005263 goto out;
5264
Jeff Layton21179d82012-08-21 08:03:32 -04005265 file_lock = locks_alloc_lock();
5266 if (!file_lock) {
5267 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5268 status = nfserr_jukebox;
5269 goto out;
5270 }
5271
Trond Myklebust11b91642014-07-29 21:34:08 -04005272 fp = lock_stp->st_stid.sc_file;
Jeff Layton21179d82012-08-21 08:03:32 -04005273 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 switch (lock->lk_type) {
5275 case NFS4_READ_LT:
5276 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005277 spin_lock(&fp->fi_lock);
5278 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005279 if (filp)
5280 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005281 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005282 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005283 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 case NFS4_WRITE_LT:
5285 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005286 spin_lock(&fp->fi_lock);
5287 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005288 if (filp)
5289 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005290 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005291 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005292 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 default:
5294 status = nfserr_inval;
5295 goto out;
5296 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005297 if (!filp) {
5298 status = nfserr_openmode;
5299 goto out;
5300 }
Jeff Layton21179d82012-08-21 08:03:32 -04005301 file_lock->fl_owner = (fl_owner_t)lock_sop;
5302 file_lock->fl_pid = current->tgid;
5303 file_lock->fl_file = filp;
5304 file_lock->fl_flags = FL_POSIX;
5305 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5306 file_lock->fl_start = lock->lk_offset;
5307 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5308 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309
Jeff Layton21179d82012-08-21 08:03:32 -04005310 conflock = locks_alloc_lock();
5311 if (!conflock) {
5312 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5313 status = nfserr_jukebox;
5314 goto out;
5315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316
Jeff Layton21179d82012-08-21 08:03:32 -04005317 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005318 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005320 update_stateid(&lock_stp->st_stid.sc_stateid);
5321 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005323 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005324 break;
5325 case (EAGAIN): /* conflock holds conflicting lock */
5326 status = nfserr_denied;
5327 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005328 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005329 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 case (EDEADLK):
5331 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005332 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005333 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005334 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005335 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005336 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338out:
Trond Myklebustde186432014-07-10 14:07:26 -04005339 if (filp)
5340 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005341 if (lock_stp) {
5342 /* Bump seqid manually if the 4.0 replay owner is openowner */
5343 if (cstate->replay_owner &&
5344 cstate->replay_owner != &lock_sop->lo_owner &&
5345 seqid_mutating_err(ntohl(status)))
5346 lock_sop->lo_owner.so_seqid++;
5347
5348 /*
5349 * If this is a new, never-before-used stateid, and we are
5350 * returning an error, then just go ahead and release it.
5351 */
5352 if (status && new)
5353 release_lock_stateid(lock_stp);
5354
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005355 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005356 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005357 if (open_stp)
5358 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005359 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005360 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005361 if (file_lock)
5362 locks_free_lock(file_lock);
5363 if (conflock)
5364 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 return status;
5366}
5367
5368/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005369 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5370 * so we do a temporary open here just to get an open file to pass to
5371 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5372 * inode operation.)
5373 */
Al Viro04da6e92012-04-13 00:00:04 -04005374static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005375{
5376 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005377 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5378 if (!err) {
5379 err = nfserrno(vfs_test_lock(file, lock));
5380 nfsd_close(file);
5381 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005382 return err;
5383}
5384
5385/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 * LOCKT operation
5387 */
Al Virob37ad282006-10-19 23:28:59 -07005388__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005389nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5390 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391{
Jeff Layton21179d82012-08-21 08:03:32 -04005392 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005393 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005394 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005395 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005397 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 return nfserr_grace;
5399
5400 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5401 return nfserr_inval;
5402
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 nfs4_lock_state();
5404
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005405 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005406 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005407 if (status)
5408 goto out;
5409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005411 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413
Jeff Layton21179d82012-08-21 08:03:32 -04005414 file_lock = locks_alloc_lock();
5415 if (!file_lock) {
5416 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5417 status = nfserr_jukebox;
5418 goto out;
5419 }
5420 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 switch (lockt->lt_type) {
5422 case NFS4_READ_LT:
5423 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005424 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 break;
5426 case NFS4_WRITE_LT:
5427 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005428 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 break;
5430 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005431 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 status = nfserr_inval;
5433 goto out;
5434 }
5435
Trond Myklebustd4f04892014-07-29 21:34:36 -04005436 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5437 cstate->clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005438 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005439 file_lock->fl_owner = (fl_owner_t)lo;
5440 file_lock->fl_pid = current->tgid;
5441 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442
Jeff Layton21179d82012-08-21 08:03:32 -04005443 file_lock->fl_start = lockt->lt_offset;
5444 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445
Jeff Layton21179d82012-08-21 08:03:32 -04005446 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447
Jeff Layton21179d82012-08-21 08:03:32 -04005448 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005449 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005450 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005451
Jeff Layton21179d82012-08-21 08:03:32 -04005452 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005454 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 }
5456out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005457 if (lo)
5458 nfs4_put_stateowner(&lo->lo_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005460 if (file_lock)
5461 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 return status;
5463}
5464
Al Virob37ad282006-10-19 23:28:59 -07005465__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005466nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005467 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005469 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005471 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005472 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005473 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005474 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5475
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5477 (long long) locku->lu_offset,
5478 (long long) locku->lu_length);
5479
5480 if (check_lock_length(locku->lu_offset, locku->lu_length))
5481 return nfserr_inval;
5482
5483 nfs4_lock_state();
5484
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005485 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005486 &locku->lu_stateid, NFS4_LOCK_STID,
5487 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005488 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005490 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005491 if (!filp) {
5492 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005493 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005494 }
Jeff Layton21179d82012-08-21 08:03:32 -04005495 file_lock = locks_alloc_lock();
5496 if (!file_lock) {
5497 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5498 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005499 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005500 }
5501 locks_init_lock(file_lock);
5502 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005503 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005504 file_lock->fl_pid = current->tgid;
5505 file_lock->fl_file = filp;
5506 file_lock->fl_flags = FL_POSIX;
5507 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5508 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509
Jeff Layton21179d82012-08-21 08:03:32 -04005510 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5511 locku->lu_length);
5512 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513
Jeff Layton21179d82012-08-21 08:03:32 -04005514 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005515 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005516 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 goto out_nfserr;
5518 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005519 update_stateid(&stp->st_stid.sc_stateid);
5520 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005521fput:
5522 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005523put_stateid:
5524 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005526 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005527 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005528 if (file_lock)
5529 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 return status;
5531
5532out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005533 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005534 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535}
5536
5537/*
5538 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005539 * true: locks held by lockowner
5540 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005542static bool
5543check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544{
5545 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005546 int status = false;
5547 struct file *filp = find_any_file(fp);
5548 struct inode *inode;
5549
5550 if (!filp) {
5551 /* Any valid lock stateid should have some sort of access */
5552 WARN_ON_ONCE(1);
5553 return status;
5554 }
5555
5556 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557
Jeff Layton1c8c6012013-06-21 08:58:15 -04005558 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005560 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005561 status = true;
5562 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005565 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005566 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 return status;
5568}
5569
Al Virob37ad282006-10-19 23:28:59 -07005570__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005571nfsd4_release_lockowner(struct svc_rqst *rqstp,
5572 struct nfsd4_compound_state *cstate,
5573 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574{
5575 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005576 struct nfs4_stateowner *sop;
5577 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005578 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005580 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005581 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005582 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005583 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584
5585 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5586 clid->cl_boot, clid->cl_id);
5587
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 nfs4_lock_state();
5589
Jeff Layton4b24ca72014-06-30 11:48:44 -04005590 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005591 if (status)
5592 goto out;
5593
Trond Myklebustd4f04892014-07-29 21:34:36 -04005594 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005595 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005596 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005597 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005598 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005599
5600 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005601 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005602
Jeff Layton882e9d22014-07-29 21:34:37 -04005603 /* see if there are still any locks associated with it */
5604 lo = lockowner(sop);
5605 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5606 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5607 status = nfserr_locks_held;
5608 spin_unlock(&clp->cl_lock);
5609 goto out;
5610 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005611 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005612
5613 atomic_inc(&sop->so_count);
5614 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005615 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005616 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005617 if (lo)
5618 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619out:
5620 nfs4_unlock_state();
5621 return status;
5622}
5623
5624static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005625alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626{
NeilBrowna55370a2005-06-23 22:03:52 -07005627 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628}
5629
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005630bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005631nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005632{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005633 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005634
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005635 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005636 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005637}
5638
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639/*
5640 * failure => all reset bets are off, nfserr_no_grace...
5641 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005642struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005643nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644{
5645 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005646 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647
NeilBrowna55370a2005-06-23 22:03:52 -07005648 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5649 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005650 if (crp) {
5651 strhashval = clientstr_hashval(name);
5652 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005653 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005654 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005655 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005656 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005657 }
5658 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659}
5660
Jeff Layton2a4317c2012-03-21 16:42:43 -04005661void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005662nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005663{
5664 list_del(&crp->cr_strhash);
5665 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005666 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005667}
5668
5669void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005670nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671{
5672 struct nfs4_client_reclaim *crp = NULL;
5673 int i;
5674
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005676 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5677 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005679 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 }
5681 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005682 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683}
5684
5685/*
5686 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005687struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005688nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689{
5690 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 struct nfs4_client_reclaim *crp = NULL;
5692
Jeff Layton278c9312012-11-12 15:00:52 -05005693 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694
Jeff Layton278c9312012-11-12 15:00:52 -05005695 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005696 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005697 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 return crp;
5699 }
5700 }
5701 return NULL;
5702}
5703
5704/*
5705* Called from OPEN. Look for clientid in reclaim list.
5706*/
Al Virob37ad282006-10-19 23:28:59 -07005707__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005708nfs4_check_open_reclaim(clientid_t *clid,
5709 struct nfsd4_compound_state *cstate,
5710 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005712 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005713
5714 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005715 status = lookup_clientid(clid, cstate, nn);
5716 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005717 return nfserr_reclaim_bad;
5718
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005719 if (nfsd4_client_record_check(cstate->clp))
5720 return nfserr_reclaim_bad;
5721
5722 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723}
5724
Bryan Schumaker65178db2011-11-01 13:35:21 -04005725#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton7ec0e362014-07-30 08:27:17 -04005726u64
5727nfsd_inject_print_clients(struct nfsd_fault_inject_op *op)
5728{
5729 struct nfs4_client *clp;
5730 u64 count = 0;
5731 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5732 nfsd_net_id);
5733 char buf[INET6_ADDRSTRLEN];
5734
5735 if (!nfsd_netns_ready(nn))
5736 return 0;
5737
5738 spin_lock(&nn->client_lock);
5739 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5740 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5741 pr_info("NFS Client: %s\n", buf);
5742 ++count;
5743 }
5744 spin_unlock(&nn->client_lock);
5745
5746 return count;
5747}
Bryan Schumaker65178db2011-11-01 13:35:21 -04005748
Jeff Laytona0926d12014-07-30 08:27:18 -04005749u64
5750nfsd_inject_forget_client(struct nfsd_fault_inject_op *op,
5751 struct sockaddr_storage *addr, size_t addr_size)
5752{
5753 u64 count = 0;
5754 struct nfs4_client *clp;
5755 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5756 nfsd_net_id);
5757
5758 if (!nfsd_netns_ready(nn))
5759 return count;
5760
5761 spin_lock(&nn->client_lock);
5762 clp = nfsd_find_client(addr, addr_size);
5763 if (clp) {
5764 if (mark_client_expired_locked(clp) == nfs_ok)
5765 ++count;
5766 else
5767 clp = NULL;
5768 }
5769 spin_unlock(&nn->client_lock);
5770
5771 if (clp)
5772 expire_client(clp);
5773
5774 return count;
5775}
5776
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005777u64
5778nfsd_inject_forget_clients(struct nfsd_fault_inject_op *op, u64 max)
5779{
5780 u64 count = 0;
5781 struct nfs4_client *clp, *next;
5782 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5783 nfsd_net_id);
5784 LIST_HEAD(reaplist);
5785
5786 if (!nfsd_netns_ready(nn))
5787 return count;
5788
5789 spin_lock(&nn->client_lock);
5790 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5791 if (mark_client_expired_locked(clp) == nfs_ok) {
5792 list_add(&clp->cl_lru, &reaplist);
5793 if (max != 0 && ++count >= max)
5794 break;
5795 }
5796 }
5797 spin_unlock(&nn->client_lock);
5798
5799 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
5800 expire_client(clp);
5801
5802 return count;
5803}
5804
Bryan Schumaker184c1842012-11-29 11:40:44 -05005805static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5806 const char *type)
5807{
5808 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005809 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005810 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5811}
5812
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005813static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04005814 struct list_head *collect,
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005815 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005816{
5817 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005818 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005819 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005820 u64 count = 0;
5821
5822 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005823 list_for_each_entry_safe(stp, st_next,
5824 &oop->oo_owner.so_stateids, st_perstateowner) {
5825 list_for_each_entry_safe(lst, lst_next,
5826 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04005827 if (func) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005828 func(lst);
Jeff Layton3738d502014-07-30 08:27:20 -04005829 if (collect)
5830 list_add(&lst->st_locks,
5831 collect);
5832 }
Bryan Schumakerfc291712012-11-29 11:40:40 -05005833 if (++count == max)
5834 return count;
5835 }
5836 }
5837 }
5838
5839 return count;
5840}
5841
5842u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5843{
Jeff Layton3738d502014-07-30 08:27:20 -04005844 return nfsd_foreach_client_lock(clp, max, NULL, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005845}
5846
Bryan Schumaker184c1842012-11-29 11:40:44 -05005847u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5848{
Jeff Layton3738d502014-07-30 08:27:20 -04005849 u64 count = nfsd_foreach_client_lock(clp, max, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005850 nfsd_print_count(clp, count, "locked files");
5851 return count;
5852}
5853
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005854static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5855{
5856 struct nfs4_openowner *oop, *next;
5857 u64 count = 0;
5858
5859 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5860 if (func)
5861 func(oop);
5862 if (++count == max)
5863 break;
5864 }
5865
5866 return count;
5867}
5868
5869u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5870{
5871 return nfsd_foreach_client_open(clp, max, release_openowner);
5872}
5873
Bryan Schumaker184c1842012-11-29 11:40:44 -05005874u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5875{
5876 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5877 nfsd_print_count(clp, count, "open files");
5878 return count;
5879}
5880
Bryan Schumaker269de302012-11-29 11:40:42 -05005881static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5882 struct list_head *victims)
5883{
5884 struct nfs4_delegation *dp, *next;
5885 u64 count = 0;
5886
Benny Halevycdc97502014-05-30 09:09:30 -04005887 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005888 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005889 if (victims) {
5890 /*
5891 * It's not safe to mess with delegations that have a
5892 * non-zero dl_time. They might have already been broken
5893 * and could be processed by the laundromat outside of
5894 * the state_lock. Just leave them be.
5895 */
5896 if (dp->dl_time != 0)
5897 continue;
5898
Jeff Layton42690672014-07-25 07:34:20 -04005899 unhash_delegation_locked(dp);
5900 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005901 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005902 if (++count == max)
5903 break;
5904 }
5905 return count;
5906}
5907
5908u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5909{
5910 struct nfs4_delegation *dp, *next;
5911 LIST_HEAD(victims);
5912 u64 count;
5913
Benny Halevycdc97502014-05-30 09:09:30 -04005914 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005915 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005916 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005917
Jeff Layton2d4a5322014-07-25 07:34:21 -04005918 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5919 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005920 revoke_delegation(dp);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005921 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005922
5923 return count;
5924}
5925
5926u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5927{
Jeff Laytondff13992014-07-08 14:02:49 -04005928 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005929 LIST_HEAD(victims);
5930 u64 count;
5931
Benny Halevycdc97502014-05-30 09:09:30 -04005932 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005933 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005934 while (!list_empty(&victims)) {
5935 dp = list_first_entry(&victims, struct nfs4_delegation,
5936 dl_recall_lru);
5937 list_del_init(&dp->dl_recall_lru);
5938 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005939 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005940 }
Benny Halevycdc97502014-05-30 09:09:30 -04005941 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005942
5943 return count;
5944}
5945
Bryan Schumaker184c1842012-11-29 11:40:44 -05005946u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5947{
5948 u64 count = 0;
5949
Benny Halevycdc97502014-05-30 09:09:30 -04005950 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005951 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005952 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005953
5954 nfsd_print_count(clp, count, "delegations");
5955 return count;
5956}
5957
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005958u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005959{
5960 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005961 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005962 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005963
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005964 if (!nfsd_netns_ready(nn))
5965 return 0;
5966
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005967 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005968 count += func(clp, max - count);
5969 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005970 break;
5971 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005972
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005973 return count;
5974}
5975
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005976struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5977{
5978 struct nfs4_client *clp;
5979 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5980
5981 if (!nfsd_netns_ready(nn))
5982 return NULL;
5983
5984 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5985 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5986 return clp;
5987 }
5988 return NULL;
5989}
5990
Bryan Schumaker65178db2011-11-01 13:35:21 -04005991#endif /* CONFIG_NFSD_FAULT_INJECTION */
5992
Meelap Shahc2f1a552007-07-17 04:04:39 -07005993/*
5994 * Since the lifetime of a delegation isn't limited to that of an open, a
5995 * client may quite reasonably hang on to a delegation as long as it has
5996 * the inode cached. This becomes an obvious problem the first time a
5997 * client's inode cache approaches the size of the server's total memory.
5998 *
5999 * For now we avoid this problem by imposing a hard limit on the number
6000 * of delegations, which varies according to the server's memory size.
6001 */
6002static void
6003set_max_delegations(void)
6004{
6005 /*
6006 * Allow at most 4 delegations per megabyte of RAM. Quick
6007 * estimates suggest that in the worst case (where every delegation
6008 * is for a different inode), a delegation could take about 1.5K,
6009 * giving a worst case usage of about 6% of memory.
6010 */
6011 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6012}
6013
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006014static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006015{
6016 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6017 int i;
6018
6019 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6020 CLIENT_HASH_SIZE, GFP_KERNEL);
6021 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006022 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006023 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6024 CLIENT_HASH_SIZE, GFP_KERNEL);
6025 if (!nn->unconf_id_hashtbl)
6026 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006027 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6028 SESSION_HASH_SIZE, GFP_KERNEL);
6029 if (!nn->sessionid_hashtbl)
6030 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006031
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006032 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006033 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006034 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006035 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006036 for (i = 0; i < SESSION_HASH_SIZE; i++)
6037 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006038 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006039 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03006040 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03006041 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006042 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03006043 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006044
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006045 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006046 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006047
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006048 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006049
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006050err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03006051 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006052err_unconf_id:
6053 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006054err:
6055 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006056}
6057
6058static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006059nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006060{
6061 int i;
6062 struct nfs4_client *clp = NULL;
6063 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6064
6065 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6066 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6067 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6068 destroy_client(clp);
6069 }
6070 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006071
Kinglong Mee2b905632014-03-26 22:09:30 +08006072 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6073 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6074 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6075 destroy_client(clp);
6076 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006077 }
6078
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006079 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006080 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006081 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006082 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006083}
6084
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006085int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006086nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07006087{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006088 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006089 int ret;
6090
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006091 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006092 if (ret)
6093 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006094 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006095 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006096 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04006097 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006098 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03006099 nn->nfsd4_grace, net);
6100 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006101 return 0;
6102}
6103
6104/* initialization to perform when the nfsd service is started: */
6105
6106int
6107nfs4_state_start(void)
6108{
6109 int ret;
6110
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006111 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006112 if (ret)
6113 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07006114 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05006115 if (laundry_wq == NULL) {
6116 ret = -ENOMEM;
6117 goto out_recovery;
6118 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006119 ret = nfsd4_create_callback_queue();
6120 if (ret)
6121 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006122
Meelap Shahc2f1a552007-07-17 04:04:39 -07006123 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006124
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006125 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006126
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006127out_free_laundry:
6128 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05006129out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006130 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131}
6132
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006133void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006134nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006138 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006140 cancel_delayed_work_sync(&nn->laundromat_work);
6141 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05006142
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04006143 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04006145 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006146 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006148 unhash_delegation_locked(dp);
6149 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150 }
Benny Halevycdc97502014-05-30 09:09:30 -04006151 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152 list_for_each_safe(pos, next, &reaplist) {
6153 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006154 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04006155 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 }
6157
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006158 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006159 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04006160 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161}
6162
6163void
6164nfs4_state_shutdown(void)
6165{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006166 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006167 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006169
6170static void
6171get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6172{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006173 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6174 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006175}
6176
6177static void
6178put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6179{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006180 if (cstate->minorversion) {
6181 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6182 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6183 }
6184}
6185
6186void
6187clear_current_stateid(struct nfsd4_compound_state *cstate)
6188{
6189 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006190}
6191
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006192/*
6193 * functions to set current state id
6194 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006195void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006196nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6197{
6198 put_stateid(cstate, &odp->od_stateid);
6199}
6200
6201void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006202nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6203{
6204 put_stateid(cstate, &open->op_stateid);
6205}
6206
6207void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006208nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6209{
6210 put_stateid(cstate, &close->cl_stateid);
6211}
6212
6213void
6214nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6215{
6216 put_stateid(cstate, &lock->lk_resp_stateid);
6217}
6218
6219/*
6220 * functions to consume current state id
6221 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006222
6223void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006224nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6225{
6226 get_stateid(cstate, &odp->od_stateid);
6227}
6228
6229void
6230nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6231{
6232 get_stateid(cstate, &drp->dr_stateid);
6233}
6234
6235void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006236nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6237{
6238 get_stateid(cstate, &fsp->fr_stateid);
6239}
6240
6241void
6242nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6243{
6244 get_stateid(cstate, &setattr->sa_stateid);
6245}
6246
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006247void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006248nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6249{
6250 get_stateid(cstate, &close->cl_stateid);
6251}
6252
6253void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006254nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006255{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006256 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006257}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006258
6259void
6260nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6261{
6262 get_stateid(cstate, &read->rd_stateid);
6263}
6264
6265void
6266nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6267{
6268 get_stateid(cstate, &write->wr_stateid);
6269}