blob: 344cd1ac3f67758fa8f9945e4385844221ce3047 [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{
470 struct idr *stateids = &cl->cl_stateids;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500471 struct nfs4_stid *stid;
472 int new_id;
473
Trond Myklebustf8338832014-07-25 07:34:19 -0400474 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500475 if (!stid)
476 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400477
Jeff Layton398c33a2013-04-29 16:21:20 -0700478 new_id = idr_alloc_cyclic(stateids, stid, 0, 0, GFP_KERNEL);
Tejun Heoebd6c702013-03-13 14:59:37 -0700479 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500480 goto out_free;
481 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500482 stid->sc_stateid.si_opaque.so_id = new_id;
483 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
484 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400485 atomic_set(&stid->sc_count, 1);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500486
J. Bruce Fields996e0932011-10-17 11:14:48 -0400487 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500488 * It shouldn't be a problem to reuse an opaque stateid value.
489 * I don't think it is for 4.1. But with 4.0 I worry that, for
490 * example, a stray write retransmission could be accepted by
491 * the server when it should have been rejected. Therefore,
492 * adopt a trick from the sctp code to attempt to maximize the
493 * amount of time until an id is reused, by ensuring they always
494 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400495 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500496 return stid;
497out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800498 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500499 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400500}
501
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400502static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
503{
Trond Myklebust60116952014-07-29 21:34:06 -0400504 struct nfs4_stid *stid;
505 struct nfs4_ol_stateid *stp;
506
507 stid = nfs4_alloc_stid(clp, stateid_slab);
508 if (!stid)
509 return NULL;
510
511 stp = openlockstateid(stid);
512 stp->st_stid.sc_free = nfs4_free_ol_stateid;
513 return stp;
514}
515
516static void nfs4_free_deleg(struct nfs4_stid *stid)
517{
Trond Myklebust60116952014-07-29 21:34:06 -0400518 kmem_cache_free(deleg_slab, stid);
519 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400520}
521
NeilBrown6282cd52014-06-04 17:39:26 +1000522/*
523 * When we recall a delegation, we should be careful not to hand it
524 * out again straight away.
525 * To ensure this we keep a pair of bloom filters ('new' and 'old')
526 * in which the filehandles of recalled delegations are "stored".
527 * If a filehandle appear in either filter, a delegation is blocked.
528 * When a delegation is recalled, the filehandle is stored in the "new"
529 * filter.
530 * Every 30 seconds we swap the filters and clear the "new" one,
531 * unless both are empty of course.
532 *
533 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
534 * low 3 bytes as hash-table indices.
535 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400536 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000537 * is used to manage concurrent access. Testing does not need the lock
538 * except when swapping the two filters.
539 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400540static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000541static struct bloom_pair {
542 int entries, old_entries;
543 time_t swap_time;
544 int new; /* index into 'set' */
545 DECLARE_BITMAP(set[2], 256);
546} blocked_delegations;
547
548static int delegation_blocked(struct knfsd_fh *fh)
549{
550 u32 hash;
551 struct bloom_pair *bd = &blocked_delegations;
552
553 if (bd->entries == 0)
554 return 0;
555 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400556 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000557 if (seconds_since_boot() - bd->swap_time > 30) {
558 bd->entries -= bd->old_entries;
559 bd->old_entries = bd->entries;
560 memset(bd->set[bd->new], 0,
561 sizeof(bd->set[0]));
562 bd->new = 1-bd->new;
563 bd->swap_time = seconds_since_boot();
564 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400565 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000566 }
567 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
568 if (test_bit(hash&255, bd->set[0]) &&
569 test_bit((hash>>8)&255, bd->set[0]) &&
570 test_bit((hash>>16)&255, bd->set[0]))
571 return 1;
572
573 if (test_bit(hash&255, bd->set[1]) &&
574 test_bit((hash>>8)&255, bd->set[1]) &&
575 test_bit((hash>>16)&255, bd->set[1]))
576 return 1;
577
578 return 0;
579}
580
581static void block_delegations(struct knfsd_fh *fh)
582{
583 u32 hash;
584 struct bloom_pair *bd = &blocked_delegations;
585
586 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
587
Jeff Laytonf54fe962014-07-25 07:34:26 -0400588 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000589 __set_bit(hash&255, bd->set[bd->new]);
590 __set_bit((hash>>8)&255, bd->set[bd->new]);
591 __set_bit((hash>>16)&255, bd->set[bd->new]);
592 if (bd->entries == 0)
593 bd->swap_time = seconds_since_boot();
594 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400595 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000596}
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598static struct nfs4_delegation *
Jeff Laytonf9416e22014-07-25 07:34:23 -0400599alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400602 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400605 n = atomic_long_inc_return(&num_delegations);
606 if (n < 0 || n > max_delegations)
607 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000608 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400609 goto out_dec;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400610 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700611 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400612 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400613
614 dp->dl_stid.sc_free = nfs4_free_deleg;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400615 /*
616 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400617 * meaning of seqid 0 isn't meaningful, really, but let's avoid
618 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400619 */
620 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700621 INIT_LIST_HEAD(&dp->dl_perfile);
622 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 INIT_LIST_HEAD(&dp->dl_recall_lru);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400624 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Jeff Layton02e12152014-07-16 10:31:57 -0400625 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400627out_dec:
628 atomic_long_dec(&num_delegations);
629 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
632void
Trond Myklebust60116952014-07-29 21:34:06 -0400633nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Trond Myklebust11b91642014-07-29 21:34:08 -0400635 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400636 struct nfs4_client *clp = s->sc_client;
637
638 if (!atomic_dec_and_test(&s->sc_count))
639 return;
640 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
641 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400642 if (fp)
643 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500646static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Jeff Layton417c6622014-07-21 09:34:57 -0400648 lockdep_assert_held(&state_lock);
649
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -0500650 if (!fp->fi_lease)
651 return;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500652 if (atomic_dec_and_test(&fp->fi_delegees)) {
653 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
654 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400655 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500656 fp->fi_deleg_file = NULL;
657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400660static void unhash_stid(struct nfs4_stid *s)
661{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500662 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400663}
664
Benny Halevy931ee562014-05-30 09:09:27 -0400665static void
666hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
667{
Benny Halevycdc97502014-05-30 09:09:30 -0400668 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400669 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400670
Benny Halevy3fb87d12014-05-30 09:09:31 -0400671 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400672 list_add(&dp->dl_perfile, &fp->fi_delegations);
673 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
674}
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676static void
Jeff Layton42690672014-07-25 07:34:20 -0400677unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Trond Myklebust11b91642014-07-29 21:34:08 -0400679 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400680
Jeff Layton42690672014-07-25 07:34:20 -0400681 lockdep_assert_held(&state_lock);
682
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400683 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400684 /* Ensure that deleg break won't try to requeue it */
685 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400686 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400687 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400689 list_del_init(&dp->dl_perfile);
690 spin_unlock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400691 if (fp)
Trond Myklebustf8338832014-07-25 07:34:19 -0400692 nfs4_put_deleg_lease(fp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400693}
694
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400695static void destroy_delegation(struct nfs4_delegation *dp)
696{
Jeff Layton42690672014-07-25 07:34:20 -0400697 spin_lock(&state_lock);
698 unhash_delegation_locked(dp);
699 spin_unlock(&state_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400700 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400701}
702
703static void revoke_delegation(struct nfs4_delegation *dp)
704{
705 struct nfs4_client *clp = dp->dl_stid.sc_client;
706
Jeff Layton2d4a5322014-07-25 07:34:21 -0400707 WARN_ON(!list_empty(&dp->dl_recall_lru));
708
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400709 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400710 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400711 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400712 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400713 spin_lock(&clp->cl_lock);
714 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
715 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400716 }
717}
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719/*
720 * SETCLIENTID state
721 */
722
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400723static unsigned int clientid_hashval(u32 id)
724{
725 return id & CLIENT_HASH_MASK;
726}
727
728static unsigned int clientstr_hashval(const char *name)
729{
730 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
731}
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400734 * We store the NONE, READ, WRITE, and BOTH bits separately in the
735 * st_{access,deny}_bmap field of the stateid, in order to track not
736 * only what share bits are currently in force, but also what
737 * combinations of share bits previous opens have used. This allows us
738 * to enforce the recommendation of rfc 3530 14.2.19 that the server
739 * return an error if the client attempt to downgrade to a combination
740 * of share bits not explicable by closing some of its previous opens.
741 *
742 * XXX: This enforcement is actually incomplete, since we don't keep
743 * track of access/deny bit combinations; so, e.g., we allow:
744 *
745 * OPEN allow read, deny write
746 * OPEN allow both, deny none
747 * DOWNGRADE allow read, deny none
748 *
749 * which we should reject.
750 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400751static unsigned int
752bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400753 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400754 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400755
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400756 for (i = 1; i < 4; i++) {
757 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400758 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400759 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400760 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400761}
762
Jeff Layton82c5ff12012-05-11 09:45:13 -0400763/* set share access for a given stateid */
764static inline void
765set_access(u32 access, struct nfs4_ol_stateid *stp)
766{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400767 unsigned char mask = 1 << access;
768
769 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
770 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400771}
772
773/* clear share access for a given stateid */
774static inline void
775clear_access(u32 access, struct nfs4_ol_stateid *stp)
776{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400777 unsigned char mask = 1 << access;
778
779 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
780 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400781}
782
783/* test whether a given stateid has access */
784static inline bool
785test_access(u32 access, struct nfs4_ol_stateid *stp)
786{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400787 unsigned char mask = 1 << access;
788
789 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400790}
791
Jeff Laytonce0fc432012-05-11 09:45:14 -0400792/* set share deny for a given stateid */
793static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400794set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400795{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400796 unsigned char mask = 1 << deny;
797
798 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
799 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400800}
801
802/* clear share deny for a given stateid */
803static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400804clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400805{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400806 unsigned char mask = 1 << deny;
807
808 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
809 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400810}
811
812/* test whether a given stateid is denying specific access */
813static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400814test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400815{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400816 unsigned char mask = 1 << deny;
817
818 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400819}
820
821static int nfs4_access_to_omode(u32 access)
822{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400823 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400824 case NFS4_SHARE_ACCESS_READ:
825 return O_RDONLY;
826 case NFS4_SHARE_ACCESS_WRITE:
827 return O_WRONLY;
828 case NFS4_SHARE_ACCESS_BOTH:
829 return O_RDWR;
830 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500831 WARN_ON_ONCE(1);
832 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400833}
834
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400835/*
836 * A stateid that had a deny mode associated with it is being released
837 * or downgraded. Recalculate the deny mode on the file.
838 */
839static void
840recalculate_deny_mode(struct nfs4_file *fp)
841{
842 struct nfs4_ol_stateid *stp;
843
844 spin_lock(&fp->fi_lock);
845 fp->fi_share_deny = 0;
846 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
847 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
848 spin_unlock(&fp->fi_lock);
849}
850
851static void
852reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
853{
854 int i;
855 bool change = false;
856
857 for (i = 1; i < 4; i++) {
858 if ((i & deny) != i) {
859 change = true;
860 clear_deny(i, stp);
861 }
862 }
863
864 /* Recalculate per-file deny mode if there was a change */
865 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -0400866 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400867}
868
Jeff Layton82c5ff12012-05-11 09:45:13 -0400869/* release all access and file references for a given stateid */
870static void
871release_all_access(struct nfs4_ol_stateid *stp)
872{
873 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -0400874 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400875
876 if (fp && stp->st_deny_bmap != 0)
877 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400878
879 for (i = 1; i < 4; i++) {
880 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -0400881 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400882 clear_access(i, stp);
883 }
884}
885
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400886static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500887{
Trond Myklebust11b91642014-07-29 21:34:08 -0400888 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -0400889
890 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500891 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400892 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500893 list_del(&stp->st_perstateowner);
894}
895
Trond Myklebust60116952014-07-29 21:34:06 -0400896static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500897{
Trond Myklebust60116952014-07-29 21:34:06 -0400898 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400899
Trond Myklebust60116952014-07-29 21:34:06 -0400900 release_all_access(stp);
Trond Myklebust60116952014-07-29 21:34:06 -0400901 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500902}
903
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400904static void __release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500905{
906 struct file *file;
907
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400908 list_del(&stp->st_locks);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500909 unhash_generic_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400910 unhash_stid(&stp->st_stid);
Trond Myklebust11b91642014-07-29 21:34:08 -0400911 file = find_any_file(stp->st_stid.sc_file);
Trond Myklebuste20fcf12014-07-10 14:07:27 -0400912 if (file)
913 filp_close(file, (fl_owner_t)lockowner(stp->st_stateowner));
Trond Myklebust60116952014-07-29 21:34:06 -0400914 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500915}
916
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400917static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500918{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400919 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500920
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400921 list_del(&lo->lo_owner.so_strhash);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400922 while (!list_empty(&lo->lo_owner.so_stateids)) {
923 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400924 struct nfs4_ol_stateid, st_perstateowner);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400925 __release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500926 }
927}
928
Trond Myklebust50cc6232014-04-18 14:44:03 -0400929static void nfs4_free_lockowner(struct nfs4_lockowner *lo)
930{
931 kfree(lo->lo_owner.so_owner.data);
932 kmem_cache_free(lockowner_slab, lo);
933}
934
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400935static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500936{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400937 unhash_lockowner(lo);
938 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500939}
940
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400941static void release_lockowner_if_empty(struct nfs4_lockowner *lo)
942{
943 if (list_empty(&lo->lo_owner.so_stateids))
944 release_lockowner(lo);
945}
946
947static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500948{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400949 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500950
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400951 lo = lockowner(stp->st_stateowner);
952 __release_lock_stateid(stp);
953 release_lockowner_if_empty(lo);
954}
955
956static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
957{
958 struct nfs4_ol_stateid *stp;
959
960 while (!list_empty(&open_stp->st_locks)) {
961 stp = list_entry(open_stp->st_locks.next,
962 struct nfs4_ol_stateid, st_locks);
963 release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500964 }
965}
966
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400967static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500968{
969 unhash_generic_stateid(stp);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400970 release_open_stateid_locks(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400971}
972
973static void release_open_stateid(struct nfs4_ol_stateid *stp)
974{
975 unhash_open_stateid(stp);
Trond Myklebust60116952014-07-29 21:34:06 -0400976 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields22839632009-01-11 14:27:17 -0500977}
978
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400979static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500980{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400981 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500982
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400983 list_del(&oo->oo_owner.so_strhash);
984 list_del(&oo->oo_perclient);
985 while (!list_empty(&oo->oo_owner.so_stateids)) {
986 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400987 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500988 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500989 }
990}
991
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400992static void release_last_closed_stateid(struct nfs4_openowner *oo)
993{
994 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
995
996 if (s) {
Trond Myklebust60116952014-07-29 21:34:06 -0400997 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400998 oo->oo_last_closed_stid = NULL;
999 }
1000}
1001
Trond Myklebust50cc6232014-04-18 14:44:03 -04001002static void nfs4_free_openowner(struct nfs4_openowner *oo)
1003{
1004 kfree(oo->oo_owner.so_owner.data);
1005 kmem_cache_free(openowner_slab, oo);
1006}
1007
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001008static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001009{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001010 unhash_openowner(oo);
1011 list_del(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001012 release_last_closed_stateid(oo);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001013 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001014}
1015
Marc Eshel5282fd72009-04-03 08:27:52 +03001016static inline int
1017hash_sessionid(struct nfs4_sessionid *sessionid)
1018{
1019 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1020
1021 return sid->sequence % SESSION_HASH_SIZE;
1022}
1023
Trond Myklebust8f199b82012-03-20 15:11:17 -04001024#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001025static inline void
1026dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1027{
1028 u32 *ptr = (u32 *)(&sessionid->data[0]);
1029 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1030}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001031#else
1032static inline void
1033dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1034{
1035}
1036#endif
1037
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001038/*
1039 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1040 * won't be used for replay.
1041 */
1042void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1043{
1044 struct nfs4_stateowner *so = cstate->replay_owner;
1045
1046 if (nfserr == nfserr_replay_me)
1047 return;
1048
1049 if (!seqid_mutating_err(ntohl(nfserr))) {
1050 cstate->replay_owner = NULL;
1051 return;
1052 }
1053 if (!so)
1054 return;
1055 if (so->so_is_open_owner)
1056 release_last_closed_stateid(openowner(so));
1057 so->so_seqid++;
1058 return;
1059}
Marc Eshel5282fd72009-04-03 08:27:52 +03001060
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001061static void
1062gen_sessionid(struct nfsd4_session *ses)
1063{
1064 struct nfs4_client *clp = ses->se_client;
1065 struct nfsd4_sessionid *sid;
1066
1067 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1068 sid->clientid = clp->cl_clientid;
1069 sid->sequence = current_sessionid++;
1070 sid->reserved = 0;
1071}
1072
1073/*
Andy Adamsona6496372009-08-28 08:45:01 -04001074 * The protocol defines ca_maxresponssize_cached to include the size of
1075 * the rpc header, but all we need to cache is the data starting after
1076 * the end of the initial SEQUENCE operation--the rest we regenerate
1077 * each time. Therefore we can advertise a ca_maxresponssize_cached
1078 * value that is the number of bytes in our cache plus a few additional
1079 * bytes. In order to stay on the safe side, and not promise more than
1080 * we can cache, those additional bytes must be the minimum possible: 24
1081 * bytes of rpc header (xid through accept state, with AUTH_NULL
1082 * verifier), 12 for the compound header (with zero-length tag), and 44
1083 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001084 */
Andy Adamsona6496372009-08-28 08:45:01 -04001085#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1086
Andy Adamson557ce262009-08-28 08:45:04 -04001087static void
1088free_session_slots(struct nfsd4_session *ses)
1089{
1090 int i;
1091
1092 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1093 kfree(ses->se_slots[i]);
1094}
1095
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001096/*
1097 * We don't actually need to cache the rpc and session headers, so we
1098 * can allocate a little less for each slot:
1099 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001100static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001101{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001102 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001103
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001104 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1105 size = 0;
1106 else
1107 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1108 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001109}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001110
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001111/*
1112 * XXX: If we run out of reserved DRC memory we could (up to a point)
1113 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001114 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001115 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001116static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001117{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001118 u32 slotsize = slot_bytes(ca);
1119 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001120 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001121
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001122 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001123 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1124 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001125 num = min_t(int, num, avail / slotsize);
1126 nfsd_drc_mem_used += num * slotsize;
1127 spin_unlock(&nfsd_drc_lock);
1128
1129 return num;
1130}
1131
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001132static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001133{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001134 int slotsize = slot_bytes(ca);
1135
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001136 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001137 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001138 spin_unlock(&nfsd_drc_lock);
1139}
1140
Kinglong Mee60810e52014-01-01 00:35:47 +08001141static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1142 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001143{
Kinglong Mee60810e52014-01-01 00:35:47 +08001144 int numslots = fattrs->maxreqs;
1145 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001146 struct nfsd4_session *new;
1147 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001148
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001149 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001150 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1151 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001152
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001153 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001154 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001155 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001156 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001157 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001158 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001159 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001160 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001161 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001162
1163 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1164 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1165
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001166 return new;
1167out_free:
1168 while (i--)
1169 kfree(new->se_slots[i]);
1170 kfree(new);
1171 return NULL;
1172}
1173
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001174static void free_conn(struct nfsd4_conn *c)
1175{
1176 svc_xprt_put(c->cn_xprt);
1177 kfree(c);
1178}
1179
1180static void nfsd4_conn_lost(struct svc_xpt_user *u)
1181{
1182 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1183 struct nfs4_client *clp = c->cn_session->se_client;
1184
1185 spin_lock(&clp->cl_lock);
1186 if (!list_empty(&c->cn_persession)) {
1187 list_del(&c->cn_persession);
1188 free_conn(c);
1189 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001190 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001191 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001192}
1193
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001194static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001195{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001196 struct nfsd4_conn *conn;
1197
1198 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1199 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001200 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001201 svc_xprt_get(rqstp->rq_xprt);
1202 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001203 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001204 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1205 return conn;
1206}
1207
J. Bruce Fields328ead22010-09-29 16:11:06 -04001208static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1209{
1210 conn->cn_session = ses;
1211 list_add(&conn->cn_persession, &ses->se_conns);
1212}
1213
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001214static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1215{
1216 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001217
1218 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001219 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001220 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001221}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001222
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001223static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001224{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001225 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001226 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001227}
1228
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001229static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001230{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001231 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001232
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001233 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001234 ret = nfsd4_register_conn(conn);
1235 if (ret)
1236 /* oops; xprt is already down: */
1237 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001238 /* We may have gained or lost a callback channel: */
1239 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001240}
1241
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001242static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001243{
1244 u32 dir = NFS4_CDFC4_FORE;
1245
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001246 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001247 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001248 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001249}
1250
1251/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001252static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001253{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001254 struct nfs4_client *clp = s->se_client;
1255 struct nfsd4_conn *c;
1256
1257 spin_lock(&clp->cl_lock);
1258 while (!list_empty(&s->se_conns)) {
1259 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1260 list_del_init(&c->cn_persession);
1261 spin_unlock(&clp->cl_lock);
1262
1263 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1264 free_conn(c);
1265
1266 spin_lock(&clp->cl_lock);
1267 }
1268 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001269}
1270
J. Bruce Fields1377b692012-09-11 21:42:40 -04001271static void __free_session(struct nfsd4_session *ses)
1272{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001273 free_session_slots(ses);
1274 kfree(ses);
1275}
1276
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001277static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001278{
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001279 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1280
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001281 lockdep_assert_held(&nn->client_lock);
1282 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001283 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001284 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001285}
Andy Adamson557ce262009-08-28 08:45:04 -04001286
Fengguang Wu135ae822012-11-10 07:20:25 -05001287static 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 -04001288{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001289 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001290 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001291
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001292 new->se_client = clp;
1293 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001294
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001295 INIT_LIST_HEAD(&new->se_conns);
1296
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001297 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001298 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001299 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001300 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001301 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001302 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001303 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001304 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001305 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001306 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001307 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001308 spin_unlock(&nn->client_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001309
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001310 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001311 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001312 /*
1313 * This is a little silly; with sessions there's no real
1314 * use for the callback address. Use the peer address
1315 * as a reasonable default for now, but consider fixing
1316 * the rpc client not to require an address in the
1317 * future:
1318 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001319 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1320 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001321 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001322}
1323
Benny Halevy9089f1b2010-05-12 00:12:26 +03001324/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001325static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001326__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001327{
1328 struct nfsd4_session *elem;
1329 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001330 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001331
1332 dump_sessionid(__func__, sessionid);
1333 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001334 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001335 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001336 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1337 NFS4_MAX_SESSIONID_LEN)) {
1338 return elem;
1339 }
1340 }
1341
1342 dprintk("%s: session not found\n", __func__);
1343 return NULL;
1344}
1345
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001346static struct nfsd4_session *
1347find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1348 __be32 *ret)
1349{
1350 struct nfsd4_session *session;
1351 __be32 status = nfserr_badsession;
1352
1353 session = __find_in_sessionid_hashtbl(sessionid, net);
1354 if (!session)
1355 goto out;
1356 status = nfsd4_get_session_locked(session);
1357 if (status)
1358 session = NULL;
1359out:
1360 *ret = status;
1361 return session;
1362}
1363
Benny Halevy9089f1b2010-05-12 00:12:26 +03001364/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001365static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001366unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001367{
1368 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001369 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001370 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001371 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001372}
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1375static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001376STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001378 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001380 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001381 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return 1;
1383}
1384
1385/*
1386 * XXX Should we use a slab cache ?
1387 * This type of memory management is somewhat inefficient, but we use it
1388 * anyway since SETCLIENTID is not a common operation.
1389 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001390static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
1392 struct nfs4_client *clp;
1393
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001394 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1395 if (clp == NULL)
1396 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001397 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001398 if (clp->cl_name.data == NULL) {
1399 kfree(clp);
1400 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001402 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001403 INIT_LIST_HEAD(&clp->cl_sessions);
1404 idr_init(&clp->cl_stateids);
1405 atomic_set(&clp->cl_refcount, 0);
1406 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1407 INIT_LIST_HEAD(&clp->cl_idhash);
1408 INIT_LIST_HEAD(&clp->cl_openowners);
1409 INIT_LIST_HEAD(&clp->cl_delegations);
1410 INIT_LIST_HEAD(&clp->cl_lru);
1411 INIT_LIST_HEAD(&clp->cl_callbacks);
1412 INIT_LIST_HEAD(&clp->cl_revoked);
1413 spin_lock_init(&clp->cl_lock);
1414 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return clp;
1416}
1417
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001418static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419free_client(struct nfs4_client *clp)
1420{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001421 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001422
1423 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001424 while (!list_empty(&clp->cl_sessions)) {
1425 struct nfsd4_session *ses;
1426 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1427 se_perclnt);
1428 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001429 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1430 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001431 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001432 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001433 free_svc_cred(&clp->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001435 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 kfree(clp);
1437}
1438
Benny Halevy84d38ac2010-05-12 00:13:16 +03001439/* must be called under the client_lock */
1440static inline void
1441unhash_client_locked(struct nfs4_client *clp)
1442{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001443 struct nfsd4_session *ses;
1444
Benny Halevy84d38ac2010-05-12 00:13:16 +03001445 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001446 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001447 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1448 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001449 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001450}
1451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001453destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001455 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001458 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001461 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001462 while (!list_empty(&clp->cl_delegations)) {
1463 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001464 unhash_delegation_locked(dp);
1465 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
Benny Halevycdc97502014-05-30 09:09:30 -04001467 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 while (!list_empty(&reaplist)) {
1469 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001470 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001471 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001473 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001474 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001475 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001476 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001477 }
NeilBrownea1da632005-06-23 22:04:17 -07001478 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001479 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1480 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001482 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001483 if (clp->cl_cb_conn.cb_xprt)
1484 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001485 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001486 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001487 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001488 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001489 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001490 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001491 unhash_client_locked(clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001492 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1493 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001494 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001497static void expire_client(struct nfs4_client *clp)
1498{
1499 nfsd4_client_record_remove(clp);
1500 destroy_client(clp);
1501}
1502
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001503static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1504{
1505 memcpy(target->cl_verifier.data, source->data,
1506 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001509static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1510{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1512 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1513}
1514
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001515static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001516{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001517 if (source->cr_principal) {
1518 target->cr_principal =
1519 kstrdup(source->cr_principal, GFP_KERNEL);
1520 if (target->cr_principal == NULL)
1521 return -ENOMEM;
1522 } else
1523 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001524 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 target->cr_uid = source->cr_uid;
1526 target->cr_gid = source->cr_gid;
1527 target->cr_group_info = source->cr_group_info;
1528 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001529 target->cr_gss_mech = source->cr_gss_mech;
1530 if (source->cr_gss_mech)
1531 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001532 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001535static long long
1536compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1537{
1538 long long res;
1539
1540 res = o1->len - o2->len;
1541 if (res)
1542 return res;
1543 return (long long)memcmp(o1->data, o2->data, o1->len);
1544}
1545
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001546static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001547{
NeilBrowna55370a2005-06-23 22:03:52 -07001548 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550
1551static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001552same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1553{
1554 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
1556
1557static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001558same_clid(clientid_t *cl1, clientid_t *cl2)
1559{
1560 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
1562
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001563static bool groups_equal(struct group_info *g1, struct group_info *g2)
1564{
1565 int i;
1566
1567 if (g1->ngroups != g2->ngroups)
1568 return false;
1569 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001570 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001571 return false;
1572 return true;
1573}
1574
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001575/*
1576 * RFC 3530 language requires clid_inuse be returned when the
1577 * "principal" associated with a requests differs from that previously
1578 * used. We use uid, gid's, and gss principal string as our best
1579 * approximation. We also don't want to allow non-gss use of a client
1580 * established using gss: in theory cr_principal should catch that
1581 * change, but in practice cr_principal can be null even in the gss case
1582 * since gssd doesn't always pass down a principal string.
1583 */
1584static bool is_gss_cred(struct svc_cred *cr)
1585{
1586 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1587 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1588}
1589
1590
Vivek Trivedi5559b502012-07-24 21:18:20 +05301591static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001592same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1593{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001594 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001595 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1596 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001597 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1598 return false;
1599 if (cr1->cr_principal == cr2->cr_principal)
1600 return true;
1601 if (!cr1->cr_principal || !cr2->cr_principal)
1602 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301603 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
1605
J. Bruce Fields57266a62013-04-13 14:27:29 -04001606static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1607{
1608 struct svc_cred *cr = &rqstp->rq_cred;
1609 u32 service;
1610
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001611 if (!cr->cr_gss_mech)
1612 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001613 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1614 return service == RPC_GSS_SVC_INTEGRITY ||
1615 service == RPC_GSS_SVC_PRIVACY;
1616}
1617
1618static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1619{
1620 struct svc_cred *cr = &rqstp->rq_cred;
1621
1622 if (!cl->cl_mach_cred)
1623 return true;
1624 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1625 return false;
1626 if (!svc_rqst_integrity_protected(rqstp))
1627 return false;
1628 if (!cr->cr_principal)
1629 return false;
1630 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1631}
1632
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001633static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001634{
1635 static u32 current_clientid = 1;
1636
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001637 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 clp->cl_clientid.cl_id = current_clientid++;
1639}
1640
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001641static void gen_confirm(struct nfs4_client *clp)
1642{
Chuck Leverab4684d2012-03-02 17:13:50 -05001643 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001644 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Jeff Laytonf4199922014-06-17 07:44:11 -04001646 /*
1647 * This is opaque to client, so no need to byte-swap. Use
1648 * __force to keep sparse happy
1649 */
1650 verf[0] = (__force __be32)get_seconds();
1651 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001652 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653}
1654
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001655static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001656{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001657 struct nfs4_stid *ret;
1658
1659 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1660 if (!ret || !ret->sc_type)
1661 return NULL;
1662 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001663}
1664
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001665static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001666{
1667 struct nfs4_stid *s;
1668
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001669 s = find_stateid(cl, t);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001670 if (!s)
1671 return NULL;
1672 if (typemask & s->sc_type)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001673 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001674 return NULL;
1675}
1676
Jeff Layton2216d442012-11-12 15:00:57 -05001677static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001678 struct svc_rqst *rqstp, nfs4_verifier *verf)
1679{
1680 struct nfs4_client *clp;
1681 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001682 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001683 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001684 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001685
1686 clp = alloc_client(name);
1687 if (clp == NULL)
1688 return NULL;
1689
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001690 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1691 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001692 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001693 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001694 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001695 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001696 }
Jeff Layton02e12152014-07-16 10:31:57 -04001697 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001698 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001699 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001700 copy_verf(clp, verf);
1701 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001702 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001703 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001704 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001705 return clp;
1706}
1707
NeilBrownfd39ca92005-06-23 22:04:03 -07001708static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001709add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1710{
1711 struct rb_node **new = &(root->rb_node), *parent = NULL;
1712 struct nfs4_client *clp;
1713
1714 while (*new) {
1715 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1716 parent = *new;
1717
1718 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1719 new = &((*new)->rb_left);
1720 else
1721 new = &((*new)->rb_right);
1722 }
1723
1724 rb_link_node(&new_clp->cl_namenode, parent, new);
1725 rb_insert_color(&new_clp->cl_namenode, root);
1726}
1727
1728static struct nfs4_client *
1729find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1730{
1731 long long cmp;
1732 struct rb_node *node = root->rb_node;
1733 struct nfs4_client *clp;
1734
1735 while (node) {
1736 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1737 cmp = compare_blob(&clp->cl_name, name);
1738 if (cmp > 0)
1739 node = node->rb_left;
1740 else if (cmp < 0)
1741 node = node->rb_right;
1742 else
1743 return clp;
1744 }
1745 return NULL;
1746}
1747
1748static void
1749add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
1751 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001752 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001754 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001755 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001757 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001758 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
1760
NeilBrownfd39ca92005-06-23 22:04:03 -07001761static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762move_to_confirmed(struct nfs4_client *clp)
1763{
1764 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001765 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001768 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001769 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001770 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001771 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 renew_client(clp);
1773}
1774
1775static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001776find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
1778 struct nfs4_client *clp;
1779 unsigned int idhashval = clientid_hashval(clid->cl_id);
1780
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001781 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001782 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001783 if ((bool)clp->cl_minorversion != sessions)
1784 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001785 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 }
1789 return NULL;
1790}
1791
1792static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001793find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1794{
1795 struct list_head *tbl = nn->conf_id_hashtbl;
1796
1797 return find_client_in_id_table(tbl, clid, sessions);
1798}
1799
1800static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001801find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001803 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001805 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806}
1807
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001808static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001809{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001810 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001811}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001812
NeilBrown28ce6052005-06-23 22:03:56 -07001813static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001814find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001815{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001816 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001817}
1818
1819static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001820find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001821{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001822 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001823}
1824
NeilBrownfd39ca92005-06-23 22:04:03 -07001825static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001826gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001828 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001829 struct sockaddr *sa = svc_addr(rqstp);
1830 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001831 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Jeff Layton7077ecb2009-08-14 12:57:58 -04001833 /* Currently, we only support tcp and tcp6 for the callback channel */
1834 if (se->se_callback_netid_len == 3 &&
1835 !memcmp(se->se_callback_netid_val, "tcp", 3))
1836 expected_family = AF_INET;
1837 else if (se->se_callback_netid_len == 4 &&
1838 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1839 expected_family = AF_INET6;
1840 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 goto out_err;
1842
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001843 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001844 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001845 (struct sockaddr *)&conn->cb_addr,
1846 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001847
J. Bruce Fields07263f12010-05-31 19:09:40 -04001848 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001850
J. Bruce Fields07263f12010-05-31 19:09:40 -04001851 if (conn->cb_addr.ss_family == AF_INET6)
1852 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001853
J. Bruce Fields07263f12010-05-31 19:09:40 -04001854 conn->cb_prog = se->se_callback_prog;
1855 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001856 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 return;
1858out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001859 conn->cb_addr.ss_family = AF_UNSPEC;
1860 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001861 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 "will not receive delegations\n",
1863 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 return;
1866}
1867
Andy Adamson074fe892009-04-03 08:28:15 +03001868/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04001869 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001870 */
Trond Myklebustb6076642014-06-30 11:48:35 -04001871static void
Andy Adamson074fe892009-04-03 08:28:15 +03001872nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1873{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001874 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04001875 struct nfsd4_slot *slot = resp->cstate.slot;
1876 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001877
Andy Adamson557ce262009-08-28 08:45:04 -04001878 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001879
Andy Adamson557ce262009-08-28 08:45:04 -04001880 slot->sl_opcnt = resp->opcnt;
1881 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001882
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001883 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001884 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001885 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001886 return;
1887 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001888 base = resp->cstate.data_offset;
1889 slot->sl_datalen = buf->len - base;
1890 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04001891 WARN("%s: sessions DRC could not cache compound\n", __func__);
1892 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001893}
1894
1895/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001896 * Encode the replay sequence operation from the slot values.
1897 * If cachethis is FALSE encode the uncached rep error on the next
1898 * operation which sets resp->p and increments resp->opcnt for
1899 * nfs4svc_encode_compoundres.
1900 *
Andy Adamson074fe892009-04-03 08:28:15 +03001901 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001902static __be32
1903nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1904 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001905{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001906 struct nfsd4_op *op;
1907 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001908
Andy Adamsonabfabf82009-07-23 19:02:18 -04001909 /* Encode the replayed sequence operation */
1910 op = &args->ops[resp->opcnt - 1];
1911 nfsd4_encode_operation(resp, op);
1912
1913 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05001914 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001915 op = &args->ops[resp->opcnt++];
1916 op->status = nfserr_retry_uncached_rep;
1917 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001918 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001919 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001920}
1921
1922/*
Andy Adamson557ce262009-08-28 08:45:04 -04001923 * The sequence operation is not cached because we can use the slot and
1924 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001925 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04001926static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001927nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1928 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001929{
Andy Adamson557ce262009-08-28 08:45:04 -04001930 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001931 struct xdr_stream *xdr = &resp->xdr;
1932 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03001933 __be32 status;
1934
Andy Adamson557ce262009-08-28 08:45:04 -04001935 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001936
Andy Adamsonabfabf82009-07-23 19:02:18 -04001937 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04001938 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04001939 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001940
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001941 p = xdr_reserve_space(xdr, slot->sl_datalen);
1942 if (!p) {
1943 WARN_ON_ONCE(1);
1944 return nfserr_serverfault;
1945 }
1946 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
1947 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03001948
Andy Adamson557ce262009-08-28 08:45:04 -04001949 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001950 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001951}
1952
Andy Adamson0733d212009-04-03 08:28:01 +03001953/*
1954 * Set the exchange_id flags returned by the server.
1955 */
1956static void
1957nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1958{
1959 /* pNFS is not supported */
1960 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1961
1962 /* Referrals are supported, Migration is not. */
1963 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1964
1965 /* set the wire flags to return to client. */
1966 clid->flags = new->cl_exchange_flags;
1967}
1968
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001969static bool client_has_state(struct nfs4_client *clp)
1970{
1971 /*
1972 * Note clp->cl_openowners check isn't quite right: there's no
1973 * need to count owners without stateid's.
1974 *
1975 * Also note we should probably be using this in 4.0 case too.
1976 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04001977 return !list_empty(&clp->cl_openowners)
1978 || !list_empty(&clp->cl_delegations)
1979 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001980}
1981
Al Virob37ad282006-10-19 23:28:59 -07001982__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001983nfsd4_exchange_id(struct svc_rqst *rqstp,
1984 struct nfsd4_compound_state *cstate,
1985 struct nfsd4_exchange_id *exid)
1986{
Andy Adamson0733d212009-04-03 08:28:01 +03001987 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001988 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04001989 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001990 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001991 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001992 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001993 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03001994
Jeff Layton363168b2009-08-14 12:57:56 -04001995 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001996 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04001997 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03001998 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04001999 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002000
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002001 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002002 return nfserr_inval;
2003
Andy Adamson0733d212009-04-03 08:28:01 +03002004 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002005 case SP4_MACH_CRED:
2006 if (!svc_rqst_integrity_protected(rqstp))
2007 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002008 case SP4_NONE:
2009 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002010 default: /* checked by xdr code */
2011 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002012 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002013 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002014 }
2015
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002016 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03002017 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002018 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002019 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002020 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2021 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2022
J. Bruce Fields136e6582012-05-12 20:37:23 -04002023 if (update) {
2024 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002025 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002026 goto out;
2027 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002028 if (!mach_creds_match(conf, rqstp)) {
2029 status = nfserr_wrong_cred;
2030 goto out;
2031 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002032 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002033 status = nfserr_perm;
2034 goto out;
2035 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002036 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002037 status = nfserr_not_same;
2038 goto out;
2039 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002040 /* case 6 */
2041 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2042 new = conf;
2043 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002044 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002045 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002046 if (client_has_state(conf)) {
2047 status = nfserr_clid_inuse;
2048 goto out;
2049 }
Andy Adamson0733d212009-04-03 08:28:01 +03002050 expire_client(conf);
2051 goto out_new;
2052 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002053 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002054 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002055 new = conf;
2056 goto out_copy;
2057 }
2058 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04002059 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002060 }
2061
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002062 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002063 status = nfserr_noent;
2064 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002065 }
2066
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002067 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002068 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03002069 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002070
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002071 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002072out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05002073 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03002074 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04002075 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03002076 goto out;
2077 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002078 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002079 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002080
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002081 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002082 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03002083out_copy:
2084 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2085 exid->clientid.cl_id = new->cl_clientid.cl_id;
2086
J. Bruce Fields778df3f2012-05-25 21:40:23 -04002087 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03002088 nfsd4_set_ex_flags(new, exid);
2089
2090 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04002091 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002092 status = nfs_ok;
2093
2094out:
2095 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03002096 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002097}
2098
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002099static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002100check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002101{
Andy Adamson88e588d2009-07-23 19:02:15 -04002102 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2103 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002104
2105 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002106 if (slot_inuse) {
2107 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002108 return nfserr_jukebox;
2109 else
2110 return nfserr_seq_misordered;
2111 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002112 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002113 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002114 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002115 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002116 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002117 return nfserr_seq_misordered;
2118}
2119
Andy Adamson49557cc2009-07-23 19:02:16 -04002120/*
2121 * Cache the create session result into the create session single DRC
2122 * slot cache by saving the xdr structure. sl_seqid has been set.
2123 * Do this for solo or embedded create session operations.
2124 */
2125static void
2126nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002127 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002128{
2129 slot->sl_status = nfserr;
2130 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2131}
2132
2133static __be32
2134nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2135 struct nfsd4_clid_slot *slot)
2136{
2137 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2138 return slot->sl_status;
2139}
2140
Mi Jinlong1b74c252011-07-14 14:50:17 +08002141#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2142 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2143 1 + /* MIN tag is length with zero, only length */ \
2144 3 + /* version, opcount, opcode */ \
2145 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2146 /* seqid, slotID, slotID, cache */ \
2147 4 ) * sizeof(__be32))
2148
2149#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2150 2 + /* verifier: AUTH_NULL, length 0 */\
2151 1 + /* status */ \
2152 1 + /* MIN tag is length with zero, only length */ \
2153 3 + /* opcount, opcode, opstatus*/ \
2154 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2155 /* seqid, slotID, slotID, slotID, status */ \
2156 5 ) * sizeof(__be32))
2157
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002158static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002159{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002160 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2161
J. Bruce Fields373cd402013-04-08 15:42:12 -04002162 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2163 return nfserr_toosmall;
2164 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2165 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002166 ca->headerpadsz = 0;
2167 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2168 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2169 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2170 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2171 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2172 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2173 /*
2174 * Note decreasing slot size below client's request may make it
2175 * difficult for client to function correctly, whereas
2176 * decreasing the number of slots will (just?) affect
2177 * performance. When short on memory we therefore prefer to
2178 * decrease number of slots instead of their size. Clients that
2179 * request larger slots than they need will get poor results:
2180 */
2181 ca->maxreqs = nfsd4_get_drc_mem(ca);
2182 if (!ca->maxreqs)
2183 return nfserr_jukebox;
2184
J. Bruce Fields373cd402013-04-08 15:42:12 -04002185 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002186}
2187
Kinglong Mee8a891632013-12-23 18:11:02 +08002188#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2189 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2190#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2191 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2192
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002193static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2194{
2195 ca->headerpadsz = 0;
2196
2197 /*
2198 * These RPC_MAX_HEADER macros are overkill, especially since we
2199 * don't even do gss on the backchannel yet. But this is still
2200 * less than 1k. Tighten up this estimate in the unlikely event
2201 * it turns out to be a problem for some client:
2202 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002203 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002204 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002205 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002206 return nfserr_toosmall;
2207 ca->maxresp_cached = 0;
2208 if (ca->maxops < 2)
2209 return nfserr_toosmall;
2210
2211 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002212}
2213
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002214static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2215{
2216 switch (cbs->flavor) {
2217 case RPC_AUTH_NULL:
2218 case RPC_AUTH_UNIX:
2219 return nfs_ok;
2220 default:
2221 /*
2222 * GSS case: the spec doesn't allow us to return this
2223 * error. But it also doesn't allow us not to support
2224 * GSS.
2225 * I'd rather this fail hard than return some error the
2226 * client might think it can already handle:
2227 */
2228 return nfserr_encr_alg_unsupp;
2229 }
2230}
2231
Andy Adamson069b6ad2009-04-03 08:27:58 +03002232__be32
2233nfsd4_create_session(struct svc_rqst *rqstp,
2234 struct nfsd4_compound_state *cstate,
2235 struct nfsd4_create_session *cr_ses)
2236{
Jeff Layton363168b2009-08-14 12:57:56 -04002237 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002238 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002239 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002240 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002241 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002242 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002243 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002244
Mi Jinlonga62573d2011-03-23 17:57:07 +08002245 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2246 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002247 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2248 if (status)
2249 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002250 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002251 if (status)
2252 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002253 status = check_backchannel_attrs(&cr_ses->back_channel);
2254 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002255 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002256 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002257 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002258 if (!new)
2259 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002260 conn = alloc_conn_from_crses(rqstp, cr_ses);
2261 if (!conn)
2262 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002263
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002264 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002265 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002266 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002267 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002268
2269 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002270 status = nfserr_wrong_cred;
2271 if (!mach_creds_match(conf, rqstp))
2272 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002273 cs_slot = &conf->cl_cs_slot;
2274 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002275 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002276 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002277 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002278 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002279 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002280 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002281 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002282 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002283 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002284 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002285 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002286 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002287 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002288 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002289 status = nfserr_wrong_cred;
2290 if (!mach_creds_match(unconf, rqstp))
2291 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002292 cs_slot = &unconf->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) {
2295 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002296 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002297 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002298 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002299 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002300 if (old) {
2301 status = mark_client_expired(old);
2302 if (status)
2303 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002304 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002305 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002306 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002307 conf = unconf;
2308 } else {
2309 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002310 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002311 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002312 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002313 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002314 * We do not support RDMA or persistent sessions
2315 */
2316 cr_ses->flags &= ~SESSION4_PERSIST;
2317 cr_ses->flags &= ~SESSION4_RDMA;
2318
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002319 init_session(rqstp, new, conf, cr_ses);
2320 nfsd4_init_conn(rqstp, conn, new);
2321
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002322 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002323 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002324 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002325 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002326
Andy Adamson49557cc2009-07-23 19:02:16 -04002327 /* cache solo and embedded create sessions under the state lock */
2328 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002329 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002330 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002331out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08002332 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002333 free_conn(conn);
2334out_free_session:
2335 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002336out_release_drc_mem:
2337 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002338 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002339}
2340
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002341static __be32 nfsd4_map_bcts_dir(u32 *dir)
2342{
2343 switch (*dir) {
2344 case NFS4_CDFC4_FORE:
2345 case NFS4_CDFC4_BACK:
2346 return nfs_ok;
2347 case NFS4_CDFC4_FORE_OR_BOTH:
2348 case NFS4_CDFC4_BACK_OR_BOTH:
2349 *dir = NFS4_CDFC4_BOTH;
2350 return nfs_ok;
2351 };
2352 return nfserr_inval;
2353}
2354
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002355__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2356{
2357 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002358 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002359 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002360
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002361 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2362 if (status)
2363 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002364 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002365 session->se_cb_prog = bc->bc_cb_program;
2366 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002367 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002368
2369 nfsd4_probe_callback(session->se_client);
2370
2371 return nfs_ok;
2372}
2373
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002374__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2375 struct nfsd4_compound_state *cstate,
2376 struct nfsd4_bind_conn_to_session *bcts)
2377{
2378 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002379 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002380 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002381 struct net *net = SVC_NET(rqstp);
2382 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002383
2384 if (!nfsd4_last_compound_op(rqstp))
2385 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002386 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002387 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002388 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002389 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002390 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002391 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002392 status = nfserr_wrong_cred;
2393 if (!mach_creds_match(session->se_client, rqstp))
2394 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002395 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002396 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002397 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002398 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002399 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002400 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002401 goto out;
2402 nfsd4_init_conn(rqstp, conn, session);
2403 status = nfs_ok;
2404out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002405 nfsd4_put_session(session);
2406out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002407 nfs4_unlock_state();
2408 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002409}
2410
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002411static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2412{
2413 if (!session)
2414 return 0;
2415 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2416}
2417
Andy Adamson069b6ad2009-04-03 08:27:58 +03002418__be32
2419nfsd4_destroy_session(struct svc_rqst *r,
2420 struct nfsd4_compound_state *cstate,
2421 struct nfsd4_destroy_session *sessionid)
2422{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002423 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002424 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002425 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002426 struct net *net = SVC_NET(r);
2427 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002428
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002429 nfs4_lock_state();
2430 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002431 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002432 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002433 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002434 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002435 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002436 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002437 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002438 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002439 if (!ses)
2440 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002441 status = nfserr_wrong_cred;
2442 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002443 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002444 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002445 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002446 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002447 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002448 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002449
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002450 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002451
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002452 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002453 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002454out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002455 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002456out_client_lock:
2457 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002458out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002459 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002460 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002461}
2462
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002463static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002464{
2465 struct nfsd4_conn *c;
2466
2467 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002468 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002469 return c;
2470 }
2471 }
2472 return NULL;
2473}
2474
J. Bruce Fields57266a62013-04-13 14:27:29 -04002475static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002476{
2477 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002478 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002479 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002480 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002481
2482 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002483 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002484 if (c)
2485 goto out_free;
2486 status = nfserr_conn_not_bound_to_session;
2487 if (clp->cl_mach_cred)
2488 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002489 __nfsd4_hash_conn(new, ses);
2490 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002491 ret = nfsd4_register_conn(new);
2492 if (ret)
2493 /* oops; xprt is already down: */
2494 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002495 return nfs_ok;
2496out_free:
2497 spin_unlock(&clp->cl_lock);
2498 free_conn(new);
2499 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002500}
2501
Mi Jinlong868b89c2011-04-27 09:09:58 +08002502static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2503{
2504 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2505
2506 return args->opcnt > session->se_fchannel.maxops;
2507}
2508
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002509static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2510 struct nfsd4_session *session)
2511{
2512 struct xdr_buf *xb = &rqstp->rq_arg;
2513
2514 return xb->len > session->se_fchannel.maxreq_sz;
2515}
2516
Andy Adamson069b6ad2009-04-03 08:27:58 +03002517__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002518nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002519 struct nfsd4_compound_state *cstate,
2520 struct nfsd4_sequence *seq)
2521{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002522 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002523 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002524 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002525 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002526 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002527 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002528 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002529 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002530 struct net *net = SVC_NET(rqstp);
2531 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002532
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002533 if (resp->opcnt != 1)
2534 return nfserr_sequence_pos;
2535
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002536 /*
2537 * Will be either used or freed by nfsd4_sequence_check_conn
2538 * below.
2539 */
2540 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2541 if (!conn)
2542 return nfserr_jukebox;
2543
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002544 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002545 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002546 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002547 goto out_no_session;
2548 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002549
Mi Jinlong868b89c2011-04-27 09:09:58 +08002550 status = nfserr_too_many_ops;
2551 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002552 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002553
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002554 status = nfserr_req_too_big;
2555 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002556 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002557
Benny Halevyb85d4c02009-04-03 08:28:08 +03002558 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002559 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002560 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002561
Andy Adamson557ce262009-08-28 08:45:04 -04002562 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002563 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2564
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002565 /* We do not negotiate the number of slots yet, so set the
2566 * maxslots to the session maxreqs which is used to encode
2567 * sr_highest_slotid and the sr_target_slot id to maxslots */
2568 seq->maxslots = session->se_fchannel.maxreqs;
2569
J. Bruce Fields73e79482012-02-13 16:39:00 -05002570 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2571 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002572 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002573 status = nfserr_seq_misordered;
2574 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002575 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002576 cstate->slot = slot;
2577 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002578 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002579 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002580 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002581 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002582 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002583 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002584 }
2585 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002586 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002587
J. Bruce Fields57266a62013-04-13 14:27:29 -04002588 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002589 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002590 if (status)
2591 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002592
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002593 buflen = (seq->cachethis) ?
2594 session->se_fchannel.maxresp_cached :
2595 session->se_fchannel.maxresp_sz;
2596 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2597 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002598 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002599 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002600 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002601
2602 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002603 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002604 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002605 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002606 if (seq->cachethis)
2607 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002608 else
2609 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002610
2611 cstate->slot = slot;
2612 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002613 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002614
Benny Halevyb85d4c02009-04-03 08:28:08 +03002615out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002616 switch (clp->cl_cb_state) {
2617 case NFSD4_CB_DOWN:
2618 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2619 break;
2620 case NFSD4_CB_FAULT:
2621 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2622 break;
2623 default:
2624 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002625 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002626 if (!list_empty(&clp->cl_revoked))
2627 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002628out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002629 if (conn)
2630 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002631 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002632 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002633out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002634 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002635 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002636}
2637
Trond Myklebustb6076642014-06-30 11:48:35 -04002638void
2639nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2640{
2641 struct nfsd4_compound_state *cs = &resp->cstate;
2642
2643 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002644 if (cs->status != nfserr_replay_cache) {
2645 nfsd4_store_cache_entry(resp);
2646 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2647 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002648 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002649 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002650 } else if (cs->clp)
2651 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002652}
2653
Mi Jinlong345c2842011-10-20 17:51:39 +08002654__be32
2655nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2656{
2657 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002658 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002659 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002660
2661 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002662 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002663 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002664 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002665
2666 if (conf) {
2667 clp = conf;
2668
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002669 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002670 status = nfserr_clientid_busy;
2671 goto out;
2672 }
2673 } else if (unconf)
2674 clp = unconf;
2675 else {
2676 status = nfserr_stale_clientid;
2677 goto out;
2678 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002679 if (!mach_creds_match(clp, rqstp)) {
2680 status = nfserr_wrong_cred;
2681 goto out;
2682 }
Mi Jinlong345c2842011-10-20 17:51:39 +08002683 expire_client(clp);
2684out:
2685 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002686 return status;
2687}
2688
Andy Adamson069b6ad2009-04-03 08:27:58 +03002689__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002690nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2691{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002692 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002693
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002694 if (rc->rca_one_fs) {
2695 if (!cstate->current_fh.fh_dentry)
2696 return nfserr_nofilehandle;
2697 /*
2698 * We don't take advantage of the rca_one_fs case.
2699 * That's OK, it's optional, we can safely ignore it.
2700 */
2701 return nfs_ok;
2702 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002703
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002704 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002705 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002706 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2707 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002708 goto out;
2709
2710 status = nfserr_stale_clientid;
2711 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002712 /*
2713 * The following error isn't really legal.
2714 * But we only get here if the client just explicitly
2715 * destroyed the client. Surely it no longer cares what
2716 * error it gets back on an operation for the dead
2717 * client.
2718 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002719 goto out;
2720
2721 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002722 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002723out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002724 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002725 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002726}
2727
2728__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002729nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2730 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002732 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002734 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002735 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002736 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2737
J. Bruce Fields63db4632012-05-18 21:54:19 -04002738 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002740 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002741 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002742 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002744 if (clp_used_exchangeid(conf))
2745 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002746 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002747 char addr_str[INET6_ADDRSTRLEN];
2748 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2749 sizeof(addr_str));
2750 dprintk("NFSD: setclientid: string in use by client "
2751 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 goto out;
2753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002755 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002756 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002759 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002760 if (new == NULL)
2761 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002762 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002763 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002765 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002766 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002767 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002768 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002769 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2771 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2772 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2773 status = nfs_ok;
2774out:
2775 nfs4_unlock_state();
2776 return status;
2777}
2778
2779
Al Virob37ad282006-10-19 23:28:59 -07002780__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002781nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2782 struct nfsd4_compound_state *cstate,
2783 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784{
NeilBrown21ab45a2005-06-23 22:04:14 -07002785 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2787 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002788 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002789 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002791 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002794
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002795 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002796 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002797 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002798 * We try hard to give out unique clientid's, so if we get an
2799 * attempt to confirm the same clientid with a different cred,
2800 * there's a bug somewhere. Let's charitably assume it's our
2801 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002802 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002803 status = nfserr_serverfault;
2804 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2805 goto out;
2806 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2807 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002808 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002809 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2810 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002812 else /* case 4: client hasn't noticed we rebooted yet? */
2813 status = nfserr_stale_clientid;
2814 goto out;
2815 }
2816 status = nfs_ok;
2817 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002818 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2819 nfsd4_probe_callback(conf);
2820 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002821 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002822 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002823 if (conf) {
2824 status = mark_client_expired(conf);
2825 if (status)
2826 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002827 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002828 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002829 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002830 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 nfs4_unlock_state();
2834 return status;
2835}
2836
J. Bruce Fields32513b42011-10-13 16:00:16 -04002837static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002839 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2840}
2841
2842/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04002843static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04002844{
Trond Myklebustca943212014-07-23 16:17:39 -04002845 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
Trond Myklebust950e0112014-06-30 11:48:31 -04002847 lockdep_assert_held(&state_lock);
2848
J. Bruce Fields32513b42011-10-13 16:00:16 -04002849 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04002850 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002851 INIT_LIST_HEAD(&fp->fi_stateids);
2852 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04002853 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002854 fp->fi_had_conflict = false;
2855 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04002856 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002857 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2858 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04002859 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860}
2861
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002862void
NeilBrowne60d4392005-06-23 22:03:01 -07002863nfsd4_free_slabs(void)
2864{
Christoph Hellwigabf11352014-05-21 07:43:03 -07002865 kmem_cache_destroy(openowner_slab);
2866 kmem_cache_destroy(lockowner_slab);
2867 kmem_cache_destroy(file_slab);
2868 kmem_cache_destroy(stateid_slab);
2869 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002870}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Bryan Schumaker72083392011-11-01 15:24:59 -04002872int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873nfsd4_init_slabs(void)
2874{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002875 openowner_slab = kmem_cache_create("nfsd4_openowners",
2876 sizeof(struct nfs4_openowner), 0, 0, NULL);
2877 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002878 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002879 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002880 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002881 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002882 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07002883 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002884 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002885 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002886 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07002887 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002888 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002889 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002890 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002891 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002892 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002893 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002894 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07002896
2897out_free_stateid_slab:
2898 kmem_cache_destroy(stateid_slab);
2899out_free_file_slab:
2900 kmem_cache_destroy(file_slab);
2901out_free_lockowner_slab:
2902 kmem_cache_destroy(lockowner_slab);
2903out_free_openowner_slab:
2904 kmem_cache_destroy(openowner_slab);
2905out:
NeilBrowne60d4392005-06-23 22:03:01 -07002906 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2907 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908}
2909
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002910static void init_nfs4_replay(struct nfs4_replay *rp)
2911{
2912 rp->rp_status = nfserr_serverfault;
2913 rp->rp_buflen = 0;
2914 rp->rp_buf = rp->rp_ibuf;
2915}
2916
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002917static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918{
2919 struct nfs4_stateowner *sop;
2920
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002921 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002922 if (!sop)
2923 return NULL;
2924
2925 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2926 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002927 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002928 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002930 sop->so_owner.len = owner->len;
2931
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002932 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002933 sop->so_client = clp;
2934 init_nfs4_replay(&sop->so_replay);
2935 return sop;
2936}
2937
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002938static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002939{
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002940 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2941
2942 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002943 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944}
2945
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002946static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04002947alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002948 struct nfsd4_compound_state *cstate)
2949{
Trond Myklebust13d6f662014-06-30 11:48:45 -04002950 struct nfs4_client *clp = cstate->clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002951 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002953 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2954 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002956 oo->oo_owner.so_is_open_owner = 1;
2957 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04002958 oo->oo_flags = NFS4_OO_NEW;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002959 if (nfsd4_has_session(cstate))
2960 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002961 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002962 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002963 INIT_LIST_HEAD(&oo->oo_close_lru);
2964 hash_openowner(oo, clp, strhashval);
2965 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966}
2967
J. Bruce Fields996e0932011-10-17 11:14:48 -04002968static 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 -04002969 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002971 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04002972 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002973 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002974 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002975 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04002976 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 stp->st_access_bmap = 0;
2978 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07002979 stp->st_openstp = NULL;
Trond Myklebust1d31a252014-07-10 14:07:25 -04002980 spin_lock(&fp->fi_lock);
2981 list_add(&stp->st_perfile, &fp->fi_stateids);
2982 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983}
2984
2985static void
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002986move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987{
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002988 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2989
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002990 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002992 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002993 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994}
2995
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002997same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2998 clientid_t *clid)
2999{
3000 return (sop->so_owner.len == owner->len) &&
3001 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
3002 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003}
3004
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003005static struct nfs4_openowner *
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003006find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
3007 bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008{
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003009 struct nfs4_stateowner *so;
3010 struct nfs4_openowner *oo;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003011 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003013 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003014 if (!so->so_is_open_owner)
3015 continue;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003016 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
3017 oo = openowner(so);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003018 clp = oo->oo_owner.so_client;
3019 if ((bool)clp->cl_minorversion != sessions)
3020 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003021 renew_client(oo->oo_owner.so_client);
3022 return oo;
3023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 }
3025 return NULL;
3026}
3027
3028/* search file_hashtbl[] for file */
3029static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003030find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
Trond Myklebustca943212014-07-23 16:17:39 -04003032 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 struct nfs4_file *fp;
3034
Trond Myklebust950e0112014-06-30 11:48:31 -04003035 lockdep_assert_held(&state_lock);
3036
Jeff Layton89876f82013-04-02 09:01:59 -04003037 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003038 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003039 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 }
3043 return NULL;
3044}
3045
Trond Myklebust950e0112014-06-30 11:48:31 -04003046static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003047find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003048{
3049 struct nfs4_file *fp;
3050
3051 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003052 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003053 spin_unlock(&state_lock);
3054 return fp;
3055}
3056
3057static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003058find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003059{
3060 struct nfs4_file *fp;
3061
3062 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003063 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003064 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003065 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003066 fp = new;
3067 }
3068 spin_unlock(&state_lock);
3069
3070 return fp;
3071}
3072
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003073/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 * Called to check deny when READ with all zero stateid or
3075 * WRITE with all zero or all one stateid
3076 */
Al Virob37ad282006-10-19 23:28:59 -07003077static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3079{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003081 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
Trond Myklebustca943212014-07-23 16:17:39 -04003083 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003084 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003085 return ret;
3086 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003087 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003088 if (fp->fi_share_deny & deny_type)
3089 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003090 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003091 put_nfs4_file(fp);
3092 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093}
3094
Jeff Layton02e12152014-07-16 10:31:57 -04003095void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096{
Trond Myklebust11b91642014-07-29 21:34:08 -04003097 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3098 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003099
Trond Myklebust11b91642014-07-29 21:34:08 -04003100 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003101
Jeff Layton02e12152014-07-16 10:31:57 -04003102 /*
3103 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003104 * already holding inode->i_lock.
3105 *
Jeff Laytondff13992014-07-08 14:02:49 -04003106 * If the dl_time != 0, then we know that it has already been
3107 * queued for a lease break. Don't queue it again.
3108 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003109 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003110 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003111 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003112 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003113 }
Jeff Layton02e12152014-07-16 10:31:57 -04003114 spin_unlock(&state_lock);
3115}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
Jeff Layton02e12152014-07-16 10:31:57 -04003117static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3118{
3119 /*
3120 * We're assuming the state code never drops its reference
3121 * without first removing the lease. Since we're in this lease
3122 * callback (and since the lease code is serialized by the kernel
3123 * lock) we know the server hasn't removed the lease yet, we know
3124 * it's safe to take a reference.
3125 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003126 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003127 nfsd4_cb_recall(dp);
3128}
3129
Jeff Layton1c8c6012013-06-21 08:58:15 -04003130/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003131static void nfsd_break_deleg_cb(struct file_lock *fl)
3132{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003133 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3134 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003135
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003136 if (!fp) {
3137 WARN(1, "(%p)->fl_owner NULL\n", fl);
3138 return;
3139 }
3140 if (fp->fi_had_conflict) {
3141 WARN(1, "duplicate break on %p\n", fp);
3142 return;
3143 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003144 /*
3145 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003146 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003147 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003148 */
3149 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
Jeff Layton02e12152014-07-16 10:31:57 -04003151 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003152 fp->fi_had_conflict = true;
3153 /*
3154 * If there are no delegations on the list, then we can't count on this
3155 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3156 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3157 * true should keep any new delegations from being hashed.
3158 */
3159 if (list_empty(&fp->fi_delegations))
3160 fl->fl_break_time = jiffies;
3161 else
3162 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3163 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003164 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165}
3166
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167static
3168int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3169{
3170 if (arg & F_UNLCK)
3171 return lease_modify(onlist, arg);
3172 else
3173 return -EAGAIN;
3174}
3175
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003176static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003177 .lm_break = nfsd_break_deleg_cb,
3178 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179};
3180
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003181static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3182{
3183 if (nfsd4_has_session(cstate))
3184 return nfs_ok;
3185 if (seqid == so->so_seqid - 1)
3186 return nfserr_replay_me;
3187 if (seqid == so->so_seqid)
3188 return nfs_ok;
3189 return nfserr_bad_seqid;
3190}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
Jeff Layton4b24ca72014-06-30 11:48:44 -04003192static __be32 lookup_clientid(clientid_t *clid,
3193 struct nfsd4_compound_state *cstate,
3194 struct nfsd_net *nn)
3195{
3196 struct nfs4_client *found;
3197
3198 if (cstate->clp) {
3199 found = cstate->clp;
3200 if (!same_clid(&found->cl_clientid, clid))
3201 return nfserr_stale_clientid;
3202 return nfs_ok;
3203 }
3204
3205 if (STALE_CLIENTID(clid, nn))
3206 return nfserr_stale_clientid;
3207
3208 /*
3209 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3210 * cached already then we know this is for is for v4.0 and "sessions"
3211 * will be false.
3212 */
3213 WARN_ON_ONCE(cstate->session);
3214 found = find_confirmed_client(clid, false, nn);
3215 if (!found)
3216 return nfserr_expired;
3217
3218 /* Cache the nfs4_client in cstate! */
3219 cstate->clp = found;
3220 atomic_inc(&found->cl_refcount);
3221 return nfs_ok;
3222}
3223
Al Virob37ad282006-10-19 23:28:59 -07003224__be32
Andy Adamson66689582009-04-03 08:28:45 +03003225nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003226 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 clientid_t *clientid = &open->op_clientid;
3229 struct nfs4_client *clp = NULL;
3230 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003231 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003232 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003234 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003236 /*
3237 * In case we need it later, after we've already created the
3238 * file and don't want to risk a further failure:
3239 */
3240 open->op_file = nfsd4_alloc_file();
3241 if (open->op_file == NULL)
3242 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
Trond Myklebust2d91e892014-06-30 11:48:46 -04003244 status = lookup_clientid(clientid, cstate, nn);
3245 if (status)
3246 return status;
3247 clp = cstate->clp;
3248
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003249 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003250 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003251 open->op_openowner = oo;
3252 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003253 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003255 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003256 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003257 release_openowner(oo);
3258 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003259 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003260 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003261 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3262 if (status)
3263 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003264 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003265new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003266 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003267 if (oo == NULL)
3268 return nfserr_jukebox;
3269 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003270alloc_stateid:
3271 open->op_stp = nfs4_alloc_stateid(clp);
3272 if (!open->op_stp)
3273 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003274 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275}
3276
Al Virob37ad282006-10-19 23:28:59 -07003277static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003278nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3279{
3280 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3281 return nfserr_openmode;
3282 else
3283 return nfs_ok;
3284}
3285
Daniel Mackc47d8322011-05-16 16:38:14 +02003286static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003287{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003288 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3289}
3290
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003291static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003292{
3293 struct nfs4_stid *ret;
3294
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003295 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003296 if (!ret)
3297 return NULL;
3298 return delegstateid(ret);
3299}
3300
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003301static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3302{
3303 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3304 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3305}
3306
Al Virob37ad282006-10-19 23:28:59 -07003307static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003308nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003309 struct nfs4_delegation **dp)
3310{
3311 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003312 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07003313
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003314 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07003315 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003316 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003317 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07003318 status = nfs4_check_delegmode(*dp, flags);
3319 if (status)
3320 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003321out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003322 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003323 return nfs_ok;
3324 if (status)
3325 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003326 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003327 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003328}
3329
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003330static struct nfs4_ol_stateid *
3331nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003333 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003334 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
Trond Myklebust1d31a252014-07-10 14:07:25 -04003336 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003337 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 /* ignore lock owners */
3339 if (local->st_stateowner->so_is_open_owner == 0)
3340 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003341 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003342 ret = local;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003343 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003346 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003347 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348}
3349
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003350static inline int nfs4_access_to_access(u32 nfs4_access)
3351{
3352 int flags = 0;
3353
3354 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3355 flags |= NFSD_MAY_READ;
3356 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3357 flags |= NFSD_MAY_WRITE;
3358 return flags;
3359}
3360
Al Virob37ad282006-10-19 23:28:59 -07003361static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3363 struct nfsd4_open *open)
3364{
3365 struct iattr iattr = {
3366 .ia_valid = ATTR_SIZE,
3367 .ia_size = 0,
3368 };
3369 if (!open->op_truncate)
3370 return 0;
3371 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003372 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3374}
3375
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003376static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003377 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3378 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003379{
Trond Myklebustde186432014-07-10 14:07:26 -04003380 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003381 __be32 status;
3382 int oflag = nfs4_access_to_omode(open->op_share_access);
3383 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003384 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003385
Trond Myklebustde186432014-07-10 14:07:26 -04003386 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003387
3388 /*
3389 * Are we trying to set a deny mode that would conflict with
3390 * current access?
3391 */
3392 status = nfs4_file_check_deny(fp, open->op_share_deny);
3393 if (status != nfs_ok) {
3394 spin_unlock(&fp->fi_lock);
3395 goto out;
3396 }
3397
3398 /* set access to the file */
3399 status = nfs4_file_get_access(fp, open->op_share_access);
3400 if (status != nfs_ok) {
3401 spin_unlock(&fp->fi_lock);
3402 goto out;
3403 }
3404
3405 /* Set access bits in stateid */
3406 old_access_bmap = stp->st_access_bmap;
3407 set_access(open->op_share_access, stp);
3408
3409 /* Set new deny mask */
3410 old_deny_bmap = stp->st_deny_bmap;
3411 set_deny(open->op_share_deny, stp);
3412 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3413
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003414 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003415 spin_unlock(&fp->fi_lock);
3416 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003417 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003418 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003419 spin_lock(&fp->fi_lock);
3420 if (!fp->fi_fds[oflag]) {
3421 fp->fi_fds[oflag] = filp;
3422 filp = NULL;
3423 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003424 }
Trond Myklebustde186432014-07-10 14:07:26 -04003425 spin_unlock(&fp->fi_lock);
3426 if (filp)
3427 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003428
3429 status = nfsd4_truncate(rqstp, cur_fh, open);
3430 if (status)
3431 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003432out:
3433 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003434out_put_access:
3435 stp->st_access_bmap = old_access_bmap;
3436 nfs4_file_put_access(fp, open->op_share_access);
3437 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3438 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003439}
3440
Al Virob37ad282006-10-19 23:28:59 -07003441static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003442nfs4_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 -07003443{
Al Virob37ad282006-10-19 23:28:59 -07003444 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003445 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003447 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003448 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003449
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003450 /* test and set deny mode */
3451 spin_lock(&fp->fi_lock);
3452 status = nfs4_file_check_deny(fp, open->op_share_deny);
3453 if (status == nfs_ok) {
3454 old_deny_bmap = stp->st_deny_bmap;
3455 set_deny(open->op_share_deny, stp);
3456 fp->fi_share_deny |=
3457 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3458 }
3459 spin_unlock(&fp->fi_lock);
3460
3461 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003464 status = nfsd4_truncate(rqstp, cur_fh, open);
3465 if (status != nfs_ok)
3466 reset_union_bmap_deny(old_deny_bmap, stp);
3467 return status;
3468}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003471nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003473 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474}
3475
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003476/* Should we give out recallable state?: */
3477static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3478{
3479 if (clp->cl_cb_state == NFSD4_CB_UP)
3480 return true;
3481 /*
3482 * In the sessions case, since we don't have to establish a
3483 * separate connection for callbacks, we assume it's OK
3484 * until we hear otherwise:
3485 */
3486 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3487}
3488
Jeff Laytond564fbe2014-07-16 10:31:58 -04003489static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003490{
3491 struct file_lock *fl;
3492
3493 fl = locks_alloc_lock();
3494 if (!fl)
3495 return NULL;
3496 locks_init_lock(fl);
3497 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003498 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003499 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3500 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003501 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003502 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003503 return fl;
3504}
3505
J. Bruce Fields99c41512013-05-21 16:21:25 -04003506static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003507{
Trond Myklebust11b91642014-07-29 21:34:08 -04003508 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003509 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003510 struct file *filp;
3511 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003512
Jeff Laytond564fbe2014-07-16 10:31:58 -04003513 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003514 if (!fl)
3515 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003516 filp = find_readable_file(fp);
3517 if (!filp) {
3518 /* We should always have a readable file here */
3519 WARN_ON_ONCE(1);
3520 return -EBADF;
3521 }
3522 fl->fl_file = filp;
3523 status = vfs_setlease(filp, fl->fl_type, &fl);
3524 if (status) {
3525 locks_free_lock(fl);
3526 goto out_fput;
3527 }
Benny Halevycdc97502014-05-30 09:09:30 -04003528 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003529 spin_lock(&fp->fi_lock);
3530 /* Did the lease get broken before we took the lock? */
3531 status = -EAGAIN;
3532 if (fp->fi_had_conflict)
3533 goto out_unlock;
3534 /* Race breaker */
3535 if (fp->fi_lease) {
3536 status = 0;
3537 atomic_inc(&fp->fi_delegees);
3538 hash_delegation_locked(dp, fp);
3539 goto out_unlock;
3540 }
3541 fp->fi_lease = fl;
3542 fp->fi_deleg_file = filp;
3543 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003544 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003545 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003546 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003547 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003548out_unlock:
3549 spin_unlock(&fp->fi_lock);
3550 spin_unlock(&state_lock);
3551out_fput:
3552 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003553 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003554}
3555
Jeff Layton0b266932014-07-25 07:34:25 -04003556static struct nfs4_delegation *
3557nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3558 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003559{
Jeff Layton0b266932014-07-25 07:34:25 -04003560 int status;
3561 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003562
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003563 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003564 return ERR_PTR(-EAGAIN);
3565
3566 dp = alloc_init_deleg(clp, fh);
3567 if (!dp)
3568 return ERR_PTR(-ENOMEM);
3569
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003570 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003571 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003572 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003573 dp->dl_stid.sc_file = fp;
Jeff Layton417c6622014-07-21 09:34:57 -04003574 if (!fp->fi_lease) {
3575 spin_unlock(&fp->fi_lock);
3576 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003577 status = nfs4_setlease(dp);
3578 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003579 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003580 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003581 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003582 status = -EAGAIN;
3583 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003584 }
Benny Halevy931ee562014-05-30 09:09:27 -04003585 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003586 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003587out_unlock:
3588 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003589 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003590out:
3591 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003592 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003593 return ERR_PTR(status);
3594 }
3595 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003596}
3597
Benny Halevy4aa89132012-02-21 14:16:44 -08003598static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3599{
3600 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3601 if (status == -EAGAIN)
3602 open->op_why_no_deleg = WND4_CONTENTION;
3603 else {
3604 open->op_why_no_deleg = WND4_RESOURCE;
3605 switch (open->op_deleg_want) {
3606 case NFS4_SHARE_WANT_READ_DELEG:
3607 case NFS4_SHARE_WANT_WRITE_DELEG:
3608 case NFS4_SHARE_WANT_ANY_DELEG:
3609 break;
3610 case NFS4_SHARE_WANT_CANCEL:
3611 open->op_why_no_deleg = WND4_CANCELLED;
3612 break;
3613 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003614 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003615 }
3616 }
3617}
3618
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619/*
3620 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003621 *
3622 * Note we don't support write delegations, and won't until the vfs has
3623 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 */
3625static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003626nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3627 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628{
3629 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003630 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3631 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003632 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003633 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003635 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003636 open->op_recall = 0;
3637 switch (open->op_claim_type) {
3638 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003639 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003640 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003641 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3642 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003643 break;
3644 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003645 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003646 /*
3647 * Let's not give out any delegations till everyone's
3648 * had the chance to reclaim theirs....
3649 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003650 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003651 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003652 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003653 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003654 /*
3655 * Also, if the file was opened for write or
3656 * create, there's a good chance the client's
3657 * about to write to it, resulting in an
3658 * immediate recall (since we don't support
3659 * write delegations):
3660 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003661 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003662 goto out_no_deleg;
3663 if (open->op_create == NFS4_OPEN_CREATE)
3664 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003665 break;
3666 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003667 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003668 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003669 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003670 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003671 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003673 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003675 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003676 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003677 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003678 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003679out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003680 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3681 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003682 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003683 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003684 open->op_recall = 1;
3685 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003686
3687 /* 4.1 client asking for a delegation? */
3688 if (open->op_deleg_want)
3689 nfsd4_open_deleg_none_ext(open, status);
3690 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691}
3692
Benny Halevye27f49c2012-02-21 14:16:54 -08003693static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3694 struct nfs4_delegation *dp)
3695{
3696 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3697 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3698 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3699 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3700 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3701 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3702 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3703 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3704 }
3705 /* Otherwise the client must be confused wanting a delegation
3706 * it already has, therefore we don't return
3707 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3708 */
3709}
3710
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711/*
3712 * called with nfs4_lock_state() held.
3713 */
Al Virob37ad282006-10-19 23:28:59 -07003714__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3716{
Andy Adamson66689582009-04-03 08:28:45 +03003717 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003718 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003720 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003721 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003722 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 /*
3725 * Lookup file; if found, lookup stateid and check open request,
3726 * and check for delegations in the process of being recalled.
3727 * If not found, create the nfs4_file struct
3728 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003729 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04003730 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04003731 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003732 if (status)
3733 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003734 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04003736 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003737 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003738 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003739 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003740 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 }
3742
3743 /*
3744 * OPEN the file, or upgrade an existing OPEN.
3745 * If truncate fails, the OPEN fails.
3746 */
3747 if (stp) {
3748 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003749 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 if (status)
3751 goto out;
3752 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003753 stp = open->op_stp;
3754 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003755 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003756 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3757 if (status) {
3758 release_open_stateid(stp);
3759 goto out;
3760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003762 update_stateid(&stp->st_stid.sc_stateid);
3763 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
Benny Halevyd24433c2012-02-16 20:57:17 +02003765 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02003766 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3767 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3768 open->op_why_no_deleg = WND4_NOT_WANTED;
3769 goto nodeleg;
3770 }
3771 }
3772
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 /*
3774 * Attempt to hand out a delegation. No error return, because the
3775 * OPEN succeeds even if we fail.
3776 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003777 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003778nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 status = nfs_ok;
3780
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003781 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003782 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003784 /* 4.1 client trying to upgrade/downgrade delegation? */
3785 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003786 open->op_deleg_want)
3787 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003788
NeilBrown13cd2182005-06-23 22:03:10 -07003789 if (fp)
3790 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003791 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003792 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 /*
3794 * To finish the open response, we just need to set the rflags.
3795 */
3796 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003797 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003798 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3800
3801 return status;
3802}
3803
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003804void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3805{
3806 if (open->op_openowner) {
3807 struct nfs4_openowner *oo = open->op_openowner;
3808
3809 if (!list_empty(&oo->oo_owner.so_stateids))
3810 list_del_init(&oo->oo_close_lru);
3811 if (oo->oo_flags & NFS4_OO_NEW) {
3812 if (status) {
3813 release_openowner(oo);
3814 open->op_openowner = NULL;
3815 } else
3816 oo->oo_flags &= ~NFS4_OO_NEW;
3817 }
3818 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003819 if (open->op_file)
3820 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003821 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04003822 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003823}
3824
Al Virob37ad282006-10-19 23:28:59 -07003825__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003826nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3827 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828{
3829 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003830 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003831 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
3833 nfs4_lock_state();
3834 dprintk("process_renew(%08x/%08x): starting\n",
3835 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003836 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003837 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003839 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003841 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003842 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 goto out;
3844 status = nfs_ok;
3845out:
3846 nfs4_unlock_state();
3847 return status;
3848}
3849
NeilBrowna76b4312005-06-23 22:04:01 -07003850static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003851nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07003852{
Jeff Layton33dcc482012-04-10 11:08:48 -04003853 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003854 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04003855 return;
3856
NeilBrowna76b4312005-06-23 22:04:01 -07003857 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003858 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003859 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003860 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003861 /*
3862 * Now that every NFSv4 client has had the chance to recover and
3863 * to see the (possibly new, possibly shorter) lease time, we
3864 * can safely set the next grace time to the current lease time:
3865 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03003866 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003867}
3868
NeilBrownfd39ca92005-06-23 22:04:03 -07003869static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003870nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871{
3872 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003873 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 struct nfs4_delegation *dp;
3875 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003876 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003877 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878
3879 nfs4_lock_state();
3880
3881 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003882 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03003883 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003884 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03003885 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 clp = list_entry(pos, struct nfs4_client, cl_lru);
3887 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3888 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003889 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 break;
3891 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003892 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03003893 dprintk("NFSD: client in use (clientid %08x)\n",
3894 clp->cl_clientid.cl_id);
3895 continue;
3896 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003897 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003898 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003899 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03003900 list_for_each_safe(pos, next, &reaplist) {
3901 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 dprintk("NFSD: purging unused client (clientid %08x)\n",
3903 clp->cl_clientid.cl_id);
3904 expire_client(clp);
3905 }
Benny Halevycdc97502014-05-30 09:09:30 -04003906 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003907 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03003909 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3910 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003912 t = dp->dl_time - cutoff;
3913 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 break;
3915 }
Jeff Layton42690672014-07-25 07:34:20 -04003916 unhash_delegation_locked(dp);
3917 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 }
Benny Halevycdc97502014-05-30 09:09:30 -04003919 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04003920 while (!list_empty(&reaplist)) {
3921 dp = list_first_entry(&reaplist, struct nfs4_delegation,
3922 dl_recall_lru);
3923 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003924 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003926 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003927 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3928 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003929 t = oo->oo_time - cutoff;
3930 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 break;
3932 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003933 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04003935 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04003937 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938}
3939
Harvey Harrisona254b242008-02-20 12:49:00 -08003940static struct workqueue_struct *laundry_wq;
3941static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08003942
3943static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003944laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945{
3946 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003947 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3948 work);
3949 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3950 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003952 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003954 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955}
3956
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003957static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07003958{
Trond Myklebust11b91642014-07-29 21:34:08 -04003959 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003960 return nfserr_bad_stateid;
3961 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962}
3963
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003965access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003967 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3968 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3969 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970}
3971
3972static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003973access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003975 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3976 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977}
3978
3979static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003980__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981{
Al Virob37ad282006-10-19 23:28:59 -07003982 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983
J. Bruce Fields02921912010-07-29 15:16:59 -04003984 /* For lock stateid's, we test the parent open, not the lock: */
3985 if (stp->st_openstp)
3986 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003987 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003989 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 goto out;
3991 status = nfs_ok;
3992out:
3993 return status;
3994}
3995
Al Virob37ad282006-10-19 23:28:59 -07003996static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003997check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003999 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004001 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004002 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 * conflicting state; so we must wait out the grace period. */
4004 return nfserr_grace;
4005 } else if (flags & WR_STATE)
4006 return nfs4_share_conflict(current_fh,
4007 NFS4_SHARE_DENY_WRITE);
4008 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4009 return nfs4_share_conflict(current_fh,
4010 NFS4_SHARE_DENY_READ);
4011}
4012
4013/*
4014 * Allow READ/WRITE during grace period on recovered state only for files
4015 * that are not able to provide mandatory locking.
4016 */
4017static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004018grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004020 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021}
4022
J. Bruce Fields81b82962011-08-23 11:03:29 -04004023/* Returns true iff a is later than b: */
4024static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4025{
Jim Rees1a9357f2013-05-17 17:33:00 -04004026 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004027}
4028
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004029static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004030{
Andy Adamson66689582009-04-03 08:28:45 +03004031 /*
4032 * When sessions are used the stateid generation number is ignored
4033 * when it is zero.
4034 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004035 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004036 return nfs_ok;
4037
4038 if (in->si_generation == ref->si_generation)
4039 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004040
J. Bruce Fields0836f582008-01-26 19:08:12 -05004041 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004042 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004043 return nfserr_bad_stateid;
4044 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004045 * However, we could see a stateid from the past, even from a
4046 * non-buggy client. For example, if the client sends a lock
4047 * while some IO is outstanding, the lock may bump si_generation
4048 * while the IO is still in flight. The client could avoid that
4049 * situation by waiting for responses on all the IO requests,
4050 * but better performance may result in retrying IO that
4051 * receives an old_stateid error if requests are rarely
4052 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004053 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004054 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004055}
4056
Chuck Lever7df302f2012-05-29 13:56:37 -04004057static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004058{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004059 struct nfs4_stid *s;
4060 struct nfs4_ol_stateid *ols;
4061 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004062
Chuck Lever7df302f2012-05-29 13:56:37 -04004063 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4064 return nfserr_bad_stateid;
4065 /* Client debugging aid. */
4066 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4067 char addr_str[INET6_ADDRSTRLEN];
4068 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4069 sizeof(addr_str));
4070 pr_warn_ratelimited("NFSD: client %s testing state ID "
4071 "with incorrect client ID\n", addr_str);
4072 return nfserr_bad_stateid;
4073 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004074 s = find_stateid(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004075 if (!s)
Chuck Lever7df302f2012-05-29 13:56:37 -04004076 return nfserr_bad_stateid;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004077 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004078 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004079 return status;
J. Bruce Fields23340032013-04-09 17:42:28 -04004080 switch (s->sc_type) {
4081 case NFS4_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004082 return nfs_ok;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004083 case NFS4_REVOKED_DELEG_STID:
4084 return nfserr_deleg_revoked;
J. Bruce Fields23340032013-04-09 17:42:28 -04004085 case NFS4_OPEN_STID:
4086 case NFS4_LOCK_STID:
4087 ols = openlockstateid(s);
4088 if (ols->st_stateowner->so_is_open_owner
4089 && !(openowner(ols->st_stateowner)->oo_flags
4090 & NFS4_OO_CONFIRMED))
4091 return nfserr_bad_stateid;
4092 return nfs_ok;
4093 default:
4094 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004095 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004096 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004097 case NFS4_CLOSED_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004098 return nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004099 }
Bryan Schumaker17456802011-07-13 10:50:48 -04004100}
4101
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004102static __be32
4103nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4104 stateid_t *stateid, unsigned char typemask,
4105 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004106{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004107 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004108
4109 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4110 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004111 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004112 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004113 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004114 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004115 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004116 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004117 if (status)
4118 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004119 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004120 if (!*s)
4121 return nfserr_bad_stateid;
4122 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004123}
4124
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125/*
4126* Checks for stateid operations
4127*/
Al Virob37ad282006-10-19 23:28:59 -07004128__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004129nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004130 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004132 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004133 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004135 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004137 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004138 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004139 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 if (filpp)
4142 *filpp = NULL;
4143
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004144 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 return nfserr_grace;
4146
4147 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004148 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149
Trond Myklebust14bcab12014-04-18 14:44:07 -04004150 nfs4_lock_state();
4151
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004152 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004153 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004154 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004155 if (status)
Trond Myklebust14bcab12014-04-18 14:44:07 -04004156 goto out;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004157 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4158 if (status)
4159 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004160 switch (s->sc_type) {
4161 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004162 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004163 status = nfs4_check_delegmode(dp, flags);
4164 if (status)
4165 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004166 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004167 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004168 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004169 WARN_ON_ONCE(1);
4170 status = nfserr_serverfault;
4171 goto out;
4172 }
Trond Myklebustde186432014-07-10 14:07:26 -04004173 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004174 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004175 break;
4176 case NFS4_OPEN_STID:
4177 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004178 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004179 status = nfs4_check_fh(current_fh, stp);
4180 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004182 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004183 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004185 status = nfs4_check_openmode(stp, flags);
4186 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004188 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004189 struct nfs4_file *fp = stp->st_stid.sc_file;
4190
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004191 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004192 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004193 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004194 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004195 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004196 break;
4197 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004198 status = nfserr_bad_stateid;
4199 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 }
4201 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004202 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004203 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204out:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004205 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 return status;
4207}
4208
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004209static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004210nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004211{
J. Bruce Fieldsa1b8ff42014-05-20 15:55:21 -04004212 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4213
Trond Myklebust11b91642014-07-29 21:34:08 -04004214 if (check_for_locks(stp->st_stid.sc_file, lo))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004215 return nfserr_locks_held;
Jeff Laytonc53530d2014-06-30 11:48:39 -04004216 release_lockowner_if_empty(lo);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004217 return nfs_ok;
4218}
4219
4220/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004221 * Test if the stateid is valid
4222 */
4223__be32
4224nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4225 struct nfsd4_test_stateid *test_stateid)
4226{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004227 struct nfsd4_test_stateid_id *stateid;
4228 struct nfs4_client *cl = cstate->session->se_client;
4229
4230 nfs4_lock_state();
4231 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004232 stateid->ts_id_status =
4233 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004234 nfs4_unlock_state();
4235
Bryan Schumaker17456802011-07-13 10:50:48 -04004236 return nfs_ok;
4237}
4238
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004239__be32
4240nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4241 struct nfsd4_free_stateid *free_stateid)
4242{
4243 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004244 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004245 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004246 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004247 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004248
4249 nfs4_lock_state();
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004250 s = find_stateid(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004251 if (!s)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004252 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004253 switch (s->sc_type) {
4254 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004255 ret = nfserr_locks_held;
4256 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004257 case NFS4_OPEN_STID:
4258 case NFS4_LOCK_STID:
4259 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4260 if (ret)
4261 goto out;
4262 if (s->sc_type == NFS4_LOCK_STID)
4263 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4264 else
4265 ret = nfserr_locks_held;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004266 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004267 case NFS4_REVOKED_DELEG_STID:
4268 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004269 spin_lock(&cl->cl_lock);
4270 list_del_init(&dp->dl_recall_lru);
4271 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004272 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004273 ret = nfs_ok;
4274 break;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004275 default:
4276 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004277 }
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004278out:
4279 nfs4_unlock_state();
4280 return ret;
4281}
4282
NeilBrown4c4cd222005-07-07 17:59:27 -07004283static inline int
4284setlkflg (int type)
4285{
4286 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4287 RD_STATE : WR_STATE;
4288}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004290static __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 -04004291{
4292 struct svc_fh *current_fh = &cstate->current_fh;
4293 struct nfs4_stateowner *sop = stp->st_stateowner;
4294 __be32 status;
4295
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004296 status = nfsd4_check_seqid(cstate, sop, seqid);
4297 if (status)
4298 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004299 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4300 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004301 /*
4302 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004303 * nfserr_replay_me from the previous step, and
4304 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004305 */
4306 return nfserr_bad_stateid;
4307 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4308 if (status)
4309 return status;
4310 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004311}
4312
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313/*
4314 * Checks for sequence id mutating operations.
4315 */
Al Virob37ad282006-10-19 23:28:59 -07004316static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004317nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004318 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004319 struct nfs4_ol_stateid **stpp,
4320 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004322 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004323 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004324 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004326 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4327 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004328
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004330 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004331 if (status)
4332 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004333 stp = openlockstateid(s);
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04004334 if (!nfsd4_has_session(cstate))
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004335 cstate->replay_owner = stp->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004337 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4338 if (!status)
4339 *stpp = stp;
4340 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004341}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004342
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004343static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4344 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004345{
4346 __be32 status;
4347 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004349 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004350 NFS4_OPEN_STID, stpp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004351 if (status)
4352 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004353 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004354 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07004355 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004356 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357}
4358
Al Virob37ad282006-10-19 23:28:59 -07004359__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004360nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004361 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362{
Al Virob37ad282006-10-19 23:28:59 -07004363 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004364 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004365 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004366 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367
Al Viroa6a9f182013-09-16 10:57:01 -04004368 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4369 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004371 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004372 if (status)
4373 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374
4375 nfs4_lock_state();
4376
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004377 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004378 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004379 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004380 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004381 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004382 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004383 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004384 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004385 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004386 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004387 update_stateid(&stp->st_stid.sc_stateid);
4388 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004389 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004390 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004391
Jeff Layton2a4317c2012-03-21 16:42:43 -04004392 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004393 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004395 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004396 if (!cstate->replay_owner)
4397 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 return status;
4399}
4400
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004401static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004403 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004404 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004405 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004406 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004407}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004408
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004409static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4410{
4411 switch (to_access) {
4412 case NFS4_SHARE_ACCESS_READ:
4413 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4414 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4415 break;
4416 case NFS4_SHARE_ACCESS_WRITE:
4417 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4418 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4419 break;
4420 case NFS4_SHARE_ACCESS_BOTH:
4421 break;
4422 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004423 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 }
4425}
4426
Al Virob37ad282006-10-19 23:28:59 -07004427__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004428nfsd4_open_downgrade(struct svc_rqst *rqstp,
4429 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004430 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431{
Al Virob37ad282006-10-19 23:28:59 -07004432 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004433 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004434 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435
Al Viroa6a9f182013-09-16 10:57:01 -04004436 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4437 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004439 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004440 if (od->od_deleg_want)
4441 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4442 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
4444 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004445 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004446 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004447 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004450 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004451 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 stp->st_access_bmap, od->od_share_access);
4453 goto out;
4454 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004455 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004456 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 stp->st_deny_bmap, od->od_share_deny);
4458 goto out;
4459 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004460 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461
Jeff Laytonce0fc432012-05-11 09:45:14 -04004462 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004464 update_stateid(&stp->st_stid.sc_stateid);
4465 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 status = nfs_ok;
4467out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004468 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004469 if (!cstate->replay_owner)
4470 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 return status;
4472}
4473
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004474static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4475{
Trond Myklebustacf92952014-06-30 11:48:37 -04004476 struct nfs4_client *clp = s->st_stid.sc_client;
4477 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4478
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004479 s->st_stid.sc_type = NFS4_CLOSED_STID;
Trond Myklebustacf92952014-06-30 11:48:37 -04004480 unhash_open_stateid(s);
4481
4482 if (clp->cl_minorversion) {
Trond Myklebustacf92952014-06-30 11:48:37 -04004483 if (list_empty(&oo->oo_owner.so_stateids))
4484 release_openowner(oo);
Trond Myklebust60116952014-07-29 21:34:06 -04004485 nfs4_put_stid(&s->st_stid);
Trond Myklebustacf92952014-06-30 11:48:37 -04004486 } else {
Trond Myklebust60116952014-07-29 21:34:06 -04004487 /*
4488 * In the 4.0 case we need to keep the owners around a
4489 * little while to handle CLOSE replay. We still do need
4490 * to release any file access that is held by them
4491 * before returning however.
4492 */
4493 release_all_access(s);
Trond Myklebust11b91642014-07-29 21:34:08 -04004494 if (s->st_stid.sc_file) {
4495 put_nfs4_file(s->st_stid.sc_file);
4496 s->st_stid.sc_file = NULL;
Trond Myklebustf8338832014-07-25 07:34:19 -04004497 }
Trond Myklebustacf92952014-06-30 11:48:37 -04004498 oo->oo_last_closed_stid = s;
Trond Myklebustacf92952014-06-30 11:48:37 -04004499 if (list_empty(&oo->oo_owner.so_stateids))
4500 move_to_close_lru(oo, clp->net);
4501 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004502}
4503
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504/*
4505 * nfs4_unlock_state() called after encode
4506 */
Al Virob37ad282006-10-19 23:28:59 -07004507__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004508nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004509 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510{
Al Virob37ad282006-10-19 23:28:59 -07004511 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004512 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004513 struct net *net = SVC_NET(rqstp);
4514 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
Al Viroa6a9f182013-09-16 10:57:01 -04004516 dprintk("NFSD: nfsd4_close on file %pd\n",
4517 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518
4519 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004520 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4521 &close->cl_stateid,
4522 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004523 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004524 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004525 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004527 update_stateid(&stp->st_stid.sc_stateid);
4528 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004530 nfsd4_close_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004532 if (!cstate->replay_owner)
4533 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 return status;
4535}
4536
Al Virob37ad282006-10-19 23:28:59 -07004537__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004538nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4539 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004541 struct nfs4_delegation *dp;
4542 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004543 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004544 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004545 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004547 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004548 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549
4550 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004551 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004552 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004553 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004554 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004555 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004556 if (status)
4557 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004558
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004559 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004561 nfs4_unlock_state();
4562
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 return status;
4564}
4565
4566
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568
Benny Halevy87df4de2008-12-15 19:42:03 +02004569static inline u64
4570end_offset(u64 start, u64 len)
4571{
4572 u64 end;
4573
4574 end = start + len;
4575 return end >= start ? end: NFS4_MAX_UINT64;
4576}
4577
4578/* last octet in a range */
4579static inline u64
4580last_byte_offset(u64 start, u64 len)
4581{
4582 u64 end;
4583
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004584 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004585 end = start + len;
4586 return end > start ? end - 1: NFS4_MAX_UINT64;
4587}
4588
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589/*
4590 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4591 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4592 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4593 * locking, this prevents us from being completely protocol-compliant. The
4594 * real solution to this problem is to start using unsigned file offsets in
4595 * the VFS, but this is a very deep change!
4596 */
4597static inline void
4598nfs4_transform_lock_offset(struct file_lock *lock)
4599{
4600 if (lock->fl_start < 0)
4601 lock->fl_start = OFFSET_MAX;
4602 if (lock->fl_end < 0)
4603 lock->fl_end = OFFSET_MAX;
4604}
4605
NeilBrownd5b90262006-04-10 22:55:22 -07004606/* Hack!: For now, we're defining this just so we can use a pointer to it
4607 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004608static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004609};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610
4611static inline void
4612nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4613{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004614 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615
NeilBrownd5b90262006-04-10 22:55:22 -07004616 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004617 lo = (struct nfs4_lockowner *) fl->fl_owner;
4618 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4619 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004620 if (!deny->ld_owner.data)
4621 /* We just don't care that much */
4622 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004623 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4624 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004625 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004626nevermind:
4627 deny->ld_owner.len = 0;
4628 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004629 deny->ld_clientid.cl_boot = 0;
4630 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 }
4632 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004633 deny->ld_length = NFS4_MAX_UINT64;
4634 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4636 deny->ld_type = NFS4_READ_LT;
4637 if (fl->fl_type != F_RDLCK)
4638 deny->ld_type = NFS4_WRITE_LT;
4639}
4640
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004641static struct nfs4_lockowner *
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004642find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4643 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644{
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004645 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4646 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004648 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4649 if (so->so_is_open_owner)
4650 continue;
4651 if (!same_owner_str(so, owner, clid))
4652 continue;
4653 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 }
4655 return NULL;
4656}
4657
4658/*
4659 * Alloc a lock owner structure.
4660 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004661 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004663 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004665static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004666alloc_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 -04004667 struct nfs4_lockowner *lo;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004668 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004670 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4671 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004673 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4674 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07004675 /* It is the openowner seqid that will be incremented in encode in the
4676 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004677 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004678 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004679 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680}
4681
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004682static struct nfs4_ol_stateid *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004683alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp,
4684 struct inode *inode,
4685 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004687 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004688 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689
J. Bruce Fields996e0932011-10-17 11:14:48 -04004690 stp = nfs4_alloc_stateid(clp);
NeilBrown5ac049a2005-06-23 22:03:03 -07004691 if (stp == NULL)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04004692 return NULL;
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004693 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004694 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4695 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07004696 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004697 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004698 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004700 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004701 list_add(&stp->st_locks, &open_stp->st_locks);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004702 spin_lock(&fp->fi_lock);
4703 list_add(&stp->st_perfile, &fp->fi_stateids);
4704 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 return stp;
4706}
4707
Jeff Laytonc53530d2014-06-30 11:48:39 -04004708static struct nfs4_ol_stateid *
4709find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4710{
4711 struct nfs4_ol_stateid *lst;
4712
4713 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004714 if (lst->st_stid.sc_file == fp)
Jeff Laytonc53530d2014-06-30 11:48:39 -04004715 return lst;
4716 }
4717 return NULL;
4718}
4719
4720
NeilBrownfd39ca92005-06-23 22:04:03 -07004721static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722check_lock_length(u64 offset, u64 length)
4723{
Benny Halevy87df4de2008-12-15 19:42:03 +02004724 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 LOFF_OVERFLOW(offset, length)));
4726}
4727
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004728static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004729{
Trond Myklebust11b91642014-07-29 21:34:08 -04004730 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004731
Jeff Layton7214e862014-07-10 14:07:33 -04004732 lockdep_assert_held(&fp->fi_lock);
4733
Jeff Layton82c5ff12012-05-11 09:45:13 -04004734 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004735 return;
Jeff Layton12659652014-07-10 14:07:28 -04004736 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004737 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004738}
4739
J. Bruce Fields2355c592012-04-25 16:56:22 -04004740static __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 -04004741{
Trond Myklebust11b91642014-07-29 21:34:08 -04004742 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004743 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4744 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004745 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004746 struct nfs4_lockowner *lo;
4747 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004748 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004749
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004750 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004751 if (!lo) {
4752 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4753 &lock->v.new.owner);
4754 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4755 if (lo == NULL)
4756 return nfserr_jukebox;
4757 } else {
4758 /* with an existing lockowner, seqids must be the same */
4759 if (!cstate->minorversion &&
4760 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004761 return nfserr_bad_seqid;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004762 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004763
4764 *lst = find_lock_stateid(lo, fi);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004765 if (*lst == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004766 *lst = alloc_init_lock_stateid(lo, fi, inode, ost);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004767 if (*lst == NULL) {
4768 release_lockowner_if_empty(lo);
4769 return nfserr_jukebox;
4770 }
4771 *new = true;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004772 }
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004773 return nfs_ok;
4774}
4775
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776/*
4777 * LOCK operation
4778 */
Al Virob37ad282006-10-19 23:28:59 -07004779__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004780nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004781 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004783 struct nfs4_openowner *open_sop = NULL;
4784 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004785 struct nfs4_ol_stateid *lock_stp;
Jeff Layton7214e862014-07-10 14:07:33 -04004786 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004787 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004788 struct file_lock *file_lock = NULL;
4789 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004790 __be32 status = 0;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004791 bool new_state = false;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004792 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004793 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004794 struct net *net = SVC_NET(rqstp);
4795 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796
4797 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4798 (long long) lock->lk_offset,
4799 (long long) lock->lk_length);
4800
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 if (check_lock_length(lock->lk_offset, lock->lk_length))
4802 return nfserr_inval;
4803
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004804 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004805 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004806 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4807 return status;
4808 }
4809
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 nfs4_lock_state();
4811
4812 if (lock->lk_is_new) {
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004813 struct nfs4_ol_stateid *open_stp = NULL;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004814
4815 if (nfsd4_has_session(cstate))
4816 /* See rfc 5661 18.10.3: given clientid is ignored: */
4817 memcpy(&lock->v.new.clientid,
4818 &cstate->session->se_client->cl_clientid,
4819 sizeof(clientid_t));
4820
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004822 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004826 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 lock->lk_new_open_seqid,
4828 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004829 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07004830 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004832 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004833 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004834 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004835 &lock->v.new.clientid))
4836 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004837 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4838 &lock_stp, &new_state);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004839 } else
Benny Halevydd453df2009-04-03 08:28:41 +03004840 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004841 lock->lk_old_lock_seqid,
4842 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004843 NFS4_LOCK_STID, &lock_stp, nn);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004844 if (status)
4845 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004846 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004848 lkflg = setlkflg(lock->lk_type);
4849 status = nfs4_check_openmode(lock_stp, lkflg);
4850 if (status)
4851 goto out;
4852
NeilBrown0dd395d2005-07-07 17:59:15 -07004853 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004854 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004855 goto out;
4856 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004857 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004858 goto out;
4859
Jeff Layton21179d82012-08-21 08:03:32 -04004860 file_lock = locks_alloc_lock();
4861 if (!file_lock) {
4862 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4863 status = nfserr_jukebox;
4864 goto out;
4865 }
4866
Trond Myklebust11b91642014-07-29 21:34:08 -04004867 fp = lock_stp->st_stid.sc_file;
Jeff Layton21179d82012-08-21 08:03:32 -04004868 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 switch (lock->lk_type) {
4870 case NFS4_READ_LT:
4871 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04004872 spin_lock(&fp->fi_lock);
4873 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004874 if (filp)
4875 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04004876 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04004877 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004878 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 case NFS4_WRITE_LT:
4880 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04004881 spin_lock(&fp->fi_lock);
4882 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004883 if (filp)
4884 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04004885 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04004886 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004887 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 default:
4889 status = nfserr_inval;
4890 goto out;
4891 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004892 if (!filp) {
4893 status = nfserr_openmode;
4894 goto out;
4895 }
Jeff Layton21179d82012-08-21 08:03:32 -04004896 file_lock->fl_owner = (fl_owner_t)lock_sop;
4897 file_lock->fl_pid = current->tgid;
4898 file_lock->fl_file = filp;
4899 file_lock->fl_flags = FL_POSIX;
4900 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4901 file_lock->fl_start = lock->lk_offset;
4902 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4903 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904
Jeff Layton21179d82012-08-21 08:03:32 -04004905 conflock = locks_alloc_lock();
4906 if (!conflock) {
4907 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4908 status = nfserr_jukebox;
4909 goto out;
4910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911
Jeff Layton21179d82012-08-21 08:03:32 -04004912 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004913 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004915 update_stateid(&lock_stp->st_stid.sc_stateid);
4916 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004918 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004919 break;
4920 case (EAGAIN): /* conflock holds conflicting lock */
4921 status = nfserr_denied;
4922 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04004923 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004924 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 case (EDEADLK):
4926 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004927 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004928 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004929 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004930 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004931 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933out:
Trond Myklebustde186432014-07-10 14:07:26 -04004934 if (filp)
4935 fput(filp);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004936 if (status && new_state)
Jeff Laytonc53530d2014-06-30 11:48:39 -04004937 release_lock_stateid(lock_stp);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004938 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004939 if (!cstate->replay_owner)
4940 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004941 if (file_lock)
4942 locks_free_lock(file_lock);
4943 if (conflock)
4944 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 return status;
4946}
4947
4948/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004949 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4950 * so we do a temporary open here just to get an open file to pass to
4951 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4952 * inode operation.)
4953 */
Al Viro04da6e92012-04-13 00:00:04 -04004954static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004955{
4956 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04004957 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4958 if (!err) {
4959 err = nfserrno(vfs_test_lock(file, lock));
4960 nfsd_close(file);
4961 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004962 return err;
4963}
4964
4965/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 * LOCKT operation
4967 */
Al Virob37ad282006-10-19 23:28:59 -07004968__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004969nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4970 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971{
Jeff Layton21179d82012-08-21 08:03:32 -04004972 struct file_lock *file_lock = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004973 struct nfs4_lockowner *lo;
Al Virob37ad282006-10-19 23:28:59 -07004974 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004975 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004977 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 return nfserr_grace;
4979
4980 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4981 return nfserr_inval;
4982
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 nfs4_lock_state();
4984
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004985 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004986 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004987 if (status)
4988 goto out;
4989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004991 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993
Jeff Layton21179d82012-08-21 08:03:32 -04004994 file_lock = locks_alloc_lock();
4995 if (!file_lock) {
4996 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4997 status = nfserr_jukebox;
4998 goto out;
4999 }
5000 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 switch (lockt->lt_type) {
5002 case NFS4_READ_LT:
5003 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005004 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 break;
5006 case NFS4_WRITE_LT:
5007 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005008 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 break;
5010 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005011 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 status = nfserr_inval;
5013 goto out;
5014 }
5015
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005016 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005017 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005018 file_lock->fl_owner = (fl_owner_t)lo;
5019 file_lock->fl_pid = current->tgid;
5020 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021
Jeff Layton21179d82012-08-21 08:03:32 -04005022 file_lock->fl_start = lockt->lt_offset;
5023 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024
Jeff Layton21179d82012-08-21 08:03:32 -04005025 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
Jeff Layton21179d82012-08-21 08:03:32 -04005027 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005028 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005029 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005030
Jeff Layton21179d82012-08-21 08:03:32 -04005031 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005033 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 }
5035out:
5036 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005037 if (file_lock)
5038 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 return status;
5040}
5041
Al Virob37ad282006-10-19 23:28:59 -07005042__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005043nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005044 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005046 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005048 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005049 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005050 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005051 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5052
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5054 (long long) locku->lu_offset,
5055 (long long) locku->lu_length);
5056
5057 if (check_lock_length(locku->lu_offset, locku->lu_length))
5058 return nfserr_inval;
5059
5060 nfs4_lock_state();
5061
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005062 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005063 &locku->lu_stateid, NFS4_LOCK_STID,
5064 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005065 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005067 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005068 if (!filp) {
5069 status = nfserr_lock_range;
5070 goto out;
5071 }
Jeff Layton21179d82012-08-21 08:03:32 -04005072 file_lock = locks_alloc_lock();
5073 if (!file_lock) {
5074 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5075 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005076 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005077 }
5078 locks_init_lock(file_lock);
5079 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005080 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005081 file_lock->fl_pid = current->tgid;
5082 file_lock->fl_file = filp;
5083 file_lock->fl_flags = FL_POSIX;
5084 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5085 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086
Jeff Layton21179d82012-08-21 08:03:32 -04005087 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5088 locku->lu_length);
5089 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090
Jeff Layton21179d82012-08-21 08:03:32 -04005091 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005092 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005093 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 goto out_nfserr;
5095 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005096 update_stateid(&stp->st_stid.sc_stateid);
5097 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005098fput:
5099 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005101 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields71c3bcd2011-09-27 21:42:29 -04005102 if (!cstate->replay_owner)
5103 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005104 if (file_lock)
5105 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 return status;
5107
5108out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005109 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005110 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111}
5112
5113/*
5114 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005115 * true: locks held by lockowner
5116 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005118static bool
5119check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120{
5121 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005122 int status = false;
5123 struct file *filp = find_any_file(fp);
5124 struct inode *inode;
5125
5126 if (!filp) {
5127 /* Any valid lock stateid should have some sort of access */
5128 WARN_ON_ONCE(1);
5129 return status;
5130 }
5131
5132 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133
Jeff Layton1c8c6012013-06-21 08:58:15 -04005134 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005136 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005137 status = true;
5138 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005141 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005142 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 return status;
5144}
5145
Al Virob37ad282006-10-19 23:28:59 -07005146__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005147nfsd4_release_lockowner(struct svc_rqst *rqstp,
5148 struct nfsd4_compound_state *cstate,
5149 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150{
5151 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Laytonfd449072014-06-30 11:48:41 -04005152 struct nfs4_stateowner *sop = NULL, *tmp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005153 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005154 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 struct xdr_netobj *owner = &rlockowner->rl_owner;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005156 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07005157 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005158 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159
5160 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5161 clid->cl_boot, clid->cl_id);
5162
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 nfs4_lock_state();
5164
Jeff Layton4b24ca72014-06-30 11:48:44 -04005165 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005166 if (status)
5167 goto out;
5168
NeilBrown3e9e3db2005-06-23 22:04:20 -07005169 status = nfserr_locks_held;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05005170
Jeff Laytonfd449072014-06-30 11:48:41 -04005171 /* Find the matching lock stateowner */
5172 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
5173 if (tmp->so_is_open_owner)
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005174 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005175 if (same_owner_str(tmp, owner, clid)) {
5176 sop = tmp;
5177 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07005179 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005180
5181 /* No matching owner found, maybe a replay? Just declare victory... */
5182 if (!sop) {
5183 status = nfs_ok;
5184 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005186
5187 lo = lockowner(sop);
5188 /* see if there are still any locks associated with it */
5189 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
Trond Myklebust11b91642014-07-29 21:34:08 -04005190 if (check_for_locks(stp->st_stid.sc_file, lo))
Jeff Laytonfd449072014-06-30 11:48:41 -04005191 goto out;
5192 }
5193
5194 status = nfs_ok;
5195 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196out:
5197 nfs4_unlock_state();
5198 return status;
5199}
5200
5201static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005202alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203{
NeilBrowna55370a2005-06-23 22:03:52 -07005204 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205}
5206
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005207bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005208nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005209{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005210 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005211
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005212 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005213 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005214}
5215
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216/*
5217 * failure => all reset bets are off, nfserr_no_grace...
5218 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005219struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005220nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221{
5222 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005223 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224
NeilBrowna55370a2005-06-23 22:03:52 -07005225 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5226 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005227 if (crp) {
5228 strhashval = clientstr_hashval(name);
5229 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005230 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005231 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005232 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005233 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005234 }
5235 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236}
5237
Jeff Layton2a4317c2012-03-21 16:42:43 -04005238void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005239nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005240{
5241 list_del(&crp->cr_strhash);
5242 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005243 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005244}
5245
5246void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005247nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248{
5249 struct nfs4_client_reclaim *crp = NULL;
5250 int i;
5251
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005253 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5254 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005256 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 }
5258 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005259 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260}
5261
5262/*
5263 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005264struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005265nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266{
5267 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 struct nfs4_client_reclaim *crp = NULL;
5269
Jeff Layton278c9312012-11-12 15:00:52 -05005270 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271
Jeff Layton278c9312012-11-12 15:00:52 -05005272 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005273 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005274 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 return crp;
5276 }
5277 }
5278 return NULL;
5279}
5280
5281/*
5282* Called from OPEN. Look for clientid in reclaim list.
5283*/
Al Virob37ad282006-10-19 23:28:59 -07005284__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005285nfs4_check_open_reclaim(clientid_t *clid,
5286 struct nfsd4_compound_state *cstate,
5287 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005289 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005290
5291 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005292 status = lookup_clientid(clid, cstate, nn);
5293 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005294 return nfserr_reclaim_bad;
5295
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005296 if (nfsd4_client_record_check(cstate->clp))
5297 return nfserr_reclaim_bad;
5298
5299 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300}
5301
Bryan Schumaker65178db2011-11-01 13:35:21 -04005302#ifdef CONFIG_NFSD_FAULT_INJECTION
5303
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005304u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5305{
J. Bruce Fields221a6872013-04-01 22:23:49 -04005306 if (mark_client_expired(clp))
5307 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005308 expire_client(clp);
5309 return 1;
5310}
5311
Bryan Schumaker184c1842012-11-29 11:40:44 -05005312u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5313{
5314 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005315 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005316 printk(KERN_INFO "NFS Client: %s\n", buf);
5317 return 1;
5318}
5319
5320static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5321 const char *type)
5322{
5323 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005324 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005325 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5326}
5327
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005328static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5329 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005330{
5331 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005332 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005333 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005334 u64 count = 0;
5335
5336 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005337 list_for_each_entry_safe(stp, st_next,
5338 &oop->oo_owner.so_stateids, st_perstateowner) {
5339 list_for_each_entry_safe(lst, lst_next,
5340 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005341 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005342 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005343 if (++count == max)
5344 return count;
5345 }
5346 }
5347 }
5348
5349 return count;
5350}
5351
5352u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5353{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005354 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005355}
5356
Bryan Schumaker184c1842012-11-29 11:40:44 -05005357u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5358{
5359 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5360 nfsd_print_count(clp, count, "locked files");
5361 return count;
5362}
5363
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005364static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5365{
5366 struct nfs4_openowner *oop, *next;
5367 u64 count = 0;
5368
5369 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5370 if (func)
5371 func(oop);
5372 if (++count == max)
5373 break;
5374 }
5375
5376 return count;
5377}
5378
5379u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5380{
5381 return nfsd_foreach_client_open(clp, max, release_openowner);
5382}
5383
Bryan Schumaker184c1842012-11-29 11:40:44 -05005384u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5385{
5386 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5387 nfsd_print_count(clp, count, "open files");
5388 return count;
5389}
5390
Bryan Schumaker269de302012-11-29 11:40:42 -05005391static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5392 struct list_head *victims)
5393{
5394 struct nfs4_delegation *dp, *next;
5395 u64 count = 0;
5396
Benny Halevycdc97502014-05-30 09:09:30 -04005397 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005398 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005399 if (victims) {
5400 /*
5401 * It's not safe to mess with delegations that have a
5402 * non-zero dl_time. They might have already been broken
5403 * and could be processed by the laundromat outside of
5404 * the state_lock. Just leave them be.
5405 */
5406 if (dp->dl_time != 0)
5407 continue;
5408
Jeff Layton42690672014-07-25 07:34:20 -04005409 unhash_delegation_locked(dp);
5410 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005411 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005412 if (++count == max)
5413 break;
5414 }
5415 return count;
5416}
5417
5418u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5419{
5420 struct nfs4_delegation *dp, *next;
5421 LIST_HEAD(victims);
5422 u64 count;
5423
Benny Halevycdc97502014-05-30 09:09:30 -04005424 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005425 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005426 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005427
Jeff Layton2d4a5322014-07-25 07:34:21 -04005428 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5429 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005430 revoke_delegation(dp);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005431 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005432
5433 return count;
5434}
5435
5436u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5437{
Jeff Laytondff13992014-07-08 14:02:49 -04005438 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005439 LIST_HEAD(victims);
5440 u64 count;
5441
Benny Halevycdc97502014-05-30 09:09:30 -04005442 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005443 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005444 while (!list_empty(&victims)) {
5445 dp = list_first_entry(&victims, struct nfs4_delegation,
5446 dl_recall_lru);
5447 list_del_init(&dp->dl_recall_lru);
5448 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005449 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005450 }
Benny Halevycdc97502014-05-30 09:09:30 -04005451 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005452
5453 return count;
5454}
5455
Bryan Schumaker184c1842012-11-29 11:40:44 -05005456u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5457{
5458 u64 count = 0;
5459
Benny Halevycdc97502014-05-30 09:09:30 -04005460 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005461 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005462 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005463
5464 nfsd_print_count(clp, count, "delegations");
5465 return count;
5466}
5467
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005468u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005469{
5470 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005471 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005472 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005473
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005474 if (!nfsd_netns_ready(nn))
5475 return 0;
5476
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005477 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005478 count += func(clp, max - count);
5479 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005480 break;
5481 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005482
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005483 return count;
5484}
5485
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005486struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5487{
5488 struct nfs4_client *clp;
5489 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5490
5491 if (!nfsd_netns_ready(nn))
5492 return NULL;
5493
5494 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5495 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5496 return clp;
5497 }
5498 return NULL;
5499}
5500
Bryan Schumaker65178db2011-11-01 13:35:21 -04005501#endif /* CONFIG_NFSD_FAULT_INJECTION */
5502
Meelap Shahc2f1a552007-07-17 04:04:39 -07005503/*
5504 * Since the lifetime of a delegation isn't limited to that of an open, a
5505 * client may quite reasonably hang on to a delegation as long as it has
5506 * the inode cached. This becomes an obvious problem the first time a
5507 * client's inode cache approaches the size of the server's total memory.
5508 *
5509 * For now we avoid this problem by imposing a hard limit on the number
5510 * of delegations, which varies according to the server's memory size.
5511 */
5512static void
5513set_max_delegations(void)
5514{
5515 /*
5516 * Allow at most 4 delegations per megabyte of RAM. Quick
5517 * estimates suggest that in the worst case (where every delegation
5518 * is for a different inode), a delegation could take about 1.5K,
5519 * giving a worst case usage of about 6% of memory.
5520 */
5521 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5522}
5523
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005524static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005525{
5526 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5527 int i;
5528
5529 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5530 CLIENT_HASH_SIZE, GFP_KERNEL);
5531 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005532 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005533 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5534 CLIENT_HASH_SIZE, GFP_KERNEL);
5535 if (!nn->unconf_id_hashtbl)
5536 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005537 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5538 OWNER_HASH_SIZE, GFP_KERNEL);
5539 if (!nn->ownerstr_hashtbl)
5540 goto err_ownerstr;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005541 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5542 SESSION_HASH_SIZE, GFP_KERNEL);
5543 if (!nn->sessionid_hashtbl)
5544 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005545
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005546 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005547 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005548 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005549 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005550 for (i = 0; i < OWNER_HASH_SIZE; i++)
5551 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005552 for (i = 0; i < SESSION_HASH_SIZE; i++)
5553 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005554 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005555 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005556 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005557 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005558 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005559 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005560
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005561 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005562 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005563
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005564 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005565
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005566err_sessionid:
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03005567 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005568err_ownerstr:
5569 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005570err_unconf_id:
5571 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005572err:
5573 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005574}
5575
5576static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005577nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005578{
5579 int i;
5580 struct nfs4_client *clp = NULL;
5581 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5582
5583 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5584 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5585 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5586 destroy_client(clp);
5587 }
5588 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005589
Kinglong Mee2b905632014-03-26 22:09:30 +08005590 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5591 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5592 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5593 destroy_client(clp);
5594 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005595 }
5596
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005597 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005598 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005599 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005600 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005601 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005602}
5603
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005604int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005605nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005606{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005607 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005608 int ret;
5609
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005610 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005611 if (ret)
5612 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005613 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005614 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005615 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005616 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005617 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005618 nn->nfsd4_grace, net);
5619 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005620 return 0;
5621}
5622
5623/* initialization to perform when the nfsd service is started: */
5624
5625int
5626nfs4_state_start(void)
5627{
5628 int ret;
5629
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005630 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005631 if (ret)
5632 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005633 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005634 if (laundry_wq == NULL) {
5635 ret = -ENOMEM;
5636 goto out_recovery;
5637 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005638 ret = nfsd4_create_callback_queue();
5639 if (ret)
5640 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005641
Meelap Shahc2f1a552007-07-17 04:04:39 -07005642 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005643
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005644 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005645
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005646out_free_laundry:
5647 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005648out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005649 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650}
5651
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005652void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005653nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005657 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005659 cancel_delayed_work_sync(&nn->laundromat_work);
5660 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005661
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005662 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04005664 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005665 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005667 unhash_delegation_locked(dp);
5668 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 }
Benny Halevycdc97502014-05-30 09:09:30 -04005670 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 list_for_each_safe(pos, next, &reaplist) {
5672 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005673 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04005674 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 }
5676
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005677 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005678 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005679 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680}
5681
5682void
5683nfs4_state_shutdown(void)
5684{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005685 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005686 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005688
5689static void
5690get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5691{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005692 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5693 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005694}
5695
5696static void
5697put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5698{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005699 if (cstate->minorversion) {
5700 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5701 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5702 }
5703}
5704
5705void
5706clear_current_stateid(struct nfsd4_compound_state *cstate)
5707{
5708 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005709}
5710
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005711/*
5712 * functions to set current state id
5713 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005714void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005715nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5716{
5717 put_stateid(cstate, &odp->od_stateid);
5718}
5719
5720void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005721nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5722{
5723 put_stateid(cstate, &open->op_stateid);
5724}
5725
5726void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005727nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5728{
5729 put_stateid(cstate, &close->cl_stateid);
5730}
5731
5732void
5733nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5734{
5735 put_stateid(cstate, &lock->lk_resp_stateid);
5736}
5737
5738/*
5739 * functions to consume current state id
5740 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005741
5742void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005743nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5744{
5745 get_stateid(cstate, &odp->od_stateid);
5746}
5747
5748void
5749nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5750{
5751 get_stateid(cstate, &drp->dr_stateid);
5752}
5753
5754void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005755nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5756{
5757 get_stateid(cstate, &fsp->fr_stateid);
5758}
5759
5760void
5761nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5762{
5763 get_stateid(cstate, &setattr->sa_stateid);
5764}
5765
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005766void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005767nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5768{
5769 get_stateid(cstate, &close->cl_stateid);
5770}
5771
5772void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005773nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005774{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005775 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005776}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01005777
5778void
5779nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5780{
5781 get_stateid(cstate, &read->rd_stateid);
5782}
5783
5784void
5785nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5786{
5787 get_stateid(cstate, &write->wr_stateid);
5788}