blob: 56999cbe84a720c790bb3a2b9ac1be885b84ce92 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
Dave Hansenaceaf782008-02-15 14:37:31 -080035#include <linux/file.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020036#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070038#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070039#include <linux/swap.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
Chuck Lever7df302f2012-05-29 13:56:37 -040041#include <linux/ratelimit.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050042#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton59766872013-02-04 12:50:00 -050043#include <linux/sunrpc/addr.h>
NeilBrown6282cd52014-06-04 17:39:26 +100044#include <linux/hash.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020045#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040046#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050047#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040048#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040050#include "netns.h"
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define NFSDDBG_FACILITY NFSDDBG_PROC
53
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050054#define all_ones {{~0,~0},~0}
55static const stateid_t one_stateid = {
56 .si_generation = ~0,
57 .si_opaque = all_ones,
58};
59static const stateid_t zero_stateid = {
60 /* all fields zero */
61};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010062static const stateid_t currentstateid = {
63 .si_generation = 1,
64};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050065
Andy Adamsonec6b5d72009-04-03 08:28:28 +030066static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070067
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050068#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
69#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010070#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040073static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040074static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
J. Bruce Fields8b671b82009-02-22 14:51:34 -080076/* Locking: */
77
78/* Currently used for almost all code touching nfsv4 state: */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080079static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
J. Bruce Fields8b671b82009-02-22 14:51:34 -080081/*
82 * Currently used for the del_recall_lru and file hash table. In an
83 * effort to decrease the scope of the client_mutex, this spinlock may
84 * eventually cover more:
85 */
Benny Halevycdc97502014-05-30 09:09:30 -040086static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080087
Jeff Laytonb401be222014-07-29 21:34:33 -040088/*
89 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
90 * the refcount on the open stateid to drop.
91 */
92static DECLARE_WAIT_QUEUE_HEAD(close_wq);
93
Christoph Hellwigabf11352014-05-21 07:43:03 -070094static struct kmem_cache *openowner_slab;
95static struct kmem_cache *lockowner_slab;
96static struct kmem_cache *file_slab;
97static struct kmem_cache *stateid_slab;
98static struct kmem_cache *deleg_slab;
NeilBrowne60d4392005-06-23 22:03:01 -070099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100void
101nfs4_lock_state(void)
102{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800103 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400106static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800107
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400108static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800109{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400110 return ses->se_flags & NFS4_SESSION_DEAD;
111}
112
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400113static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
114{
115 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400116 return nfserr_jukebox;
117 ses->se_flags |= NFS4_SESSION_DEAD;
118 return nfs_ok;
119}
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121void
122nfs4_unlock_state(void)
123{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800124 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
J. Bruce Fields221a6872013-04-01 22:23:49 -0400127static bool is_client_expired(struct nfs4_client *clp)
128{
129 return clp->cl_time == 0;
130}
131
J. Bruce Fields221a6872013-04-01 22:23:49 -0400132static __be32 get_client_locked(struct nfs4_client *clp)
133{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400134 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
135
136 lockdep_assert_held(&nn->client_lock);
137
J. Bruce Fields221a6872013-04-01 22:23:49 -0400138 if (is_client_expired(clp))
139 return nfserr_expired;
140 atomic_inc(&clp->cl_refcount);
141 return nfs_ok;
142}
143
144/* must be called under the client_lock */
145static inline void
146renew_client_locked(struct nfs4_client *clp)
147{
148 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
149
150 if (is_client_expired(clp)) {
151 WARN_ON(1);
152 printk("%s: client (clientid %08x/%08x) already expired\n",
153 __func__,
154 clp->cl_clientid.cl_boot,
155 clp->cl_clientid.cl_id);
156 return;
157 }
158
159 dprintk("renewing client (clientid %08x/%08x)\n",
160 clp->cl_clientid.cl_boot,
161 clp->cl_clientid.cl_id);
162 list_move_tail(&clp->cl_lru, &nn->client_lru);
163 clp->cl_time = get_seconds();
164}
165
166static inline void
167renew_client(struct nfs4_client *clp)
168{
169 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
170
171 spin_lock(&nn->client_lock);
172 renew_client_locked(clp);
173 spin_unlock(&nn->client_lock);
174}
175
Fengguang Wuba138432013-04-16 22:14:15 -0400176static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400177{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400178 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
179
180 lockdep_assert_held(&nn->client_lock);
181
J. Bruce Fields221a6872013-04-01 22:23:49 -0400182 if (!atomic_dec_and_test(&clp->cl_refcount))
183 return;
184 if (!is_client_expired(clp))
185 renew_client_locked(clp);
186}
187
Jeff Layton4b24ca72014-06-30 11:48:44 -0400188static void put_client_renew(struct nfs4_client *clp)
189{
190 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
191
Jeff Laytond6c249b2014-07-08 14:02:50 -0400192 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
193 return;
194 if (!is_client_expired(clp))
195 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400196 spin_unlock(&nn->client_lock);
197}
198
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400199static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
200{
201 __be32 status;
202
203 if (is_session_dead(ses))
204 return nfserr_badsession;
205 status = get_client_locked(ses->se_client);
206 if (status)
207 return status;
208 atomic_inc(&ses->se_ref);
209 return nfs_ok;
210}
211
212static void nfsd4_put_session_locked(struct nfsd4_session *ses)
213{
214 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400215 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
216
217 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400218
219 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
220 free_session(ses);
221 put_client_renew_locked(clp);
222}
223
224static void nfsd4_put_session(struct nfsd4_session *ses)
225{
226 struct nfs4_client *clp = ses->se_client;
227 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
228
229 spin_lock(&nn->client_lock);
230 nfsd4_put_session_locked(ses);
231 spin_unlock(&nn->client_lock);
232}
233
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400234static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400235same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400236{
237 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400238 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400239}
240
241static struct nfs4_openowner *
242find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400243 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400244{
245 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400246
Trond Myklebustd4f04892014-07-29 21:34:36 -0400247 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400248
Trond Myklebustd4f04892014-07-29 21:34:36 -0400249 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
250 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400251 if (!so->so_is_open_owner)
252 continue;
Trond Myklebustd4f04892014-07-29 21:34:36 -0400253 if (same_owner_str(so, &open->op_owner)) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400254 atomic_inc(&so->so_count);
Trond Myklebustd4f04892014-07-29 21:34:36 -0400255 return openowner(so);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400256 }
257 }
258 return NULL;
259}
260
261static struct nfs4_openowner *
262find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400263 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400264{
265 struct nfs4_openowner *oo;
266
Trond Myklebustd4f04892014-07-29 21:34:36 -0400267 spin_lock(&clp->cl_lock);
268 oo = find_openstateowner_str_locked(hashval, open, clp);
269 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400270 return oo;
271}
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273static inline u32
274opaque_hashval(const void *ptr, int nbytes)
275{
276 unsigned char *cptr = (unsigned char *) ptr;
277
278 u32 x = 0;
279 while (nbytes--) {
280 x *= 37;
281 x += *cptr++;
282 }
283 return x;
284}
285
J. Bruce Fields32513b42011-10-13 16:00:16 -0400286static void nfsd4_free_file(struct nfs4_file *f)
287{
288 kmem_cache_free(file_slab, f);
289}
290
NeilBrown13cd2182005-06-23 22:03:10 -0700291static inline void
292put_nfs4_file(struct nfs4_file *fi)
293{
Jeff Layton02e12152014-07-16 10:31:57 -0400294 might_lock(&state_lock);
295
Benny Halevycdc97502014-05-30 09:09:30 -0400296 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton89876f82013-04-02 09:01:59 -0400297 hlist_del(&fi->fi_hash);
Benny Halevycdc97502014-05-30 09:09:30 -0400298 spin_unlock(&state_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400299 nfsd4_free_file(fi);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800300 }
NeilBrown13cd2182005-06-23 22:03:10 -0700301}
302
303static inline void
304get_nfs4_file(struct nfs4_file *fi)
305{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800306 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700307}
308
Trond Myklebustde186432014-07-10 14:07:26 -0400309static struct file *
310__nfs4_get_fd(struct nfs4_file *f, int oflag)
311{
312 if (f->fi_fds[oflag])
313 return get_file(f->fi_fds[oflag]);
314 return NULL;
315}
316
317static struct file *
318find_writeable_file_locked(struct nfs4_file *f)
319{
320 struct file *ret;
321
322 lockdep_assert_held(&f->fi_lock);
323
324 ret = __nfs4_get_fd(f, O_WRONLY);
325 if (!ret)
326 ret = __nfs4_get_fd(f, O_RDWR);
327 return ret;
328}
329
330static struct file *
331find_writeable_file(struct nfs4_file *f)
332{
333 struct file *ret;
334
335 spin_lock(&f->fi_lock);
336 ret = find_writeable_file_locked(f);
337 spin_unlock(&f->fi_lock);
338
339 return ret;
340}
341
342static struct file *find_readable_file_locked(struct nfs4_file *f)
343{
344 struct file *ret;
345
346 lockdep_assert_held(&f->fi_lock);
347
348 ret = __nfs4_get_fd(f, O_RDONLY);
349 if (!ret)
350 ret = __nfs4_get_fd(f, O_RDWR);
351 return ret;
352}
353
354static struct file *
355find_readable_file(struct nfs4_file *f)
356{
357 struct file *ret;
358
359 spin_lock(&f->fi_lock);
360 ret = find_readable_file_locked(f);
361 spin_unlock(&f->fi_lock);
362
363 return ret;
364}
365
366static struct file *
367find_any_file(struct nfs4_file *f)
368{
369 struct file *ret;
370
371 spin_lock(&f->fi_lock);
372 ret = __nfs4_get_fd(f, O_RDWR);
373 if (!ret) {
374 ret = __nfs4_get_fd(f, O_WRONLY);
375 if (!ret)
376 ret = __nfs4_get_fd(f, O_RDONLY);
377 }
378 spin_unlock(&f->fi_lock);
379 return ret;
380}
381
Trond Myklebust02a35082014-07-25 07:34:22 -0400382static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800383unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700384
385/*
386 * Open owner state (share locks)
387 */
388
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500389/* hash tables for lock and open owners */
390#define OWNER_HASH_BITS 8
391#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
392#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700393
Trond Myklebustd4f04892014-07-29 21:34:36 -0400394static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400395{
396 unsigned int ret;
397
398 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500399 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400400}
NeilBrownef0f3392006-04-10 22:55:41 -0700401
NeilBrownef0f3392006-04-10 22:55:41 -0700402/* hash table for nfs4_file */
403#define FILE_HASH_BITS 8
404#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800405
Trond Myklebustca943212014-07-23 16:17:39 -0400406static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400407{
Trond Myklebustca943212014-07-23 16:17:39 -0400408 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
409}
410
411static unsigned int file_hashval(struct knfsd_fh *fh)
412{
413 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
414}
415
416static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
417{
418 return fh1->fh_size == fh2->fh_size &&
419 !memcmp(fh1->fh_base.fh_pad,
420 fh2->fh_base.fh_pad,
421 fh1->fh_size);
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400422}
423
Jeff Layton89876f82013-04-02 09:01:59 -0400424static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700425
Jeff Layton12659652014-07-10 14:07:28 -0400426static void
427__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400428{
Jeff Layton7214e862014-07-10 14:07:33 -0400429 lockdep_assert_held(&fp->fi_lock);
430
Jeff Layton12659652014-07-10 14:07:28 -0400431 if (access & NFS4_SHARE_ACCESS_WRITE)
432 atomic_inc(&fp->fi_access[O_WRONLY]);
433 if (access & NFS4_SHARE_ACCESS_READ)
434 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400435}
436
Jeff Layton12659652014-07-10 14:07:28 -0400437static __be32
438nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400439{
Jeff Layton7214e862014-07-10 14:07:33 -0400440 lockdep_assert_held(&fp->fi_lock);
441
Jeff Layton12659652014-07-10 14:07:28 -0400442 /* Does this access mode make sense? */
443 if (access & ~NFS4_SHARE_ACCESS_BOTH)
444 return nfserr_inval;
445
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400446 /* Does it conflict with a deny mode already set? */
447 if ((access & fp->fi_share_deny) != 0)
448 return nfserr_share_denied;
449
Jeff Layton12659652014-07-10 14:07:28 -0400450 __nfs4_file_get_access(fp, access);
451 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400452}
453
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400454static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
455{
456 /* Common case is that there is no deny mode. */
457 if (deny) {
458 /* Does this deny mode make sense? */
459 if (deny & ~NFS4_SHARE_DENY_BOTH)
460 return nfserr_inval;
461
462 if ((deny & NFS4_SHARE_DENY_READ) &&
463 atomic_read(&fp->fi_access[O_RDONLY]))
464 return nfserr_share_denied;
465
466 if ((deny & NFS4_SHARE_DENY_WRITE) &&
467 atomic_read(&fp->fi_access[O_WRONLY]))
468 return nfserr_share_denied;
469 }
470 return nfs_ok;
471}
472
J. Bruce Fields998db522010-08-07 09:21:41 -0400473static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400474{
Trond Myklebustde186432014-07-10 14:07:26 -0400475 might_lock(&fp->fi_lock);
476
477 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
478 struct file *f1 = NULL;
479 struct file *f2 = NULL;
480
Jeff Layton6d338b52014-07-10 14:07:29 -0400481 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400482 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400483 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400484 spin_unlock(&fp->fi_lock);
485 if (f1)
486 fput(f1);
487 if (f2)
488 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400489 }
490}
491
Jeff Layton12659652014-07-10 14:07:28 -0400492static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400493{
Jeff Layton12659652014-07-10 14:07:28 -0400494 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
495
496 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400497 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400498 if (access & NFS4_SHARE_ACCESS_READ)
499 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400500}
501
Trond Myklebust60116952014-07-29 21:34:06 -0400502static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
503 struct kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400504{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500505 struct nfs4_stid *stid;
506 int new_id;
507
Trond Myklebustf8338832014-07-25 07:34:19 -0400508 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500509 if (!stid)
510 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400511
Jeff Layton4770d722014-07-29 21:34:10 -0400512 idr_preload(GFP_KERNEL);
513 spin_lock(&cl->cl_lock);
514 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
515 spin_unlock(&cl->cl_lock);
516 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700517 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500518 goto out_free;
519 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500520 stid->sc_stateid.si_opaque.so_id = new_id;
521 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
522 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400523 atomic_set(&stid->sc_count, 1);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500524
J. Bruce Fields996e0932011-10-17 11:14:48 -0400525 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500526 * It shouldn't be a problem to reuse an opaque stateid value.
527 * I don't think it is for 4.1. But with 4.0 I worry that, for
528 * example, a stray write retransmission could be accepted by
529 * the server when it should have been rejected. Therefore,
530 * adopt a trick from the sctp code to attempt to maximize the
531 * amount of time until an id is reused, by ensuring they always
532 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400533 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500534 return stid;
535out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800536 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500537 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400538}
539
Jeff Laytonb49e0842014-07-29 21:34:11 -0400540static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400541{
Trond Myklebust60116952014-07-29 21:34:06 -0400542 struct nfs4_stid *stid;
543 struct nfs4_ol_stateid *stp;
544
545 stid = nfs4_alloc_stid(clp, stateid_slab);
546 if (!stid)
547 return NULL;
548
549 stp = openlockstateid(stid);
550 stp->st_stid.sc_free = nfs4_free_ol_stateid;
551 return stp;
552}
553
554static void nfs4_free_deleg(struct nfs4_stid *stid)
555{
Trond Myklebust60116952014-07-29 21:34:06 -0400556 kmem_cache_free(deleg_slab, stid);
557 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400558}
559
NeilBrown6282cd52014-06-04 17:39:26 +1000560/*
561 * When we recall a delegation, we should be careful not to hand it
562 * out again straight away.
563 * To ensure this we keep a pair of bloom filters ('new' and 'old')
564 * in which the filehandles of recalled delegations are "stored".
565 * If a filehandle appear in either filter, a delegation is blocked.
566 * When a delegation is recalled, the filehandle is stored in the "new"
567 * filter.
568 * Every 30 seconds we swap the filters and clear the "new" one,
569 * unless both are empty of course.
570 *
571 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
572 * low 3 bytes as hash-table indices.
573 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400574 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000575 * is used to manage concurrent access. Testing does not need the lock
576 * except when swapping the two filters.
577 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400578static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000579static struct bloom_pair {
580 int entries, old_entries;
581 time_t swap_time;
582 int new; /* index into 'set' */
583 DECLARE_BITMAP(set[2], 256);
584} blocked_delegations;
585
586static int delegation_blocked(struct knfsd_fh *fh)
587{
588 u32 hash;
589 struct bloom_pair *bd = &blocked_delegations;
590
591 if (bd->entries == 0)
592 return 0;
593 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400594 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000595 if (seconds_since_boot() - bd->swap_time > 30) {
596 bd->entries -= bd->old_entries;
597 bd->old_entries = bd->entries;
598 memset(bd->set[bd->new], 0,
599 sizeof(bd->set[0]));
600 bd->new = 1-bd->new;
601 bd->swap_time = seconds_since_boot();
602 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400603 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000604 }
605 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
606 if (test_bit(hash&255, bd->set[0]) &&
607 test_bit((hash>>8)&255, bd->set[0]) &&
608 test_bit((hash>>16)&255, bd->set[0]))
609 return 1;
610
611 if (test_bit(hash&255, bd->set[1]) &&
612 test_bit((hash>>8)&255, bd->set[1]) &&
613 test_bit((hash>>16)&255, bd->set[1]))
614 return 1;
615
616 return 0;
617}
618
619static void block_delegations(struct knfsd_fh *fh)
620{
621 u32 hash;
622 struct bloom_pair *bd = &blocked_delegations;
623
624 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
625
Jeff Laytonf54fe962014-07-25 07:34:26 -0400626 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000627 __set_bit(hash&255, bd->set[bd->new]);
628 __set_bit((hash>>8)&255, bd->set[bd->new]);
629 __set_bit((hash>>16)&255, bd->set[bd->new]);
630 if (bd->entries == 0)
631 bd->swap_time = seconds_since_boot();
632 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400633 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000634}
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636static struct nfs4_delegation *
Jeff Laytonf9416e22014-07-25 07:34:23 -0400637alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400640 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400643 n = atomic_long_inc_return(&num_delegations);
644 if (n < 0 || n > max_delegations)
645 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000646 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400647 goto out_dec;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400648 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700649 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400650 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400651
652 dp->dl_stid.sc_free = nfs4_free_deleg;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400653 /*
654 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400655 * meaning of seqid 0 isn't meaningful, really, but let's avoid
656 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400657 */
658 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700659 INIT_LIST_HEAD(&dp->dl_perfile);
660 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 INIT_LIST_HEAD(&dp->dl_recall_lru);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400662 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Jeff Layton02e12152014-07-16 10:31:57 -0400663 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400665out_dec:
666 atomic_long_dec(&num_delegations);
667 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
670void
Trond Myklebust60116952014-07-29 21:34:06 -0400671nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Trond Myklebust11b91642014-07-29 21:34:08 -0400673 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400674 struct nfs4_client *clp = s->sc_client;
675
Jeff Layton4770d722014-07-29 21:34:10 -0400676 might_lock(&clp->cl_lock);
677
Jeff Laytonb401be222014-07-29 21:34:33 -0400678 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
679 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400680 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400681 }
Trond Myklebust60116952014-07-29 21:34:06 -0400682 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400683 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400684 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400685 if (fp)
686 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500689static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Jeff Layton417c6622014-07-21 09:34:57 -0400691 lockdep_assert_held(&state_lock);
692
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -0500693 if (!fp->fi_lease)
694 return;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500695 if (atomic_dec_and_test(&fp->fi_delegees)) {
696 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
697 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400698 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500699 fp->fi_deleg_file = NULL;
700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400703static void unhash_stid(struct nfs4_stid *s)
704{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500705 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400706}
707
Benny Halevy931ee562014-05-30 09:09:27 -0400708static void
709hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
710{
Benny Halevycdc97502014-05-30 09:09:30 -0400711 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400712 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400713
Trond Myklebust67cb1272014-07-29 21:34:17 -0400714 atomic_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400715 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400716 list_add(&dp->dl_perfile, &fp->fi_delegations);
717 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
718}
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720static void
Jeff Layton42690672014-07-25 07:34:20 -0400721unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Trond Myklebust11b91642014-07-29 21:34:08 -0400723 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400724
Jeff Layton42690672014-07-25 07:34:20 -0400725 lockdep_assert_held(&state_lock);
726
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400727 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400728 /* Ensure that deleg break won't try to requeue it */
729 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400730 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400731 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400733 list_del_init(&dp->dl_perfile);
734 spin_unlock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400735 if (fp)
Trond Myklebustf8338832014-07-25 07:34:19 -0400736 nfs4_put_deleg_lease(fp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400737}
738
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400739static void destroy_delegation(struct nfs4_delegation *dp)
740{
Jeff Layton42690672014-07-25 07:34:20 -0400741 spin_lock(&state_lock);
742 unhash_delegation_locked(dp);
743 spin_unlock(&state_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400744 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400745}
746
747static void revoke_delegation(struct nfs4_delegation *dp)
748{
749 struct nfs4_client *clp = dp->dl_stid.sc_client;
750
Jeff Layton2d4a5322014-07-25 07:34:21 -0400751 WARN_ON(!list_empty(&dp->dl_recall_lru));
752
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400753 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400754 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400755 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400756 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400757 spin_lock(&clp->cl_lock);
758 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
759 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400760 }
761}
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763/*
764 * SETCLIENTID state
765 */
766
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400767static unsigned int clientid_hashval(u32 id)
768{
769 return id & CLIENT_HASH_MASK;
770}
771
772static unsigned int clientstr_hashval(const char *name)
773{
774 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
775}
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400778 * We store the NONE, READ, WRITE, and BOTH bits separately in the
779 * st_{access,deny}_bmap field of the stateid, in order to track not
780 * only what share bits are currently in force, but also what
781 * combinations of share bits previous opens have used. This allows us
782 * to enforce the recommendation of rfc 3530 14.2.19 that the server
783 * return an error if the client attempt to downgrade to a combination
784 * of share bits not explicable by closing some of its previous opens.
785 *
786 * XXX: This enforcement is actually incomplete, since we don't keep
787 * track of access/deny bit combinations; so, e.g., we allow:
788 *
789 * OPEN allow read, deny write
790 * OPEN allow both, deny none
791 * DOWNGRADE allow read, deny none
792 *
793 * which we should reject.
794 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400795static unsigned int
796bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400797 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400798 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400799
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400800 for (i = 1; i < 4; i++) {
801 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400802 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400803 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400804 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400805}
806
Jeff Layton82c5ff12012-05-11 09:45:13 -0400807/* set share access for a given stateid */
808static inline void
809set_access(u32 access, struct nfs4_ol_stateid *stp)
810{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400811 unsigned char mask = 1 << access;
812
813 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
814 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400815}
816
817/* clear share access for a given stateid */
818static inline void
819clear_access(u32 access, struct nfs4_ol_stateid *stp)
820{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400821 unsigned char mask = 1 << access;
822
823 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
824 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400825}
826
827/* test whether a given stateid has access */
828static inline bool
829test_access(u32 access, struct nfs4_ol_stateid *stp)
830{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400831 unsigned char mask = 1 << access;
832
833 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400834}
835
Jeff Laytonce0fc432012-05-11 09:45:14 -0400836/* set share deny for a given stateid */
837static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400838set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400839{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400840 unsigned char mask = 1 << deny;
841
842 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
843 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400844}
845
846/* clear share deny for a given stateid */
847static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400848clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400849{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400850 unsigned char mask = 1 << deny;
851
852 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
853 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400854}
855
856/* test whether a given stateid is denying specific access */
857static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400858test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400859{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400860 unsigned char mask = 1 << deny;
861
862 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400863}
864
865static int nfs4_access_to_omode(u32 access)
866{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400867 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400868 case NFS4_SHARE_ACCESS_READ:
869 return O_RDONLY;
870 case NFS4_SHARE_ACCESS_WRITE:
871 return O_WRONLY;
872 case NFS4_SHARE_ACCESS_BOTH:
873 return O_RDWR;
874 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500875 WARN_ON_ONCE(1);
876 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400877}
878
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400879/*
880 * A stateid that had a deny mode associated with it is being released
881 * or downgraded. Recalculate the deny mode on the file.
882 */
883static void
884recalculate_deny_mode(struct nfs4_file *fp)
885{
886 struct nfs4_ol_stateid *stp;
887
888 spin_lock(&fp->fi_lock);
889 fp->fi_share_deny = 0;
890 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
891 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
892 spin_unlock(&fp->fi_lock);
893}
894
895static void
896reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
897{
898 int i;
899 bool change = false;
900
901 for (i = 1; i < 4; i++) {
902 if ((i & deny) != i) {
903 change = true;
904 clear_deny(i, stp);
905 }
906 }
907
908 /* Recalculate per-file deny mode if there was a change */
909 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -0400910 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400911}
912
Jeff Layton82c5ff12012-05-11 09:45:13 -0400913/* release all access and file references for a given stateid */
914static void
915release_all_access(struct nfs4_ol_stateid *stp)
916{
917 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -0400918 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400919
920 if (fp && stp->st_deny_bmap != 0)
921 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400922
923 for (i = 1; i < 4; i++) {
924 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -0400925 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400926 clear_access(i, stp);
927 }
928}
929
Jeff Layton6b180f02014-07-29 21:34:26 -0400930static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
931{
Jeff Laytona819ecc2014-07-29 21:34:38 -0400932 struct nfs4_client *clp = sop->so_client;
933
934 might_lock(&clp->cl_lock);
935
936 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -0400937 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -0400938 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -0400939 spin_unlock(&clp->cl_lock);
Jeff Layton6b180f02014-07-29 21:34:26 -0400940 kfree(sop->so_owner.data);
941 sop->so_ops->so_free(sop);
942}
943
Jeff Layton4ae098d2014-07-29 21:34:43 -0400944static void unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500945{
Trond Myklebust11b91642014-07-29 21:34:08 -0400946 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -0400947
Jeff Layton1c755dc2014-07-29 21:34:12 -0400948 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
949
Trond Myklebust1d31a252014-07-10 14:07:25 -0400950 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500951 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400952 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500953 list_del(&stp->st_perstateowner);
954}
955
Trond Myklebust60116952014-07-29 21:34:06 -0400956static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500957{
Trond Myklebust60116952014-07-29 21:34:06 -0400958 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400959
Trond Myklebust60116952014-07-29 21:34:06 -0400960 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -0400961 if (stp->st_stateowner)
962 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -0400963 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500964}
965
Jeff Laytonb49e0842014-07-29 21:34:11 -0400966static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500967{
Jeff Laytonb49e0842014-07-29 21:34:11 -0400968 struct nfs4_ol_stateid *stp = openlockstateid(stid);
969 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500970 struct file *file;
971
Jeff Laytonb49e0842014-07-29 21:34:11 -0400972 file = find_any_file(stp->st_stid.sc_file);
973 if (file)
974 filp_close(file, (fl_owner_t)lo);
975 nfs4_free_ol_stateid(stid);
976}
977
Jeff Layton2c41beb2014-07-29 21:34:41 -0400978/*
979 * Put the persistent reference to an already unhashed generic stateid, while
980 * holding the cl_lock. If it's the last reference, then put it onto the
981 * reaplist for later destruction.
982 */
983static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
984 struct list_head *reaplist)
985{
986 struct nfs4_stid *s = &stp->st_stid;
987 struct nfs4_client *clp = s->sc_client;
988
989 lockdep_assert_held(&clp->cl_lock);
990
991 WARN_ON_ONCE(!list_empty(&stp->st_locks));
992
993 if (!atomic_dec_and_test(&s->sc_count)) {
994 wake_up_all(&close_wq);
995 return;
996 }
997
998 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
999 list_add(&stp->st_locks, reaplist);
1000}
1001
Jeff Layton3c1c9952014-07-29 21:34:39 -04001002static void unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1003{
1004 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1005
1006 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
1007
1008 list_del_init(&stp->st_locks);
Jeff Layton4ae098d2014-07-29 21:34:43 -04001009 unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001010 unhash_stid(&stp->st_stid);
1011}
1012
Jeff Layton5adfd882014-07-29 21:34:31 -04001013static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001014{
Jeff Layton1c755dc2014-07-29 21:34:12 -04001015 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1016
1017 spin_lock(&oo->oo_owner.so_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001018 unhash_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001019 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04001020 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001021}
1022
Trond Myklebustc58c6612014-07-29 21:34:35 -04001023static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001024{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001025 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001026
Trond Myklebustd4f04892014-07-29 21:34:36 -04001027 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001028
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001029 list_del_init(&lo->lo_owner.so_strhash);
1030}
1031
Jeff Layton2c41beb2014-07-29 21:34:41 -04001032/*
1033 * Free a list of generic stateids that were collected earlier after being
1034 * fully unhashed.
1035 */
1036static void
1037free_ol_stateid_reaplist(struct list_head *reaplist)
1038{
1039 struct nfs4_ol_stateid *stp;
1040
1041 might_sleep();
1042
1043 while (!list_empty(reaplist)) {
1044 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1045 st_locks);
1046 list_del(&stp->st_locks);
1047 stp->st_stid.sc_free(&stp->st_stid);
1048 }
1049}
1050
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001051static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001052{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001053 struct nfs4_client *clp = lo->lo_owner.so_client;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001054 struct nfs4_ol_stateid *stp;
1055 struct list_head reaplist;
1056
1057 INIT_LIST_HEAD(&reaplist);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001058
Trond Myklebustd4f04892014-07-29 21:34:36 -04001059 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001060 unhash_lockowner_locked(lo);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001061 while (!list_empty(&lo->lo_owner.so_stateids)) {
1062 stp = list_first_entry(&lo->lo_owner.so_stateids,
1063 struct nfs4_ol_stateid, st_perstateowner);
1064 unhash_lock_stateid(stp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001065 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001066 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001067 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001068 free_ol_stateid_reaplist(&reaplist);
Jeff Layton6b180f02014-07-29 21:34:26 -04001069 nfs4_put_stateowner(&lo->lo_owner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001070}
1071
Jeff Laytond83017f2014-07-29 21:34:42 -04001072static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1073 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001074{
1075 struct nfs4_ol_stateid *stp;
1076
1077 while (!list_empty(&open_stp->st_locks)) {
1078 stp = list_entry(open_stp->st_locks.next,
1079 struct nfs4_ol_stateid, st_locks);
Jeff Laytond83017f2014-07-29 21:34:42 -04001080 unhash_lock_stateid(stp);
1081 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001082 }
1083}
1084
Jeff Laytond83017f2014-07-29 21:34:42 -04001085static void unhash_open_stateid(struct nfs4_ol_stateid *stp,
1086 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001087{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001088 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1089
Jeff Layton4ae098d2014-07-29 21:34:43 -04001090 unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001091 release_open_stateid_locks(stp, reaplist);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001092}
1093
1094static void release_open_stateid(struct nfs4_ol_stateid *stp)
1095{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001096 LIST_HEAD(reaplist);
1097
1098 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04001099 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001100 put_ol_stateid_locked(stp, &reaplist);
1101 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1102 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001103}
1104
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001105static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001106{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001107 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001108
Trond Myklebustd4f04892014-07-29 21:34:36 -04001109 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001110
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001111 list_del_init(&oo->oo_owner.so_strhash);
1112 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001113}
1114
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001115static void release_last_closed_stateid(struct nfs4_openowner *oo)
1116{
Jeff Layton217526e2014-07-30 08:27:11 -04001117 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1118 nfsd_net_id);
1119 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001120
Jeff Layton217526e2014-07-30 08:27:11 -04001121 spin_lock(&nn->client_lock);
1122 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001123 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001124 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001125 oo->oo_last_closed_stid = NULL;
1126 }
Jeff Layton217526e2014-07-30 08:27:11 -04001127 spin_unlock(&nn->client_lock);
1128 if (s)
1129 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001130}
1131
Jeff Layton2c41beb2014-07-29 21:34:41 -04001132static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001133{
1134 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001135 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001136 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001137
Jeff Layton2c41beb2014-07-29 21:34:41 -04001138 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001139
Trond Myklebustd4f04892014-07-29 21:34:36 -04001140 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001141 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001142 while (!list_empty(&oo->oo_owner.so_stateids)) {
1143 stp = list_first_entry(&oo->oo_owner.so_stateids,
1144 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytond83017f2014-07-29 21:34:42 -04001145 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001146 put_ol_stateid_locked(stp, &reaplist);
1147 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001148 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001149 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001150 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001151 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001152}
1153
Marc Eshel5282fd72009-04-03 08:27:52 +03001154static inline int
1155hash_sessionid(struct nfs4_sessionid *sessionid)
1156{
1157 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1158
1159 return sid->sequence % SESSION_HASH_SIZE;
1160}
1161
Trond Myklebust8f199b82012-03-20 15:11:17 -04001162#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001163static inline void
1164dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1165{
1166 u32 *ptr = (u32 *)(&sessionid->data[0]);
1167 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1168}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001169#else
1170static inline void
1171dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1172{
1173}
1174#endif
1175
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001176/*
1177 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1178 * won't be used for replay.
1179 */
1180void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1181{
1182 struct nfs4_stateowner *so = cstate->replay_owner;
1183
1184 if (nfserr == nfserr_replay_me)
1185 return;
1186
1187 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001188 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001189 return;
1190 }
1191 if (!so)
1192 return;
1193 if (so->so_is_open_owner)
1194 release_last_closed_stateid(openowner(so));
1195 so->so_seqid++;
1196 return;
1197}
Marc Eshel5282fd72009-04-03 08:27:52 +03001198
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001199static void
1200gen_sessionid(struct nfsd4_session *ses)
1201{
1202 struct nfs4_client *clp = ses->se_client;
1203 struct nfsd4_sessionid *sid;
1204
1205 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1206 sid->clientid = clp->cl_clientid;
1207 sid->sequence = current_sessionid++;
1208 sid->reserved = 0;
1209}
1210
1211/*
Andy Adamsona6496372009-08-28 08:45:01 -04001212 * The protocol defines ca_maxresponssize_cached to include the size of
1213 * the rpc header, but all we need to cache is the data starting after
1214 * the end of the initial SEQUENCE operation--the rest we regenerate
1215 * each time. Therefore we can advertise a ca_maxresponssize_cached
1216 * value that is the number of bytes in our cache plus a few additional
1217 * bytes. In order to stay on the safe side, and not promise more than
1218 * we can cache, those additional bytes must be the minimum possible: 24
1219 * bytes of rpc header (xid through accept state, with AUTH_NULL
1220 * verifier), 12 for the compound header (with zero-length tag), and 44
1221 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001222 */
Andy Adamsona6496372009-08-28 08:45:01 -04001223#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1224
Andy Adamson557ce262009-08-28 08:45:04 -04001225static void
1226free_session_slots(struct nfsd4_session *ses)
1227{
1228 int i;
1229
1230 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1231 kfree(ses->se_slots[i]);
1232}
1233
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001234/*
1235 * We don't actually need to cache the rpc and session headers, so we
1236 * can allocate a little less for each slot:
1237 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001238static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001239{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001240 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001241
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001242 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1243 size = 0;
1244 else
1245 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1246 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001247}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001248
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001249/*
1250 * XXX: If we run out of reserved DRC memory we could (up to a point)
1251 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001252 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001253 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001254static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001255{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001256 u32 slotsize = slot_bytes(ca);
1257 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001258 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001259
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001260 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001261 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1262 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001263 num = min_t(int, num, avail / slotsize);
1264 nfsd_drc_mem_used += num * slotsize;
1265 spin_unlock(&nfsd_drc_lock);
1266
1267 return num;
1268}
1269
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001270static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001271{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001272 int slotsize = slot_bytes(ca);
1273
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001274 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001275 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001276 spin_unlock(&nfsd_drc_lock);
1277}
1278
Kinglong Mee60810e52014-01-01 00:35:47 +08001279static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1280 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001281{
Kinglong Mee60810e52014-01-01 00:35:47 +08001282 int numslots = fattrs->maxreqs;
1283 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001284 struct nfsd4_session *new;
1285 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001286
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001287 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001288 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1289 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001290
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001291 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001292 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001293 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001294 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001295 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001296 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001297 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001298 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001299 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001300
1301 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1302 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1303
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001304 return new;
1305out_free:
1306 while (i--)
1307 kfree(new->se_slots[i]);
1308 kfree(new);
1309 return NULL;
1310}
1311
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001312static void free_conn(struct nfsd4_conn *c)
1313{
1314 svc_xprt_put(c->cn_xprt);
1315 kfree(c);
1316}
1317
1318static void nfsd4_conn_lost(struct svc_xpt_user *u)
1319{
1320 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1321 struct nfs4_client *clp = c->cn_session->se_client;
1322
1323 spin_lock(&clp->cl_lock);
1324 if (!list_empty(&c->cn_persession)) {
1325 list_del(&c->cn_persession);
1326 free_conn(c);
1327 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001328 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001329 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001330}
1331
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001332static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001333{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001334 struct nfsd4_conn *conn;
1335
1336 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1337 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001338 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001339 svc_xprt_get(rqstp->rq_xprt);
1340 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001341 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001342 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1343 return conn;
1344}
1345
J. Bruce Fields328ead22010-09-29 16:11:06 -04001346static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1347{
1348 conn->cn_session = ses;
1349 list_add(&conn->cn_persession, &ses->se_conns);
1350}
1351
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001352static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1353{
1354 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001355
1356 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001357 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001358 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001359}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001360
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001361static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001362{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001363 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001364 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001365}
1366
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001367static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001368{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001369 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001370
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001371 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001372 ret = nfsd4_register_conn(conn);
1373 if (ret)
1374 /* oops; xprt is already down: */
1375 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001376 /* We may have gained or lost a callback channel: */
1377 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001378}
1379
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001380static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001381{
1382 u32 dir = NFS4_CDFC4_FORE;
1383
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001384 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001385 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001386 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001387}
1388
1389/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001390static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001391{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001392 struct nfs4_client *clp = s->se_client;
1393 struct nfsd4_conn *c;
1394
1395 spin_lock(&clp->cl_lock);
1396 while (!list_empty(&s->se_conns)) {
1397 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1398 list_del_init(&c->cn_persession);
1399 spin_unlock(&clp->cl_lock);
1400
1401 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1402 free_conn(c);
1403
1404 spin_lock(&clp->cl_lock);
1405 }
1406 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001407}
1408
J. Bruce Fields1377b692012-09-11 21:42:40 -04001409static void __free_session(struct nfsd4_session *ses)
1410{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001411 free_session_slots(ses);
1412 kfree(ses);
1413}
1414
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001415static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001416{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001417 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001418 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001419 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001420}
Andy Adamson557ce262009-08-28 08:45:04 -04001421
Fengguang Wu135ae822012-11-10 07:20:25 -05001422static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001423{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001424 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001425 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001426
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001427 new->se_client = clp;
1428 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001429
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001430 INIT_LIST_HEAD(&new->se_conns);
1431
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001432 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001433 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001434 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001435 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001436 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001437 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001438 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001439 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001440 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001441 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001442
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001443 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001444 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001445 /*
1446 * This is a little silly; with sessions there's no real
1447 * use for the callback address. Use the peer address
1448 * as a reasonable default for now, but consider fixing
1449 * the rpc client not to require an address in the
1450 * future:
1451 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001452 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1453 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001454 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001455}
1456
Benny Halevy9089f1b2010-05-12 00:12:26 +03001457/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001458static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001459__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001460{
1461 struct nfsd4_session *elem;
1462 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001463 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001464
Trond Myklebust0a880a22014-07-30 08:27:10 -04001465 lockdep_assert_held(&nn->client_lock);
1466
Marc Eshel5282fd72009-04-03 08:27:52 +03001467 dump_sessionid(__func__, sessionid);
1468 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001469 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001470 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001471 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1472 NFS4_MAX_SESSIONID_LEN)) {
1473 return elem;
1474 }
1475 }
1476
1477 dprintk("%s: session not found\n", __func__);
1478 return NULL;
1479}
1480
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001481static struct nfsd4_session *
1482find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1483 __be32 *ret)
1484{
1485 struct nfsd4_session *session;
1486 __be32 status = nfserr_badsession;
1487
1488 session = __find_in_sessionid_hashtbl(sessionid, net);
1489 if (!session)
1490 goto out;
1491 status = nfsd4_get_session_locked(session);
1492 if (status)
1493 session = NULL;
1494out:
1495 *ret = status;
1496 return session;
1497}
1498
Benny Halevy9089f1b2010-05-12 00:12:26 +03001499/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001500static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001501unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001502{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001503 struct nfs4_client *clp = ses->se_client;
1504 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1505
1506 lockdep_assert_held(&nn->client_lock);
1507
Andy Adamson7116ed62009-04-03 08:27:43 +03001508 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001509 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001510 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001511 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001512}
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1515static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001516STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001518 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001520 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001521 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return 1;
1523}
1524
1525/*
1526 * XXX Should we use a slab cache ?
1527 * This type of memory management is somewhat inefficient, but we use it
1528 * anyway since SETCLIENTID is not a common operation.
1529 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001530static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
1532 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001533 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001535 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1536 if (clp == NULL)
1537 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001538 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001539 if (clp->cl_name.data == NULL)
1540 goto err_no_name;
1541 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1542 OWNER_HASH_SIZE, GFP_KERNEL);
1543 if (!clp->cl_ownerstr_hashtbl)
1544 goto err_no_hashtbl;
1545 for (i = 0; i < OWNER_HASH_SIZE; i++)
1546 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001547 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001548 INIT_LIST_HEAD(&clp->cl_sessions);
1549 idr_init(&clp->cl_stateids);
1550 atomic_set(&clp->cl_refcount, 0);
1551 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1552 INIT_LIST_HEAD(&clp->cl_idhash);
1553 INIT_LIST_HEAD(&clp->cl_openowners);
1554 INIT_LIST_HEAD(&clp->cl_delegations);
1555 INIT_LIST_HEAD(&clp->cl_lru);
1556 INIT_LIST_HEAD(&clp->cl_callbacks);
1557 INIT_LIST_HEAD(&clp->cl_revoked);
1558 spin_lock_init(&clp->cl_lock);
1559 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001561err_no_hashtbl:
1562 kfree(clp->cl_name.data);
1563err_no_name:
1564 kfree(clp);
1565 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001568static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569free_client(struct nfs4_client *clp)
1570{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001571 while (!list_empty(&clp->cl_sessions)) {
1572 struct nfsd4_session *ses;
1573 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1574 se_perclnt);
1575 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001576 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1577 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001578 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001579 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001580 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001581 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001583 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 kfree(clp);
1585}
1586
Benny Halevy84d38ac2010-05-12 00:13:16 +03001587/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001588static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001589unhash_client_locked(struct nfs4_client *clp)
1590{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001591 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001592 struct nfsd4_session *ses;
1593
Trond Myklebust0a880a22014-07-30 08:27:10 -04001594 lockdep_assert_held(&nn->client_lock);
1595
Trond Myklebust4beb3452014-07-30 08:27:02 -04001596 /* Mark the client as expired! */
1597 clp->cl_time = 0;
1598 /* Make it invisible */
1599 if (!list_empty(&clp->cl_idhash)) {
1600 list_del_init(&clp->cl_idhash);
1601 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1602 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1603 else
1604 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1605 }
1606 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001607 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001608 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1609 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001610 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001611}
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001614unhash_client(struct nfs4_client *clp)
1615{
1616 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1617
1618 spin_lock(&nn->client_lock);
1619 unhash_client_locked(clp);
1620 spin_unlock(&nn->client_lock);
1621}
1622
Jeff Layton97403d92014-07-30 08:27:12 -04001623static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1624{
1625 if (atomic_read(&clp->cl_refcount))
1626 return nfserr_jukebox;
1627 unhash_client_locked(clp);
1628 return nfs_ok;
1629}
1630
Trond Myklebust4beb3452014-07-30 08:27:02 -04001631static void
1632__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001634 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 struct list_head reaplist;
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001639 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001640 while (!list_empty(&clp->cl_delegations)) {
1641 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001642 unhash_delegation_locked(dp);
1643 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 }
Benny Halevycdc97502014-05-30 09:09:30 -04001645 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 while (!list_empty(&reaplist)) {
1647 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001648 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001649 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001651 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001652 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001653 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001654 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001655 }
NeilBrownea1da632005-06-23 22:04:17 -07001656 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001657 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Jeff Laytond3134b12014-07-29 21:34:32 -04001658 atomic_inc(&oo->oo_owner.so_count);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001659 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001661 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001662 if (clp->cl_cb_conn.cb_xprt)
1663 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001664 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
1666
Trond Myklebust4beb3452014-07-30 08:27:02 -04001667static void
1668destroy_client(struct nfs4_client *clp)
1669{
1670 unhash_client(clp);
1671 __destroy_client(clp);
1672}
1673
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001674static void expire_client(struct nfs4_client *clp)
1675{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001676 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001677 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001678 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001679}
1680
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001681static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1682{
1683 memcpy(target->cl_verifier.data, source->data,
1684 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001687static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1688{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1690 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1691}
1692
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001693static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001694{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001695 if (source->cr_principal) {
1696 target->cr_principal =
1697 kstrdup(source->cr_principal, GFP_KERNEL);
1698 if (target->cr_principal == NULL)
1699 return -ENOMEM;
1700 } else
1701 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001702 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 target->cr_uid = source->cr_uid;
1704 target->cr_gid = source->cr_gid;
1705 target->cr_group_info = source->cr_group_info;
1706 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001707 target->cr_gss_mech = source->cr_gss_mech;
1708 if (source->cr_gss_mech)
1709 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001710 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711}
1712
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001713static long long
1714compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1715{
1716 long long res;
1717
1718 res = o1->len - o2->len;
1719 if (res)
1720 return res;
1721 return (long long)memcmp(o1->data, o2->data, o1->len);
1722}
1723
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001724static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001725{
NeilBrowna55370a2005-06-23 22:03:52 -07001726 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728
1729static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001730same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1731{
1732 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
1734
1735static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001736same_clid(clientid_t *cl1, clientid_t *cl2)
1737{
1738 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739}
1740
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001741static bool groups_equal(struct group_info *g1, struct group_info *g2)
1742{
1743 int i;
1744
1745 if (g1->ngroups != g2->ngroups)
1746 return false;
1747 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001748 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001749 return false;
1750 return true;
1751}
1752
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001753/*
1754 * RFC 3530 language requires clid_inuse be returned when the
1755 * "principal" associated with a requests differs from that previously
1756 * used. We use uid, gid's, and gss principal string as our best
1757 * approximation. We also don't want to allow non-gss use of a client
1758 * established using gss: in theory cr_principal should catch that
1759 * change, but in practice cr_principal can be null even in the gss case
1760 * since gssd doesn't always pass down a principal string.
1761 */
1762static bool is_gss_cred(struct svc_cred *cr)
1763{
1764 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1765 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1766}
1767
1768
Vivek Trivedi5559b502012-07-24 21:18:20 +05301769static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001770same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1771{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001772 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001773 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1774 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001775 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1776 return false;
1777 if (cr1->cr_principal == cr2->cr_principal)
1778 return true;
1779 if (!cr1->cr_principal || !cr2->cr_principal)
1780 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301781 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782}
1783
J. Bruce Fields57266a62013-04-13 14:27:29 -04001784static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1785{
1786 struct svc_cred *cr = &rqstp->rq_cred;
1787 u32 service;
1788
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001789 if (!cr->cr_gss_mech)
1790 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001791 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1792 return service == RPC_GSS_SVC_INTEGRITY ||
1793 service == RPC_GSS_SVC_PRIVACY;
1794}
1795
1796static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1797{
1798 struct svc_cred *cr = &rqstp->rq_cred;
1799
1800 if (!cl->cl_mach_cred)
1801 return true;
1802 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1803 return false;
1804 if (!svc_rqst_integrity_protected(rqstp))
1805 return false;
1806 if (!cr->cr_principal)
1807 return false;
1808 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1809}
1810
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001811static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001812{
1813 static u32 current_clientid = 1;
1814
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001815 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 clp->cl_clientid.cl_id = current_clientid++;
1817}
1818
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001819static void gen_confirm(struct nfs4_client *clp)
1820{
Chuck Leverab4684d2012-03-02 17:13:50 -05001821 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001822 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Jeff Laytonf4199922014-06-17 07:44:11 -04001824 /*
1825 * This is opaque to client, so no need to byte-swap. Use
1826 * __force to keep sparse happy
1827 */
1828 verf[0] = (__force __be32)get_seconds();
1829 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001830 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831}
1832
Jeff Layton4770d722014-07-29 21:34:10 -04001833static struct nfs4_stid *
1834find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001835{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001836 struct nfs4_stid *ret;
1837
1838 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1839 if (!ret || !ret->sc_type)
1840 return NULL;
1841 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001842}
1843
Jeff Layton4770d722014-07-29 21:34:10 -04001844static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001845find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001846{
1847 struct nfs4_stid *s;
1848
Jeff Layton4770d722014-07-29 21:34:10 -04001849 spin_lock(&cl->cl_lock);
1850 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001851 if (s != NULL) {
1852 if (typemask & s->sc_type)
1853 atomic_inc(&s->sc_count);
1854 else
1855 s = NULL;
1856 }
Jeff Layton4770d722014-07-29 21:34:10 -04001857 spin_unlock(&cl->cl_lock);
1858 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001859}
1860
Jeff Layton2216d442012-11-12 15:00:57 -05001861static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001862 struct svc_rqst *rqstp, nfs4_verifier *verf)
1863{
1864 struct nfs4_client *clp;
1865 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001866 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001867 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001868
1869 clp = alloc_client(name);
1870 if (clp == NULL)
1871 return NULL;
1872
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001873 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1874 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001875 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001876 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001877 }
Jeff Layton02e12152014-07-16 10:31:57 -04001878 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001879 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001880 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001881 copy_verf(clp, verf);
1882 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001883 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001884 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001885 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001886 return clp;
1887}
1888
NeilBrownfd39ca92005-06-23 22:04:03 -07001889static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001890add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1891{
1892 struct rb_node **new = &(root->rb_node), *parent = NULL;
1893 struct nfs4_client *clp;
1894
1895 while (*new) {
1896 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1897 parent = *new;
1898
1899 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1900 new = &((*new)->rb_left);
1901 else
1902 new = &((*new)->rb_right);
1903 }
1904
1905 rb_link_node(&new_clp->cl_namenode, parent, new);
1906 rb_insert_color(&new_clp->cl_namenode, root);
1907}
1908
1909static struct nfs4_client *
1910find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1911{
1912 long long cmp;
1913 struct rb_node *node = root->rb_node;
1914 struct nfs4_client *clp;
1915
1916 while (node) {
1917 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1918 cmp = compare_blob(&clp->cl_name, name);
1919 if (cmp > 0)
1920 node = node->rb_left;
1921 else if (cmp < 0)
1922 node = node->rb_right;
1923 else
1924 return clp;
1925 }
1926 return NULL;
1927}
1928
1929static void
1930add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
1932 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001933 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Trond Myklebust0a880a22014-07-30 08:27:10 -04001935 lockdep_assert_held(&nn->client_lock);
1936
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001937 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001938 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001940 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001941 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942}
1943
NeilBrownfd39ca92005-06-23 22:04:03 -07001944static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945move_to_confirmed(struct nfs4_client *clp)
1946{
1947 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001948 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Trond Myklebust0a880a22014-07-30 08:27:10 -04001950 lockdep_assert_held(&nn->client_lock);
1951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001953 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001954 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001955 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001956 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001957 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958}
1959
1960static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001961find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
1963 struct nfs4_client *clp;
1964 unsigned int idhashval = clientid_hashval(clid->cl_id);
1965
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001966 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001967 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001968 if ((bool)clp->cl_minorversion != sessions)
1969 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001970 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 }
1974 return NULL;
1975}
1976
1977static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001978find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1979{
1980 struct list_head *tbl = nn->conf_id_hashtbl;
1981
Trond Myklebust0a880a22014-07-30 08:27:10 -04001982 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001983 return find_client_in_id_table(tbl, clid, sessions);
1984}
1985
1986static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001987find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001989 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Trond Myklebust0a880a22014-07-30 08:27:10 -04001991 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001992 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001995static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001996{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001997 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001998}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001999
NeilBrown28ce6052005-06-23 22:03:56 -07002000static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002001find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002002{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002003 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002004 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002005}
2006
2007static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002008find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002009{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002010 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002011 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002012}
2013
NeilBrownfd39ca92005-06-23 22:04:03 -07002014static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002015gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002017 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002018 struct sockaddr *sa = svc_addr(rqstp);
2019 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002020 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Jeff Layton7077ecb2009-08-14 12:57:58 -04002022 /* Currently, we only support tcp and tcp6 for the callback channel */
2023 if (se->se_callback_netid_len == 3 &&
2024 !memcmp(se->se_callback_netid_val, "tcp", 3))
2025 expected_family = AF_INET;
2026 else if (se->se_callback_netid_len == 4 &&
2027 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2028 expected_family = AF_INET6;
2029 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 goto out_err;
2031
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002032 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002033 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002034 (struct sockaddr *)&conn->cb_addr,
2035 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002036
J. Bruce Fields07263f12010-05-31 19:09:40 -04002037 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002039
J. Bruce Fields07263f12010-05-31 19:09:40 -04002040 if (conn->cb_addr.ss_family == AF_INET6)
2041 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002042
J. Bruce Fields07263f12010-05-31 19:09:40 -04002043 conn->cb_prog = se->se_callback_prog;
2044 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002045 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 return;
2047out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002048 conn->cb_addr.ss_family = AF_UNSPEC;
2049 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002050 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 "will not receive delegations\n",
2052 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 return;
2055}
2056
Andy Adamson074fe892009-04-03 08:28:15 +03002057/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002058 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002059 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002060static void
Andy Adamson074fe892009-04-03 08:28:15 +03002061nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2062{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002063 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002064 struct nfsd4_slot *slot = resp->cstate.slot;
2065 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002066
Andy Adamson557ce262009-08-28 08:45:04 -04002067 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002068
Andy Adamson557ce262009-08-28 08:45:04 -04002069 slot->sl_opcnt = resp->opcnt;
2070 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002071
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002072 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002073 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002074 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002075 return;
2076 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002077 base = resp->cstate.data_offset;
2078 slot->sl_datalen = buf->len - base;
2079 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04002080 WARN("%s: sessions DRC could not cache compound\n", __func__);
2081 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002082}
2083
2084/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002085 * Encode the replay sequence operation from the slot values.
2086 * If cachethis is FALSE encode the uncached rep error on the next
2087 * operation which sets resp->p and increments resp->opcnt for
2088 * nfs4svc_encode_compoundres.
2089 *
Andy Adamson074fe892009-04-03 08:28:15 +03002090 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002091static __be32
2092nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2093 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002094{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002095 struct nfsd4_op *op;
2096 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002097
Andy Adamsonabfabf82009-07-23 19:02:18 -04002098 /* Encode the replayed sequence operation */
2099 op = &args->ops[resp->opcnt - 1];
2100 nfsd4_encode_operation(resp, op);
2101
2102 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002103 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002104 op = &args->ops[resp->opcnt++];
2105 op->status = nfserr_retry_uncached_rep;
2106 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002107 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002108 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002109}
2110
2111/*
Andy Adamson557ce262009-08-28 08:45:04 -04002112 * The sequence operation is not cached because we can use the slot and
2113 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002114 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002115static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002116nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2117 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002118{
Andy Adamson557ce262009-08-28 08:45:04 -04002119 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002120 struct xdr_stream *xdr = &resp->xdr;
2121 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002122 __be32 status;
2123
Andy Adamson557ce262009-08-28 08:45:04 -04002124 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002125
Andy Adamsonabfabf82009-07-23 19:02:18 -04002126 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002127 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002128 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002129
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002130 p = xdr_reserve_space(xdr, slot->sl_datalen);
2131 if (!p) {
2132 WARN_ON_ONCE(1);
2133 return nfserr_serverfault;
2134 }
2135 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2136 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002137
Andy Adamson557ce262009-08-28 08:45:04 -04002138 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002139 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002140}
2141
Andy Adamson0733d212009-04-03 08:28:01 +03002142/*
2143 * Set the exchange_id flags returned by the server.
2144 */
2145static void
2146nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2147{
2148 /* pNFS is not supported */
2149 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2150
2151 /* Referrals are supported, Migration is not. */
2152 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2153
2154 /* set the wire flags to return to client. */
2155 clid->flags = new->cl_exchange_flags;
2156}
2157
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002158static bool client_has_state(struct nfs4_client *clp)
2159{
2160 /*
2161 * Note clp->cl_openowners check isn't quite right: there's no
2162 * need to count owners without stateid's.
2163 *
2164 * Also note we should probably be using this in 4.0 case too.
2165 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002166 return !list_empty(&clp->cl_openowners)
2167 || !list_empty(&clp->cl_delegations)
2168 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002169}
2170
Al Virob37ad282006-10-19 23:28:59 -07002171__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002172nfsd4_exchange_id(struct svc_rqst *rqstp,
2173 struct nfsd4_compound_state *cstate,
2174 struct nfsd4_exchange_id *exid)
2175{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002176 struct nfs4_client *conf, *new;
2177 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002178 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002179 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002180 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002181 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002182 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002183 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002184
Jeff Layton363168b2009-08-14 12:57:56 -04002185 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002186 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002187 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002188 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002189 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002190
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002191 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002192 return nfserr_inval;
2193
Andy Adamson0733d212009-04-03 08:28:01 +03002194 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002195 case SP4_MACH_CRED:
2196 if (!svc_rqst_integrity_protected(rqstp))
2197 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002198 case SP4_NONE:
2199 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002200 default: /* checked by xdr code */
2201 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002202 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002203 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002204 }
2205
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002206 new = create_client(exid->clname, rqstp, &verf);
2207 if (new == NULL)
2208 return nfserr_jukebox;
2209
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002210 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03002211 nfs4_lock_state();
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002212 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002213 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002214 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002215 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2216 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2217
J. Bruce Fields136e6582012-05-12 20:37:23 -04002218 if (update) {
2219 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002220 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002221 goto out;
2222 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002223 if (!mach_creds_match(conf, rqstp)) {
2224 status = nfserr_wrong_cred;
2225 goto out;
2226 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002227 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002228 status = nfserr_perm;
2229 goto out;
2230 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002231 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002232 status = nfserr_not_same;
2233 goto out;
2234 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002235 /* case 6 */
2236 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002237 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002238 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002239 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002240 if (client_has_state(conf)) {
2241 status = nfserr_clid_inuse;
2242 goto out;
2243 }
Andy Adamson0733d212009-04-03 08:28:01 +03002244 goto out_new;
2245 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002246 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002247 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002248 goto out_copy;
2249 }
2250 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002251 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002252 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002253 }
2254
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002255 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002256 status = nfserr_noent;
2257 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002258 }
2259
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002260 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002261 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002262 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002263
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002264 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002265out_new:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002266 if (conf)
2267 unhash_client_locked(conf);
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002268 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002269 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002270
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002271 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002272 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002273 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002274out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002275 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2276 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002277
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002278 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2279 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002280
2281 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002282 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002283 status = nfs_ok;
2284
2285out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002286 spin_unlock(&nn->client_lock);
Andy Adamson0733d212009-04-03 08:28:01 +03002287 nfs4_unlock_state();
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002288 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002289 expire_client(new);
2290 if (unconf)
2291 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002292 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002293}
2294
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002295static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002296check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002297{
Andy Adamson88e588d2009-07-23 19:02:15 -04002298 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2299 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002300
2301 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002302 if (slot_inuse) {
2303 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002304 return nfserr_jukebox;
2305 else
2306 return nfserr_seq_misordered;
2307 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002308 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002309 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002310 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002311 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002312 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002313 return nfserr_seq_misordered;
2314}
2315
Andy Adamson49557cc2009-07-23 19:02:16 -04002316/*
2317 * Cache the create session result into the create session single DRC
2318 * slot cache by saving the xdr structure. sl_seqid has been set.
2319 * Do this for solo or embedded create session operations.
2320 */
2321static void
2322nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002323 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002324{
2325 slot->sl_status = nfserr;
2326 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2327}
2328
2329static __be32
2330nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2331 struct nfsd4_clid_slot *slot)
2332{
2333 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2334 return slot->sl_status;
2335}
2336
Mi Jinlong1b74c252011-07-14 14:50:17 +08002337#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2338 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2339 1 + /* MIN tag is length with zero, only length */ \
2340 3 + /* version, opcount, opcode */ \
2341 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2342 /* seqid, slotID, slotID, cache */ \
2343 4 ) * sizeof(__be32))
2344
2345#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2346 2 + /* verifier: AUTH_NULL, length 0 */\
2347 1 + /* status */ \
2348 1 + /* MIN tag is length with zero, only length */ \
2349 3 + /* opcount, opcode, opstatus*/ \
2350 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2351 /* seqid, slotID, slotID, slotID, status */ \
2352 5 ) * sizeof(__be32))
2353
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002354static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002355{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002356 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2357
J. Bruce Fields373cd402013-04-08 15:42:12 -04002358 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2359 return nfserr_toosmall;
2360 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2361 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002362 ca->headerpadsz = 0;
2363 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2364 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2365 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2366 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2367 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2368 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2369 /*
2370 * Note decreasing slot size below client's request may make it
2371 * difficult for client to function correctly, whereas
2372 * decreasing the number of slots will (just?) affect
2373 * performance. When short on memory we therefore prefer to
2374 * decrease number of slots instead of their size. Clients that
2375 * request larger slots than they need will get poor results:
2376 */
2377 ca->maxreqs = nfsd4_get_drc_mem(ca);
2378 if (!ca->maxreqs)
2379 return nfserr_jukebox;
2380
J. Bruce Fields373cd402013-04-08 15:42:12 -04002381 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002382}
2383
Kinglong Mee8a891632013-12-23 18:11:02 +08002384#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2385 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2386#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2387 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2388
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002389static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2390{
2391 ca->headerpadsz = 0;
2392
2393 /*
2394 * These RPC_MAX_HEADER macros are overkill, especially since we
2395 * don't even do gss on the backchannel yet. But this is still
2396 * less than 1k. Tighten up this estimate in the unlikely event
2397 * it turns out to be a problem for some client:
2398 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002399 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002400 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002401 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002402 return nfserr_toosmall;
2403 ca->maxresp_cached = 0;
2404 if (ca->maxops < 2)
2405 return nfserr_toosmall;
2406
2407 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002408}
2409
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002410static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2411{
2412 switch (cbs->flavor) {
2413 case RPC_AUTH_NULL:
2414 case RPC_AUTH_UNIX:
2415 return nfs_ok;
2416 default:
2417 /*
2418 * GSS case: the spec doesn't allow us to return this
2419 * error. But it also doesn't allow us not to support
2420 * GSS.
2421 * I'd rather this fail hard than return some error the
2422 * client might think it can already handle:
2423 */
2424 return nfserr_encr_alg_unsupp;
2425 }
2426}
2427
Andy Adamson069b6ad2009-04-03 08:27:58 +03002428__be32
2429nfsd4_create_session(struct svc_rqst *rqstp,
2430 struct nfsd4_compound_state *cstate,
2431 struct nfsd4_create_session *cr_ses)
2432{
Jeff Layton363168b2009-08-14 12:57:56 -04002433 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002434 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002435 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002436 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002437 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002438 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002439 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002440 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002441
Mi Jinlonga62573d2011-03-23 17:57:07 +08002442 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2443 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002444 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2445 if (status)
2446 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002447 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002448 if (status)
2449 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002450 status = check_backchannel_attrs(&cr_ses->back_channel);
2451 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002452 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002453 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002454 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002455 if (!new)
2456 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002457 conn = alloc_conn_from_crses(rqstp, cr_ses);
2458 if (!conn)
2459 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002460
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002461 nfs4_lock_state();
Jeff Laytond20c11d2014-07-30 08:27:07 -04002462 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002463 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002464 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002465 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002466
2467 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002468 status = nfserr_wrong_cred;
2469 if (!mach_creds_match(conf, rqstp))
2470 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002471 cs_slot = &conf->cl_cs_slot;
2472 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002473 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002474 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002475 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002476 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002477 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002478 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002479 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002480 } else if (unconf) {
2481 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002482 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002483 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002484 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002485 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002486 status = nfserr_wrong_cred;
2487 if (!mach_creds_match(unconf, rqstp))
2488 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002489 cs_slot = &unconf->cl_cs_slot;
2490 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002491 if (status) {
2492 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002493 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002494 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002495 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002496 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002497 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002498 status = mark_client_expired_locked(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002499 if (status)
2500 goto out_free_conn;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002501 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002502 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002503 conf = unconf;
2504 } else {
2505 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002506 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002507 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002508 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002509 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002510 * We do not support RDMA or persistent sessions
2511 */
2512 cr_ses->flags &= ~SESSION4_PERSIST;
2513 cr_ses->flags &= ~SESSION4_RDMA;
2514
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002515 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002516 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002517
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002518 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002519 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002520 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002521 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002522
Jeff Laytond20c11d2014-07-30 08:27:07 -04002523 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002524 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002525 spin_unlock(&nn->client_lock);
2526 /* init connection and backchannel */
2527 nfsd4_init_conn(rqstp, conn, new);
2528 nfsd4_put_session(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002529 nfs4_unlock_state();
Jeff Laytond20c11d2014-07-30 08:27:07 -04002530 if (old)
2531 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002532 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002533out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002534 spin_unlock(&nn->client_lock);
Yanchuan Nian266533c2012-12-24 18:11:45 +08002535 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002536 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002537 if (old)
2538 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002539out_free_session:
2540 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002541out_release_drc_mem:
2542 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002543 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002544}
2545
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002546static __be32 nfsd4_map_bcts_dir(u32 *dir)
2547{
2548 switch (*dir) {
2549 case NFS4_CDFC4_FORE:
2550 case NFS4_CDFC4_BACK:
2551 return nfs_ok;
2552 case NFS4_CDFC4_FORE_OR_BOTH:
2553 case NFS4_CDFC4_BACK_OR_BOTH:
2554 *dir = NFS4_CDFC4_BOTH;
2555 return nfs_ok;
2556 };
2557 return nfserr_inval;
2558}
2559
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002560__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2561{
2562 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002563 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002564 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002565
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002566 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2567 if (status)
2568 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002569 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002570 session->se_cb_prog = bc->bc_cb_program;
2571 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002572 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002573
2574 nfsd4_probe_callback(session->se_client);
2575
2576 return nfs_ok;
2577}
2578
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002579__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2580 struct nfsd4_compound_state *cstate,
2581 struct nfsd4_bind_conn_to_session *bcts)
2582{
2583 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002584 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002585 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002586 struct net *net = SVC_NET(rqstp);
2587 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002588
2589 if (!nfsd4_last_compound_op(rqstp))
2590 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002591 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002592 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002593 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002594 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002595 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002596 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002597 status = nfserr_wrong_cred;
2598 if (!mach_creds_match(session->se_client, rqstp))
2599 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002600 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002601 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002602 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002603 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002604 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002605 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002606 goto out;
2607 nfsd4_init_conn(rqstp, conn, session);
2608 status = nfs_ok;
2609out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002610 nfsd4_put_session(session);
2611out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002612 nfs4_unlock_state();
2613 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002614}
2615
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002616static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2617{
2618 if (!session)
2619 return 0;
2620 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2621}
2622
Andy Adamson069b6ad2009-04-03 08:27:58 +03002623__be32
2624nfsd4_destroy_session(struct svc_rqst *r,
2625 struct nfsd4_compound_state *cstate,
2626 struct nfsd4_destroy_session *sessionid)
2627{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002628 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002629 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002630 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002631 struct net *net = SVC_NET(r);
2632 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002633
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002634 nfs4_lock_state();
2635 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002636 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002637 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002638 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002639 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002640 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002641 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002642 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002643 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002644 if (!ses)
2645 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002646 status = nfserr_wrong_cred;
2647 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002648 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002649 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002650 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002651 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002652 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002653 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002654
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002655 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002656
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002657 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002658 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002659out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002660 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002661out_client_lock:
2662 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002663out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002664 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002665 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002666}
2667
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002668static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002669{
2670 struct nfsd4_conn *c;
2671
2672 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002673 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002674 return c;
2675 }
2676 }
2677 return NULL;
2678}
2679
J. Bruce Fields57266a62013-04-13 14:27:29 -04002680static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002681{
2682 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002683 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002684 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002685 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002686
2687 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002688 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002689 if (c)
2690 goto out_free;
2691 status = nfserr_conn_not_bound_to_session;
2692 if (clp->cl_mach_cred)
2693 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002694 __nfsd4_hash_conn(new, ses);
2695 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002696 ret = nfsd4_register_conn(new);
2697 if (ret)
2698 /* oops; xprt is already down: */
2699 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002700 return nfs_ok;
2701out_free:
2702 spin_unlock(&clp->cl_lock);
2703 free_conn(new);
2704 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002705}
2706
Mi Jinlong868b89c2011-04-27 09:09:58 +08002707static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2708{
2709 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2710
2711 return args->opcnt > session->se_fchannel.maxops;
2712}
2713
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002714static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2715 struct nfsd4_session *session)
2716{
2717 struct xdr_buf *xb = &rqstp->rq_arg;
2718
2719 return xb->len > session->se_fchannel.maxreq_sz;
2720}
2721
Andy Adamson069b6ad2009-04-03 08:27:58 +03002722__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002723nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002724 struct nfsd4_compound_state *cstate,
2725 struct nfsd4_sequence *seq)
2726{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002727 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002728 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002729 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002730 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002731 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002732 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002733 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002734 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002735 struct net *net = SVC_NET(rqstp);
2736 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002737
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002738 if (resp->opcnt != 1)
2739 return nfserr_sequence_pos;
2740
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002741 /*
2742 * Will be either used or freed by nfsd4_sequence_check_conn
2743 * below.
2744 */
2745 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2746 if (!conn)
2747 return nfserr_jukebox;
2748
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002749 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002750 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002751 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002752 goto out_no_session;
2753 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002754
Mi Jinlong868b89c2011-04-27 09:09:58 +08002755 status = nfserr_too_many_ops;
2756 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002757 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002758
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002759 status = nfserr_req_too_big;
2760 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002761 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002762
Benny Halevyb85d4c02009-04-03 08:28:08 +03002763 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002764 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002765 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002766
Andy Adamson557ce262009-08-28 08:45:04 -04002767 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002768 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2769
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002770 /* We do not negotiate the number of slots yet, so set the
2771 * maxslots to the session maxreqs which is used to encode
2772 * sr_highest_slotid and the sr_target_slot id to maxslots */
2773 seq->maxslots = session->se_fchannel.maxreqs;
2774
J. Bruce Fields73e79482012-02-13 16:39:00 -05002775 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2776 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002777 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002778 status = nfserr_seq_misordered;
2779 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002780 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002781 cstate->slot = slot;
2782 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002783 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002784 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002785 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002786 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002787 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002788 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002789 }
2790 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002791 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002792
J. Bruce Fields57266a62013-04-13 14:27:29 -04002793 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002794 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002795 if (status)
2796 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002797
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002798 buflen = (seq->cachethis) ?
2799 session->se_fchannel.maxresp_cached :
2800 session->se_fchannel.maxresp_sz;
2801 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2802 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002803 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002804 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002805 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002806
2807 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002808 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002809 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002810 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002811 if (seq->cachethis)
2812 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002813 else
2814 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002815
2816 cstate->slot = slot;
2817 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002818 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002819
Benny Halevyb85d4c02009-04-03 08:28:08 +03002820out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002821 switch (clp->cl_cb_state) {
2822 case NFSD4_CB_DOWN:
2823 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2824 break;
2825 case NFSD4_CB_FAULT:
2826 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2827 break;
2828 default:
2829 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002830 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002831 if (!list_empty(&clp->cl_revoked))
2832 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002833out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002834 if (conn)
2835 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002836 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002837 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002838out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002839 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002840 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002841}
2842
Trond Myklebustb6076642014-06-30 11:48:35 -04002843void
2844nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2845{
2846 struct nfsd4_compound_state *cs = &resp->cstate;
2847
2848 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002849 if (cs->status != nfserr_replay_cache) {
2850 nfsd4_store_cache_entry(resp);
2851 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2852 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002853 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002854 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002855 } else if (cs->clp)
2856 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002857}
2858
Mi Jinlong345c2842011-10-20 17:51:39 +08002859__be32
2860nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2861{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002862 struct nfs4_client *conf, *unconf;
2863 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002864 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002865 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002866
2867 nfs4_lock_state();
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002868 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002869 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002870 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002871 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002872
2873 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002874 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002875 status = nfserr_clientid_busy;
2876 goto out;
2877 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002878 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08002879 } else if (unconf)
2880 clp = unconf;
2881 else {
2882 status = nfserr_stale_clientid;
2883 goto out;
2884 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002885 if (!mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002886 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002887 status = nfserr_wrong_cred;
2888 goto out;
2889 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002890 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002891out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002892 spin_unlock(&nn->client_lock);
Mi Jinlong345c2842011-10-20 17:51:39 +08002893 nfs4_unlock_state();
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002894 if (clp)
2895 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002896 return status;
2897}
2898
Andy Adamson069b6ad2009-04-03 08:27:58 +03002899__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002900nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2901{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002902 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002903
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002904 if (rc->rca_one_fs) {
2905 if (!cstate->current_fh.fh_dentry)
2906 return nfserr_nofilehandle;
2907 /*
2908 * We don't take advantage of the rca_one_fs case.
2909 * That's OK, it's optional, we can safely ignore it.
2910 */
2911 return nfs_ok;
2912 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002913
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002914 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002915 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002916 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2917 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002918 goto out;
2919
2920 status = nfserr_stale_clientid;
2921 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002922 /*
2923 * The following error isn't really legal.
2924 * But we only get here if the client just explicitly
2925 * destroyed the client. Surely it no longer cares what
2926 * error it gets back on an operation for the dead
2927 * client.
2928 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002929 goto out;
2930
2931 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002932 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002933out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002934 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002935 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002936}
2937
2938__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002939nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2940 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002942 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002944 struct nfs4_client *conf, *new;
2945 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002946 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002947 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2948
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002949 new = create_client(clname, rqstp, &clverifier);
2950 if (new == NULL)
2951 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002952 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 nfs4_lock_state();
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002954 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002955 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002956 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002957 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002959 if (clp_used_exchangeid(conf))
2960 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002961 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002962 char addr_str[INET6_ADDRSTRLEN];
2963 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2964 sizeof(addr_str));
2965 dprintk("NFSD: setclientid: string in use by client "
2966 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 goto out;
2968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002970 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002971 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002972 unhash_client_locked(unconf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002973 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002974 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002976 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002977 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002978 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002979 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002980 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2982 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2983 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002984 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 status = nfs_ok;
2986out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002987 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 nfs4_unlock_state();
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002989 if (new)
2990 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002991 if (unconf)
2992 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 return status;
2994}
2995
2996
Al Virob37ad282006-10-19 23:28:59 -07002997__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002998nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2999 struct nfsd4_compound_state *cstate,
3000 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001{
NeilBrown21ab45a2005-06-23 22:04:14 -07003002 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003003 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3005 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003006 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003007 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003009 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07003012
Jeff Laytond20c11d2014-07-30 08:27:07 -04003013 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003014 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003015 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003016 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003017 * We try hard to give out unique clientid's, so if we get an
3018 * attempt to confirm the same clientid with a different cred,
3019 * there's a bug somewhere. Let's charitably assume it's our
3020 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003021 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04003022 status = nfserr_serverfault;
3023 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3024 goto out;
3025 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3026 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003027 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003028 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3029 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003031 else /* case 4: client hasn't noticed we rebooted yet? */
3032 status = nfserr_stale_clientid;
3033 goto out;
3034 }
3035 status = nfs_ok;
3036 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003037 old = unconf;
3038 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003039 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003040 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003041 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3042 if (old) {
3043 status = mark_client_expired_locked(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003044 if (status)
3045 goto out;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003046 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003047 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003048 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003049 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003050 get_client_locked(conf);
3051 spin_unlock(&nn->client_lock);
3052 nfsd4_probe_callback(conf);
3053 spin_lock(&nn->client_lock);
3054 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003056 spin_unlock(&nn->client_lock);
3057 if (old)
3058 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 nfs4_unlock_state();
3060 return status;
3061}
3062
J. Bruce Fields32513b42011-10-13 16:00:16 -04003063static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003065 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3066}
3067
3068/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003069static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003070{
Trond Myklebustca943212014-07-23 16:17:39 -04003071 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
Trond Myklebust950e0112014-06-30 11:48:31 -04003073 lockdep_assert_held(&state_lock);
3074
J. Bruce Fields32513b42011-10-13 16:00:16 -04003075 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003076 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003077 INIT_LIST_HEAD(&fp->fi_stateids);
3078 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003079 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003080 fp->fi_had_conflict = false;
3081 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003082 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003083 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3084 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04003085 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086}
3087
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003088void
NeilBrowne60d4392005-06-23 22:03:01 -07003089nfsd4_free_slabs(void)
3090{
Christoph Hellwigabf11352014-05-21 07:43:03 -07003091 kmem_cache_destroy(openowner_slab);
3092 kmem_cache_destroy(lockowner_slab);
3093 kmem_cache_destroy(file_slab);
3094 kmem_cache_destroy(stateid_slab);
3095 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003096}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
Bryan Schumaker72083392011-11-01 15:24:59 -04003098int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099nfsd4_init_slabs(void)
3100{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003101 openowner_slab = kmem_cache_create("nfsd4_openowners",
3102 sizeof(struct nfs4_openowner), 0, 0, NULL);
3103 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003104 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003105 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003106 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003107 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003108 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003109 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003110 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003111 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003112 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003113 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003114 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003115 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003116 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003117 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003118 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003119 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003120 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003122
3123out_free_stateid_slab:
3124 kmem_cache_destroy(stateid_slab);
3125out_free_file_slab:
3126 kmem_cache_destroy(file_slab);
3127out_free_lockowner_slab:
3128 kmem_cache_destroy(lockowner_slab);
3129out_free_openowner_slab:
3130 kmem_cache_destroy(openowner_slab);
3131out:
NeilBrowne60d4392005-06-23 22:03:01 -07003132 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3133 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134}
3135
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003136static void init_nfs4_replay(struct nfs4_replay *rp)
3137{
3138 rp->rp_status = nfserr_serverfault;
3139 rp->rp_buflen = 0;
3140 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003141 mutex_init(&rp->rp_mutex);
3142}
3143
3144static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3145 struct nfs4_stateowner *so)
3146{
3147 if (!nfsd4_has_session(cstate)) {
3148 mutex_lock(&so->so_replay.rp_mutex);
3149 cstate->replay_owner = so;
3150 atomic_inc(&so->so_count);
3151 }
3152}
3153
3154void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3155{
3156 struct nfs4_stateowner *so = cstate->replay_owner;
3157
3158 if (so != NULL) {
3159 cstate->replay_owner = NULL;
3160 mutex_unlock(&so->so_replay.rp_mutex);
3161 nfs4_put_stateowner(so);
3162 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003163}
3164
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003165static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166{
3167 struct nfs4_stateowner *sop;
3168
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003169 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003170 if (!sop)
3171 return NULL;
3172
3173 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3174 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003175 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003176 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003178 sop->so_owner.len = owner->len;
3179
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003180 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003181 sop->so_client = clp;
3182 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003183 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003184 return sop;
3185}
3186
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003187static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003188{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003189 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003190
Trond Myklebustd4f04892014-07-29 21:34:36 -04003191 list_add(&oo->oo_owner.so_strhash,
3192 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003193 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194}
3195
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003196static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3197{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003198 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003199}
3200
Jeff Layton6b180f02014-07-29 21:34:26 -04003201static void nfs4_free_openowner(struct nfs4_stateowner *so)
3202{
3203 struct nfs4_openowner *oo = openowner(so);
3204
3205 kmem_cache_free(openowner_slab, oo);
3206}
3207
3208static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003209 .so_unhash = nfs4_unhash_openowner,
3210 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003211};
3212
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003213static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003214alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003215 struct nfsd4_compound_state *cstate)
3216{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003217 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003218 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003220 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3221 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003223 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003224 oo->oo_owner.so_is_open_owner = 1;
3225 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003226 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003227 if (nfsd4_has_session(cstate))
3228 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003229 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003230 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003231 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003232 spin_lock(&clp->cl_lock);
3233 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003234 if (ret == NULL) {
3235 hash_openowner(oo, clp, strhashval);
3236 ret = oo;
3237 } else
3238 nfs4_free_openowner(&oo->oo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003239 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003240 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241}
3242
J. Bruce Fields996e0932011-10-17 11:14:48 -04003243static 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 -04003244 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003246 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003247 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003248 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003249 stp->st_stateowner = &oo->oo_owner;
Jeff Laytond3134b12014-07-29 21:34:32 -04003250 atomic_inc(&stp->st_stateowner->so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07003251 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003252 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 stp->st_access_bmap = 0;
3254 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003255 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003256 spin_lock(&oo->oo_owner.so_client->cl_lock);
3257 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003258 spin_lock(&fp->fi_lock);
3259 list_add(&stp->st_perfile, &fp->fi_stateids);
3260 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003261 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262}
3263
Jeff Laytond3134b12014-07-29 21:34:32 -04003264/*
3265 * In the 4.0 case we need to keep the owners around a little while to handle
3266 * CLOSE replay. We still do need to release any file access that is held by
3267 * them before returning however.
3268 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003270move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271{
Jeff Layton217526e2014-07-30 08:27:11 -04003272 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003273 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3274 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3275 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003276
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003277 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
Jeff Laytonb401be222014-07-29 21:34:33 -04003279 /*
3280 * We know that we hold one reference via nfsd4_close, and another
3281 * "persistent" reference for the client. If the refcount is higher
3282 * than 2, then there are still calls in progress that are using this
3283 * stateid. We can't put the sc_file reference until they are finished.
3284 * Wait for the refcount to drop to 2. Since it has been unhashed,
3285 * there should be no danger of the refcount going back up again at
3286 * this point.
3287 */
3288 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3289
Jeff Laytond3134b12014-07-29 21:34:32 -04003290 release_all_access(s);
3291 if (s->st_stid.sc_file) {
3292 put_nfs4_file(s->st_stid.sc_file);
3293 s->st_stid.sc_file = NULL;
3294 }
Jeff Layton217526e2014-07-30 08:27:11 -04003295
3296 spin_lock(&nn->client_lock);
3297 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003298 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003299 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003300 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003301 spin_unlock(&nn->client_lock);
3302 if (last)
3303 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304}
3305
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306/* search file_hashtbl[] for file */
3307static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003308find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309{
Trond Myklebustca943212014-07-23 16:17:39 -04003310 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 struct nfs4_file *fp;
3312
Trond Myklebust950e0112014-06-30 11:48:31 -04003313 lockdep_assert_held(&state_lock);
3314
Jeff Layton89876f82013-04-02 09:01:59 -04003315 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003316 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003317 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 }
3321 return NULL;
3322}
3323
Trond Myklebust950e0112014-06-30 11:48:31 -04003324static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003325find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003326{
3327 struct nfs4_file *fp;
3328
3329 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003330 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003331 spin_unlock(&state_lock);
3332 return fp;
3333}
3334
3335static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003336find_or_add_file(struct nfs4_file *new, 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 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003343 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003344 fp = new;
3345 }
3346 spin_unlock(&state_lock);
3347
3348 return fp;
3349}
3350
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003351/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 * Called to check deny when READ with all zero stateid or
3353 * WRITE with all zero or all one stateid
3354 */
Al Virob37ad282006-10-19 23:28:59 -07003355static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3357{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003359 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360
Trond Myklebustca943212014-07-23 16:17:39 -04003361 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003362 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003363 return ret;
3364 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003365 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003366 if (fp->fi_share_deny & deny_type)
3367 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003368 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003369 put_nfs4_file(fp);
3370 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371}
3372
Jeff Layton02e12152014-07-16 10:31:57 -04003373void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374{
Trond Myklebust11b91642014-07-29 21:34:08 -04003375 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3376 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003377
Trond Myklebust11b91642014-07-29 21:34:08 -04003378 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003379
Jeff Layton02e12152014-07-16 10:31:57 -04003380 /*
3381 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003382 * already holding inode->i_lock.
3383 *
Jeff Laytondff13992014-07-08 14:02:49 -04003384 * If the dl_time != 0, then we know that it has already been
3385 * queued for a lease break. Don't queue it again.
3386 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003387 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003388 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003389 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003390 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003391 }
Jeff Layton02e12152014-07-16 10:31:57 -04003392 spin_unlock(&state_lock);
3393}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394
Jeff Layton02e12152014-07-16 10:31:57 -04003395static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3396{
3397 /*
3398 * We're assuming the state code never drops its reference
3399 * without first removing the lease. Since we're in this lease
3400 * callback (and since the lease code is serialized by the kernel
3401 * lock) we know the server hasn't removed the lease yet, we know
3402 * it's safe to take a reference.
3403 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003404 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003405 nfsd4_cb_recall(dp);
3406}
3407
Jeff Layton1c8c6012013-06-21 08:58:15 -04003408/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003409static void nfsd_break_deleg_cb(struct file_lock *fl)
3410{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003411 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3412 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003413
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003414 if (!fp) {
3415 WARN(1, "(%p)->fl_owner NULL\n", fl);
3416 return;
3417 }
3418 if (fp->fi_had_conflict) {
3419 WARN(1, "duplicate break on %p\n", fp);
3420 return;
3421 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003422 /*
3423 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003424 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003425 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003426 */
3427 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428
Jeff Layton02e12152014-07-16 10:31:57 -04003429 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003430 fp->fi_had_conflict = true;
3431 /*
3432 * If there are no delegations on the list, then we can't count on this
3433 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3434 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3435 * true should keep any new delegations from being hashed.
3436 */
3437 if (list_empty(&fp->fi_delegations))
3438 fl->fl_break_time = jiffies;
3439 else
3440 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3441 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003442 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443}
3444
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445static
3446int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3447{
3448 if (arg & F_UNLCK)
3449 return lease_modify(onlist, arg);
3450 else
3451 return -EAGAIN;
3452}
3453
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003454static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003455 .lm_break = nfsd_break_deleg_cb,
3456 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457};
3458
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003459static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3460{
3461 if (nfsd4_has_session(cstate))
3462 return nfs_ok;
3463 if (seqid == so->so_seqid - 1)
3464 return nfserr_replay_me;
3465 if (seqid == so->so_seqid)
3466 return nfs_ok;
3467 return nfserr_bad_seqid;
3468}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
Jeff Layton4b24ca72014-06-30 11:48:44 -04003470static __be32 lookup_clientid(clientid_t *clid,
3471 struct nfsd4_compound_state *cstate,
3472 struct nfsd_net *nn)
3473{
3474 struct nfs4_client *found;
3475
3476 if (cstate->clp) {
3477 found = cstate->clp;
3478 if (!same_clid(&found->cl_clientid, clid))
3479 return nfserr_stale_clientid;
3480 return nfs_ok;
3481 }
3482
3483 if (STALE_CLIENTID(clid, nn))
3484 return nfserr_stale_clientid;
3485
3486 /*
3487 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3488 * cached already then we know this is for is for v4.0 and "sessions"
3489 * will be false.
3490 */
3491 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003492 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003493 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003494 if (!found) {
3495 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003496 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003497 }
3498 atomic_inc(&found->cl_refcount);
3499 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003500
3501 /* Cache the nfs4_client in cstate! */
3502 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003503 return nfs_ok;
3504}
3505
Al Virob37ad282006-10-19 23:28:59 -07003506__be32
Andy Adamson66689582009-04-03 08:28:45 +03003507nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003508 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 clientid_t *clientid = &open->op_clientid;
3511 struct nfs4_client *clp = NULL;
3512 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003513 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003514 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003516 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003518 /*
3519 * In case we need it later, after we've already created the
3520 * file and don't want to risk a further failure:
3521 */
3522 open->op_file = nfsd4_alloc_file();
3523 if (open->op_file == NULL)
3524 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525
Trond Myklebust2d91e892014-06-30 11:48:46 -04003526 status = lookup_clientid(clientid, cstate, nn);
3527 if (status)
3528 return status;
3529 clp = cstate->clp;
3530
Trond Myklebustd4f04892014-07-29 21:34:36 -04003531 strhashval = ownerstr_hashval(&open->op_owner);
3532 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003533 open->op_openowner = oo;
3534 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003535 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003537 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003538 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003539 release_openowner(oo);
3540 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003541 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003542 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003543 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3544 if (status)
3545 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003546 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003547new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003548 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003549 if (oo == NULL)
3550 return nfserr_jukebox;
3551 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003552alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003553 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003554 if (!open->op_stp)
3555 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003556 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557}
3558
Al Virob37ad282006-10-19 23:28:59 -07003559static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003560nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3561{
3562 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3563 return nfserr_openmode;
3564 else
3565 return nfs_ok;
3566}
3567
Daniel Mackc47d8322011-05-16 16:38:14 +02003568static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003569{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003570 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3571}
3572
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003573static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003574{
3575 struct nfs4_stid *ret;
3576
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003577 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003578 if (!ret)
3579 return NULL;
3580 return delegstateid(ret);
3581}
3582
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003583static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3584{
3585 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3586 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3587}
3588
Al Virob37ad282006-10-19 23:28:59 -07003589static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003590nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003591 struct nfs4_delegation **dp)
3592{
3593 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003594 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003595 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003596
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003597 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3598 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003599 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003600 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003601 status = nfs4_check_delegmode(deleg, flags);
3602 if (status) {
3603 nfs4_put_stid(&deleg->dl_stid);
3604 goto out;
3605 }
3606 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003607out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003608 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003609 return nfs_ok;
3610 if (status)
3611 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003612 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003613 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003614}
3615
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003616static struct nfs4_ol_stateid *
3617nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003619 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003620 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621
Trond Myklebust1d31a252014-07-10 14:07:25 -04003622 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003623 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 /* ignore lock owners */
3625 if (local->st_stateowner->so_is_open_owner == 0)
3626 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003627 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003628 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003629 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003630 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003633 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003634 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635}
3636
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003637static inline int nfs4_access_to_access(u32 nfs4_access)
3638{
3639 int flags = 0;
3640
3641 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3642 flags |= NFSD_MAY_READ;
3643 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3644 flags |= NFSD_MAY_WRITE;
3645 return flags;
3646}
3647
Al Virob37ad282006-10-19 23:28:59 -07003648static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3650 struct nfsd4_open *open)
3651{
3652 struct iattr iattr = {
3653 .ia_valid = ATTR_SIZE,
3654 .ia_size = 0,
3655 };
3656 if (!open->op_truncate)
3657 return 0;
3658 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003659 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3661}
3662
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003663static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003664 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3665 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003666{
Trond Myklebustde186432014-07-10 14:07:26 -04003667 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003668 __be32 status;
3669 int oflag = nfs4_access_to_omode(open->op_share_access);
3670 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003671 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003672
Trond Myklebustde186432014-07-10 14:07:26 -04003673 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003674
3675 /*
3676 * Are we trying to set a deny mode that would conflict with
3677 * current access?
3678 */
3679 status = nfs4_file_check_deny(fp, open->op_share_deny);
3680 if (status != nfs_ok) {
3681 spin_unlock(&fp->fi_lock);
3682 goto out;
3683 }
3684
3685 /* set access to the file */
3686 status = nfs4_file_get_access(fp, open->op_share_access);
3687 if (status != nfs_ok) {
3688 spin_unlock(&fp->fi_lock);
3689 goto out;
3690 }
3691
3692 /* Set access bits in stateid */
3693 old_access_bmap = stp->st_access_bmap;
3694 set_access(open->op_share_access, stp);
3695
3696 /* Set new deny mask */
3697 old_deny_bmap = stp->st_deny_bmap;
3698 set_deny(open->op_share_deny, stp);
3699 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3700
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003701 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003702 spin_unlock(&fp->fi_lock);
3703 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003704 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003705 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003706 spin_lock(&fp->fi_lock);
3707 if (!fp->fi_fds[oflag]) {
3708 fp->fi_fds[oflag] = filp;
3709 filp = NULL;
3710 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003711 }
Trond Myklebustde186432014-07-10 14:07:26 -04003712 spin_unlock(&fp->fi_lock);
3713 if (filp)
3714 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003715
3716 status = nfsd4_truncate(rqstp, cur_fh, open);
3717 if (status)
3718 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003719out:
3720 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003721out_put_access:
3722 stp->st_access_bmap = old_access_bmap;
3723 nfs4_file_put_access(fp, open->op_share_access);
3724 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3725 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003726}
3727
Al Virob37ad282006-10-19 23:28:59 -07003728static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003729nfs4_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 -07003730{
Al Virob37ad282006-10-19 23:28:59 -07003731 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003732 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003734 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003735 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003736
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003737 /* test and set deny mode */
3738 spin_lock(&fp->fi_lock);
3739 status = nfs4_file_check_deny(fp, open->op_share_deny);
3740 if (status == nfs_ok) {
3741 old_deny_bmap = stp->st_deny_bmap;
3742 set_deny(open->op_share_deny, stp);
3743 fp->fi_share_deny |=
3744 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3745 }
3746 spin_unlock(&fp->fi_lock);
3747
3748 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003751 status = nfsd4_truncate(rqstp, cur_fh, open);
3752 if (status != nfs_ok)
3753 reset_union_bmap_deny(old_deny_bmap, stp);
3754 return status;
3755}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003758nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003760 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761}
3762
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003763/* Should we give out recallable state?: */
3764static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3765{
3766 if (clp->cl_cb_state == NFSD4_CB_UP)
3767 return true;
3768 /*
3769 * In the sessions case, since we don't have to establish a
3770 * separate connection for callbacks, we assume it's OK
3771 * until we hear otherwise:
3772 */
3773 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3774}
3775
Jeff Laytond564fbe2014-07-16 10:31:58 -04003776static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003777{
3778 struct file_lock *fl;
3779
3780 fl = locks_alloc_lock();
3781 if (!fl)
3782 return NULL;
3783 locks_init_lock(fl);
3784 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003785 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003786 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3787 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003788 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003789 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003790 return fl;
3791}
3792
J. Bruce Fields99c41512013-05-21 16:21:25 -04003793static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003794{
Trond Myklebust11b91642014-07-29 21:34:08 -04003795 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003796 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003797 struct file *filp;
3798 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003799
Jeff Laytond564fbe2014-07-16 10:31:58 -04003800 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003801 if (!fl)
3802 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003803 filp = find_readable_file(fp);
3804 if (!filp) {
3805 /* We should always have a readable file here */
3806 WARN_ON_ONCE(1);
3807 return -EBADF;
3808 }
3809 fl->fl_file = filp;
3810 status = vfs_setlease(filp, fl->fl_type, &fl);
3811 if (status) {
3812 locks_free_lock(fl);
3813 goto out_fput;
3814 }
Benny Halevycdc97502014-05-30 09:09:30 -04003815 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003816 spin_lock(&fp->fi_lock);
3817 /* Did the lease get broken before we took the lock? */
3818 status = -EAGAIN;
3819 if (fp->fi_had_conflict)
3820 goto out_unlock;
3821 /* Race breaker */
3822 if (fp->fi_lease) {
3823 status = 0;
3824 atomic_inc(&fp->fi_delegees);
3825 hash_delegation_locked(dp, fp);
3826 goto out_unlock;
3827 }
3828 fp->fi_lease = fl;
3829 fp->fi_deleg_file = filp;
3830 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003831 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003832 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003833 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003834 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003835out_unlock:
3836 spin_unlock(&fp->fi_lock);
3837 spin_unlock(&state_lock);
3838out_fput:
3839 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003840 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003841}
3842
Jeff Layton0b266932014-07-25 07:34:25 -04003843static struct nfs4_delegation *
3844nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3845 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003846{
Jeff Layton0b266932014-07-25 07:34:25 -04003847 int status;
3848 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003849
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003850 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003851 return ERR_PTR(-EAGAIN);
3852
3853 dp = alloc_init_deleg(clp, fh);
3854 if (!dp)
3855 return ERR_PTR(-ENOMEM);
3856
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003857 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003858 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003859 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003860 dp->dl_stid.sc_file = fp;
Jeff Layton417c6622014-07-21 09:34:57 -04003861 if (!fp->fi_lease) {
3862 spin_unlock(&fp->fi_lock);
3863 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003864 status = nfs4_setlease(dp);
3865 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003866 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003867 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003868 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003869 status = -EAGAIN;
3870 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003871 }
Benny Halevy931ee562014-05-30 09:09:27 -04003872 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003873 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003874out_unlock:
3875 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003876 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003877out:
3878 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003879 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003880 return ERR_PTR(status);
3881 }
3882 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003883}
3884
Benny Halevy4aa89132012-02-21 14:16:44 -08003885static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3886{
3887 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3888 if (status == -EAGAIN)
3889 open->op_why_no_deleg = WND4_CONTENTION;
3890 else {
3891 open->op_why_no_deleg = WND4_RESOURCE;
3892 switch (open->op_deleg_want) {
3893 case NFS4_SHARE_WANT_READ_DELEG:
3894 case NFS4_SHARE_WANT_WRITE_DELEG:
3895 case NFS4_SHARE_WANT_ANY_DELEG:
3896 break;
3897 case NFS4_SHARE_WANT_CANCEL:
3898 open->op_why_no_deleg = WND4_CANCELLED;
3899 break;
3900 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003901 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003902 }
3903 }
3904}
3905
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906/*
3907 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003908 *
3909 * Note we don't support write delegations, and won't until the vfs has
3910 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 */
3912static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003913nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3914 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915{
3916 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003917 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3918 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003919 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003920 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003922 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003923 open->op_recall = 0;
3924 switch (open->op_claim_type) {
3925 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003926 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003927 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003928 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3929 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003930 break;
3931 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003932 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003933 /*
3934 * Let's not give out any delegations till everyone's
3935 * had the chance to reclaim theirs....
3936 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003937 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003938 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003939 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003940 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003941 /*
3942 * Also, if the file was opened for write or
3943 * create, there's a good chance the client's
3944 * about to write to it, resulting in an
3945 * immediate recall (since we don't support
3946 * write delegations):
3947 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003948 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003949 goto out_no_deleg;
3950 if (open->op_create == NFS4_OPEN_CREATE)
3951 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003952 break;
3953 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003954 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003955 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003956 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003957 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003958 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003960 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003962 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003963 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003964 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003965 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003966 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003967out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003968 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3969 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003970 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003971 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003972 open->op_recall = 1;
3973 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003974
3975 /* 4.1 client asking for a delegation? */
3976 if (open->op_deleg_want)
3977 nfsd4_open_deleg_none_ext(open, status);
3978 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979}
3980
Benny Halevye27f49c2012-02-21 14:16:54 -08003981static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3982 struct nfs4_delegation *dp)
3983{
3984 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3985 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3986 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3987 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3988 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3989 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3990 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3991 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3992 }
3993 /* Otherwise the client must be confused wanting a delegation
3994 * it already has, therefore we don't return
3995 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3996 */
3997}
3998
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999/*
4000 * called with nfs4_lock_state() held.
4001 */
Al Virob37ad282006-10-19 23:28:59 -07004002__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4004{
Andy Adamson66689582009-04-03 08:28:45 +03004005 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004006 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004008 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004009 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004010 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 /*
4013 * Lookup file; if found, lookup stateid and check open request,
4014 * and check for delegations in the process of being recalled.
4015 * If not found, create the nfs4_file struct
4016 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004017 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004018 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004019 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004020 if (status)
4021 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04004022 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004024 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004025 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004026 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004027 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004028 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 }
4030
4031 /*
4032 * OPEN the file, or upgrade an existing OPEN.
4033 * If truncate fails, the OPEN fails.
4034 */
4035 if (stp) {
4036 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004037 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 if (status)
4039 goto out;
4040 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004041 stp = open->op_stp;
4042 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04004043 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004044 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4045 if (status) {
4046 release_open_stateid(stp);
4047 goto out;
4048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004050 update_stateid(&stp->st_stid.sc_stateid);
4051 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052
Benny Halevyd24433c2012-02-16 20:57:17 +02004053 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004054 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4055 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4056 open->op_why_no_deleg = WND4_NOT_WANTED;
4057 goto nodeleg;
4058 }
4059 }
4060
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 /*
4062 * Attempt to hand out a delegation. No error return, because the
4063 * OPEN succeeds even if we fail.
4064 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004065 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004066nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 status = nfs_ok;
4068
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004069 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004070 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004072 /* 4.1 client trying to upgrade/downgrade delegation? */
4073 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004074 open->op_deleg_want)
4075 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004076
NeilBrown13cd2182005-06-23 22:03:10 -07004077 if (fp)
4078 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004079 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05004080 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 /*
4082 * To finish the open response, we just need to set the rflags.
4083 */
4084 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004085 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004086 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004088 if (dp)
4089 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004090 if (stp)
4091 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092
4093 return status;
4094}
4095
Jeff Layton58fb12e2014-07-29 21:34:27 -04004096void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4097 struct nfsd4_open *open, __be32 status)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004098{
4099 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004100 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004101
Jeff Laytond3134b12014-07-29 21:34:32 -04004102 nfsd4_cstate_assign_replay(cstate, so);
4103 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004104 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004105 if (open->op_file)
4106 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004107 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004108 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004109}
4110
Al Virob37ad282006-10-19 23:28:59 -07004111__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004112nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4113 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114{
4115 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004116 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004117 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118
4119 nfs4_lock_state();
4120 dprintk("process_renew(%08x/%08x): starting\n",
4121 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004122 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004123 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004125 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004127 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004128 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 goto out;
4130 status = nfs_ok;
4131out:
4132 nfs4_unlock_state();
4133 return status;
4134}
4135
NeilBrowna76b4312005-06-23 22:04:01 -07004136static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004137nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004138{
Jeff Layton33dcc482012-04-10 11:08:48 -04004139 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004140 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004141 return;
4142
NeilBrowna76b4312005-06-23 22:04:01 -07004143 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004144 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004145 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004146 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004147 /*
4148 * Now that every NFSv4 client has had the chance to recover and
4149 * to see the (possibly new, possibly shorter) lease time, we
4150 * can safely set the next grace time to the current lease time:
4151 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03004152 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07004153}
4154
NeilBrownfd39ca92005-06-23 22:04:03 -07004155static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004156nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157{
4158 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004159 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004161 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004163 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004164 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165
4166 nfs4_lock_state();
4167
4168 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004169 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004170 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004171 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004172 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 clp = list_entry(pos, struct nfs4_client, cl_lru);
4174 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4175 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004176 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 break;
4178 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004179 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004180 dprintk("NFSD: client in use (clientid %08x)\n",
4181 clp->cl_clientid.cl_id);
4182 continue;
4183 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004184 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004185 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004186 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004187 list_for_each_safe(pos, next, &reaplist) {
4188 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 dprintk("NFSD: purging unused client (clientid %08x)\n",
4190 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004191 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 expire_client(clp);
4193 }
Benny Halevycdc97502014-05-30 09:09:30 -04004194 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004195 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004197 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4198 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004200 t = dp->dl_time - cutoff;
4201 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 break;
4203 }
Jeff Layton42690672014-07-25 07:34:20 -04004204 unhash_delegation_locked(dp);
4205 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 }
Benny Halevycdc97502014-05-30 09:09:30 -04004207 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004208 while (!list_empty(&reaplist)) {
4209 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4210 dl_recall_lru);
4211 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004212 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 }
Jeff Layton217526e2014-07-30 08:27:11 -04004214
4215 spin_lock(&nn->client_lock);
4216 while (!list_empty(&nn->close_lru)) {
4217 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4218 oo_close_lru);
4219 if (time_after((unsigned long)oo->oo_time,
4220 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004221 t = oo->oo_time - cutoff;
4222 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 break;
4224 }
Jeff Layton217526e2014-07-30 08:27:11 -04004225 list_del_init(&oo->oo_close_lru);
4226 stp = oo->oo_last_closed_stid;
4227 oo->oo_last_closed_stid = NULL;
4228 spin_unlock(&nn->client_lock);
4229 nfs4_put_stid(&stp->st_stid);
4230 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 }
Jeff Layton217526e2014-07-30 08:27:11 -04004232 spin_unlock(&nn->client_lock);
4233
Jeff Laytona832e7a2014-05-30 09:09:26 -04004234 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04004236 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237}
4238
Harvey Harrisona254b242008-02-20 12:49:00 -08004239static struct workqueue_struct *laundry_wq;
4240static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004241
4242static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004243laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244{
4245 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004246 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4247 work);
4248 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4249 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004251 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004253 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254}
4255
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004256static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004257{
Trond Myklebust11b91642014-07-29 21:34:08 -04004258 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004259 return nfserr_bad_stateid;
4260 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261}
4262
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004264access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004266 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4267 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4268 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269}
4270
4271static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004272access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004274 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4275 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276}
4277
4278static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004279__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280{
Al Virob37ad282006-10-19 23:28:59 -07004281 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282
J. Bruce Fields02921912010-07-29 15:16:59 -04004283 /* For lock stateid's, we test the parent open, not the lock: */
4284 if (stp->st_openstp)
4285 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004286 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004288 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 goto out;
4290 status = nfs_ok;
4291out:
4292 return status;
4293}
4294
Al Virob37ad282006-10-19 23:28:59 -07004295static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004296check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004298 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004300 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004301 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 * conflicting state; so we must wait out the grace period. */
4303 return nfserr_grace;
4304 } else if (flags & WR_STATE)
4305 return nfs4_share_conflict(current_fh,
4306 NFS4_SHARE_DENY_WRITE);
4307 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4308 return nfs4_share_conflict(current_fh,
4309 NFS4_SHARE_DENY_READ);
4310}
4311
4312/*
4313 * Allow READ/WRITE during grace period on recovered state only for files
4314 * that are not able to provide mandatory locking.
4315 */
4316static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004317grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004319 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320}
4321
J. Bruce Fields81b82962011-08-23 11:03:29 -04004322/* Returns true iff a is later than b: */
4323static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4324{
Jim Rees1a9357f2013-05-17 17:33:00 -04004325 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004326}
4327
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004328static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004329{
Andy Adamson66689582009-04-03 08:28:45 +03004330 /*
4331 * When sessions are used the stateid generation number is ignored
4332 * when it is zero.
4333 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004334 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004335 return nfs_ok;
4336
4337 if (in->si_generation == ref->si_generation)
4338 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004339
J. Bruce Fields0836f582008-01-26 19:08:12 -05004340 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004341 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004342 return nfserr_bad_stateid;
4343 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004344 * However, we could see a stateid from the past, even from a
4345 * non-buggy client. For example, if the client sends a lock
4346 * while some IO is outstanding, the lock may bump si_generation
4347 * while the IO is still in flight. The client could avoid that
4348 * situation by waiting for responses on all the IO requests,
4349 * but better performance may result in retrying IO that
4350 * receives an old_stateid error if requests are rarely
4351 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004352 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004353 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004354}
4355
Chuck Lever7df302f2012-05-29 13:56:37 -04004356static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004357{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004358 struct nfs4_stid *s;
4359 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004360 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004361
Chuck Lever7df302f2012-05-29 13:56:37 -04004362 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004363 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004364 /* Client debugging aid. */
4365 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4366 char addr_str[INET6_ADDRSTRLEN];
4367 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4368 sizeof(addr_str));
4369 pr_warn_ratelimited("NFSD: client %s testing state ID "
4370 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004371 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004372 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004373 spin_lock(&cl->cl_lock);
4374 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004375 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004376 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004377 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004378 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004379 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004380 switch (s->sc_type) {
4381 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004382 status = nfs_ok;
4383 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004384 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004385 status = nfserr_deleg_revoked;
4386 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004387 case NFS4_OPEN_STID:
4388 case NFS4_LOCK_STID:
4389 ols = openlockstateid(s);
4390 if (ols->st_stateowner->so_is_open_owner
4391 && !(openowner(ols->st_stateowner)->oo_flags
4392 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004393 status = nfserr_bad_stateid;
4394 else
4395 status = nfs_ok;
4396 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004397 default:
4398 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004399 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004400 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004401 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004402 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004403 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004404out_unlock:
4405 spin_unlock(&cl->cl_lock);
4406 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004407}
4408
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004409static __be32
4410nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4411 stateid_t *stateid, unsigned char typemask,
4412 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004413{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004414 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004415
4416 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4417 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004418 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004419 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004420 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004421 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004422 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004423 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004424 if (status)
4425 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004426 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004427 if (!*s)
4428 return nfserr_bad_stateid;
4429 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004430}
4431
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432/*
4433* Checks for stateid operations
4434*/
Al Virob37ad282006-10-19 23:28:59 -07004435__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004436nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004437 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004439 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004440 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004442 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004444 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004445 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004446 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 if (filpp)
4449 *filpp = NULL;
4450
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004451 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 return nfserr_grace;
4453
4454 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004455 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456
Trond Myklebust14bcab12014-04-18 14:44:07 -04004457 nfs4_lock_state();
4458
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004459 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004460 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004461 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004462 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004463 goto unlock_state;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004464 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4465 if (status)
4466 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004467 switch (s->sc_type) {
4468 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004469 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004470 status = nfs4_check_delegmode(dp, flags);
4471 if (status)
4472 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004473 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004474 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004475 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004476 WARN_ON_ONCE(1);
4477 status = nfserr_serverfault;
4478 goto out;
4479 }
Trond Myklebustde186432014-07-10 14:07:26 -04004480 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004481 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004482 break;
4483 case NFS4_OPEN_STID:
4484 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004485 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004486 status = nfs4_check_fh(current_fh, stp);
4487 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004489 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004490 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004492 status = nfs4_check_openmode(stp, flags);
4493 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004495 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004496 struct nfs4_file *fp = stp->st_stid.sc_file;
4497
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004498 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004499 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004500 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004501 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004502 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004503 break;
4504 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004505 status = nfserr_bad_stateid;
4506 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 }
4508 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004509 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004510 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004512 nfs4_put_stid(s);
4513unlock_state:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004514 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 return status;
4516}
4517
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004518/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004519 * Test if the stateid is valid
4520 */
4521__be32
4522nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4523 struct nfsd4_test_stateid *test_stateid)
4524{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004525 struct nfsd4_test_stateid_id *stateid;
4526 struct nfs4_client *cl = cstate->session->se_client;
4527
4528 nfs4_lock_state();
4529 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004530 stateid->ts_id_status =
4531 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004532 nfs4_unlock_state();
4533
Bryan Schumaker17456802011-07-13 10:50:48 -04004534 return nfs_ok;
4535}
4536
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004537__be32
4538nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4539 struct nfsd4_free_stateid *free_stateid)
4540{
4541 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004542 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004543 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004544 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004545 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004546 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004547
4548 nfs4_lock_state();
Jeff Layton1af71cc2014-07-29 21:34:14 -04004549 spin_lock(&cl->cl_lock);
4550 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004551 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004552 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004553 switch (s->sc_type) {
4554 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004555 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004556 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004557 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004558 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4559 if (ret)
4560 break;
4561 ret = nfserr_locks_held;
4562 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004563 case NFS4_LOCK_STID:
4564 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4565 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004566 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004567 stp = openlockstateid(s);
4568 ret = nfserr_locks_held;
4569 if (check_for_locks(stp->st_stid.sc_file,
4570 lockowner(stp->st_stateowner)))
4571 break;
4572 unhash_lock_stateid(stp);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004573 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004574 nfs4_put_stid(s);
4575 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004576 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004577 case NFS4_REVOKED_DELEG_STID:
4578 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004579 list_del_init(&dp->dl_recall_lru);
4580 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004581 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004582 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004583 goto out;
4584 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004585 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004586out_unlock:
4587 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004588out:
4589 nfs4_unlock_state();
4590 return ret;
4591}
4592
NeilBrown4c4cd222005-07-07 17:59:27 -07004593static inline int
4594setlkflg (int type)
4595{
4596 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4597 RD_STATE : WR_STATE;
4598}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004600static __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 -04004601{
4602 struct svc_fh *current_fh = &cstate->current_fh;
4603 struct nfs4_stateowner *sop = stp->st_stateowner;
4604 __be32 status;
4605
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004606 status = nfsd4_check_seqid(cstate, sop, seqid);
4607 if (status)
4608 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004609 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4610 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004611 /*
4612 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004613 * nfserr_replay_me from the previous step, and
4614 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004615 */
4616 return nfserr_bad_stateid;
4617 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4618 if (status)
4619 return status;
4620 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004621}
4622
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623/*
4624 * Checks for sequence id mutating operations.
4625 */
Al Virob37ad282006-10-19 23:28:59 -07004626static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004627nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004628 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004629 struct nfs4_ol_stateid **stpp,
4630 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004632 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004633 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004634 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004636 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4637 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004638
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004640 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004641 if (status)
4642 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004643 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004644 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004646 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004647 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004648 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004649 else
4650 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004651 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004652}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004653
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004654static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4655 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004656{
4657 __be32 status;
4658 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004659 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004661 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004662 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004663 if (status)
4664 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004665 oo = openowner(stp->st_stateowner);
4666 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4667 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004668 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004669 }
4670 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004671 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672}
4673
Al Virob37ad282006-10-19 23:28:59 -07004674__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004675nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004676 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677{
Al Virob37ad282006-10-19 23:28:59 -07004678 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004679 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004680 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004681 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682
Al Viroa6a9f182013-09-16 10:57:01 -04004683 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4684 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004686 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004687 if (status)
4688 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689
4690 nfs4_lock_state();
4691
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004692 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004693 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004694 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004695 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004696 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004697 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004698 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004699 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004700 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004701 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004702 update_stateid(&stp->st_stid.sc_stateid);
4703 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004704 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004705 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004706
Jeff Layton2a4317c2012-03-21 16:42:43 -04004707 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004708 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004709put_stateid:
4710 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004712 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004713 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 return status;
4715}
4716
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004717static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004719 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004720 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004721 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004722 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004723}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004724
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004725static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4726{
4727 switch (to_access) {
4728 case NFS4_SHARE_ACCESS_READ:
4729 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4730 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4731 break;
4732 case NFS4_SHARE_ACCESS_WRITE:
4733 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4734 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4735 break;
4736 case NFS4_SHARE_ACCESS_BOTH:
4737 break;
4738 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004739 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 }
4741}
4742
Al Virob37ad282006-10-19 23:28:59 -07004743__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004744nfsd4_open_downgrade(struct svc_rqst *rqstp,
4745 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004746 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747{
Al Virob37ad282006-10-19 23:28:59 -07004748 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004749 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004750 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751
Al Viroa6a9f182013-09-16 10:57:01 -04004752 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4753 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004755 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004756 if (od->od_deleg_want)
4757 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4758 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759
4760 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004761 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004762 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004763 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004766 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004767 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004769 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004771 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004772 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004774 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004776 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777
Jeff Laytonce0fc432012-05-11 09:45:14 -04004778 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004780 update_stateid(&stp->st_stid.sc_stateid);
4781 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004783put_stateid:
4784 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004786 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004787 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 return status;
4789}
4790
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004791static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4792{
Trond Myklebustacf92952014-06-30 11:48:37 -04004793 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytond83017f2014-07-29 21:34:42 -04004794 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004795
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004796 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04004797 spin_lock(&clp->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04004798 unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004799
Jeff Laytond83017f2014-07-29 21:34:42 -04004800 if (clp->cl_minorversion) {
4801 put_ol_stateid_locked(s, &reaplist);
4802 spin_unlock(&clp->cl_lock);
4803 free_ol_stateid_reaplist(&reaplist);
4804 } else {
4805 spin_unlock(&clp->cl_lock);
4806 free_ol_stateid_reaplist(&reaplist);
Jeff Laytond3134b12014-07-29 21:34:32 -04004807 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04004808 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004809}
4810
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811/*
4812 * nfs4_unlock_state() called after encode
4813 */
Al Virob37ad282006-10-19 23:28:59 -07004814__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004815nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004816 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817{
Al Virob37ad282006-10-19 23:28:59 -07004818 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004819 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004820 struct net *net = SVC_NET(rqstp);
4821 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822
Al Viroa6a9f182013-09-16 10:57:01 -04004823 dprintk("NFSD: nfsd4_close on file %pd\n",
4824 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
4826 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004827 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4828 &close->cl_stateid,
4829 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004830 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004831 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004832 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004834 update_stateid(&stp->st_stid.sc_stateid);
4835 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004837 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004838
4839 /* put reference from nfs4_preprocess_seqid_op */
4840 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841out:
Jeff Layton58fb12e2014-07-29 21:34:27 -04004842 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 return status;
4844}
4845
Al Virob37ad282006-10-19 23:28:59 -07004846__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004847nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4848 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004850 struct nfs4_delegation *dp;
4851 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004852 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004853 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004854 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004856 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004857 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858
4859 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004860 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004861 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004862 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004863 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004864 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004865 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004866 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004867
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004868 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004869put_stateid:
4870 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004872 nfs4_unlock_state();
4873
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 return status;
4875}
4876
4877
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
Benny Halevy87df4de2008-12-15 19:42:03 +02004880static inline u64
4881end_offset(u64 start, u64 len)
4882{
4883 u64 end;
4884
4885 end = start + len;
4886 return end >= start ? end: NFS4_MAX_UINT64;
4887}
4888
4889/* last octet in a range */
4890static inline u64
4891last_byte_offset(u64 start, u64 len)
4892{
4893 u64 end;
4894
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004895 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004896 end = start + len;
4897 return end > start ? end - 1: NFS4_MAX_UINT64;
4898}
4899
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900/*
4901 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4902 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4903 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4904 * locking, this prevents us from being completely protocol-compliant. The
4905 * real solution to this problem is to start using unsigned file offsets in
4906 * the VFS, but this is a very deep change!
4907 */
4908static inline void
4909nfs4_transform_lock_offset(struct file_lock *lock)
4910{
4911 if (lock->fl_start < 0)
4912 lock->fl_start = OFFSET_MAX;
4913 if (lock->fl_end < 0)
4914 lock->fl_end = OFFSET_MAX;
4915}
4916
NeilBrownd5b90262006-04-10 22:55:22 -07004917/* Hack!: For now, we're defining this just so we can use a pointer to it
4918 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004919static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004920};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921
4922static inline void
4923nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4924{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004925 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926
NeilBrownd5b90262006-04-10 22:55:22 -07004927 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004928 lo = (struct nfs4_lockowner *) fl->fl_owner;
4929 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4930 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004931 if (!deny->ld_owner.data)
4932 /* We just don't care that much */
4933 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004934 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4935 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004936 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004937nevermind:
4938 deny->ld_owner.len = 0;
4939 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004940 deny->ld_clientid.cl_boot = 0;
4941 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 }
4943 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004944 deny->ld_length = NFS4_MAX_UINT64;
4945 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4947 deny->ld_type = NFS4_READ_LT;
4948 if (fl->fl_type != F_RDLCK)
4949 deny->ld_type = NFS4_WRITE_LT;
4950}
4951
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004952static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004953find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004954 struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004956 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004957 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958
Trond Myklebust0a880a22014-07-30 08:27:10 -04004959 lockdep_assert_held(&clp->cl_lock);
4960
Trond Myklebustd4f04892014-07-29 21:34:36 -04004961 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4962 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004963 if (so->so_is_open_owner)
4964 continue;
Trond Myklebustd4f04892014-07-29 21:34:36 -04004965 if (!same_owner_str(so, owner))
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004966 continue;
Jeff Layton5db1c032014-07-29 21:34:28 -04004967 atomic_inc(&so->so_count);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004968 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 }
4970 return NULL;
4971}
4972
Trond Myklebustc58c6612014-07-29 21:34:35 -04004973static struct nfs4_lockowner *
4974find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004975 struct nfs4_client *clp)
Trond Myklebustc58c6612014-07-29 21:34:35 -04004976{
4977 struct nfs4_lockowner *lo;
4978
Trond Myklebustd4f04892014-07-29 21:34:36 -04004979 spin_lock(&clp->cl_lock);
4980 lo = find_lockowner_str_locked(clid, owner, clp);
4981 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004982 return lo;
4983}
4984
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004985static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4986{
Trond Myklebustc58c6612014-07-29 21:34:35 -04004987 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004988}
4989
Jeff Layton6b180f02014-07-29 21:34:26 -04004990static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4991{
4992 struct nfs4_lockowner *lo = lockowner(sop);
4993
4994 kmem_cache_free(lockowner_slab, lo);
4995}
4996
4997static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004998 .so_unhash = nfs4_unhash_lockowner,
4999 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005000};
5001
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002/*
5003 * Alloc a lock owner structure.
5004 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005005 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005007 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005009static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005010alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5011 struct nfs4_ol_stateid *open_stp,
5012 struct nfsd4_lock *lock)
5013{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005014 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005016 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5017 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005019 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5020 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005021 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005022 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005023 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005024 ret = find_lockowner_str_locked(&clp->cl_clientid,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005025 &lock->lk_new_owner, clp);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005026 if (ret == NULL) {
5027 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005028 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005029 ret = lo;
5030 } else
5031 nfs4_free_lockowner(&lo->lo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005032 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005033 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034}
5035
Jeff Layton356a95e2014-07-29 21:34:13 -04005036static void
5037init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5038 struct nfs4_file *fp, struct inode *inode,
5039 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005041 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042
Jeff Layton356a95e2014-07-29 21:34:13 -04005043 lockdep_assert_held(&clp->cl_lock);
5044
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005045 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005046 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005047 stp->st_stateowner = &lo->lo_owner;
Trond Myklebuste4f1dd72014-07-29 21:34:30 -04005048 atomic_inc(&lo->lo_owner.so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07005049 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005050 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04005051 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005052 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005054 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005055 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005056 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005057 spin_lock(&fp->fi_lock);
5058 list_add(&stp->st_perfile, &fp->fi_stateids);
5059 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060}
5061
Jeff Laytonc53530d2014-06-30 11:48:39 -04005062static struct nfs4_ol_stateid *
5063find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5064{
5065 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005066 struct nfs4_client *clp = lo->lo_owner.so_client;
5067
5068 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005069
5070 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005071 if (lst->st_stid.sc_file == fp) {
5072 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005073 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005074 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005075 }
5076 return NULL;
5077}
5078
Jeff Layton356a95e2014-07-29 21:34:13 -04005079static struct nfs4_ol_stateid *
5080find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5081 struct inode *inode, struct nfs4_ol_stateid *ost,
5082 bool *new)
5083{
5084 struct nfs4_stid *ns = NULL;
5085 struct nfs4_ol_stateid *lst;
5086 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5087 struct nfs4_client *clp = oo->oo_owner.so_client;
5088
5089 spin_lock(&clp->cl_lock);
5090 lst = find_lock_stateid(lo, fi);
5091 if (lst == NULL) {
5092 spin_unlock(&clp->cl_lock);
5093 ns = nfs4_alloc_stid(clp, stateid_slab);
5094 if (ns == NULL)
5095 return NULL;
5096
5097 spin_lock(&clp->cl_lock);
5098 lst = find_lock_stateid(lo, fi);
5099 if (likely(!lst)) {
5100 lst = openlockstateid(ns);
5101 init_lock_stateid(lst, lo, fi, inode, ost);
5102 ns = NULL;
5103 *new = true;
5104 }
5105 }
5106 spin_unlock(&clp->cl_lock);
5107 if (ns)
5108 nfs4_put_stid(ns);
5109 return lst;
5110}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005111
NeilBrownfd39ca92005-06-23 22:04:03 -07005112static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113check_lock_length(u64 offset, u64 length)
5114{
Benny Halevy87df4de2008-12-15 19:42:03 +02005115 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 LOFF_OVERFLOW(offset, length)));
5117}
5118
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005119static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005120{
Trond Myklebust11b91642014-07-29 21:34:08 -04005121 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005122
Jeff Layton7214e862014-07-10 14:07:33 -04005123 lockdep_assert_held(&fp->fi_lock);
5124
Jeff Layton82c5ff12012-05-11 09:45:13 -04005125 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005126 return;
Jeff Layton12659652014-07-10 14:07:28 -04005127 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005128 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005129}
5130
Jeff Layton356a95e2014-07-29 21:34:13 -04005131static __be32
5132lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5133 struct nfs4_ol_stateid *ost,
5134 struct nfsd4_lock *lock,
5135 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005136{
Jeff Layton5db1c032014-07-29 21:34:28 -04005137 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005138 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005139 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5140 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005141 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005142 struct nfs4_lockowner *lo;
5143 unsigned int strhashval;
5144
Trond Myklebustd4f04892014-07-29 21:34:36 -04005145 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005146 if (!lo) {
Trond Myklebustd4f04892014-07-29 21:34:36 -04005147 strhashval = ownerstr_hashval(&lock->v.new.owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005148 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5149 if (lo == NULL)
5150 return nfserr_jukebox;
5151 } else {
5152 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005153 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005154 if (!cstate->minorversion &&
5155 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005156 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005157 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005158
Jeff Layton356a95e2014-07-29 21:34:13 -04005159 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005160 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005161 status = nfserr_jukebox;
5162 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005163 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005164 status = nfs_ok;
5165out:
5166 nfs4_put_stateowner(&lo->lo_owner);
5167 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005168}
5169
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170/*
5171 * LOCK operation
5172 */
Al Virob37ad282006-10-19 23:28:59 -07005173__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005174nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005175 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005177 struct nfs4_openowner *open_sop = NULL;
5178 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005179 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005180 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005181 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005182 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005183 struct file_lock *file_lock = NULL;
5184 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005185 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005186 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005187 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005188 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005189 struct net *net = SVC_NET(rqstp);
5190 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191
5192 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5193 (long long) lock->lk_offset,
5194 (long long) lock->lk_length);
5195
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 if (check_lock_length(lock->lk_offset, lock->lk_length))
5197 return nfserr_inval;
5198
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005199 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005200 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005201 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5202 return status;
5203 }
5204
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 nfs4_lock_state();
5206
5207 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005208 if (nfsd4_has_session(cstate))
5209 /* See rfc 5661 18.10.3: given clientid is ignored: */
5210 memcpy(&lock->v.new.clientid,
5211 &cstate->session->se_client->cl_clientid,
5212 sizeof(clientid_t));
5213
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005215 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005219 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 lock->lk_new_open_seqid,
5221 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005222 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005223 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005225 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005226 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005227 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005228 &lock->v.new.clientid))
5229 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005230 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005231 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005232 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005233 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005234 lock->lk_old_lock_seqid,
5235 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005236 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005237 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005238 if (status)
5239 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005240 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005242 lkflg = setlkflg(lock->lk_type);
5243 status = nfs4_check_openmode(lock_stp, lkflg);
5244 if (status)
5245 goto out;
5246
NeilBrown0dd395d2005-07-07 17:59:15 -07005247 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005248 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005249 goto out;
5250 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005251 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005252 goto out;
5253
Jeff Layton21179d82012-08-21 08:03:32 -04005254 file_lock = locks_alloc_lock();
5255 if (!file_lock) {
5256 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5257 status = nfserr_jukebox;
5258 goto out;
5259 }
5260
Trond Myklebust11b91642014-07-29 21:34:08 -04005261 fp = lock_stp->st_stid.sc_file;
Jeff Layton21179d82012-08-21 08:03:32 -04005262 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 switch (lock->lk_type) {
5264 case NFS4_READ_LT:
5265 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005266 spin_lock(&fp->fi_lock);
5267 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005268 if (filp)
5269 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005270 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005271 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005272 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 case NFS4_WRITE_LT:
5274 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005275 spin_lock(&fp->fi_lock);
5276 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005277 if (filp)
5278 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005279 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005280 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005281 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 default:
5283 status = nfserr_inval;
5284 goto out;
5285 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005286 if (!filp) {
5287 status = nfserr_openmode;
5288 goto out;
5289 }
Jeff Layton21179d82012-08-21 08:03:32 -04005290 file_lock->fl_owner = (fl_owner_t)lock_sop;
5291 file_lock->fl_pid = current->tgid;
5292 file_lock->fl_file = filp;
5293 file_lock->fl_flags = FL_POSIX;
5294 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5295 file_lock->fl_start = lock->lk_offset;
5296 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5297 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298
Jeff Layton21179d82012-08-21 08:03:32 -04005299 conflock = locks_alloc_lock();
5300 if (!conflock) {
5301 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5302 status = nfserr_jukebox;
5303 goto out;
5304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305
Jeff Layton21179d82012-08-21 08:03:32 -04005306 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005307 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005309 update_stateid(&lock_stp->st_stid.sc_stateid);
5310 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005312 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005313 break;
5314 case (EAGAIN): /* conflock holds conflicting lock */
5315 status = nfserr_denied;
5316 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005317 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005318 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 case (EDEADLK):
5320 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005321 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005322 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005323 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005324 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005325 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327out:
Trond Myklebustde186432014-07-10 14:07:26 -04005328 if (filp)
5329 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005330 if (lock_stp) {
5331 /* Bump seqid manually if the 4.0 replay owner is openowner */
5332 if (cstate->replay_owner &&
5333 cstate->replay_owner != &lock_sop->lo_owner &&
5334 seqid_mutating_err(ntohl(status)))
5335 lock_sop->lo_owner.so_seqid++;
5336
5337 /*
5338 * If this is a new, never-before-used stateid, and we are
5339 * returning an error, then just go ahead and release it.
5340 */
5341 if (status && new)
5342 release_lock_stateid(lock_stp);
5343
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005344 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005345 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005346 if (open_stp)
5347 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005348 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005349 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005350 if (file_lock)
5351 locks_free_lock(file_lock);
5352 if (conflock)
5353 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 return status;
5355}
5356
5357/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005358 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5359 * so we do a temporary open here just to get an open file to pass to
5360 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5361 * inode operation.)
5362 */
Al Viro04da6e92012-04-13 00:00:04 -04005363static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005364{
5365 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005366 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5367 if (!err) {
5368 err = nfserrno(vfs_test_lock(file, lock));
5369 nfsd_close(file);
5370 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005371 return err;
5372}
5373
5374/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 * LOCKT operation
5376 */
Al Virob37ad282006-10-19 23:28:59 -07005377__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005378nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5379 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380{
Jeff Layton21179d82012-08-21 08:03:32 -04005381 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005382 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005383 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005384 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005386 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 return nfserr_grace;
5388
5389 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5390 return nfserr_inval;
5391
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 nfs4_lock_state();
5393
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005394 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005395 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005396 if (status)
5397 goto out;
5398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005400 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402
Jeff Layton21179d82012-08-21 08:03:32 -04005403 file_lock = locks_alloc_lock();
5404 if (!file_lock) {
5405 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5406 status = nfserr_jukebox;
5407 goto out;
5408 }
5409 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 switch (lockt->lt_type) {
5411 case NFS4_READ_LT:
5412 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005413 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 break;
5415 case NFS4_WRITE_LT:
5416 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005417 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 break;
5419 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005420 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 status = nfserr_inval;
5422 goto out;
5423 }
5424
Trond Myklebustd4f04892014-07-29 21:34:36 -04005425 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5426 cstate->clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005427 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005428 file_lock->fl_owner = (fl_owner_t)lo;
5429 file_lock->fl_pid = current->tgid;
5430 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431
Jeff Layton21179d82012-08-21 08:03:32 -04005432 file_lock->fl_start = lockt->lt_offset;
5433 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434
Jeff Layton21179d82012-08-21 08:03:32 -04005435 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436
Jeff Layton21179d82012-08-21 08:03:32 -04005437 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005438 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005439 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005440
Jeff Layton21179d82012-08-21 08:03:32 -04005441 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005443 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 }
5445out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005446 if (lo)
5447 nfs4_put_stateowner(&lo->lo_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005449 if (file_lock)
5450 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 return status;
5452}
5453
Al Virob37ad282006-10-19 23:28:59 -07005454__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005455nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005456 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005458 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005460 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005461 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005462 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005463 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5464
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5466 (long long) locku->lu_offset,
5467 (long long) locku->lu_length);
5468
5469 if (check_lock_length(locku->lu_offset, locku->lu_length))
5470 return nfserr_inval;
5471
5472 nfs4_lock_state();
5473
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005474 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005475 &locku->lu_stateid, NFS4_LOCK_STID,
5476 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005477 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005479 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005480 if (!filp) {
5481 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005482 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005483 }
Jeff Layton21179d82012-08-21 08:03:32 -04005484 file_lock = locks_alloc_lock();
5485 if (!file_lock) {
5486 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5487 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005488 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005489 }
5490 locks_init_lock(file_lock);
5491 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005492 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005493 file_lock->fl_pid = current->tgid;
5494 file_lock->fl_file = filp;
5495 file_lock->fl_flags = FL_POSIX;
5496 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5497 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498
Jeff Layton21179d82012-08-21 08:03:32 -04005499 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5500 locku->lu_length);
5501 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502
Jeff Layton21179d82012-08-21 08:03:32 -04005503 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005504 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005505 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 goto out_nfserr;
5507 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005508 update_stateid(&stp->st_stid.sc_stateid);
5509 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005510fput:
5511 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005512put_stateid:
5513 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005515 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005516 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005517 if (file_lock)
5518 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 return status;
5520
5521out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005522 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005523 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524}
5525
5526/*
5527 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005528 * true: locks held by lockowner
5529 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005531static bool
5532check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533{
5534 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005535 int status = false;
5536 struct file *filp = find_any_file(fp);
5537 struct inode *inode;
5538
5539 if (!filp) {
5540 /* Any valid lock stateid should have some sort of access */
5541 WARN_ON_ONCE(1);
5542 return status;
5543 }
5544
5545 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546
Jeff Layton1c8c6012013-06-21 08:58:15 -04005547 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005549 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005550 status = true;
5551 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005554 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005555 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 return status;
5557}
5558
Al Virob37ad282006-10-19 23:28:59 -07005559__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005560nfsd4_release_lockowner(struct svc_rqst *rqstp,
5561 struct nfsd4_compound_state *cstate,
5562 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563{
5564 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005565 struct nfs4_stateowner *sop;
5566 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005567 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005569 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005570 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005571 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005572 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573
5574 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5575 clid->cl_boot, clid->cl_id);
5576
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 nfs4_lock_state();
5578
Jeff Layton4b24ca72014-06-30 11:48:44 -04005579 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005580 if (status)
5581 goto out;
5582
Trond Myklebustd4f04892014-07-29 21:34:36 -04005583 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005584 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005585 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005586 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005587 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005588
5589 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005590 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005591
Jeff Layton882e9d22014-07-29 21:34:37 -04005592 /* see if there are still any locks associated with it */
5593 lo = lockowner(sop);
5594 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5595 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5596 status = nfserr_locks_held;
5597 spin_unlock(&clp->cl_lock);
5598 goto out;
5599 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005600 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005601
5602 atomic_inc(&sop->so_count);
5603 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005604 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005605 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005606 if (lo)
5607 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608out:
5609 nfs4_unlock_state();
5610 return status;
5611}
5612
5613static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005614alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615{
NeilBrowna55370a2005-06-23 22:03:52 -07005616 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617}
5618
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005619bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005620nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005621{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005622 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005623
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005624 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005625 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005626}
5627
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628/*
5629 * failure => all reset bets are off, nfserr_no_grace...
5630 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005631struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005632nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633{
5634 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005635 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636
NeilBrowna55370a2005-06-23 22:03:52 -07005637 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5638 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005639 if (crp) {
5640 strhashval = clientstr_hashval(name);
5641 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005642 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005643 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005644 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005645 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005646 }
5647 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648}
5649
Jeff Layton2a4317c2012-03-21 16:42:43 -04005650void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005651nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005652{
5653 list_del(&crp->cr_strhash);
5654 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005655 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005656}
5657
5658void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005659nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660{
5661 struct nfs4_client_reclaim *crp = NULL;
5662 int i;
5663
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005665 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5666 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005668 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 }
5670 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005671 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672}
5673
5674/*
5675 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005676struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005677nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678{
5679 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 struct nfs4_client_reclaim *crp = NULL;
5681
Jeff Layton278c9312012-11-12 15:00:52 -05005682 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683
Jeff Layton278c9312012-11-12 15:00:52 -05005684 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005685 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005686 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 return crp;
5688 }
5689 }
5690 return NULL;
5691}
5692
5693/*
5694* Called from OPEN. Look for clientid in reclaim list.
5695*/
Al Virob37ad282006-10-19 23:28:59 -07005696__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005697nfs4_check_open_reclaim(clientid_t *clid,
5698 struct nfsd4_compound_state *cstate,
5699 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005701 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005702
5703 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005704 status = lookup_clientid(clid, cstate, nn);
5705 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005706 return nfserr_reclaim_bad;
5707
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005708 if (nfsd4_client_record_check(cstate->clp))
5709 return nfserr_reclaim_bad;
5710
5711 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712}
5713
Bryan Schumaker65178db2011-11-01 13:35:21 -04005714#ifdef CONFIG_NFSD_FAULT_INJECTION
5715
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005716u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5717{
Jeff Laytond20c11d2014-07-30 08:27:07 -04005718 __be32 ret;
5719 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
5720
5721 spin_lock(&nn->client_lock);
5722 ret = mark_client_expired_locked(clp);
5723 spin_unlock(&nn->client_lock);
5724 if (ret != nfs_ok)
J. Bruce Fields221a6872013-04-01 22:23:49 -04005725 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005726 expire_client(clp);
5727 return 1;
5728}
5729
Bryan Schumaker184c1842012-11-29 11:40:44 -05005730u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5731{
5732 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005733 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005734 printk(KERN_INFO "NFS Client: %s\n", buf);
5735 return 1;
5736}
5737
5738static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5739 const char *type)
5740{
5741 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005742 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005743 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5744}
5745
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005746static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5747 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005748{
5749 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005750 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005751 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005752 u64 count = 0;
5753
5754 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005755 list_for_each_entry_safe(stp, st_next,
5756 &oop->oo_owner.so_stateids, st_perstateowner) {
5757 list_for_each_entry_safe(lst, lst_next,
5758 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005759 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005760 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005761 if (++count == max)
5762 return count;
5763 }
5764 }
5765 }
5766
5767 return count;
5768}
5769
5770u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5771{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005772 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005773}
5774
Bryan Schumaker184c1842012-11-29 11:40:44 -05005775u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5776{
5777 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5778 nfsd_print_count(clp, count, "locked files");
5779 return count;
5780}
5781
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005782static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5783{
5784 struct nfs4_openowner *oop, *next;
5785 u64 count = 0;
5786
5787 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5788 if (func)
5789 func(oop);
5790 if (++count == max)
5791 break;
5792 }
5793
5794 return count;
5795}
5796
5797u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5798{
5799 return nfsd_foreach_client_open(clp, max, release_openowner);
5800}
5801
Bryan Schumaker184c1842012-11-29 11:40:44 -05005802u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5803{
5804 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5805 nfsd_print_count(clp, count, "open files");
5806 return count;
5807}
5808
Bryan Schumaker269de302012-11-29 11:40:42 -05005809static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5810 struct list_head *victims)
5811{
5812 struct nfs4_delegation *dp, *next;
5813 u64 count = 0;
5814
Benny Halevycdc97502014-05-30 09:09:30 -04005815 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005816 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005817 if (victims) {
5818 /*
5819 * It's not safe to mess with delegations that have a
5820 * non-zero dl_time. They might have already been broken
5821 * and could be processed by the laundromat outside of
5822 * the state_lock. Just leave them be.
5823 */
5824 if (dp->dl_time != 0)
5825 continue;
5826
Jeff Layton42690672014-07-25 07:34:20 -04005827 unhash_delegation_locked(dp);
5828 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005829 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005830 if (++count == max)
5831 break;
5832 }
5833 return count;
5834}
5835
5836u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5837{
5838 struct nfs4_delegation *dp, *next;
5839 LIST_HEAD(victims);
5840 u64 count;
5841
Benny Halevycdc97502014-05-30 09:09:30 -04005842 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005843 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005844 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005845
Jeff Layton2d4a5322014-07-25 07:34:21 -04005846 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5847 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005848 revoke_delegation(dp);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005849 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005850
5851 return count;
5852}
5853
5854u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5855{
Jeff Laytondff13992014-07-08 14:02:49 -04005856 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005857 LIST_HEAD(victims);
5858 u64 count;
5859
Benny Halevycdc97502014-05-30 09:09:30 -04005860 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005861 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005862 while (!list_empty(&victims)) {
5863 dp = list_first_entry(&victims, struct nfs4_delegation,
5864 dl_recall_lru);
5865 list_del_init(&dp->dl_recall_lru);
5866 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005867 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005868 }
Benny Halevycdc97502014-05-30 09:09:30 -04005869 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005870
5871 return count;
5872}
5873
Bryan Schumaker184c1842012-11-29 11:40:44 -05005874u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5875{
5876 u64 count = 0;
5877
Benny Halevycdc97502014-05-30 09:09:30 -04005878 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005879 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005880 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005881
5882 nfsd_print_count(clp, count, "delegations");
5883 return count;
5884}
5885
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005886u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005887{
5888 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005889 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005890 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005891
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005892 if (!nfsd_netns_ready(nn))
5893 return 0;
5894
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005895 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005896 count += func(clp, max - count);
5897 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005898 break;
5899 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005900
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005901 return count;
5902}
5903
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005904struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5905{
5906 struct nfs4_client *clp;
5907 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5908
5909 if (!nfsd_netns_ready(nn))
5910 return NULL;
5911
5912 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5913 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5914 return clp;
5915 }
5916 return NULL;
5917}
5918
Bryan Schumaker65178db2011-11-01 13:35:21 -04005919#endif /* CONFIG_NFSD_FAULT_INJECTION */
5920
Meelap Shahc2f1a552007-07-17 04:04:39 -07005921/*
5922 * Since the lifetime of a delegation isn't limited to that of an open, a
5923 * client may quite reasonably hang on to a delegation as long as it has
5924 * the inode cached. This becomes an obvious problem the first time a
5925 * client's inode cache approaches the size of the server's total memory.
5926 *
5927 * For now we avoid this problem by imposing a hard limit on the number
5928 * of delegations, which varies according to the server's memory size.
5929 */
5930static void
5931set_max_delegations(void)
5932{
5933 /*
5934 * Allow at most 4 delegations per megabyte of RAM. Quick
5935 * estimates suggest that in the worst case (where every delegation
5936 * is for a different inode), a delegation could take about 1.5K,
5937 * giving a worst case usage of about 6% of memory.
5938 */
5939 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5940}
5941
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005942static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005943{
5944 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5945 int i;
5946
5947 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5948 CLIENT_HASH_SIZE, GFP_KERNEL);
5949 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005950 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005951 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5952 CLIENT_HASH_SIZE, GFP_KERNEL);
5953 if (!nn->unconf_id_hashtbl)
5954 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005955 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5956 SESSION_HASH_SIZE, GFP_KERNEL);
5957 if (!nn->sessionid_hashtbl)
5958 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005959
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005960 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005961 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005962 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005963 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005964 for (i = 0; i < SESSION_HASH_SIZE; i++)
5965 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005966 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005967 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005968 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005969 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005970 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005971 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005972
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005973 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005974 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005975
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005976 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005977
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005978err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005979 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005980err_unconf_id:
5981 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005982err:
5983 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005984}
5985
5986static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005987nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005988{
5989 int i;
5990 struct nfs4_client *clp = NULL;
5991 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5992
5993 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5994 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5995 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5996 destroy_client(clp);
5997 }
5998 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005999
Kinglong Mee2b905632014-03-26 22:09:30 +08006000 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6001 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6002 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6003 destroy_client(clp);
6004 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006005 }
6006
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006007 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006008 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006009 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006010 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006011}
6012
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006013int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006014nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07006015{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006016 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006017 int ret;
6018
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006019 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006020 if (ret)
6021 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006022 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006023 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006024 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04006025 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006026 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03006027 nn->nfsd4_grace, net);
6028 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006029 return 0;
6030}
6031
6032/* initialization to perform when the nfsd service is started: */
6033
6034int
6035nfs4_state_start(void)
6036{
6037 int ret;
6038
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006039 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006040 if (ret)
6041 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07006042 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05006043 if (laundry_wq == NULL) {
6044 ret = -ENOMEM;
6045 goto out_recovery;
6046 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006047 ret = nfsd4_create_callback_queue();
6048 if (ret)
6049 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006050
Meelap Shahc2f1a552007-07-17 04:04:39 -07006051 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006052
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006053 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006054
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006055out_free_laundry:
6056 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05006057out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006058 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059}
6060
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006061void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006062nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006066 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006068 cancel_delayed_work_sync(&nn->laundromat_work);
6069 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05006070
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04006071 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04006073 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006074 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006076 unhash_delegation_locked(dp);
6077 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 }
Benny Halevycdc97502014-05-30 09:09:30 -04006079 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 list_for_each_safe(pos, next, &reaplist) {
6081 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006082 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04006083 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 }
6085
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006086 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006087 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04006088 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089}
6090
6091void
6092nfs4_state_shutdown(void)
6093{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006094 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006095 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006097
6098static void
6099get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6100{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006101 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6102 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006103}
6104
6105static void
6106put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6107{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006108 if (cstate->minorversion) {
6109 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6110 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6111 }
6112}
6113
6114void
6115clear_current_stateid(struct nfsd4_compound_state *cstate)
6116{
6117 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006118}
6119
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006120/*
6121 * functions to set current state id
6122 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006123void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006124nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6125{
6126 put_stateid(cstate, &odp->od_stateid);
6127}
6128
6129void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006130nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6131{
6132 put_stateid(cstate, &open->op_stateid);
6133}
6134
6135void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006136nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6137{
6138 put_stateid(cstate, &close->cl_stateid);
6139}
6140
6141void
6142nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6143{
6144 put_stateid(cstate, &lock->lk_resp_stateid);
6145}
6146
6147/*
6148 * functions to consume current state id
6149 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006150
6151void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006152nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6153{
6154 get_stateid(cstate, &odp->od_stateid);
6155}
6156
6157void
6158nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6159{
6160 get_stateid(cstate, &drp->dr_stateid);
6161}
6162
6163void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006164nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6165{
6166 get_stateid(cstate, &fsp->fr_stateid);
6167}
6168
6169void
6170nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6171{
6172 get_stateid(cstate, &setattr->sa_stateid);
6173}
6174
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006175void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006176nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6177{
6178 get_stateid(cstate, &close->cl_stateid);
6179}
6180
6181void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006182nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006183{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006184 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006185}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006186
6187void
6188nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6189{
6190 get_stateid(cstate, &read->rd_stateid);
6191}
6192
6193void
6194nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6195{
6196 get_stateid(cstate, &write->wr_stateid);
6197}