blob: bb37cc4dd57397a4abbe6460b0cb4cfc9a488c33 [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
Christoph Hellwigabf11352014-05-21 07:43:03 -070088static struct kmem_cache *openowner_slab;
89static struct kmem_cache *lockowner_slab;
90static struct kmem_cache *file_slab;
91static struct kmem_cache *stateid_slab;
92static struct kmem_cache *deleg_slab;
NeilBrowne60d4392005-06-23 22:03:01 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094void
95nfs4_lock_state(void)
96{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080097 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400100static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800101
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400102static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800103{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400104 return ses->se_flags & NFS4_SESSION_DEAD;
105}
106
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400107static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
108{
109 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400110 return nfserr_jukebox;
111 ses->se_flags |= NFS4_SESSION_DEAD;
112 return nfs_ok;
113}
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115void
116nfs4_unlock_state(void)
117{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800118 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
J. Bruce Fields221a6872013-04-01 22:23:49 -0400121static bool is_client_expired(struct nfs4_client *clp)
122{
123 return clp->cl_time == 0;
124}
125
126static __be32 mark_client_expired_locked(struct nfs4_client *clp)
127{
128 if (atomic_read(&clp->cl_refcount))
129 return nfserr_jukebox;
130 clp->cl_time = 0;
131 return nfs_ok;
132}
133
134static __be32 mark_client_expired(struct nfs4_client *clp)
135{
136 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
137 __be32 ret;
138
139 spin_lock(&nn->client_lock);
140 ret = mark_client_expired_locked(clp);
141 spin_unlock(&nn->client_lock);
142 return ret;
143}
144
145static __be32 get_client_locked(struct nfs4_client *clp)
146{
147 if (is_client_expired(clp))
148 return nfserr_expired;
149 atomic_inc(&clp->cl_refcount);
150 return nfs_ok;
151}
152
153/* must be called under the client_lock */
154static inline void
155renew_client_locked(struct nfs4_client *clp)
156{
157 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
158
159 if (is_client_expired(clp)) {
160 WARN_ON(1);
161 printk("%s: client (clientid %08x/%08x) already expired\n",
162 __func__,
163 clp->cl_clientid.cl_boot,
164 clp->cl_clientid.cl_id);
165 return;
166 }
167
168 dprintk("renewing client (clientid %08x/%08x)\n",
169 clp->cl_clientid.cl_boot,
170 clp->cl_clientid.cl_id);
171 list_move_tail(&clp->cl_lru, &nn->client_lru);
172 clp->cl_time = get_seconds();
173}
174
175static inline void
176renew_client(struct nfs4_client *clp)
177{
178 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
179
180 spin_lock(&nn->client_lock);
181 renew_client_locked(clp);
182 spin_unlock(&nn->client_lock);
183}
184
Fengguang Wuba138432013-04-16 22:14:15 -0400185static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400186{
187 if (!atomic_dec_and_test(&clp->cl_refcount))
188 return;
189 if (!is_client_expired(clp))
190 renew_client_locked(clp);
191}
192
Jeff Layton4b24ca72014-06-30 11:48:44 -0400193static void put_client_renew(struct nfs4_client *clp)
194{
195 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
196
Jeff Laytond6c249b2014-07-08 14:02:50 -0400197 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
198 return;
199 if (!is_client_expired(clp))
200 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400201 spin_unlock(&nn->client_lock);
202}
203
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400204static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
205{
206 __be32 status;
207
208 if (is_session_dead(ses))
209 return nfserr_badsession;
210 status = get_client_locked(ses->se_client);
211 if (status)
212 return status;
213 atomic_inc(&ses->se_ref);
214 return nfs_ok;
215}
216
217static void nfsd4_put_session_locked(struct nfsd4_session *ses)
218{
219 struct nfs4_client *clp = ses->se_client;
220
221 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
222 free_session(ses);
223 put_client_renew_locked(clp);
224}
225
226static void nfsd4_put_session(struct nfsd4_session *ses)
227{
228 struct nfs4_client *clp = ses->se_client;
229 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
230
231 spin_lock(&nn->client_lock);
232 nfsd4_put_session_locked(ses);
233 spin_unlock(&nn->client_lock);
234}
235
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237static inline u32
238opaque_hashval(const void *ptr, int nbytes)
239{
240 unsigned char *cptr = (unsigned char *) ptr;
241
242 u32 x = 0;
243 while (nbytes--) {
244 x *= 37;
245 x += *cptr++;
246 }
247 return x;
248}
249
J. Bruce Fields32513b42011-10-13 16:00:16 -0400250static void nfsd4_free_file(struct nfs4_file *f)
251{
252 kmem_cache_free(file_slab, f);
253}
254
NeilBrown13cd2182005-06-23 22:03:10 -0700255static inline void
256put_nfs4_file(struct nfs4_file *fi)
257{
Jeff Layton02e12152014-07-16 10:31:57 -0400258 might_lock(&state_lock);
259
Benny Halevycdc97502014-05-30 09:09:30 -0400260 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton89876f82013-04-02 09:01:59 -0400261 hlist_del(&fi->fi_hash);
Benny Halevycdc97502014-05-30 09:09:30 -0400262 spin_unlock(&state_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400263 nfsd4_free_file(fi);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800264 }
NeilBrown13cd2182005-06-23 22:03:10 -0700265}
266
267static inline void
268get_nfs4_file(struct nfs4_file *fi)
269{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800270 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700271}
272
Trond Myklebustde186432014-07-10 14:07:26 -0400273static struct file *
274__nfs4_get_fd(struct nfs4_file *f, int oflag)
275{
276 if (f->fi_fds[oflag])
277 return get_file(f->fi_fds[oflag]);
278 return NULL;
279}
280
281static struct file *
282find_writeable_file_locked(struct nfs4_file *f)
283{
284 struct file *ret;
285
286 lockdep_assert_held(&f->fi_lock);
287
288 ret = __nfs4_get_fd(f, O_WRONLY);
289 if (!ret)
290 ret = __nfs4_get_fd(f, O_RDWR);
291 return ret;
292}
293
294static struct file *
295find_writeable_file(struct nfs4_file *f)
296{
297 struct file *ret;
298
299 spin_lock(&f->fi_lock);
300 ret = find_writeable_file_locked(f);
301 spin_unlock(&f->fi_lock);
302
303 return ret;
304}
305
306static struct file *find_readable_file_locked(struct nfs4_file *f)
307{
308 struct file *ret;
309
310 lockdep_assert_held(&f->fi_lock);
311
312 ret = __nfs4_get_fd(f, O_RDONLY);
313 if (!ret)
314 ret = __nfs4_get_fd(f, O_RDWR);
315 return ret;
316}
317
318static struct file *
319find_readable_file(struct nfs4_file *f)
320{
321 struct file *ret;
322
323 spin_lock(&f->fi_lock);
324 ret = find_readable_file_locked(f);
325 spin_unlock(&f->fi_lock);
326
327 return ret;
328}
329
330static struct file *
331find_any_file(struct nfs4_file *f)
332{
333 struct file *ret;
334
335 spin_lock(&f->fi_lock);
336 ret = __nfs4_get_fd(f, O_RDWR);
337 if (!ret) {
338 ret = __nfs4_get_fd(f, O_WRONLY);
339 if (!ret)
340 ret = __nfs4_get_fd(f, O_RDONLY);
341 }
342 spin_unlock(&f->fi_lock);
343 return ret;
344}
345
Trond Myklebust02a35082014-07-25 07:34:22 -0400346static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800347unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700348
349/*
350 * Open owner state (share locks)
351 */
352
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500353/* hash tables for lock and open owners */
354#define OWNER_HASH_BITS 8
355#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
356#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700357
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500358static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400359{
360 unsigned int ret;
361
362 ret = opaque_hashval(ownername->data, ownername->len);
363 ret += clientid;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500364 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400365}
NeilBrownef0f3392006-04-10 22:55:41 -0700366
NeilBrownef0f3392006-04-10 22:55:41 -0700367/* hash table for nfs4_file */
368#define FILE_HASH_BITS 8
369#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800370
Trond Myklebustca943212014-07-23 16:17:39 -0400371static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400372{
Trond Myklebustca943212014-07-23 16:17:39 -0400373 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
374}
375
376static unsigned int file_hashval(struct knfsd_fh *fh)
377{
378 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
379}
380
381static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
382{
383 return fh1->fh_size == fh2->fh_size &&
384 !memcmp(fh1->fh_base.fh_pad,
385 fh2->fh_base.fh_pad,
386 fh1->fh_size);
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400387}
388
Jeff Layton89876f82013-04-02 09:01:59 -0400389static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700390
Jeff Layton12659652014-07-10 14:07:28 -0400391static void
392__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400393{
Jeff Layton7214e862014-07-10 14:07:33 -0400394 lockdep_assert_held(&fp->fi_lock);
395
Jeff Layton12659652014-07-10 14:07:28 -0400396 if (access & NFS4_SHARE_ACCESS_WRITE)
397 atomic_inc(&fp->fi_access[O_WRONLY]);
398 if (access & NFS4_SHARE_ACCESS_READ)
399 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400400}
401
Jeff Layton12659652014-07-10 14:07:28 -0400402static __be32
403nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400404{
Jeff Layton7214e862014-07-10 14:07:33 -0400405 lockdep_assert_held(&fp->fi_lock);
406
Jeff Layton12659652014-07-10 14:07:28 -0400407 /* Does this access mode make sense? */
408 if (access & ~NFS4_SHARE_ACCESS_BOTH)
409 return nfserr_inval;
410
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400411 /* Does it conflict with a deny mode already set? */
412 if ((access & fp->fi_share_deny) != 0)
413 return nfserr_share_denied;
414
Jeff Layton12659652014-07-10 14:07:28 -0400415 __nfs4_file_get_access(fp, access);
416 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400417}
418
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400419static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
420{
421 /* Common case is that there is no deny mode. */
422 if (deny) {
423 /* Does this deny mode make sense? */
424 if (deny & ~NFS4_SHARE_DENY_BOTH)
425 return nfserr_inval;
426
427 if ((deny & NFS4_SHARE_DENY_READ) &&
428 atomic_read(&fp->fi_access[O_RDONLY]))
429 return nfserr_share_denied;
430
431 if ((deny & NFS4_SHARE_DENY_WRITE) &&
432 atomic_read(&fp->fi_access[O_WRONLY]))
433 return nfserr_share_denied;
434 }
435 return nfs_ok;
436}
437
J. Bruce Fields998db522010-08-07 09:21:41 -0400438static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400439{
Trond Myklebustde186432014-07-10 14:07:26 -0400440 might_lock(&fp->fi_lock);
441
442 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
443 struct file *f1 = NULL;
444 struct file *f2 = NULL;
445
Jeff Layton6d338b52014-07-10 14:07:29 -0400446 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400447 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400448 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400449 spin_unlock(&fp->fi_lock);
450 if (f1)
451 fput(f1);
452 if (f2)
453 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400454 }
455}
456
Jeff Layton12659652014-07-10 14:07:28 -0400457static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400458{
Jeff Layton12659652014-07-10 14:07:28 -0400459 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
460
461 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400462 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400463 if (access & NFS4_SHARE_ACCESS_READ)
464 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400465}
466
Trond Myklebust60116952014-07-29 21:34:06 -0400467static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
468 struct kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400469{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500470 struct nfs4_stid *stid;
471 int new_id;
472
Trond Myklebustf8338832014-07-25 07:34:19 -0400473 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500474 if (!stid)
475 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400476
Jeff Layton4770d722014-07-29 21:34:10 -0400477 idr_preload(GFP_KERNEL);
478 spin_lock(&cl->cl_lock);
479 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
480 spin_unlock(&cl->cl_lock);
481 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700482 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500483 goto out_free;
484 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500485 stid->sc_stateid.si_opaque.so_id = new_id;
486 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
487 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400488 atomic_set(&stid->sc_count, 1);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500489
J. Bruce Fields996e0932011-10-17 11:14:48 -0400490 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500491 * It shouldn't be a problem to reuse an opaque stateid value.
492 * I don't think it is for 4.1. But with 4.0 I worry that, for
493 * example, a stray write retransmission could be accepted by
494 * the server when it should have been rejected. Therefore,
495 * adopt a trick from the sctp code to attempt to maximize the
496 * amount of time until an id is reused, by ensuring they always
497 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400498 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500499 return stid;
500out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800501 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500502 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400503}
504
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400505static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
506{
Trond Myklebust60116952014-07-29 21:34:06 -0400507 struct nfs4_stid *stid;
508 struct nfs4_ol_stateid *stp;
509
510 stid = nfs4_alloc_stid(clp, stateid_slab);
511 if (!stid)
512 return NULL;
513
514 stp = openlockstateid(stid);
515 stp->st_stid.sc_free = nfs4_free_ol_stateid;
516 return stp;
517}
518
519static void nfs4_free_deleg(struct nfs4_stid *stid)
520{
Trond Myklebust60116952014-07-29 21:34:06 -0400521 kmem_cache_free(deleg_slab, stid);
522 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400523}
524
NeilBrown6282cd52014-06-04 17:39:26 +1000525/*
526 * When we recall a delegation, we should be careful not to hand it
527 * out again straight away.
528 * To ensure this we keep a pair of bloom filters ('new' and 'old')
529 * in which the filehandles of recalled delegations are "stored".
530 * If a filehandle appear in either filter, a delegation is blocked.
531 * When a delegation is recalled, the filehandle is stored in the "new"
532 * filter.
533 * Every 30 seconds we swap the filters and clear the "new" one,
534 * unless both are empty of course.
535 *
536 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
537 * low 3 bytes as hash-table indices.
538 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400539 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000540 * is used to manage concurrent access. Testing does not need the lock
541 * except when swapping the two filters.
542 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400543static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000544static struct bloom_pair {
545 int entries, old_entries;
546 time_t swap_time;
547 int new; /* index into 'set' */
548 DECLARE_BITMAP(set[2], 256);
549} blocked_delegations;
550
551static int delegation_blocked(struct knfsd_fh *fh)
552{
553 u32 hash;
554 struct bloom_pair *bd = &blocked_delegations;
555
556 if (bd->entries == 0)
557 return 0;
558 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400559 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000560 if (seconds_since_boot() - bd->swap_time > 30) {
561 bd->entries -= bd->old_entries;
562 bd->old_entries = bd->entries;
563 memset(bd->set[bd->new], 0,
564 sizeof(bd->set[0]));
565 bd->new = 1-bd->new;
566 bd->swap_time = seconds_since_boot();
567 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400568 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000569 }
570 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
571 if (test_bit(hash&255, bd->set[0]) &&
572 test_bit((hash>>8)&255, bd->set[0]) &&
573 test_bit((hash>>16)&255, bd->set[0]))
574 return 1;
575
576 if (test_bit(hash&255, bd->set[1]) &&
577 test_bit((hash>>8)&255, bd->set[1]) &&
578 test_bit((hash>>16)&255, bd->set[1]))
579 return 1;
580
581 return 0;
582}
583
584static void block_delegations(struct knfsd_fh *fh)
585{
586 u32 hash;
587 struct bloom_pair *bd = &blocked_delegations;
588
589 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
590
Jeff Laytonf54fe962014-07-25 07:34:26 -0400591 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000592 __set_bit(hash&255, bd->set[bd->new]);
593 __set_bit((hash>>8)&255, bd->set[bd->new]);
594 __set_bit((hash>>16)&255, bd->set[bd->new]);
595 if (bd->entries == 0)
596 bd->swap_time = seconds_since_boot();
597 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400598 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601static struct nfs4_delegation *
Jeff Laytonf9416e22014-07-25 07:34:23 -0400602alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400605 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400608 n = atomic_long_inc_return(&num_delegations);
609 if (n < 0 || n > max_delegations)
610 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000611 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400612 goto out_dec;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400613 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700614 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400615 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400616
617 dp->dl_stid.sc_free = nfs4_free_deleg;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400618 /*
619 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400620 * meaning of seqid 0 isn't meaningful, really, but let's avoid
621 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400622 */
623 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700624 INIT_LIST_HEAD(&dp->dl_perfile);
625 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 INIT_LIST_HEAD(&dp->dl_recall_lru);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400627 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Jeff Layton02e12152014-07-16 10:31:57 -0400628 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400630out_dec:
631 atomic_long_dec(&num_delegations);
632 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
635void
Trond Myklebust60116952014-07-29 21:34:06 -0400636nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Trond Myklebust11b91642014-07-29 21:34:08 -0400638 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400639 struct nfs4_client *clp = s->sc_client;
640
Jeff Layton4770d722014-07-29 21:34:10 -0400641 might_lock(&clp->cl_lock);
642
643 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock))
Trond Myklebust60116952014-07-29 21:34:06 -0400644 return;
645 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400646 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400647 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400648 if (fp)
649 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500652static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Jeff Layton417c6622014-07-21 09:34:57 -0400654 lockdep_assert_held(&state_lock);
655
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -0500656 if (!fp->fi_lease)
657 return;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500658 if (atomic_dec_and_test(&fp->fi_delegees)) {
659 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
660 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400661 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500662 fp->fi_deleg_file = NULL;
663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400666static void unhash_stid(struct nfs4_stid *s)
667{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500668 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400669}
670
Benny Halevy931ee562014-05-30 09:09:27 -0400671static void
672hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
673{
Benny Halevycdc97502014-05-30 09:09:30 -0400674 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400675 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400676
Benny Halevy3fb87d12014-05-30 09:09:31 -0400677 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400678 list_add(&dp->dl_perfile, &fp->fi_delegations);
679 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
680}
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682static void
Jeff Layton42690672014-07-25 07:34:20 -0400683unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Trond Myklebust11b91642014-07-29 21:34:08 -0400685 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400686
Jeff Layton42690672014-07-25 07:34:20 -0400687 lockdep_assert_held(&state_lock);
688
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400689 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400690 /* Ensure that deleg break won't try to requeue it */
691 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400692 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400693 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400695 list_del_init(&dp->dl_perfile);
696 spin_unlock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400697 if (fp)
Trond Myklebustf8338832014-07-25 07:34:19 -0400698 nfs4_put_deleg_lease(fp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400699}
700
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400701static void destroy_delegation(struct nfs4_delegation *dp)
702{
Jeff Layton42690672014-07-25 07:34:20 -0400703 spin_lock(&state_lock);
704 unhash_delegation_locked(dp);
705 spin_unlock(&state_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400706 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400707}
708
709static void revoke_delegation(struct nfs4_delegation *dp)
710{
711 struct nfs4_client *clp = dp->dl_stid.sc_client;
712
Jeff Layton2d4a5322014-07-25 07:34:21 -0400713 WARN_ON(!list_empty(&dp->dl_recall_lru));
714
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400715 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400716 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400717 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400718 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400719 spin_lock(&clp->cl_lock);
720 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
721 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400722 }
723}
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725/*
726 * SETCLIENTID state
727 */
728
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400729static unsigned int clientid_hashval(u32 id)
730{
731 return id & CLIENT_HASH_MASK;
732}
733
734static unsigned int clientstr_hashval(const char *name)
735{
736 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
737}
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400740 * We store the NONE, READ, WRITE, and BOTH bits separately in the
741 * st_{access,deny}_bmap field of the stateid, in order to track not
742 * only what share bits are currently in force, but also what
743 * combinations of share bits previous opens have used. This allows us
744 * to enforce the recommendation of rfc 3530 14.2.19 that the server
745 * return an error if the client attempt to downgrade to a combination
746 * of share bits not explicable by closing some of its previous opens.
747 *
748 * XXX: This enforcement is actually incomplete, since we don't keep
749 * track of access/deny bit combinations; so, e.g., we allow:
750 *
751 * OPEN allow read, deny write
752 * OPEN allow both, deny none
753 * DOWNGRADE allow read, deny none
754 *
755 * which we should reject.
756 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400757static unsigned int
758bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400759 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400760 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400761
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400762 for (i = 1; i < 4; i++) {
763 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400764 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400765 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400766 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400767}
768
Jeff Layton82c5ff12012-05-11 09:45:13 -0400769/* set share access for a given stateid */
770static inline void
771set_access(u32 access, struct nfs4_ol_stateid *stp)
772{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400773 unsigned char mask = 1 << access;
774
775 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
776 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400777}
778
779/* clear share access for a given stateid */
780static inline void
781clear_access(u32 access, struct nfs4_ol_stateid *stp)
782{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400783 unsigned char mask = 1 << access;
784
785 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
786 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400787}
788
789/* test whether a given stateid has access */
790static inline bool
791test_access(u32 access, struct nfs4_ol_stateid *stp)
792{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400793 unsigned char mask = 1 << access;
794
795 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400796}
797
Jeff Laytonce0fc432012-05-11 09:45:14 -0400798/* set share deny for a given stateid */
799static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400800set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400801{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400802 unsigned char mask = 1 << deny;
803
804 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
805 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400806}
807
808/* clear share deny for a given stateid */
809static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400810clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400811{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400812 unsigned char mask = 1 << deny;
813
814 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
815 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400816}
817
818/* test whether a given stateid is denying specific access */
819static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400820test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400821{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400822 unsigned char mask = 1 << deny;
823
824 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400825}
826
827static int nfs4_access_to_omode(u32 access)
828{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400829 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400830 case NFS4_SHARE_ACCESS_READ:
831 return O_RDONLY;
832 case NFS4_SHARE_ACCESS_WRITE:
833 return O_WRONLY;
834 case NFS4_SHARE_ACCESS_BOTH:
835 return O_RDWR;
836 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500837 WARN_ON_ONCE(1);
838 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400839}
840
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400841/*
842 * A stateid that had a deny mode associated with it is being released
843 * or downgraded. Recalculate the deny mode on the file.
844 */
845static void
846recalculate_deny_mode(struct nfs4_file *fp)
847{
848 struct nfs4_ol_stateid *stp;
849
850 spin_lock(&fp->fi_lock);
851 fp->fi_share_deny = 0;
852 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
853 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
854 spin_unlock(&fp->fi_lock);
855}
856
857static void
858reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
859{
860 int i;
861 bool change = false;
862
863 for (i = 1; i < 4; i++) {
864 if ((i & deny) != i) {
865 change = true;
866 clear_deny(i, stp);
867 }
868 }
869
870 /* Recalculate per-file deny mode if there was a change */
871 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -0400872 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400873}
874
Jeff Layton82c5ff12012-05-11 09:45:13 -0400875/* release all access and file references for a given stateid */
876static void
877release_all_access(struct nfs4_ol_stateid *stp)
878{
879 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -0400880 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400881
882 if (fp && stp->st_deny_bmap != 0)
883 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400884
885 for (i = 1; i < 4; i++) {
886 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -0400887 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400888 clear_access(i, stp);
889 }
890}
891
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400892static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500893{
Trond Myklebust11b91642014-07-29 21:34:08 -0400894 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -0400895
896 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500897 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400898 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500899 list_del(&stp->st_perstateowner);
900}
901
Trond Myklebust60116952014-07-29 21:34:06 -0400902static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500903{
Trond Myklebust60116952014-07-29 21:34:06 -0400904 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400905
Trond Myklebust60116952014-07-29 21:34:06 -0400906 release_all_access(stp);
Trond Myklebust60116952014-07-29 21:34:06 -0400907 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500908}
909
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400910static void __release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500911{
912 struct file *file;
913
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400914 list_del(&stp->st_locks);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500915 unhash_generic_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400916 unhash_stid(&stp->st_stid);
Trond Myklebust11b91642014-07-29 21:34:08 -0400917 file = find_any_file(stp->st_stid.sc_file);
Trond Myklebuste20fcf12014-07-10 14:07:27 -0400918 if (file)
919 filp_close(file, (fl_owner_t)lockowner(stp->st_stateowner));
Trond Myklebust60116952014-07-29 21:34:06 -0400920 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500921}
922
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400923static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500924{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400925 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500926
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400927 list_del(&lo->lo_owner.so_strhash);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400928 while (!list_empty(&lo->lo_owner.so_stateids)) {
929 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400930 struct nfs4_ol_stateid, st_perstateowner);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400931 __release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500932 }
933}
934
Trond Myklebust50cc6232014-04-18 14:44:03 -0400935static void nfs4_free_lockowner(struct nfs4_lockowner *lo)
936{
937 kfree(lo->lo_owner.so_owner.data);
938 kmem_cache_free(lockowner_slab, lo);
939}
940
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400941static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500942{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400943 unhash_lockowner(lo);
944 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500945}
946
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400947static void release_lockowner_if_empty(struct nfs4_lockowner *lo)
948{
949 if (list_empty(&lo->lo_owner.so_stateids))
950 release_lockowner(lo);
951}
952
953static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500954{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400955 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500956
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400957 lo = lockowner(stp->st_stateowner);
958 __release_lock_stateid(stp);
959 release_lockowner_if_empty(lo);
960}
961
962static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
963{
964 struct nfs4_ol_stateid *stp;
965
966 while (!list_empty(&open_stp->st_locks)) {
967 stp = list_entry(open_stp->st_locks.next,
968 struct nfs4_ol_stateid, st_locks);
969 release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500970 }
971}
972
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400973static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500974{
975 unhash_generic_stateid(stp);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400976 release_open_stateid_locks(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400977}
978
979static void release_open_stateid(struct nfs4_ol_stateid *stp)
980{
981 unhash_open_stateid(stp);
Trond Myklebust60116952014-07-29 21:34:06 -0400982 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields22839632009-01-11 14:27:17 -0500983}
984
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400985static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500986{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400987 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500988
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400989 list_del(&oo->oo_owner.so_strhash);
990 list_del(&oo->oo_perclient);
991 while (!list_empty(&oo->oo_owner.so_stateids)) {
992 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400993 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500994 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500995 }
996}
997
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400998static void release_last_closed_stateid(struct nfs4_openowner *oo)
999{
1000 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
1001
1002 if (s) {
Trond Myklebust60116952014-07-29 21:34:06 -04001003 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001004 oo->oo_last_closed_stid = NULL;
1005 }
1006}
1007
Trond Myklebust50cc6232014-04-18 14:44:03 -04001008static void nfs4_free_openowner(struct nfs4_openowner *oo)
1009{
1010 kfree(oo->oo_owner.so_owner.data);
1011 kmem_cache_free(openowner_slab, oo);
1012}
1013
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001014static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001015{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001016 unhash_openowner(oo);
1017 list_del(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001018 release_last_closed_stateid(oo);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001019 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001020}
1021
Marc Eshel5282fd72009-04-03 08:27:52 +03001022static inline int
1023hash_sessionid(struct nfs4_sessionid *sessionid)
1024{
1025 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1026
1027 return sid->sequence % SESSION_HASH_SIZE;
1028}
1029
Trond Myklebust8f199b82012-03-20 15:11:17 -04001030#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001031static inline void
1032dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1033{
1034 u32 *ptr = (u32 *)(&sessionid->data[0]);
1035 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1036}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001037#else
1038static inline void
1039dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1040{
1041}
1042#endif
1043
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001044/*
1045 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1046 * won't be used for replay.
1047 */
1048void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1049{
1050 struct nfs4_stateowner *so = cstate->replay_owner;
1051
1052 if (nfserr == nfserr_replay_me)
1053 return;
1054
1055 if (!seqid_mutating_err(ntohl(nfserr))) {
1056 cstate->replay_owner = NULL;
1057 return;
1058 }
1059 if (!so)
1060 return;
1061 if (so->so_is_open_owner)
1062 release_last_closed_stateid(openowner(so));
1063 so->so_seqid++;
1064 return;
1065}
Marc Eshel5282fd72009-04-03 08:27:52 +03001066
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001067static void
1068gen_sessionid(struct nfsd4_session *ses)
1069{
1070 struct nfs4_client *clp = ses->se_client;
1071 struct nfsd4_sessionid *sid;
1072
1073 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1074 sid->clientid = clp->cl_clientid;
1075 sid->sequence = current_sessionid++;
1076 sid->reserved = 0;
1077}
1078
1079/*
Andy Adamsona6496372009-08-28 08:45:01 -04001080 * The protocol defines ca_maxresponssize_cached to include the size of
1081 * the rpc header, but all we need to cache is the data starting after
1082 * the end of the initial SEQUENCE operation--the rest we regenerate
1083 * each time. Therefore we can advertise a ca_maxresponssize_cached
1084 * value that is the number of bytes in our cache plus a few additional
1085 * bytes. In order to stay on the safe side, and not promise more than
1086 * we can cache, those additional bytes must be the minimum possible: 24
1087 * bytes of rpc header (xid through accept state, with AUTH_NULL
1088 * verifier), 12 for the compound header (with zero-length tag), and 44
1089 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001090 */
Andy Adamsona6496372009-08-28 08:45:01 -04001091#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1092
Andy Adamson557ce262009-08-28 08:45:04 -04001093static void
1094free_session_slots(struct nfsd4_session *ses)
1095{
1096 int i;
1097
1098 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1099 kfree(ses->se_slots[i]);
1100}
1101
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001102/*
1103 * We don't actually need to cache the rpc and session headers, so we
1104 * can allocate a little less for each slot:
1105 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001106static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001107{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001108 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001109
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001110 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1111 size = 0;
1112 else
1113 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1114 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001115}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001116
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001117/*
1118 * XXX: If we run out of reserved DRC memory we could (up to a point)
1119 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001120 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001121 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001122static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001123{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001124 u32 slotsize = slot_bytes(ca);
1125 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001126 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001127
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001128 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001129 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1130 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001131 num = min_t(int, num, avail / slotsize);
1132 nfsd_drc_mem_used += num * slotsize;
1133 spin_unlock(&nfsd_drc_lock);
1134
1135 return num;
1136}
1137
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001138static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001139{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001140 int slotsize = slot_bytes(ca);
1141
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001142 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001143 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001144 spin_unlock(&nfsd_drc_lock);
1145}
1146
Kinglong Mee60810e52014-01-01 00:35:47 +08001147static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1148 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001149{
Kinglong Mee60810e52014-01-01 00:35:47 +08001150 int numslots = fattrs->maxreqs;
1151 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001152 struct nfsd4_session *new;
1153 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001154
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001155 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001156 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1157 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001158
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001159 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001160 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001161 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001162 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001163 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001164 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001165 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001166 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001167 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001168
1169 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1170 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1171
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001172 return new;
1173out_free:
1174 while (i--)
1175 kfree(new->se_slots[i]);
1176 kfree(new);
1177 return NULL;
1178}
1179
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001180static void free_conn(struct nfsd4_conn *c)
1181{
1182 svc_xprt_put(c->cn_xprt);
1183 kfree(c);
1184}
1185
1186static void nfsd4_conn_lost(struct svc_xpt_user *u)
1187{
1188 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1189 struct nfs4_client *clp = c->cn_session->se_client;
1190
1191 spin_lock(&clp->cl_lock);
1192 if (!list_empty(&c->cn_persession)) {
1193 list_del(&c->cn_persession);
1194 free_conn(c);
1195 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001196 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001197 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001198}
1199
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001200static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001201{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001202 struct nfsd4_conn *conn;
1203
1204 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1205 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001206 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001207 svc_xprt_get(rqstp->rq_xprt);
1208 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001209 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001210 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1211 return conn;
1212}
1213
J. Bruce Fields328ead22010-09-29 16:11:06 -04001214static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1215{
1216 conn->cn_session = ses;
1217 list_add(&conn->cn_persession, &ses->se_conns);
1218}
1219
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001220static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1221{
1222 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001223
1224 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001225 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001226 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001227}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001228
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001229static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001230{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001231 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001232 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001233}
1234
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001235static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001236{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001237 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001238
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001239 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001240 ret = nfsd4_register_conn(conn);
1241 if (ret)
1242 /* oops; xprt is already down: */
1243 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001244 /* We may have gained or lost a callback channel: */
1245 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001246}
1247
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001248static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001249{
1250 u32 dir = NFS4_CDFC4_FORE;
1251
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001252 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001253 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001254 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001255}
1256
1257/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001258static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001259{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001260 struct nfs4_client *clp = s->se_client;
1261 struct nfsd4_conn *c;
1262
1263 spin_lock(&clp->cl_lock);
1264 while (!list_empty(&s->se_conns)) {
1265 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1266 list_del_init(&c->cn_persession);
1267 spin_unlock(&clp->cl_lock);
1268
1269 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1270 free_conn(c);
1271
1272 spin_lock(&clp->cl_lock);
1273 }
1274 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001275}
1276
J. Bruce Fields1377b692012-09-11 21:42:40 -04001277static void __free_session(struct nfsd4_session *ses)
1278{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001279 free_session_slots(ses);
1280 kfree(ses);
1281}
1282
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001283static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001284{
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001285 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1286
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001287 lockdep_assert_held(&nn->client_lock);
1288 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001289 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001290 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001291}
Andy Adamson557ce262009-08-28 08:45:04 -04001292
Fengguang Wu135ae822012-11-10 07:20:25 -05001293static 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 -04001294{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001295 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001296 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001297
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001298 new->se_client = clp;
1299 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001300
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001301 INIT_LIST_HEAD(&new->se_conns);
1302
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001303 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001304 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001305 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001306 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001307 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001308 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001309 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001310 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001311 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001312 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001313 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001314 spin_unlock(&nn->client_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001315
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001316 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001317 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001318 /*
1319 * This is a little silly; with sessions there's no real
1320 * use for the callback address. Use the peer address
1321 * as a reasonable default for now, but consider fixing
1322 * the rpc client not to require an address in the
1323 * future:
1324 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001325 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1326 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001327 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001328}
1329
Benny Halevy9089f1b2010-05-12 00:12:26 +03001330/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001331static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001332__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001333{
1334 struct nfsd4_session *elem;
1335 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001336 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001337
1338 dump_sessionid(__func__, sessionid);
1339 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001340 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001341 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001342 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1343 NFS4_MAX_SESSIONID_LEN)) {
1344 return elem;
1345 }
1346 }
1347
1348 dprintk("%s: session not found\n", __func__);
1349 return NULL;
1350}
1351
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001352static struct nfsd4_session *
1353find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1354 __be32 *ret)
1355{
1356 struct nfsd4_session *session;
1357 __be32 status = nfserr_badsession;
1358
1359 session = __find_in_sessionid_hashtbl(sessionid, net);
1360 if (!session)
1361 goto out;
1362 status = nfsd4_get_session_locked(session);
1363 if (status)
1364 session = NULL;
1365out:
1366 *ret = status;
1367 return session;
1368}
1369
Benny Halevy9089f1b2010-05-12 00:12:26 +03001370/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001371static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001372unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001373{
1374 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001375 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001376 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001377 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001378}
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1381static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001382STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001384 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001386 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001387 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return 1;
1389}
1390
1391/*
1392 * XXX Should we use a slab cache ?
1393 * This type of memory management is somewhat inefficient, but we use it
1394 * anyway since SETCLIENTID is not a common operation.
1395 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001396static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
1398 struct nfs4_client *clp;
1399
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001400 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1401 if (clp == NULL)
1402 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001403 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001404 if (clp->cl_name.data == NULL) {
1405 kfree(clp);
1406 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001408 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001409 INIT_LIST_HEAD(&clp->cl_sessions);
1410 idr_init(&clp->cl_stateids);
1411 atomic_set(&clp->cl_refcount, 0);
1412 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1413 INIT_LIST_HEAD(&clp->cl_idhash);
1414 INIT_LIST_HEAD(&clp->cl_openowners);
1415 INIT_LIST_HEAD(&clp->cl_delegations);
1416 INIT_LIST_HEAD(&clp->cl_lru);
1417 INIT_LIST_HEAD(&clp->cl_callbacks);
1418 INIT_LIST_HEAD(&clp->cl_revoked);
1419 spin_lock_init(&clp->cl_lock);
1420 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 return clp;
1422}
1423
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001424static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425free_client(struct nfs4_client *clp)
1426{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001427 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001428
1429 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001430 while (!list_empty(&clp->cl_sessions)) {
1431 struct nfsd4_session *ses;
1432 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1433 se_perclnt);
1434 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001435 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1436 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001437 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001438 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001439 free_svc_cred(&clp->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001441 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 kfree(clp);
1443}
1444
Benny Halevy84d38ac2010-05-12 00:13:16 +03001445/* must be called under the client_lock */
1446static inline void
1447unhash_client_locked(struct nfs4_client *clp)
1448{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001449 struct nfsd4_session *ses;
1450
Benny Halevy84d38ac2010-05-12 00:13:16 +03001451 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001452 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001453 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1454 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001455 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001456}
1457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001459destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001461 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001464 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001467 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001468 while (!list_empty(&clp->cl_delegations)) {
1469 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001470 unhash_delegation_locked(dp);
1471 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 }
Benny Halevycdc97502014-05-30 09:09:30 -04001473 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 while (!list_empty(&reaplist)) {
1475 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001476 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001477 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001479 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001480 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001481 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001482 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001483 }
NeilBrownea1da632005-06-23 22:04:17 -07001484 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001485 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1486 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001488 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001489 if (clp->cl_cb_conn.cb_xprt)
1490 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001491 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001492 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001493 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001494 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001495 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001496 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001497 unhash_client_locked(clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001498 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1499 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001500 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501}
1502
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001503static void expire_client(struct nfs4_client *clp)
1504{
1505 nfsd4_client_record_remove(clp);
1506 destroy_client(clp);
1507}
1508
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001509static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1510{
1511 memcpy(target->cl_verifier.data, source->data,
1512 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001515static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1516{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1518 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1519}
1520
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001521static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001522{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001523 if (source->cr_principal) {
1524 target->cr_principal =
1525 kstrdup(source->cr_principal, GFP_KERNEL);
1526 if (target->cr_principal == NULL)
1527 return -ENOMEM;
1528 } else
1529 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001530 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 target->cr_uid = source->cr_uid;
1532 target->cr_gid = source->cr_gid;
1533 target->cr_group_info = source->cr_group_info;
1534 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001535 target->cr_gss_mech = source->cr_gss_mech;
1536 if (source->cr_gss_mech)
1537 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001538 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001541static long long
1542compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1543{
1544 long long res;
1545
1546 res = o1->len - o2->len;
1547 if (res)
1548 return res;
1549 return (long long)memcmp(o1->data, o2->data, o1->len);
1550}
1551
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001552static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001553{
NeilBrowna55370a2005-06-23 22:03:52 -07001554 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
1556
1557static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001558same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1559{
1560 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
1562
1563static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001564same_clid(clientid_t *cl1, clientid_t *cl2)
1565{
1566 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
1568
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001569static bool groups_equal(struct group_info *g1, struct group_info *g2)
1570{
1571 int i;
1572
1573 if (g1->ngroups != g2->ngroups)
1574 return false;
1575 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001576 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001577 return false;
1578 return true;
1579}
1580
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001581/*
1582 * RFC 3530 language requires clid_inuse be returned when the
1583 * "principal" associated with a requests differs from that previously
1584 * used. We use uid, gid's, and gss principal string as our best
1585 * approximation. We also don't want to allow non-gss use of a client
1586 * established using gss: in theory cr_principal should catch that
1587 * change, but in practice cr_principal can be null even in the gss case
1588 * since gssd doesn't always pass down a principal string.
1589 */
1590static bool is_gss_cred(struct svc_cred *cr)
1591{
1592 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1593 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1594}
1595
1596
Vivek Trivedi5559b502012-07-24 21:18:20 +05301597static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001598same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1599{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001600 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001601 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1602 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001603 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1604 return false;
1605 if (cr1->cr_principal == cr2->cr_principal)
1606 return true;
1607 if (!cr1->cr_principal || !cr2->cr_principal)
1608 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301609 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
1611
J. Bruce Fields57266a62013-04-13 14:27:29 -04001612static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1613{
1614 struct svc_cred *cr = &rqstp->rq_cred;
1615 u32 service;
1616
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001617 if (!cr->cr_gss_mech)
1618 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001619 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1620 return service == RPC_GSS_SVC_INTEGRITY ||
1621 service == RPC_GSS_SVC_PRIVACY;
1622}
1623
1624static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1625{
1626 struct svc_cred *cr = &rqstp->rq_cred;
1627
1628 if (!cl->cl_mach_cred)
1629 return true;
1630 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1631 return false;
1632 if (!svc_rqst_integrity_protected(rqstp))
1633 return false;
1634 if (!cr->cr_principal)
1635 return false;
1636 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1637}
1638
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001639static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001640{
1641 static u32 current_clientid = 1;
1642
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001643 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 clp->cl_clientid.cl_id = current_clientid++;
1645}
1646
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001647static void gen_confirm(struct nfs4_client *clp)
1648{
Chuck Leverab4684d2012-03-02 17:13:50 -05001649 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001650 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Jeff Laytonf4199922014-06-17 07:44:11 -04001652 /*
1653 * This is opaque to client, so no need to byte-swap. Use
1654 * __force to keep sparse happy
1655 */
1656 verf[0] = (__force __be32)get_seconds();
1657 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001658 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
1660
Jeff Layton4770d722014-07-29 21:34:10 -04001661static struct nfs4_stid *
1662find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001663{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001664 struct nfs4_stid *ret;
1665
1666 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1667 if (!ret || !ret->sc_type)
1668 return NULL;
1669 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001670}
1671
Jeff Layton4770d722014-07-29 21:34:10 -04001672static struct nfs4_stid *
1673find_stateid(struct nfs4_client *cl, stateid_t *t)
1674{
1675 struct nfs4_stid *ret;
1676
1677 spin_lock(&cl->cl_lock);
1678 ret = find_stateid_locked(cl, t);
1679 spin_unlock(&cl->cl_lock);
1680 return ret;
1681}
1682
1683static struct nfs4_stid *
1684find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001685{
1686 struct nfs4_stid *s;
1687
Jeff Layton4770d722014-07-29 21:34:10 -04001688 spin_lock(&cl->cl_lock);
1689 s = find_stateid_locked(cl, t);
1690 if (s != NULL && !(typemask & s->sc_type))
1691 s = NULL;
1692 spin_unlock(&cl->cl_lock);
1693 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001694}
1695
Jeff Layton2216d442012-11-12 15:00:57 -05001696static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001697 struct svc_rqst *rqstp, nfs4_verifier *verf)
1698{
1699 struct nfs4_client *clp;
1700 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001701 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001702 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001703 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001704
1705 clp = alloc_client(name);
1706 if (clp == NULL)
1707 return NULL;
1708
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001709 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1710 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001711 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001712 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001713 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001714 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001715 }
Jeff Layton02e12152014-07-16 10:31:57 -04001716 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001717 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001718 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001719 copy_verf(clp, verf);
1720 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001721 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001722 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001723 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001724 return clp;
1725}
1726
NeilBrownfd39ca92005-06-23 22:04:03 -07001727static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001728add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1729{
1730 struct rb_node **new = &(root->rb_node), *parent = NULL;
1731 struct nfs4_client *clp;
1732
1733 while (*new) {
1734 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1735 parent = *new;
1736
1737 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1738 new = &((*new)->rb_left);
1739 else
1740 new = &((*new)->rb_right);
1741 }
1742
1743 rb_link_node(&new_clp->cl_namenode, parent, new);
1744 rb_insert_color(&new_clp->cl_namenode, root);
1745}
1746
1747static struct nfs4_client *
1748find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1749{
1750 long long cmp;
1751 struct rb_node *node = root->rb_node;
1752 struct nfs4_client *clp;
1753
1754 while (node) {
1755 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1756 cmp = compare_blob(&clp->cl_name, name);
1757 if (cmp > 0)
1758 node = node->rb_left;
1759 else if (cmp < 0)
1760 node = node->rb_right;
1761 else
1762 return clp;
1763 }
1764 return NULL;
1765}
1766
1767static void
1768add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
1770 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001771 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001773 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001774 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001776 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001777 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
1779
NeilBrownfd39ca92005-06-23 22:04:03 -07001780static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781move_to_confirmed(struct nfs4_client *clp)
1782{
1783 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001784 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001787 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001788 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001789 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001790 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 renew_client(clp);
1792}
1793
1794static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001795find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
1797 struct nfs4_client *clp;
1798 unsigned int idhashval = clientid_hashval(clid->cl_id);
1799
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001800 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001801 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001802 if ((bool)clp->cl_minorversion != sessions)
1803 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001804 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
1808 return NULL;
1809}
1810
1811static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001812find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1813{
1814 struct list_head *tbl = nn->conf_id_hashtbl;
1815
1816 return find_client_in_id_table(tbl, clid, sessions);
1817}
1818
1819static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001820find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001822 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001824 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001827static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001828{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001829 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001830}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001831
NeilBrown28ce6052005-06-23 22:03:56 -07001832static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001833find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001834{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001835 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001836}
1837
1838static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001839find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001840{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001841 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001842}
1843
NeilBrownfd39ca92005-06-23 22:04:03 -07001844static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001845gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001847 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001848 struct sockaddr *sa = svc_addr(rqstp);
1849 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001850 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Jeff Layton7077ecb2009-08-14 12:57:58 -04001852 /* Currently, we only support tcp and tcp6 for the callback channel */
1853 if (se->se_callback_netid_len == 3 &&
1854 !memcmp(se->se_callback_netid_val, "tcp", 3))
1855 expected_family = AF_INET;
1856 else if (se->se_callback_netid_len == 4 &&
1857 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1858 expected_family = AF_INET6;
1859 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 goto out_err;
1861
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001862 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001863 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001864 (struct sockaddr *)&conn->cb_addr,
1865 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001866
J. Bruce Fields07263f12010-05-31 19:09:40 -04001867 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001869
J. Bruce Fields07263f12010-05-31 19:09:40 -04001870 if (conn->cb_addr.ss_family == AF_INET6)
1871 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001872
J. Bruce Fields07263f12010-05-31 19:09:40 -04001873 conn->cb_prog = se->se_callback_prog;
1874 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001875 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 return;
1877out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001878 conn->cb_addr.ss_family = AF_UNSPEC;
1879 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001880 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 "will not receive delegations\n",
1882 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return;
1885}
1886
Andy Adamson074fe892009-04-03 08:28:15 +03001887/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04001888 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001889 */
Trond Myklebustb6076642014-06-30 11:48:35 -04001890static void
Andy Adamson074fe892009-04-03 08:28:15 +03001891nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1892{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001893 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04001894 struct nfsd4_slot *slot = resp->cstate.slot;
1895 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001896
Andy Adamson557ce262009-08-28 08:45:04 -04001897 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001898
Andy Adamson557ce262009-08-28 08:45:04 -04001899 slot->sl_opcnt = resp->opcnt;
1900 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001901
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001902 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001903 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001904 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001905 return;
1906 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001907 base = resp->cstate.data_offset;
1908 slot->sl_datalen = buf->len - base;
1909 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04001910 WARN("%s: sessions DRC could not cache compound\n", __func__);
1911 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001912}
1913
1914/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001915 * Encode the replay sequence operation from the slot values.
1916 * If cachethis is FALSE encode the uncached rep error on the next
1917 * operation which sets resp->p and increments resp->opcnt for
1918 * nfs4svc_encode_compoundres.
1919 *
Andy Adamson074fe892009-04-03 08:28:15 +03001920 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001921static __be32
1922nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1923 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001924{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001925 struct nfsd4_op *op;
1926 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001927
Andy Adamsonabfabf82009-07-23 19:02:18 -04001928 /* Encode the replayed sequence operation */
1929 op = &args->ops[resp->opcnt - 1];
1930 nfsd4_encode_operation(resp, op);
1931
1932 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05001933 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001934 op = &args->ops[resp->opcnt++];
1935 op->status = nfserr_retry_uncached_rep;
1936 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001937 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001938 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001939}
1940
1941/*
Andy Adamson557ce262009-08-28 08:45:04 -04001942 * The sequence operation is not cached because we can use the slot and
1943 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001944 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04001945static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001946nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1947 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001948{
Andy Adamson557ce262009-08-28 08:45:04 -04001949 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001950 struct xdr_stream *xdr = &resp->xdr;
1951 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03001952 __be32 status;
1953
Andy Adamson557ce262009-08-28 08:45:04 -04001954 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001955
Andy Adamsonabfabf82009-07-23 19:02:18 -04001956 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04001957 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04001958 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001959
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001960 p = xdr_reserve_space(xdr, slot->sl_datalen);
1961 if (!p) {
1962 WARN_ON_ONCE(1);
1963 return nfserr_serverfault;
1964 }
1965 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
1966 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03001967
Andy Adamson557ce262009-08-28 08:45:04 -04001968 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001969 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001970}
1971
Andy Adamson0733d212009-04-03 08:28:01 +03001972/*
1973 * Set the exchange_id flags returned by the server.
1974 */
1975static void
1976nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1977{
1978 /* pNFS is not supported */
1979 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1980
1981 /* Referrals are supported, Migration is not. */
1982 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1983
1984 /* set the wire flags to return to client. */
1985 clid->flags = new->cl_exchange_flags;
1986}
1987
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001988static bool client_has_state(struct nfs4_client *clp)
1989{
1990 /*
1991 * Note clp->cl_openowners check isn't quite right: there's no
1992 * need to count owners without stateid's.
1993 *
1994 * Also note we should probably be using this in 4.0 case too.
1995 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04001996 return !list_empty(&clp->cl_openowners)
1997 || !list_empty(&clp->cl_delegations)
1998 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001999}
2000
Al Virob37ad282006-10-19 23:28:59 -07002001__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002002nfsd4_exchange_id(struct svc_rqst *rqstp,
2003 struct nfsd4_compound_state *cstate,
2004 struct nfsd4_exchange_id *exid)
2005{
Andy Adamson0733d212009-04-03 08:28:01 +03002006 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002007 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002008 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002009 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002010 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002011 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002012 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002013
Jeff Layton363168b2009-08-14 12:57:56 -04002014 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002015 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002016 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002017 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002018 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002019
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002020 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002021 return nfserr_inval;
2022
Andy Adamson0733d212009-04-03 08:28:01 +03002023 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002024 case SP4_MACH_CRED:
2025 if (!svc_rqst_integrity_protected(rqstp))
2026 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002027 case SP4_NONE:
2028 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002029 default: /* checked by xdr code */
2030 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002031 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002032 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002033 }
2034
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002035 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03002036 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002037 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002038 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002039 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2040 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2041
J. Bruce Fields136e6582012-05-12 20:37:23 -04002042 if (update) {
2043 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002044 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002045 goto out;
2046 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002047 if (!mach_creds_match(conf, rqstp)) {
2048 status = nfserr_wrong_cred;
2049 goto out;
2050 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002051 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002052 status = nfserr_perm;
2053 goto out;
2054 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002055 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002056 status = nfserr_not_same;
2057 goto out;
2058 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002059 /* case 6 */
2060 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2061 new = conf;
2062 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002063 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002064 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002065 if (client_has_state(conf)) {
2066 status = nfserr_clid_inuse;
2067 goto out;
2068 }
Andy Adamson0733d212009-04-03 08:28:01 +03002069 expire_client(conf);
2070 goto out_new;
2071 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002072 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002073 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002074 new = conf;
2075 goto out_copy;
2076 }
2077 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04002078 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002079 }
2080
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002081 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002082 status = nfserr_noent;
2083 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002084 }
2085
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002086 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002087 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03002088 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002089
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002090 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002091out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05002092 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03002093 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04002094 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03002095 goto out;
2096 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002097 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002098 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002099
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002100 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002101 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03002102out_copy:
2103 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2104 exid->clientid.cl_id = new->cl_clientid.cl_id;
2105
J. Bruce Fields778df3f2012-05-25 21:40:23 -04002106 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03002107 nfsd4_set_ex_flags(new, exid);
2108
2109 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04002110 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002111 status = nfs_ok;
2112
2113out:
2114 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03002115 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002116}
2117
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002118static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002119check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002120{
Andy Adamson88e588d2009-07-23 19:02:15 -04002121 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2122 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002123
2124 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002125 if (slot_inuse) {
2126 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002127 return nfserr_jukebox;
2128 else
2129 return nfserr_seq_misordered;
2130 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002131 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002132 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002133 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002134 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002135 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002136 return nfserr_seq_misordered;
2137}
2138
Andy Adamson49557cc2009-07-23 19:02:16 -04002139/*
2140 * Cache the create session result into the create session single DRC
2141 * slot cache by saving the xdr structure. sl_seqid has been set.
2142 * Do this for solo or embedded create session operations.
2143 */
2144static void
2145nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002146 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002147{
2148 slot->sl_status = nfserr;
2149 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2150}
2151
2152static __be32
2153nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2154 struct nfsd4_clid_slot *slot)
2155{
2156 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2157 return slot->sl_status;
2158}
2159
Mi Jinlong1b74c252011-07-14 14:50:17 +08002160#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2161 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2162 1 + /* MIN tag is length with zero, only length */ \
2163 3 + /* version, opcount, opcode */ \
2164 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2165 /* seqid, slotID, slotID, cache */ \
2166 4 ) * sizeof(__be32))
2167
2168#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2169 2 + /* verifier: AUTH_NULL, length 0 */\
2170 1 + /* status */ \
2171 1 + /* MIN tag is length with zero, only length */ \
2172 3 + /* opcount, opcode, opstatus*/ \
2173 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2174 /* seqid, slotID, slotID, slotID, status */ \
2175 5 ) * sizeof(__be32))
2176
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002177static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002178{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002179 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2180
J. Bruce Fields373cd402013-04-08 15:42:12 -04002181 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2182 return nfserr_toosmall;
2183 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2184 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002185 ca->headerpadsz = 0;
2186 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2187 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2188 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2189 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2190 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2191 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2192 /*
2193 * Note decreasing slot size below client's request may make it
2194 * difficult for client to function correctly, whereas
2195 * decreasing the number of slots will (just?) affect
2196 * performance. When short on memory we therefore prefer to
2197 * decrease number of slots instead of their size. Clients that
2198 * request larger slots than they need will get poor results:
2199 */
2200 ca->maxreqs = nfsd4_get_drc_mem(ca);
2201 if (!ca->maxreqs)
2202 return nfserr_jukebox;
2203
J. Bruce Fields373cd402013-04-08 15:42:12 -04002204 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002205}
2206
Kinglong Mee8a891632013-12-23 18:11:02 +08002207#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2208 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2209#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2210 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2211
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002212static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2213{
2214 ca->headerpadsz = 0;
2215
2216 /*
2217 * These RPC_MAX_HEADER macros are overkill, especially since we
2218 * don't even do gss on the backchannel yet. But this is still
2219 * less than 1k. Tighten up this estimate in the unlikely event
2220 * it turns out to be a problem for some client:
2221 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002222 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002223 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002224 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002225 return nfserr_toosmall;
2226 ca->maxresp_cached = 0;
2227 if (ca->maxops < 2)
2228 return nfserr_toosmall;
2229
2230 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002231}
2232
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002233static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2234{
2235 switch (cbs->flavor) {
2236 case RPC_AUTH_NULL:
2237 case RPC_AUTH_UNIX:
2238 return nfs_ok;
2239 default:
2240 /*
2241 * GSS case: the spec doesn't allow us to return this
2242 * error. But it also doesn't allow us not to support
2243 * GSS.
2244 * I'd rather this fail hard than return some error the
2245 * client might think it can already handle:
2246 */
2247 return nfserr_encr_alg_unsupp;
2248 }
2249}
2250
Andy Adamson069b6ad2009-04-03 08:27:58 +03002251__be32
2252nfsd4_create_session(struct svc_rqst *rqstp,
2253 struct nfsd4_compound_state *cstate,
2254 struct nfsd4_create_session *cr_ses)
2255{
Jeff Layton363168b2009-08-14 12:57:56 -04002256 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002257 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002258 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002259 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002260 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002261 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002262 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002263
Mi Jinlonga62573d2011-03-23 17:57:07 +08002264 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2265 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002266 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2267 if (status)
2268 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002269 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002270 if (status)
2271 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002272 status = check_backchannel_attrs(&cr_ses->back_channel);
2273 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002274 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002275 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002276 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002277 if (!new)
2278 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002279 conn = alloc_conn_from_crses(rqstp, cr_ses);
2280 if (!conn)
2281 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002282
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002283 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002284 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002285 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002286 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002287
2288 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002289 status = nfserr_wrong_cred;
2290 if (!mach_creds_match(conf, rqstp))
2291 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002292 cs_slot = &conf->cl_cs_slot;
2293 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002294 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002295 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002296 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002297 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002298 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002299 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002300 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002301 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002302 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002303 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002304 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002305 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002306 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002307 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002308 status = nfserr_wrong_cred;
2309 if (!mach_creds_match(unconf, rqstp))
2310 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002311 cs_slot = &unconf->cl_cs_slot;
2312 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002313 if (status) {
2314 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002315 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002316 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002317 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002318 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002319 if (old) {
2320 status = mark_client_expired(old);
2321 if (status)
2322 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002323 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002324 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002325 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002326 conf = unconf;
2327 } else {
2328 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002329 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002330 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002331 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002332 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002333 * We do not support RDMA or persistent sessions
2334 */
2335 cr_ses->flags &= ~SESSION4_PERSIST;
2336 cr_ses->flags &= ~SESSION4_RDMA;
2337
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002338 init_session(rqstp, new, conf, cr_ses);
2339 nfsd4_init_conn(rqstp, conn, new);
2340
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002341 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002342 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002343 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002344 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002345
Andy Adamson49557cc2009-07-23 19:02:16 -04002346 /* cache solo and embedded create sessions under the state lock */
2347 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002348 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002349 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002350out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08002351 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002352 free_conn(conn);
2353out_free_session:
2354 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002355out_release_drc_mem:
2356 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002357 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002358}
2359
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002360static __be32 nfsd4_map_bcts_dir(u32 *dir)
2361{
2362 switch (*dir) {
2363 case NFS4_CDFC4_FORE:
2364 case NFS4_CDFC4_BACK:
2365 return nfs_ok;
2366 case NFS4_CDFC4_FORE_OR_BOTH:
2367 case NFS4_CDFC4_BACK_OR_BOTH:
2368 *dir = NFS4_CDFC4_BOTH;
2369 return nfs_ok;
2370 };
2371 return nfserr_inval;
2372}
2373
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002374__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2375{
2376 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002377 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002378 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002379
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002380 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2381 if (status)
2382 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002383 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002384 session->se_cb_prog = bc->bc_cb_program;
2385 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002386 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002387
2388 nfsd4_probe_callback(session->se_client);
2389
2390 return nfs_ok;
2391}
2392
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002393__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2394 struct nfsd4_compound_state *cstate,
2395 struct nfsd4_bind_conn_to_session *bcts)
2396{
2397 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002398 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002399 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002400 struct net *net = SVC_NET(rqstp);
2401 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002402
2403 if (!nfsd4_last_compound_op(rqstp))
2404 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002405 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002406 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002407 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002408 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002409 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002410 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002411 status = nfserr_wrong_cred;
2412 if (!mach_creds_match(session->se_client, rqstp))
2413 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002414 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002415 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002416 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002417 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002418 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002419 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002420 goto out;
2421 nfsd4_init_conn(rqstp, conn, session);
2422 status = nfs_ok;
2423out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002424 nfsd4_put_session(session);
2425out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002426 nfs4_unlock_state();
2427 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002428}
2429
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002430static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2431{
2432 if (!session)
2433 return 0;
2434 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2435}
2436
Andy Adamson069b6ad2009-04-03 08:27:58 +03002437__be32
2438nfsd4_destroy_session(struct svc_rqst *r,
2439 struct nfsd4_compound_state *cstate,
2440 struct nfsd4_destroy_session *sessionid)
2441{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002442 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002443 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002444 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002445 struct net *net = SVC_NET(r);
2446 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002447
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002448 nfs4_lock_state();
2449 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002450 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002451 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002452 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002453 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002454 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002455 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002456 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002457 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002458 if (!ses)
2459 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002460 status = nfserr_wrong_cred;
2461 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002462 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002463 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002464 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002465 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002466 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002467 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002468
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002469 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002470
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002471 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002472 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002473out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002474 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002475out_client_lock:
2476 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002477out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002478 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002479 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002480}
2481
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002482static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002483{
2484 struct nfsd4_conn *c;
2485
2486 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002487 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002488 return c;
2489 }
2490 }
2491 return NULL;
2492}
2493
J. Bruce Fields57266a62013-04-13 14:27:29 -04002494static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002495{
2496 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002497 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002498 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002499 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002500
2501 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002502 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002503 if (c)
2504 goto out_free;
2505 status = nfserr_conn_not_bound_to_session;
2506 if (clp->cl_mach_cred)
2507 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002508 __nfsd4_hash_conn(new, ses);
2509 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002510 ret = nfsd4_register_conn(new);
2511 if (ret)
2512 /* oops; xprt is already down: */
2513 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002514 return nfs_ok;
2515out_free:
2516 spin_unlock(&clp->cl_lock);
2517 free_conn(new);
2518 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002519}
2520
Mi Jinlong868b89c2011-04-27 09:09:58 +08002521static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2522{
2523 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2524
2525 return args->opcnt > session->se_fchannel.maxops;
2526}
2527
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002528static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2529 struct nfsd4_session *session)
2530{
2531 struct xdr_buf *xb = &rqstp->rq_arg;
2532
2533 return xb->len > session->se_fchannel.maxreq_sz;
2534}
2535
Andy Adamson069b6ad2009-04-03 08:27:58 +03002536__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002537nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002538 struct nfsd4_compound_state *cstate,
2539 struct nfsd4_sequence *seq)
2540{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002541 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002542 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002543 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002544 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002545 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002546 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002547 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002548 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002549 struct net *net = SVC_NET(rqstp);
2550 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002551
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002552 if (resp->opcnt != 1)
2553 return nfserr_sequence_pos;
2554
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002555 /*
2556 * Will be either used or freed by nfsd4_sequence_check_conn
2557 * below.
2558 */
2559 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2560 if (!conn)
2561 return nfserr_jukebox;
2562
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002563 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002564 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002565 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002566 goto out_no_session;
2567 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002568
Mi Jinlong868b89c2011-04-27 09:09:58 +08002569 status = nfserr_too_many_ops;
2570 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002571 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002572
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002573 status = nfserr_req_too_big;
2574 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002575 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002576
Benny Halevyb85d4c02009-04-03 08:28:08 +03002577 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002578 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002579 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002580
Andy Adamson557ce262009-08-28 08:45:04 -04002581 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002582 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2583
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002584 /* We do not negotiate the number of slots yet, so set the
2585 * maxslots to the session maxreqs which is used to encode
2586 * sr_highest_slotid and the sr_target_slot id to maxslots */
2587 seq->maxslots = session->se_fchannel.maxreqs;
2588
J. Bruce Fields73e79482012-02-13 16:39:00 -05002589 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2590 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002591 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002592 status = nfserr_seq_misordered;
2593 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002594 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002595 cstate->slot = slot;
2596 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002597 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002598 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002599 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002600 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002601 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002602 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002603 }
2604 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002605 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002606
J. Bruce Fields57266a62013-04-13 14:27:29 -04002607 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002608 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002609 if (status)
2610 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002611
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002612 buflen = (seq->cachethis) ?
2613 session->se_fchannel.maxresp_cached :
2614 session->se_fchannel.maxresp_sz;
2615 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2616 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002617 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002618 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002619 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002620
2621 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002622 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002623 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002624 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002625 if (seq->cachethis)
2626 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002627 else
2628 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002629
2630 cstate->slot = slot;
2631 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002632 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002633
Benny Halevyb85d4c02009-04-03 08:28:08 +03002634out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002635 switch (clp->cl_cb_state) {
2636 case NFSD4_CB_DOWN:
2637 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2638 break;
2639 case NFSD4_CB_FAULT:
2640 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2641 break;
2642 default:
2643 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002644 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002645 if (!list_empty(&clp->cl_revoked))
2646 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002647out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002648 if (conn)
2649 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002650 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002651 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002652out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002653 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002654 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002655}
2656
Trond Myklebustb6076642014-06-30 11:48:35 -04002657void
2658nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2659{
2660 struct nfsd4_compound_state *cs = &resp->cstate;
2661
2662 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002663 if (cs->status != nfserr_replay_cache) {
2664 nfsd4_store_cache_entry(resp);
2665 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2666 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002667 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002668 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002669 } else if (cs->clp)
2670 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002671}
2672
Mi Jinlong345c2842011-10-20 17:51:39 +08002673__be32
2674nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2675{
2676 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002677 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002678 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002679
2680 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002681 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002682 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002683 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002684
2685 if (conf) {
2686 clp = conf;
2687
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002688 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002689 status = nfserr_clientid_busy;
2690 goto out;
2691 }
2692 } else if (unconf)
2693 clp = unconf;
2694 else {
2695 status = nfserr_stale_clientid;
2696 goto out;
2697 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002698 if (!mach_creds_match(clp, rqstp)) {
2699 status = nfserr_wrong_cred;
2700 goto out;
2701 }
Mi Jinlong345c2842011-10-20 17:51:39 +08002702 expire_client(clp);
2703out:
2704 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002705 return status;
2706}
2707
Andy Adamson069b6ad2009-04-03 08:27:58 +03002708__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002709nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2710{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002711 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002712
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002713 if (rc->rca_one_fs) {
2714 if (!cstate->current_fh.fh_dentry)
2715 return nfserr_nofilehandle;
2716 /*
2717 * We don't take advantage of the rca_one_fs case.
2718 * That's OK, it's optional, we can safely ignore it.
2719 */
2720 return nfs_ok;
2721 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002722
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002723 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002724 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002725 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2726 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002727 goto out;
2728
2729 status = nfserr_stale_clientid;
2730 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002731 /*
2732 * The following error isn't really legal.
2733 * But we only get here if the client just explicitly
2734 * destroyed the client. Surely it no longer cares what
2735 * error it gets back on an operation for the dead
2736 * client.
2737 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002738 goto out;
2739
2740 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002741 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002742out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002743 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002744 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002745}
2746
2747__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002748nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2749 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002751 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002753 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002754 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002755 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2756
J. Bruce Fields63db4632012-05-18 21:54:19 -04002757 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002759 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002760 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002761 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002763 if (clp_used_exchangeid(conf))
2764 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002765 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002766 char addr_str[INET6_ADDRSTRLEN];
2767 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2768 sizeof(addr_str));
2769 dprintk("NFSD: setclientid: string in use by client "
2770 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 goto out;
2772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002774 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002775 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002778 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002779 if (new == NULL)
2780 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002781 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002782 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002784 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002785 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002786 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002787 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002788 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2790 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2791 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2792 status = nfs_ok;
2793out:
2794 nfs4_unlock_state();
2795 return status;
2796}
2797
2798
Al Virob37ad282006-10-19 23:28:59 -07002799__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002800nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2801 struct nfsd4_compound_state *cstate,
2802 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803{
NeilBrown21ab45a2005-06-23 22:04:14 -07002804 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2806 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002807 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002808 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002810 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002813
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002814 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002815 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002816 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002817 * We try hard to give out unique clientid's, so if we get an
2818 * attempt to confirm the same clientid with a different cred,
2819 * there's a bug somewhere. Let's charitably assume it's our
2820 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002821 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002822 status = nfserr_serverfault;
2823 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2824 goto out;
2825 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2826 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002827 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002828 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2829 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002831 else /* case 4: client hasn't noticed we rebooted yet? */
2832 status = nfserr_stale_clientid;
2833 goto out;
2834 }
2835 status = nfs_ok;
2836 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002837 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2838 nfsd4_probe_callback(conf);
2839 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002840 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002841 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002842 if (conf) {
2843 status = mark_client_expired(conf);
2844 if (status)
2845 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002846 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002847 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002848 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002849 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 nfs4_unlock_state();
2853 return status;
2854}
2855
J. Bruce Fields32513b42011-10-13 16:00:16 -04002856static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002858 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2859}
2860
2861/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04002862static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04002863{
Trond Myklebustca943212014-07-23 16:17:39 -04002864 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Trond Myklebust950e0112014-06-30 11:48:31 -04002866 lockdep_assert_held(&state_lock);
2867
J. Bruce Fields32513b42011-10-13 16:00:16 -04002868 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04002869 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002870 INIT_LIST_HEAD(&fp->fi_stateids);
2871 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04002872 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002873 fp->fi_had_conflict = false;
2874 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04002875 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002876 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2877 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04002878 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879}
2880
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002881void
NeilBrowne60d4392005-06-23 22:03:01 -07002882nfsd4_free_slabs(void)
2883{
Christoph Hellwigabf11352014-05-21 07:43:03 -07002884 kmem_cache_destroy(openowner_slab);
2885 kmem_cache_destroy(lockowner_slab);
2886 kmem_cache_destroy(file_slab);
2887 kmem_cache_destroy(stateid_slab);
2888 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002889}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
Bryan Schumaker72083392011-11-01 15:24:59 -04002891int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892nfsd4_init_slabs(void)
2893{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002894 openowner_slab = kmem_cache_create("nfsd4_openowners",
2895 sizeof(struct nfs4_openowner), 0, 0, NULL);
2896 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002897 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002898 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002899 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002900 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002901 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07002902 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002903 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002904 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002905 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07002906 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002907 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002908 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002909 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002910 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002911 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002912 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002913 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07002915
2916out_free_stateid_slab:
2917 kmem_cache_destroy(stateid_slab);
2918out_free_file_slab:
2919 kmem_cache_destroy(file_slab);
2920out_free_lockowner_slab:
2921 kmem_cache_destroy(lockowner_slab);
2922out_free_openowner_slab:
2923 kmem_cache_destroy(openowner_slab);
2924out:
NeilBrowne60d4392005-06-23 22:03:01 -07002925 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2926 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927}
2928
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002929static void init_nfs4_replay(struct nfs4_replay *rp)
2930{
2931 rp->rp_status = nfserr_serverfault;
2932 rp->rp_buflen = 0;
2933 rp->rp_buf = rp->rp_ibuf;
2934}
2935
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002936static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937{
2938 struct nfs4_stateowner *sop;
2939
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002940 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002941 if (!sop)
2942 return NULL;
2943
2944 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2945 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002946 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002947 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002949 sop->so_owner.len = owner->len;
2950
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002951 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002952 sop->so_client = clp;
2953 init_nfs4_replay(&sop->so_replay);
2954 return sop;
2955}
2956
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002957static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002958{
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002959 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2960
2961 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002962 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963}
2964
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002965static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04002966alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002967 struct nfsd4_compound_state *cstate)
2968{
Trond Myklebust13d6f662014-06-30 11:48:45 -04002969 struct nfs4_client *clp = cstate->clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002970 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002972 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2973 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002975 oo->oo_owner.so_is_open_owner = 1;
2976 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04002977 oo->oo_flags = NFS4_OO_NEW;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002978 if (nfsd4_has_session(cstate))
2979 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002980 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002981 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002982 INIT_LIST_HEAD(&oo->oo_close_lru);
2983 hash_openowner(oo, clp, strhashval);
2984 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985}
2986
J. Bruce Fields996e0932011-10-17 11:14:48 -04002987static 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 -04002988 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002990 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04002991 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002992 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002993 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002994 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04002995 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 stp->st_access_bmap = 0;
2997 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07002998 stp->st_openstp = NULL;
Trond Myklebust1d31a252014-07-10 14:07:25 -04002999 spin_lock(&fp->fi_lock);
3000 list_add(&stp->st_perfile, &fp->fi_stateids);
3001 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002}
3003
3004static void
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003005move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006{
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003007 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3008
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003009 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003011 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003012 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013}
3014
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04003016same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
3017 clientid_t *clid)
3018{
3019 return (sop->so_owner.len == owner->len) &&
3020 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
3021 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022}
3023
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003024static struct nfs4_openowner *
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003025find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
3026 bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027{
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003028 struct nfs4_stateowner *so;
3029 struct nfs4_openowner *oo;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003030 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003032 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003033 if (!so->so_is_open_owner)
3034 continue;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003035 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
3036 oo = openowner(so);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003037 clp = oo->oo_owner.so_client;
3038 if ((bool)clp->cl_minorversion != sessions)
3039 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003040 renew_client(oo->oo_owner.so_client);
3041 return oo;
3042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 }
3044 return NULL;
3045}
3046
3047/* search file_hashtbl[] for file */
3048static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003049find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
Trond Myklebustca943212014-07-23 16:17:39 -04003051 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 struct nfs4_file *fp;
3053
Trond Myklebust950e0112014-06-30 11:48:31 -04003054 lockdep_assert_held(&state_lock);
3055
Jeff Layton89876f82013-04-02 09:01:59 -04003056 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003057 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003058 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 }
3062 return NULL;
3063}
3064
Trond Myklebust950e0112014-06-30 11:48:31 -04003065static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003066find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003067{
3068 struct nfs4_file *fp;
3069
3070 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003071 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003072 spin_unlock(&state_lock);
3073 return fp;
3074}
3075
3076static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003077find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003078{
3079 struct nfs4_file *fp;
3080
3081 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003082 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003083 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003084 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003085 fp = new;
3086 }
3087 spin_unlock(&state_lock);
3088
3089 return fp;
3090}
3091
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003092/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 * Called to check deny when READ with all zero stateid or
3094 * WRITE with all zero or all one stateid
3095 */
Al Virob37ad282006-10-19 23:28:59 -07003096static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3098{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003100 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Trond Myklebustca943212014-07-23 16:17:39 -04003102 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003103 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003104 return ret;
3105 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003106 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003107 if (fp->fi_share_deny & deny_type)
3108 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003109 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003110 put_nfs4_file(fp);
3111 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112}
3113
Jeff Layton02e12152014-07-16 10:31:57 -04003114void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115{
Trond Myklebust11b91642014-07-29 21:34:08 -04003116 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3117 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003118
Trond Myklebust11b91642014-07-29 21:34:08 -04003119 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003120
Jeff Layton02e12152014-07-16 10:31:57 -04003121 /*
3122 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003123 * already holding inode->i_lock.
3124 *
Jeff Laytondff13992014-07-08 14:02:49 -04003125 * If the dl_time != 0, then we know that it has already been
3126 * queued for a lease break. Don't queue it again.
3127 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003128 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003129 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003130 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003131 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003132 }
Jeff Layton02e12152014-07-16 10:31:57 -04003133 spin_unlock(&state_lock);
3134}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
Jeff Layton02e12152014-07-16 10:31:57 -04003136static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3137{
3138 /*
3139 * We're assuming the state code never drops its reference
3140 * without first removing the lease. Since we're in this lease
3141 * callback (and since the lease code is serialized by the kernel
3142 * lock) we know the server hasn't removed the lease yet, we know
3143 * it's safe to take a reference.
3144 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003145 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003146 nfsd4_cb_recall(dp);
3147}
3148
Jeff Layton1c8c6012013-06-21 08:58:15 -04003149/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003150static void nfsd_break_deleg_cb(struct file_lock *fl)
3151{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003152 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3153 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003154
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003155 if (!fp) {
3156 WARN(1, "(%p)->fl_owner NULL\n", fl);
3157 return;
3158 }
3159 if (fp->fi_had_conflict) {
3160 WARN(1, "duplicate break on %p\n", fp);
3161 return;
3162 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003163 /*
3164 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003165 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003166 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003167 */
3168 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
Jeff Layton02e12152014-07-16 10:31:57 -04003170 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003171 fp->fi_had_conflict = true;
3172 /*
3173 * If there are no delegations on the list, then we can't count on this
3174 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3175 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3176 * true should keep any new delegations from being hashed.
3177 */
3178 if (list_empty(&fp->fi_delegations))
3179 fl->fl_break_time = jiffies;
3180 else
3181 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3182 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003183 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184}
3185
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186static
3187int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3188{
3189 if (arg & F_UNLCK)
3190 return lease_modify(onlist, arg);
3191 else
3192 return -EAGAIN;
3193}
3194
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003195static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003196 .lm_break = nfsd_break_deleg_cb,
3197 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198};
3199
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003200static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3201{
3202 if (nfsd4_has_session(cstate))
3203 return nfs_ok;
3204 if (seqid == so->so_seqid - 1)
3205 return nfserr_replay_me;
3206 if (seqid == so->so_seqid)
3207 return nfs_ok;
3208 return nfserr_bad_seqid;
3209}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
Jeff Layton4b24ca72014-06-30 11:48:44 -04003211static __be32 lookup_clientid(clientid_t *clid,
3212 struct nfsd4_compound_state *cstate,
3213 struct nfsd_net *nn)
3214{
3215 struct nfs4_client *found;
3216
3217 if (cstate->clp) {
3218 found = cstate->clp;
3219 if (!same_clid(&found->cl_clientid, clid))
3220 return nfserr_stale_clientid;
3221 return nfs_ok;
3222 }
3223
3224 if (STALE_CLIENTID(clid, nn))
3225 return nfserr_stale_clientid;
3226
3227 /*
3228 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3229 * cached already then we know this is for is for v4.0 and "sessions"
3230 * will be false.
3231 */
3232 WARN_ON_ONCE(cstate->session);
3233 found = find_confirmed_client(clid, false, nn);
3234 if (!found)
3235 return nfserr_expired;
3236
3237 /* Cache the nfs4_client in cstate! */
3238 cstate->clp = found;
3239 atomic_inc(&found->cl_refcount);
3240 return nfs_ok;
3241}
3242
Al Virob37ad282006-10-19 23:28:59 -07003243__be32
Andy Adamson66689582009-04-03 08:28:45 +03003244nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003245 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 clientid_t *clientid = &open->op_clientid;
3248 struct nfs4_client *clp = NULL;
3249 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003250 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003251 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003253 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003255 /*
3256 * In case we need it later, after we've already created the
3257 * file and don't want to risk a further failure:
3258 */
3259 open->op_file = nfsd4_alloc_file();
3260 if (open->op_file == NULL)
3261 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
Trond Myklebust2d91e892014-06-30 11:48:46 -04003263 status = lookup_clientid(clientid, cstate, nn);
3264 if (status)
3265 return status;
3266 clp = cstate->clp;
3267
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003268 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003269 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003270 open->op_openowner = oo;
3271 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003272 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003274 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003275 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003276 release_openowner(oo);
3277 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003278 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003279 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003280 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3281 if (status)
3282 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003283 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003284new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003285 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003286 if (oo == NULL)
3287 return nfserr_jukebox;
3288 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003289alloc_stateid:
3290 open->op_stp = nfs4_alloc_stateid(clp);
3291 if (!open->op_stp)
3292 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003293 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294}
3295
Al Virob37ad282006-10-19 23:28:59 -07003296static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003297nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3298{
3299 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3300 return nfserr_openmode;
3301 else
3302 return nfs_ok;
3303}
3304
Daniel Mackc47d8322011-05-16 16:38:14 +02003305static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003306{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003307 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3308}
3309
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003310static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003311{
3312 struct nfs4_stid *ret;
3313
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003314 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003315 if (!ret)
3316 return NULL;
3317 return delegstateid(ret);
3318}
3319
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003320static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3321{
3322 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3323 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3324}
3325
Al Virob37ad282006-10-19 23:28:59 -07003326static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003327nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003328 struct nfs4_delegation **dp)
3329{
3330 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003331 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07003332
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003333 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07003334 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003335 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003336 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07003337 status = nfs4_check_delegmode(*dp, flags);
3338 if (status)
3339 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003340out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003341 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003342 return nfs_ok;
3343 if (status)
3344 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003345 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003346 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003347}
3348
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003349static struct nfs4_ol_stateid *
3350nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003352 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003353 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354
Trond Myklebust1d31a252014-07-10 14:07:25 -04003355 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003356 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 /* ignore lock owners */
3358 if (local->st_stateowner->so_is_open_owner == 0)
3359 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003360 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003361 ret = local;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003362 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003365 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003366 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367}
3368
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003369static inline int nfs4_access_to_access(u32 nfs4_access)
3370{
3371 int flags = 0;
3372
3373 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3374 flags |= NFSD_MAY_READ;
3375 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3376 flags |= NFSD_MAY_WRITE;
3377 return flags;
3378}
3379
Al Virob37ad282006-10-19 23:28:59 -07003380static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3382 struct nfsd4_open *open)
3383{
3384 struct iattr iattr = {
3385 .ia_valid = ATTR_SIZE,
3386 .ia_size = 0,
3387 };
3388 if (!open->op_truncate)
3389 return 0;
3390 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003391 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3393}
3394
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003395static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003396 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3397 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003398{
Trond Myklebustde186432014-07-10 14:07:26 -04003399 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003400 __be32 status;
3401 int oflag = nfs4_access_to_omode(open->op_share_access);
3402 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003403 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003404
Trond Myklebustde186432014-07-10 14:07:26 -04003405 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003406
3407 /*
3408 * Are we trying to set a deny mode that would conflict with
3409 * current access?
3410 */
3411 status = nfs4_file_check_deny(fp, open->op_share_deny);
3412 if (status != nfs_ok) {
3413 spin_unlock(&fp->fi_lock);
3414 goto out;
3415 }
3416
3417 /* set access to the file */
3418 status = nfs4_file_get_access(fp, open->op_share_access);
3419 if (status != nfs_ok) {
3420 spin_unlock(&fp->fi_lock);
3421 goto out;
3422 }
3423
3424 /* Set access bits in stateid */
3425 old_access_bmap = stp->st_access_bmap;
3426 set_access(open->op_share_access, stp);
3427
3428 /* Set new deny mask */
3429 old_deny_bmap = stp->st_deny_bmap;
3430 set_deny(open->op_share_deny, stp);
3431 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3432
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003433 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003434 spin_unlock(&fp->fi_lock);
3435 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003436 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003437 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003438 spin_lock(&fp->fi_lock);
3439 if (!fp->fi_fds[oflag]) {
3440 fp->fi_fds[oflag] = filp;
3441 filp = NULL;
3442 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003443 }
Trond Myklebustde186432014-07-10 14:07:26 -04003444 spin_unlock(&fp->fi_lock);
3445 if (filp)
3446 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003447
3448 status = nfsd4_truncate(rqstp, cur_fh, open);
3449 if (status)
3450 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003451out:
3452 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003453out_put_access:
3454 stp->st_access_bmap = old_access_bmap;
3455 nfs4_file_put_access(fp, open->op_share_access);
3456 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3457 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003458}
3459
Al Virob37ad282006-10-19 23:28:59 -07003460static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003461nfs4_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 -07003462{
Al Virob37ad282006-10-19 23:28:59 -07003463 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003464 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003466 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003467 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003468
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003469 /* test and set deny mode */
3470 spin_lock(&fp->fi_lock);
3471 status = nfs4_file_check_deny(fp, open->op_share_deny);
3472 if (status == nfs_ok) {
3473 old_deny_bmap = stp->st_deny_bmap;
3474 set_deny(open->op_share_deny, stp);
3475 fp->fi_share_deny |=
3476 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3477 }
3478 spin_unlock(&fp->fi_lock);
3479
3480 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003483 status = nfsd4_truncate(rqstp, cur_fh, open);
3484 if (status != nfs_ok)
3485 reset_union_bmap_deny(old_deny_bmap, stp);
3486 return status;
3487}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003490nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003492 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493}
3494
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003495/* Should we give out recallable state?: */
3496static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3497{
3498 if (clp->cl_cb_state == NFSD4_CB_UP)
3499 return true;
3500 /*
3501 * In the sessions case, since we don't have to establish a
3502 * separate connection for callbacks, we assume it's OK
3503 * until we hear otherwise:
3504 */
3505 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3506}
3507
Jeff Laytond564fbe2014-07-16 10:31:58 -04003508static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003509{
3510 struct file_lock *fl;
3511
3512 fl = locks_alloc_lock();
3513 if (!fl)
3514 return NULL;
3515 locks_init_lock(fl);
3516 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003517 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003518 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3519 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003520 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003521 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003522 return fl;
3523}
3524
J. Bruce Fields99c41512013-05-21 16:21:25 -04003525static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003526{
Trond Myklebust11b91642014-07-29 21:34:08 -04003527 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003528 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003529 struct file *filp;
3530 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003531
Jeff Laytond564fbe2014-07-16 10:31:58 -04003532 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003533 if (!fl)
3534 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003535 filp = find_readable_file(fp);
3536 if (!filp) {
3537 /* We should always have a readable file here */
3538 WARN_ON_ONCE(1);
3539 return -EBADF;
3540 }
3541 fl->fl_file = filp;
3542 status = vfs_setlease(filp, fl->fl_type, &fl);
3543 if (status) {
3544 locks_free_lock(fl);
3545 goto out_fput;
3546 }
Benny Halevycdc97502014-05-30 09:09:30 -04003547 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003548 spin_lock(&fp->fi_lock);
3549 /* Did the lease get broken before we took the lock? */
3550 status = -EAGAIN;
3551 if (fp->fi_had_conflict)
3552 goto out_unlock;
3553 /* Race breaker */
3554 if (fp->fi_lease) {
3555 status = 0;
3556 atomic_inc(&fp->fi_delegees);
3557 hash_delegation_locked(dp, fp);
3558 goto out_unlock;
3559 }
3560 fp->fi_lease = fl;
3561 fp->fi_deleg_file = filp;
3562 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003563 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003564 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003565 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003566 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003567out_unlock:
3568 spin_unlock(&fp->fi_lock);
3569 spin_unlock(&state_lock);
3570out_fput:
3571 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003572 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003573}
3574
Jeff Layton0b266932014-07-25 07:34:25 -04003575static struct nfs4_delegation *
3576nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3577 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003578{
Jeff Layton0b266932014-07-25 07:34:25 -04003579 int status;
3580 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003581
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003582 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003583 return ERR_PTR(-EAGAIN);
3584
3585 dp = alloc_init_deleg(clp, fh);
3586 if (!dp)
3587 return ERR_PTR(-ENOMEM);
3588
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003589 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003590 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003591 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003592 dp->dl_stid.sc_file = fp;
Jeff Layton417c6622014-07-21 09:34:57 -04003593 if (!fp->fi_lease) {
3594 spin_unlock(&fp->fi_lock);
3595 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003596 status = nfs4_setlease(dp);
3597 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003598 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003599 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003600 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003601 status = -EAGAIN;
3602 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003603 }
Benny Halevy931ee562014-05-30 09:09:27 -04003604 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003605 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003606out_unlock:
3607 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003608 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003609out:
3610 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003611 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003612 return ERR_PTR(status);
3613 }
3614 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003615}
3616
Benny Halevy4aa89132012-02-21 14:16:44 -08003617static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3618{
3619 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3620 if (status == -EAGAIN)
3621 open->op_why_no_deleg = WND4_CONTENTION;
3622 else {
3623 open->op_why_no_deleg = WND4_RESOURCE;
3624 switch (open->op_deleg_want) {
3625 case NFS4_SHARE_WANT_READ_DELEG:
3626 case NFS4_SHARE_WANT_WRITE_DELEG:
3627 case NFS4_SHARE_WANT_ANY_DELEG:
3628 break;
3629 case NFS4_SHARE_WANT_CANCEL:
3630 open->op_why_no_deleg = WND4_CANCELLED;
3631 break;
3632 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003633 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003634 }
3635 }
3636}
3637
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638/*
3639 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003640 *
3641 * Note we don't support write delegations, and won't until the vfs has
3642 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 */
3644static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003645nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3646 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647{
3648 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003649 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3650 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003651 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003652 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003654 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003655 open->op_recall = 0;
3656 switch (open->op_claim_type) {
3657 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003658 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003659 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003660 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3661 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003662 break;
3663 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003664 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003665 /*
3666 * Let's not give out any delegations till everyone's
3667 * had the chance to reclaim theirs....
3668 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003669 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003670 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003671 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003672 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003673 /*
3674 * Also, if the file was opened for write or
3675 * create, there's a good chance the client's
3676 * about to write to it, resulting in an
3677 * immediate recall (since we don't support
3678 * write delegations):
3679 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003680 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003681 goto out_no_deleg;
3682 if (open->op_create == NFS4_OPEN_CREATE)
3683 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003684 break;
3685 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003686 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003687 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003688 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003689 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003690 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003692 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003694 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003695 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003696 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003697 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003698out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003699 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3700 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003701 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003702 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003703 open->op_recall = 1;
3704 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003705
3706 /* 4.1 client asking for a delegation? */
3707 if (open->op_deleg_want)
3708 nfsd4_open_deleg_none_ext(open, status);
3709 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710}
3711
Benny Halevye27f49c2012-02-21 14:16:54 -08003712static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3713 struct nfs4_delegation *dp)
3714{
3715 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3716 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3717 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3718 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3719 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3720 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3721 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3722 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3723 }
3724 /* Otherwise the client must be confused wanting a delegation
3725 * it already has, therefore we don't return
3726 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3727 */
3728}
3729
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730/*
3731 * called with nfs4_lock_state() held.
3732 */
Al Virob37ad282006-10-19 23:28:59 -07003733__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3735{
Andy Adamson66689582009-04-03 08:28:45 +03003736 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003737 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003739 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003740 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003741 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 /*
3744 * Lookup file; if found, lookup stateid and check open request,
3745 * and check for delegations in the process of being recalled.
3746 * If not found, create the nfs4_file struct
3747 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003748 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04003749 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04003750 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003751 if (status)
3752 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003753 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04003755 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003756 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003757 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003758 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003759 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 }
3761
3762 /*
3763 * OPEN the file, or upgrade an existing OPEN.
3764 * If truncate fails, the OPEN fails.
3765 */
3766 if (stp) {
3767 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003768 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 if (status)
3770 goto out;
3771 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003772 stp = open->op_stp;
3773 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003774 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003775 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3776 if (status) {
3777 release_open_stateid(stp);
3778 goto out;
3779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003781 update_stateid(&stp->st_stid.sc_stateid);
3782 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783
Benny Halevyd24433c2012-02-16 20:57:17 +02003784 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02003785 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3786 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3787 open->op_why_no_deleg = WND4_NOT_WANTED;
3788 goto nodeleg;
3789 }
3790 }
3791
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 /*
3793 * Attempt to hand out a delegation. No error return, because the
3794 * OPEN succeeds even if we fail.
3795 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003796 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003797nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 status = nfs_ok;
3799
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003800 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003801 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003803 /* 4.1 client trying to upgrade/downgrade delegation? */
3804 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003805 open->op_deleg_want)
3806 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003807
NeilBrown13cd2182005-06-23 22:03:10 -07003808 if (fp)
3809 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003810 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003811 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 /*
3813 * To finish the open response, we just need to set the rflags.
3814 */
3815 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003816 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003817 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3819
3820 return status;
3821}
3822
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003823void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3824{
3825 if (open->op_openowner) {
3826 struct nfs4_openowner *oo = open->op_openowner;
3827
3828 if (!list_empty(&oo->oo_owner.so_stateids))
3829 list_del_init(&oo->oo_close_lru);
3830 if (oo->oo_flags & NFS4_OO_NEW) {
3831 if (status) {
3832 release_openowner(oo);
3833 open->op_openowner = NULL;
3834 } else
3835 oo->oo_flags &= ~NFS4_OO_NEW;
3836 }
3837 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003838 if (open->op_file)
3839 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003840 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04003841 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003842}
3843
Al Virob37ad282006-10-19 23:28:59 -07003844__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003845nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3846 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847{
3848 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003849 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003850 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851
3852 nfs4_lock_state();
3853 dprintk("process_renew(%08x/%08x): starting\n",
3854 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003855 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003856 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003858 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003860 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003861 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 goto out;
3863 status = nfs_ok;
3864out:
3865 nfs4_unlock_state();
3866 return status;
3867}
3868
NeilBrowna76b4312005-06-23 22:04:01 -07003869static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003870nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07003871{
Jeff Layton33dcc482012-04-10 11:08:48 -04003872 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003873 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04003874 return;
3875
NeilBrowna76b4312005-06-23 22:04:01 -07003876 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003877 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003878 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003879 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003880 /*
3881 * Now that every NFSv4 client has had the chance to recover and
3882 * to see the (possibly new, possibly shorter) lease time, we
3883 * can safely set the next grace time to the current lease time:
3884 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03003885 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003886}
3887
NeilBrownfd39ca92005-06-23 22:04:03 -07003888static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003889nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890{
3891 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003892 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 struct nfs4_delegation *dp;
3894 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003895 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003896 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897
3898 nfs4_lock_state();
3899
3900 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003901 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03003902 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003903 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03003904 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 clp = list_entry(pos, struct nfs4_client, cl_lru);
3906 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3907 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003908 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 break;
3910 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003911 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03003912 dprintk("NFSD: client in use (clientid %08x)\n",
3913 clp->cl_clientid.cl_id);
3914 continue;
3915 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003916 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003917 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003918 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03003919 list_for_each_safe(pos, next, &reaplist) {
3920 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 dprintk("NFSD: purging unused client (clientid %08x)\n",
3922 clp->cl_clientid.cl_id);
3923 expire_client(clp);
3924 }
Benny Halevycdc97502014-05-30 09:09:30 -04003925 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003926 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03003928 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3929 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003931 t = dp->dl_time - cutoff;
3932 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 break;
3934 }
Jeff Layton42690672014-07-25 07:34:20 -04003935 unhash_delegation_locked(dp);
3936 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 }
Benny Halevycdc97502014-05-30 09:09:30 -04003938 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04003939 while (!list_empty(&reaplist)) {
3940 dp = list_first_entry(&reaplist, struct nfs4_delegation,
3941 dl_recall_lru);
3942 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003943 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003945 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003946 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3947 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003948 t = oo->oo_time - cutoff;
3949 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 break;
3951 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003952 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04003954 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04003956 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957}
3958
Harvey Harrisona254b242008-02-20 12:49:00 -08003959static struct workqueue_struct *laundry_wq;
3960static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08003961
3962static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003963laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964{
3965 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003966 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3967 work);
3968 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3969 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003971 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003973 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974}
3975
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003976static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07003977{
Trond Myklebust11b91642014-07-29 21:34:08 -04003978 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003979 return nfserr_bad_stateid;
3980 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981}
3982
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003984access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003986 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3987 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3988 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989}
3990
3991static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003992access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003994 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3995 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996}
3997
3998static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003999__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000{
Al Virob37ad282006-10-19 23:28:59 -07004001 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002
J. Bruce Fields02921912010-07-29 15:16:59 -04004003 /* For lock stateid's, we test the parent open, not the lock: */
4004 if (stp->st_openstp)
4005 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004006 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004008 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 goto out;
4010 status = nfs_ok;
4011out:
4012 return status;
4013}
4014
Al Virob37ad282006-10-19 23:28:59 -07004015static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004016check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004018 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004020 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004021 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 * conflicting state; so we must wait out the grace period. */
4023 return nfserr_grace;
4024 } else if (flags & WR_STATE)
4025 return nfs4_share_conflict(current_fh,
4026 NFS4_SHARE_DENY_WRITE);
4027 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4028 return nfs4_share_conflict(current_fh,
4029 NFS4_SHARE_DENY_READ);
4030}
4031
4032/*
4033 * Allow READ/WRITE during grace period on recovered state only for files
4034 * that are not able to provide mandatory locking.
4035 */
4036static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004037grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004039 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040}
4041
J. Bruce Fields81b82962011-08-23 11:03:29 -04004042/* Returns true iff a is later than b: */
4043static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4044{
Jim Rees1a9357f2013-05-17 17:33:00 -04004045 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004046}
4047
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004048static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004049{
Andy Adamson66689582009-04-03 08:28:45 +03004050 /*
4051 * When sessions are used the stateid generation number is ignored
4052 * when it is zero.
4053 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004054 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004055 return nfs_ok;
4056
4057 if (in->si_generation == ref->si_generation)
4058 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004059
J. Bruce Fields0836f582008-01-26 19:08:12 -05004060 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004061 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004062 return nfserr_bad_stateid;
4063 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004064 * However, we could see a stateid from the past, even from a
4065 * non-buggy client. For example, if the client sends a lock
4066 * while some IO is outstanding, the lock may bump si_generation
4067 * while the IO is still in flight. The client could avoid that
4068 * situation by waiting for responses on all the IO requests,
4069 * but better performance may result in retrying IO that
4070 * receives an old_stateid error if requests are rarely
4071 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004072 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004073 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004074}
4075
Chuck Lever7df302f2012-05-29 13:56:37 -04004076static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004077{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004078 struct nfs4_stid *s;
4079 struct nfs4_ol_stateid *ols;
4080 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004081
Chuck Lever7df302f2012-05-29 13:56:37 -04004082 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4083 return nfserr_bad_stateid;
4084 /* Client debugging aid. */
4085 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4086 char addr_str[INET6_ADDRSTRLEN];
4087 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4088 sizeof(addr_str));
4089 pr_warn_ratelimited("NFSD: client %s testing state ID "
4090 "with incorrect client ID\n", addr_str);
4091 return nfserr_bad_stateid;
4092 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004093 s = find_stateid(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004094 if (!s)
Chuck Lever7df302f2012-05-29 13:56:37 -04004095 return nfserr_bad_stateid;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004096 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004097 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004098 return status;
J. Bruce Fields23340032013-04-09 17:42:28 -04004099 switch (s->sc_type) {
4100 case NFS4_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004101 return nfs_ok;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004102 case NFS4_REVOKED_DELEG_STID:
4103 return nfserr_deleg_revoked;
J. Bruce Fields23340032013-04-09 17:42:28 -04004104 case NFS4_OPEN_STID:
4105 case NFS4_LOCK_STID:
4106 ols = openlockstateid(s);
4107 if (ols->st_stateowner->so_is_open_owner
4108 && !(openowner(ols->st_stateowner)->oo_flags
4109 & NFS4_OO_CONFIRMED))
4110 return nfserr_bad_stateid;
4111 return nfs_ok;
4112 default:
4113 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004114 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004115 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004116 case NFS4_CLOSED_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004117 return nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004118 }
Bryan Schumaker17456802011-07-13 10:50:48 -04004119}
4120
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004121static __be32
4122nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4123 stateid_t *stateid, unsigned char typemask,
4124 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004125{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004126 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004127
4128 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4129 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004130 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004131 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004132 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004133 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004134 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004135 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004136 if (status)
4137 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004138 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004139 if (!*s)
4140 return nfserr_bad_stateid;
4141 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004142}
4143
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144/*
4145* Checks for stateid operations
4146*/
Al Virob37ad282006-10-19 23:28:59 -07004147__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004148nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004149 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004151 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004152 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004154 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004156 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004157 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004158 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 if (filpp)
4161 *filpp = NULL;
4162
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004163 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 return nfserr_grace;
4165
4166 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004167 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168
Trond Myklebust14bcab12014-04-18 14:44:07 -04004169 nfs4_lock_state();
4170
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004171 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004172 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004173 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004174 if (status)
Trond Myklebust14bcab12014-04-18 14:44:07 -04004175 goto out;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004176 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4177 if (status)
4178 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004179 switch (s->sc_type) {
4180 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004181 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004182 status = nfs4_check_delegmode(dp, flags);
4183 if (status)
4184 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004185 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004186 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004187 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004188 WARN_ON_ONCE(1);
4189 status = nfserr_serverfault;
4190 goto out;
4191 }
Trond Myklebustde186432014-07-10 14:07:26 -04004192 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004193 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004194 break;
4195 case NFS4_OPEN_STID:
4196 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004197 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004198 status = nfs4_check_fh(current_fh, stp);
4199 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004201 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004202 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004204 status = nfs4_check_openmode(stp, flags);
4205 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004207 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004208 struct nfs4_file *fp = stp->st_stid.sc_file;
4209
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004210 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004211 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004212 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004213 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004214 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004215 break;
4216 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004217 status = nfserr_bad_stateid;
4218 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 }
4220 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004221 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004222 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223out:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004224 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 return status;
4226}
4227
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004228static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004229nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004230{
J. Bruce Fieldsa1b8ff42014-05-20 15:55:21 -04004231 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4232
Trond Myklebust11b91642014-07-29 21:34:08 -04004233 if (check_for_locks(stp->st_stid.sc_file, lo))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004234 return nfserr_locks_held;
Jeff Laytonc53530d2014-06-30 11:48:39 -04004235 release_lockowner_if_empty(lo);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004236 return nfs_ok;
4237}
4238
4239/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004240 * Test if the stateid is valid
4241 */
4242__be32
4243nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4244 struct nfsd4_test_stateid *test_stateid)
4245{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004246 struct nfsd4_test_stateid_id *stateid;
4247 struct nfs4_client *cl = cstate->session->se_client;
4248
4249 nfs4_lock_state();
4250 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004251 stateid->ts_id_status =
4252 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004253 nfs4_unlock_state();
4254
Bryan Schumaker17456802011-07-13 10:50:48 -04004255 return nfs_ok;
4256}
4257
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004258__be32
4259nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4260 struct nfsd4_free_stateid *free_stateid)
4261{
4262 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004263 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004264 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004265 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004266 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004267
4268 nfs4_lock_state();
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004269 s = find_stateid(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004270 if (!s)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004271 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004272 switch (s->sc_type) {
4273 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004274 ret = nfserr_locks_held;
4275 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004276 case NFS4_OPEN_STID:
4277 case NFS4_LOCK_STID:
4278 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4279 if (ret)
4280 goto out;
4281 if (s->sc_type == NFS4_LOCK_STID)
4282 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4283 else
4284 ret = nfserr_locks_held;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004285 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004286 case NFS4_REVOKED_DELEG_STID:
4287 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004288 spin_lock(&cl->cl_lock);
4289 list_del_init(&dp->dl_recall_lru);
4290 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004291 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004292 ret = nfs_ok;
4293 break;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004294 default:
4295 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004296 }
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004297out:
4298 nfs4_unlock_state();
4299 return ret;
4300}
4301
NeilBrown4c4cd222005-07-07 17:59:27 -07004302static inline int
4303setlkflg (int type)
4304{
4305 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4306 RD_STATE : WR_STATE;
4307}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004309static __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 -04004310{
4311 struct svc_fh *current_fh = &cstate->current_fh;
4312 struct nfs4_stateowner *sop = stp->st_stateowner;
4313 __be32 status;
4314
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004315 status = nfsd4_check_seqid(cstate, sop, seqid);
4316 if (status)
4317 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004318 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4319 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004320 /*
4321 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004322 * nfserr_replay_me from the previous step, and
4323 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004324 */
4325 return nfserr_bad_stateid;
4326 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4327 if (status)
4328 return status;
4329 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004330}
4331
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332/*
4333 * Checks for sequence id mutating operations.
4334 */
Al Virob37ad282006-10-19 23:28:59 -07004335static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004336nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004337 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004338 struct nfs4_ol_stateid **stpp,
4339 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004341 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004342 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004343 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004345 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4346 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004347
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004349 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004350 if (status)
4351 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004352 stp = openlockstateid(s);
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04004353 if (!nfsd4_has_session(cstate))
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004354 cstate->replay_owner = stp->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004356 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4357 if (!status)
4358 *stpp = stp;
4359 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004360}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004361
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004362static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4363 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004364{
4365 __be32 status;
4366 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004368 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004369 NFS4_OPEN_STID, stpp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004370 if (status)
4371 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004372 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004373 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07004374 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004375 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376}
4377
Al Virob37ad282006-10-19 23:28:59 -07004378__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004379nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004380 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381{
Al Virob37ad282006-10-19 23:28:59 -07004382 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004383 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004384 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004385 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386
Al Viroa6a9f182013-09-16 10:57:01 -04004387 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4388 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004390 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004391 if (status)
4392 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393
4394 nfs4_lock_state();
4395
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004396 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004397 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004398 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004399 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004400 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004401 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004402 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004403 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004404 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004405 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004406 update_stateid(&stp->st_stid.sc_stateid);
4407 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004408 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004409 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004410
Jeff Layton2a4317c2012-03-21 16:42:43 -04004411 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004412 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004414 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004415 if (!cstate->replay_owner)
4416 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 return status;
4418}
4419
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004420static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004422 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004423 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004424 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004425 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004426}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004427
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004428static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4429{
4430 switch (to_access) {
4431 case NFS4_SHARE_ACCESS_READ:
4432 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4433 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4434 break;
4435 case NFS4_SHARE_ACCESS_WRITE:
4436 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4437 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4438 break;
4439 case NFS4_SHARE_ACCESS_BOTH:
4440 break;
4441 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004442 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 }
4444}
4445
Al Virob37ad282006-10-19 23:28:59 -07004446__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004447nfsd4_open_downgrade(struct svc_rqst *rqstp,
4448 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004449 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450{
Al Virob37ad282006-10-19 23:28:59 -07004451 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004452 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004453 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454
Al Viroa6a9f182013-09-16 10:57:01 -04004455 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4456 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004458 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004459 if (od->od_deleg_want)
4460 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4461 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
4463 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004464 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004465 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004466 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004469 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004470 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 stp->st_access_bmap, od->od_share_access);
4472 goto out;
4473 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004474 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004475 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 stp->st_deny_bmap, od->od_share_deny);
4477 goto out;
4478 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004479 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480
Jeff Laytonce0fc432012-05-11 09:45:14 -04004481 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004483 update_stateid(&stp->st_stid.sc_stateid);
4484 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 status = nfs_ok;
4486out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004487 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004488 if (!cstate->replay_owner)
4489 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 return status;
4491}
4492
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004493static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4494{
Trond Myklebustacf92952014-06-30 11:48:37 -04004495 struct nfs4_client *clp = s->st_stid.sc_client;
4496 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4497
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004498 s->st_stid.sc_type = NFS4_CLOSED_STID;
Trond Myklebustacf92952014-06-30 11:48:37 -04004499 unhash_open_stateid(s);
4500
4501 if (clp->cl_minorversion) {
Trond Myklebustacf92952014-06-30 11:48:37 -04004502 if (list_empty(&oo->oo_owner.so_stateids))
4503 release_openowner(oo);
Trond Myklebust60116952014-07-29 21:34:06 -04004504 nfs4_put_stid(&s->st_stid);
Trond Myklebustacf92952014-06-30 11:48:37 -04004505 } else {
Trond Myklebust60116952014-07-29 21:34:06 -04004506 /*
4507 * In the 4.0 case we need to keep the owners around a
4508 * little while to handle CLOSE replay. We still do need
4509 * to release any file access that is held by them
4510 * before returning however.
4511 */
4512 release_all_access(s);
Trond Myklebust11b91642014-07-29 21:34:08 -04004513 if (s->st_stid.sc_file) {
4514 put_nfs4_file(s->st_stid.sc_file);
4515 s->st_stid.sc_file = NULL;
Trond Myklebustf8338832014-07-25 07:34:19 -04004516 }
Trond Myklebustacf92952014-06-30 11:48:37 -04004517 oo->oo_last_closed_stid = s;
Trond Myklebustacf92952014-06-30 11:48:37 -04004518 if (list_empty(&oo->oo_owner.so_stateids))
4519 move_to_close_lru(oo, clp->net);
4520 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004521}
4522
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523/*
4524 * nfs4_unlock_state() called after encode
4525 */
Al Virob37ad282006-10-19 23:28:59 -07004526__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004527nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004528 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529{
Al Virob37ad282006-10-19 23:28:59 -07004530 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004531 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004532 struct net *net = SVC_NET(rqstp);
4533 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534
Al Viroa6a9f182013-09-16 10:57:01 -04004535 dprintk("NFSD: nfsd4_close on file %pd\n",
4536 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537
4538 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004539 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4540 &close->cl_stateid,
4541 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004542 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004543 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004544 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004546 update_stateid(&stp->st_stid.sc_stateid);
4547 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004549 nfsd4_close_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004551 if (!cstate->replay_owner)
4552 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 return status;
4554}
4555
Al Virob37ad282006-10-19 23:28:59 -07004556__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004557nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4558 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004560 struct nfs4_delegation *dp;
4561 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004562 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004563 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004564 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004566 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004567 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568
4569 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004570 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004571 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004572 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004573 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004574 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004575 if (status)
4576 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004577
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004578 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004580 nfs4_unlock_state();
4581
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 return status;
4583}
4584
4585
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
Benny Halevy87df4de2008-12-15 19:42:03 +02004588static inline u64
4589end_offset(u64 start, u64 len)
4590{
4591 u64 end;
4592
4593 end = start + len;
4594 return end >= start ? end: NFS4_MAX_UINT64;
4595}
4596
4597/* last octet in a range */
4598static inline u64
4599last_byte_offset(u64 start, u64 len)
4600{
4601 u64 end;
4602
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004603 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004604 end = start + len;
4605 return end > start ? end - 1: NFS4_MAX_UINT64;
4606}
4607
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608/*
4609 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4610 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4611 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4612 * locking, this prevents us from being completely protocol-compliant. The
4613 * real solution to this problem is to start using unsigned file offsets in
4614 * the VFS, but this is a very deep change!
4615 */
4616static inline void
4617nfs4_transform_lock_offset(struct file_lock *lock)
4618{
4619 if (lock->fl_start < 0)
4620 lock->fl_start = OFFSET_MAX;
4621 if (lock->fl_end < 0)
4622 lock->fl_end = OFFSET_MAX;
4623}
4624
NeilBrownd5b90262006-04-10 22:55:22 -07004625/* Hack!: For now, we're defining this just so we can use a pointer to it
4626 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004627static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004628};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629
4630static inline void
4631nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4632{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004633 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634
NeilBrownd5b90262006-04-10 22:55:22 -07004635 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004636 lo = (struct nfs4_lockowner *) fl->fl_owner;
4637 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4638 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004639 if (!deny->ld_owner.data)
4640 /* We just don't care that much */
4641 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004642 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4643 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004644 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004645nevermind:
4646 deny->ld_owner.len = 0;
4647 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004648 deny->ld_clientid.cl_boot = 0;
4649 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 }
4651 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004652 deny->ld_length = NFS4_MAX_UINT64;
4653 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4655 deny->ld_type = NFS4_READ_LT;
4656 if (fl->fl_type != F_RDLCK)
4657 deny->ld_type = NFS4_WRITE_LT;
4658}
4659
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004660static struct nfs4_lockowner *
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004661find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4662 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663{
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004664 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4665 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004667 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4668 if (so->so_is_open_owner)
4669 continue;
4670 if (!same_owner_str(so, owner, clid))
4671 continue;
4672 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 }
4674 return NULL;
4675}
4676
4677/*
4678 * Alloc a lock owner structure.
4679 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004680 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004682 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004684static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004685alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004686 struct nfs4_lockowner *lo;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004687 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004689 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4690 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004692 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4693 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07004694 /* It is the openowner seqid that will be incremented in encode in the
4695 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004696 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004697 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004698 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699}
4700
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004701static struct nfs4_ol_stateid *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004702alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp,
4703 struct inode *inode,
4704 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004706 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004707 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
J. Bruce Fields996e0932011-10-17 11:14:48 -04004709 stp = nfs4_alloc_stateid(clp);
NeilBrown5ac049a2005-06-23 22:03:03 -07004710 if (stp == NULL)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04004711 return NULL;
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004712 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004713 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4714 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07004715 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004716 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004717 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004719 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004720 list_add(&stp->st_locks, &open_stp->st_locks);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004721 spin_lock(&fp->fi_lock);
4722 list_add(&stp->st_perfile, &fp->fi_stateids);
4723 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 return stp;
4725}
4726
Jeff Laytonc53530d2014-06-30 11:48:39 -04004727static struct nfs4_ol_stateid *
4728find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4729{
4730 struct nfs4_ol_stateid *lst;
4731
4732 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004733 if (lst->st_stid.sc_file == fp)
Jeff Laytonc53530d2014-06-30 11:48:39 -04004734 return lst;
4735 }
4736 return NULL;
4737}
4738
4739
NeilBrownfd39ca92005-06-23 22:04:03 -07004740static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741check_lock_length(u64 offset, u64 length)
4742{
Benny Halevy87df4de2008-12-15 19:42:03 +02004743 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 LOFF_OVERFLOW(offset, length)));
4745}
4746
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004747static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004748{
Trond Myklebust11b91642014-07-29 21:34:08 -04004749 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004750
Jeff Layton7214e862014-07-10 14:07:33 -04004751 lockdep_assert_held(&fp->fi_lock);
4752
Jeff Layton82c5ff12012-05-11 09:45:13 -04004753 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004754 return;
Jeff Layton12659652014-07-10 14:07:28 -04004755 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004756 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004757}
4758
J. Bruce Fields2355c592012-04-25 16:56:22 -04004759static __be32 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, struct nfs4_ol_stateid *ost, struct nfsd4_lock *lock, struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004760{
Trond Myklebust11b91642014-07-29 21:34:08 -04004761 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004762 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4763 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004764 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004765 struct nfs4_lockowner *lo;
4766 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004767 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004768
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004769 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004770 if (!lo) {
4771 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4772 &lock->v.new.owner);
4773 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4774 if (lo == NULL)
4775 return nfserr_jukebox;
4776 } else {
4777 /* with an existing lockowner, seqids must be the same */
4778 if (!cstate->minorversion &&
4779 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004780 return nfserr_bad_seqid;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004781 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004782
4783 *lst = find_lock_stateid(lo, fi);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004784 if (*lst == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004785 *lst = alloc_init_lock_stateid(lo, fi, inode, ost);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004786 if (*lst == NULL) {
4787 release_lockowner_if_empty(lo);
4788 return nfserr_jukebox;
4789 }
4790 *new = true;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004791 }
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004792 return nfs_ok;
4793}
4794
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795/*
4796 * LOCK operation
4797 */
Al Virob37ad282006-10-19 23:28:59 -07004798__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004799nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004800 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004802 struct nfs4_openowner *open_sop = NULL;
4803 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004804 struct nfs4_ol_stateid *lock_stp;
Jeff Layton7214e862014-07-10 14:07:33 -04004805 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004806 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004807 struct file_lock *file_lock = NULL;
4808 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004809 __be32 status = 0;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004810 bool new_state = false;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004811 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004812 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004813 struct net *net = SVC_NET(rqstp);
4814 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815
4816 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4817 (long long) lock->lk_offset,
4818 (long long) lock->lk_length);
4819
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 if (check_lock_length(lock->lk_offset, lock->lk_length))
4821 return nfserr_inval;
4822
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004823 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004824 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004825 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4826 return status;
4827 }
4828
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 nfs4_lock_state();
4830
4831 if (lock->lk_is_new) {
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004832 struct nfs4_ol_stateid *open_stp = NULL;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004833
4834 if (nfsd4_has_session(cstate))
4835 /* See rfc 5661 18.10.3: given clientid is ignored: */
4836 memcpy(&lock->v.new.clientid,
4837 &cstate->session->se_client->cl_clientid,
4838 sizeof(clientid_t));
4839
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004841 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004845 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 lock->lk_new_open_seqid,
4847 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004848 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07004849 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004851 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004852 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004853 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004854 &lock->v.new.clientid))
4855 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004856 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4857 &lock_stp, &new_state);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004858 } else
Benny Halevydd453df2009-04-03 08:28:41 +03004859 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004860 lock->lk_old_lock_seqid,
4861 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004862 NFS4_LOCK_STID, &lock_stp, nn);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004863 if (status)
4864 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004865 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004867 lkflg = setlkflg(lock->lk_type);
4868 status = nfs4_check_openmode(lock_stp, lkflg);
4869 if (status)
4870 goto out;
4871
NeilBrown0dd395d2005-07-07 17:59:15 -07004872 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004873 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004874 goto out;
4875 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004876 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004877 goto out;
4878
Jeff Layton21179d82012-08-21 08:03:32 -04004879 file_lock = locks_alloc_lock();
4880 if (!file_lock) {
4881 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4882 status = nfserr_jukebox;
4883 goto out;
4884 }
4885
Trond Myklebust11b91642014-07-29 21:34:08 -04004886 fp = lock_stp->st_stid.sc_file;
Jeff Layton21179d82012-08-21 08:03:32 -04004887 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 switch (lock->lk_type) {
4889 case NFS4_READ_LT:
4890 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04004891 spin_lock(&fp->fi_lock);
4892 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004893 if (filp)
4894 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04004895 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04004896 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004897 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 case NFS4_WRITE_LT:
4899 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04004900 spin_lock(&fp->fi_lock);
4901 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004902 if (filp)
4903 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04004904 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04004905 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004906 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 default:
4908 status = nfserr_inval;
4909 goto out;
4910 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004911 if (!filp) {
4912 status = nfserr_openmode;
4913 goto out;
4914 }
Jeff Layton21179d82012-08-21 08:03:32 -04004915 file_lock->fl_owner = (fl_owner_t)lock_sop;
4916 file_lock->fl_pid = current->tgid;
4917 file_lock->fl_file = filp;
4918 file_lock->fl_flags = FL_POSIX;
4919 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4920 file_lock->fl_start = lock->lk_offset;
4921 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4922 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923
Jeff Layton21179d82012-08-21 08:03:32 -04004924 conflock = locks_alloc_lock();
4925 if (!conflock) {
4926 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4927 status = nfserr_jukebox;
4928 goto out;
4929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930
Jeff Layton21179d82012-08-21 08:03:32 -04004931 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004932 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004934 update_stateid(&lock_stp->st_stid.sc_stateid);
4935 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004937 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004938 break;
4939 case (EAGAIN): /* conflock holds conflicting lock */
4940 status = nfserr_denied;
4941 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04004942 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004943 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 case (EDEADLK):
4945 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004946 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004947 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004948 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004949 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004950 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952out:
Trond Myklebustde186432014-07-10 14:07:26 -04004953 if (filp)
4954 fput(filp);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004955 if (status && new_state)
Jeff Laytonc53530d2014-06-30 11:48:39 -04004956 release_lock_stateid(lock_stp);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004957 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004958 if (!cstate->replay_owner)
4959 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004960 if (file_lock)
4961 locks_free_lock(file_lock);
4962 if (conflock)
4963 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 return status;
4965}
4966
4967/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004968 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4969 * so we do a temporary open here just to get an open file to pass to
4970 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4971 * inode operation.)
4972 */
Al Viro04da6e92012-04-13 00:00:04 -04004973static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004974{
4975 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04004976 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4977 if (!err) {
4978 err = nfserrno(vfs_test_lock(file, lock));
4979 nfsd_close(file);
4980 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004981 return err;
4982}
4983
4984/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 * LOCKT operation
4986 */
Al Virob37ad282006-10-19 23:28:59 -07004987__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004988nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4989 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990{
Jeff Layton21179d82012-08-21 08:03:32 -04004991 struct file_lock *file_lock = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004992 struct nfs4_lockowner *lo;
Al Virob37ad282006-10-19 23:28:59 -07004993 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004994 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004996 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 return nfserr_grace;
4998
4999 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5000 return nfserr_inval;
5001
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 nfs4_lock_state();
5003
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005004 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005005 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005006 if (status)
5007 goto out;
5008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005010 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012
Jeff Layton21179d82012-08-21 08:03:32 -04005013 file_lock = locks_alloc_lock();
5014 if (!file_lock) {
5015 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5016 status = nfserr_jukebox;
5017 goto out;
5018 }
5019 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 switch (lockt->lt_type) {
5021 case NFS4_READ_LT:
5022 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005023 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 break;
5025 case NFS4_WRITE_LT:
5026 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005027 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 break;
5029 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005030 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 status = nfserr_inval;
5032 goto out;
5033 }
5034
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005035 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005036 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005037 file_lock->fl_owner = (fl_owner_t)lo;
5038 file_lock->fl_pid = current->tgid;
5039 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040
Jeff Layton21179d82012-08-21 08:03:32 -04005041 file_lock->fl_start = lockt->lt_offset;
5042 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043
Jeff Layton21179d82012-08-21 08:03:32 -04005044 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045
Jeff Layton21179d82012-08-21 08:03:32 -04005046 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005047 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005048 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005049
Jeff Layton21179d82012-08-21 08:03:32 -04005050 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005052 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 }
5054out:
5055 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005056 if (file_lock)
5057 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 return status;
5059}
5060
Al Virob37ad282006-10-19 23:28:59 -07005061__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005062nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005063 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005065 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005067 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005068 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005069 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005070 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5071
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5073 (long long) locku->lu_offset,
5074 (long long) locku->lu_length);
5075
5076 if (check_lock_length(locku->lu_offset, locku->lu_length))
5077 return nfserr_inval;
5078
5079 nfs4_lock_state();
5080
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005081 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005082 &locku->lu_stateid, NFS4_LOCK_STID,
5083 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005084 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005086 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005087 if (!filp) {
5088 status = nfserr_lock_range;
5089 goto out;
5090 }
Jeff Layton21179d82012-08-21 08:03:32 -04005091 file_lock = locks_alloc_lock();
5092 if (!file_lock) {
5093 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5094 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005095 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005096 }
5097 locks_init_lock(file_lock);
5098 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005099 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005100 file_lock->fl_pid = current->tgid;
5101 file_lock->fl_file = filp;
5102 file_lock->fl_flags = FL_POSIX;
5103 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5104 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105
Jeff Layton21179d82012-08-21 08:03:32 -04005106 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5107 locku->lu_length);
5108 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109
Jeff Layton21179d82012-08-21 08:03:32 -04005110 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005111 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005112 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 goto out_nfserr;
5114 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005115 update_stateid(&stp->st_stid.sc_stateid);
5116 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005117fput:
5118 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005120 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields71c3bcd2011-09-27 21:42:29 -04005121 if (!cstate->replay_owner)
5122 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005123 if (file_lock)
5124 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 return status;
5126
5127out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005128 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005129 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130}
5131
5132/*
5133 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005134 * true: locks held by lockowner
5135 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005137static bool
5138check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139{
5140 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005141 int status = false;
5142 struct file *filp = find_any_file(fp);
5143 struct inode *inode;
5144
5145 if (!filp) {
5146 /* Any valid lock stateid should have some sort of access */
5147 WARN_ON_ONCE(1);
5148 return status;
5149 }
5150
5151 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152
Jeff Layton1c8c6012013-06-21 08:58:15 -04005153 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005155 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005156 status = true;
5157 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005160 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005161 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 return status;
5163}
5164
Al Virob37ad282006-10-19 23:28:59 -07005165__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005166nfsd4_release_lockowner(struct svc_rqst *rqstp,
5167 struct nfsd4_compound_state *cstate,
5168 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169{
5170 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Laytonfd449072014-06-30 11:48:41 -04005171 struct nfs4_stateowner *sop = NULL, *tmp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005172 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005173 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 struct xdr_netobj *owner = &rlockowner->rl_owner;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005175 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07005176 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005177 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178
5179 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5180 clid->cl_boot, clid->cl_id);
5181
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 nfs4_lock_state();
5183
Jeff Layton4b24ca72014-06-30 11:48:44 -04005184 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005185 if (status)
5186 goto out;
5187
NeilBrown3e9e3db2005-06-23 22:04:20 -07005188 status = nfserr_locks_held;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05005189
Jeff Laytonfd449072014-06-30 11:48:41 -04005190 /* Find the matching lock stateowner */
5191 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
5192 if (tmp->so_is_open_owner)
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005193 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005194 if (same_owner_str(tmp, owner, clid)) {
5195 sop = tmp;
5196 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07005198 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005199
5200 /* No matching owner found, maybe a replay? Just declare victory... */
5201 if (!sop) {
5202 status = nfs_ok;
5203 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005205
5206 lo = lockowner(sop);
5207 /* see if there are still any locks associated with it */
5208 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
Trond Myklebust11b91642014-07-29 21:34:08 -04005209 if (check_for_locks(stp->st_stid.sc_file, lo))
Jeff Laytonfd449072014-06-30 11:48:41 -04005210 goto out;
5211 }
5212
5213 status = nfs_ok;
5214 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215out:
5216 nfs4_unlock_state();
5217 return status;
5218}
5219
5220static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005221alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222{
NeilBrowna55370a2005-06-23 22:03:52 -07005223 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224}
5225
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005226bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005227nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005228{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005229 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005230
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005231 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005232 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005233}
5234
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235/*
5236 * failure => all reset bets are off, nfserr_no_grace...
5237 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005238struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005239nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240{
5241 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005242 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243
NeilBrowna55370a2005-06-23 22:03:52 -07005244 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5245 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005246 if (crp) {
5247 strhashval = clientstr_hashval(name);
5248 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005249 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005250 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005251 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005252 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005253 }
5254 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255}
5256
Jeff Layton2a4317c2012-03-21 16:42:43 -04005257void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005258nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005259{
5260 list_del(&crp->cr_strhash);
5261 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005262 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005263}
5264
5265void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005266nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267{
5268 struct nfs4_client_reclaim *crp = NULL;
5269 int i;
5270
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005272 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5273 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005275 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 }
5277 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005278 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279}
5280
5281/*
5282 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005283struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005284nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285{
5286 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 struct nfs4_client_reclaim *crp = NULL;
5288
Jeff Layton278c9312012-11-12 15:00:52 -05005289 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290
Jeff Layton278c9312012-11-12 15:00:52 -05005291 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005292 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005293 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 return crp;
5295 }
5296 }
5297 return NULL;
5298}
5299
5300/*
5301* Called from OPEN. Look for clientid in reclaim list.
5302*/
Al Virob37ad282006-10-19 23:28:59 -07005303__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005304nfs4_check_open_reclaim(clientid_t *clid,
5305 struct nfsd4_compound_state *cstate,
5306 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005308 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005309
5310 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005311 status = lookup_clientid(clid, cstate, nn);
5312 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005313 return nfserr_reclaim_bad;
5314
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005315 if (nfsd4_client_record_check(cstate->clp))
5316 return nfserr_reclaim_bad;
5317
5318 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319}
5320
Bryan Schumaker65178db2011-11-01 13:35:21 -04005321#ifdef CONFIG_NFSD_FAULT_INJECTION
5322
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005323u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5324{
J. Bruce Fields221a6872013-04-01 22:23:49 -04005325 if (mark_client_expired(clp))
5326 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005327 expire_client(clp);
5328 return 1;
5329}
5330
Bryan Schumaker184c1842012-11-29 11:40:44 -05005331u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5332{
5333 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005334 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005335 printk(KERN_INFO "NFS Client: %s\n", buf);
5336 return 1;
5337}
5338
5339static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5340 const char *type)
5341{
5342 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005343 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005344 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5345}
5346
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005347static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5348 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005349{
5350 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005351 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005352 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005353 u64 count = 0;
5354
5355 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005356 list_for_each_entry_safe(stp, st_next,
5357 &oop->oo_owner.so_stateids, st_perstateowner) {
5358 list_for_each_entry_safe(lst, lst_next,
5359 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005360 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005361 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005362 if (++count == max)
5363 return count;
5364 }
5365 }
5366 }
5367
5368 return count;
5369}
5370
5371u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5372{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005373 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005374}
5375
Bryan Schumaker184c1842012-11-29 11:40:44 -05005376u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5377{
5378 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5379 nfsd_print_count(clp, count, "locked files");
5380 return count;
5381}
5382
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005383static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5384{
5385 struct nfs4_openowner *oop, *next;
5386 u64 count = 0;
5387
5388 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5389 if (func)
5390 func(oop);
5391 if (++count == max)
5392 break;
5393 }
5394
5395 return count;
5396}
5397
5398u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5399{
5400 return nfsd_foreach_client_open(clp, max, release_openowner);
5401}
5402
Bryan Schumaker184c1842012-11-29 11:40:44 -05005403u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5404{
5405 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5406 nfsd_print_count(clp, count, "open files");
5407 return count;
5408}
5409
Bryan Schumaker269de302012-11-29 11:40:42 -05005410static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5411 struct list_head *victims)
5412{
5413 struct nfs4_delegation *dp, *next;
5414 u64 count = 0;
5415
Benny Halevycdc97502014-05-30 09:09:30 -04005416 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005417 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005418 if (victims) {
5419 /*
5420 * It's not safe to mess with delegations that have a
5421 * non-zero dl_time. They might have already been broken
5422 * and could be processed by the laundromat outside of
5423 * the state_lock. Just leave them be.
5424 */
5425 if (dp->dl_time != 0)
5426 continue;
5427
Jeff Layton42690672014-07-25 07:34:20 -04005428 unhash_delegation_locked(dp);
5429 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005430 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005431 if (++count == max)
5432 break;
5433 }
5434 return count;
5435}
5436
5437u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5438{
5439 struct nfs4_delegation *dp, *next;
5440 LIST_HEAD(victims);
5441 u64 count;
5442
Benny Halevycdc97502014-05-30 09:09:30 -04005443 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005444 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005445 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005446
Jeff Layton2d4a5322014-07-25 07:34:21 -04005447 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5448 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005449 revoke_delegation(dp);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005450 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005451
5452 return count;
5453}
5454
5455u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5456{
Jeff Laytondff13992014-07-08 14:02:49 -04005457 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005458 LIST_HEAD(victims);
5459 u64 count;
5460
Benny Halevycdc97502014-05-30 09:09:30 -04005461 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005462 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005463 while (!list_empty(&victims)) {
5464 dp = list_first_entry(&victims, struct nfs4_delegation,
5465 dl_recall_lru);
5466 list_del_init(&dp->dl_recall_lru);
5467 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005468 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005469 }
Benny Halevycdc97502014-05-30 09:09:30 -04005470 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005471
5472 return count;
5473}
5474
Bryan Schumaker184c1842012-11-29 11:40:44 -05005475u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5476{
5477 u64 count = 0;
5478
Benny Halevycdc97502014-05-30 09:09:30 -04005479 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005480 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005481 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005482
5483 nfsd_print_count(clp, count, "delegations");
5484 return count;
5485}
5486
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005487u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005488{
5489 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005490 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005491 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005492
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005493 if (!nfsd_netns_ready(nn))
5494 return 0;
5495
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005496 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005497 count += func(clp, max - count);
5498 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005499 break;
5500 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005501
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005502 return count;
5503}
5504
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005505struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5506{
5507 struct nfs4_client *clp;
5508 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5509
5510 if (!nfsd_netns_ready(nn))
5511 return NULL;
5512
5513 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5514 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5515 return clp;
5516 }
5517 return NULL;
5518}
5519
Bryan Schumaker65178db2011-11-01 13:35:21 -04005520#endif /* CONFIG_NFSD_FAULT_INJECTION */
5521
Meelap Shahc2f1a552007-07-17 04:04:39 -07005522/*
5523 * Since the lifetime of a delegation isn't limited to that of an open, a
5524 * client may quite reasonably hang on to a delegation as long as it has
5525 * the inode cached. This becomes an obvious problem the first time a
5526 * client's inode cache approaches the size of the server's total memory.
5527 *
5528 * For now we avoid this problem by imposing a hard limit on the number
5529 * of delegations, which varies according to the server's memory size.
5530 */
5531static void
5532set_max_delegations(void)
5533{
5534 /*
5535 * Allow at most 4 delegations per megabyte of RAM. Quick
5536 * estimates suggest that in the worst case (where every delegation
5537 * is for a different inode), a delegation could take about 1.5K,
5538 * giving a worst case usage of about 6% of memory.
5539 */
5540 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5541}
5542
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005543static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005544{
5545 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5546 int i;
5547
5548 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5549 CLIENT_HASH_SIZE, GFP_KERNEL);
5550 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005551 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005552 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5553 CLIENT_HASH_SIZE, GFP_KERNEL);
5554 if (!nn->unconf_id_hashtbl)
5555 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005556 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5557 OWNER_HASH_SIZE, GFP_KERNEL);
5558 if (!nn->ownerstr_hashtbl)
5559 goto err_ownerstr;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005560 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5561 SESSION_HASH_SIZE, GFP_KERNEL);
5562 if (!nn->sessionid_hashtbl)
5563 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005564
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005565 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005566 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005567 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005568 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005569 for (i = 0; i < OWNER_HASH_SIZE; i++)
5570 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005571 for (i = 0; i < SESSION_HASH_SIZE; i++)
5572 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005573 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005574 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005575 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005576 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005577 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005578 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005579
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005580 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005581 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005582
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005583 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005584
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005585err_sessionid:
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03005586 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005587err_ownerstr:
5588 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005589err_unconf_id:
5590 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005591err:
5592 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005593}
5594
5595static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005596nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005597{
5598 int i;
5599 struct nfs4_client *clp = NULL;
5600 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5601
5602 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5603 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5604 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5605 destroy_client(clp);
5606 }
5607 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005608
Kinglong Mee2b905632014-03-26 22:09:30 +08005609 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5610 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5611 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5612 destroy_client(clp);
5613 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005614 }
5615
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005616 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005617 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005618 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005619 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005620 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005621}
5622
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005623int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005624nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005625{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005626 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005627 int ret;
5628
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005629 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005630 if (ret)
5631 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005632 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005633 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005634 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005635 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005636 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005637 nn->nfsd4_grace, net);
5638 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005639 return 0;
5640}
5641
5642/* initialization to perform when the nfsd service is started: */
5643
5644int
5645nfs4_state_start(void)
5646{
5647 int ret;
5648
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005649 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005650 if (ret)
5651 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005652 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005653 if (laundry_wq == NULL) {
5654 ret = -ENOMEM;
5655 goto out_recovery;
5656 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005657 ret = nfsd4_create_callback_queue();
5658 if (ret)
5659 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005660
Meelap Shahc2f1a552007-07-17 04:04:39 -07005661 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005662
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005663 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005664
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005665out_free_laundry:
5666 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005667out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005668 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669}
5670
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005671void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005672nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005676 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005678 cancel_delayed_work_sync(&nn->laundromat_work);
5679 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005680
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005681 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04005683 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005684 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005686 unhash_delegation_locked(dp);
5687 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 }
Benny Halevycdc97502014-05-30 09:09:30 -04005689 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 list_for_each_safe(pos, next, &reaplist) {
5691 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005692 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04005693 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 }
5695
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005696 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005697 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005698 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699}
5700
5701void
5702nfs4_state_shutdown(void)
5703{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005704 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005705 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005707
5708static void
5709get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5710{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005711 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5712 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005713}
5714
5715static void
5716put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5717{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005718 if (cstate->minorversion) {
5719 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5720 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5721 }
5722}
5723
5724void
5725clear_current_stateid(struct nfsd4_compound_state *cstate)
5726{
5727 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005728}
5729
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005730/*
5731 * functions to set current state id
5732 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005733void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005734nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5735{
5736 put_stateid(cstate, &odp->od_stateid);
5737}
5738
5739void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005740nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5741{
5742 put_stateid(cstate, &open->op_stateid);
5743}
5744
5745void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005746nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5747{
5748 put_stateid(cstate, &close->cl_stateid);
5749}
5750
5751void
5752nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5753{
5754 put_stateid(cstate, &lock->lk_resp_stateid);
5755}
5756
5757/*
5758 * functions to consume current state id
5759 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005760
5761void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005762nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5763{
5764 get_stateid(cstate, &odp->od_stateid);
5765}
5766
5767void
5768nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5769{
5770 get_stateid(cstate, &drp->dr_stateid);
5771}
5772
5773void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005774nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5775{
5776 get_stateid(cstate, &fsp->fr_stateid);
5777}
5778
5779void
5780nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5781{
5782 get_stateid(cstate, &setattr->sa_stateid);
5783}
5784
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005785void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005786nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5787{
5788 get_stateid(cstate, &close->cl_stateid);
5789}
5790
5791void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005792nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005793{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005794 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005795}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01005796
5797void
5798nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5799{
5800 get_stateid(cstate, &read->rd_stateid);
5801}
5802
5803void
5804nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5805{
5806 get_stateid(cstate, &write->wr_stateid);
5807}