blob: 3ac6e2fdabe52339e308fce38a2056c3ade580db [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
Dave Hansenaceaf782008-02-15 14:37:31 -080035#include <linux/file.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020036#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070038#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070039#include <linux/swap.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
Chuck Lever7df302f2012-05-29 13:56:37 -040041#include <linux/ratelimit.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050042#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton59766872013-02-04 12:50:00 -050043#include <linux/sunrpc/addr.h>
NeilBrown6282cd52014-06-04 17:39:26 +100044#include <linux/hash.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020045#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040046#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050047#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040048#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040050#include "netns.h"
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define NFSDDBG_FACILITY NFSDDBG_PROC
53
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050054#define all_ones {{~0,~0},~0}
55static const stateid_t one_stateid = {
56 .si_generation = ~0,
57 .si_opaque = all_ones,
58};
59static const stateid_t zero_stateid = {
60 /* all fields zero */
61};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010062static const stateid_t currentstateid = {
63 .si_generation = 1,
64};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050065
Andy Adamsonec6b5d72009-04-03 08:28:28 +030066static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070067
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050068#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
69#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010070#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040073static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040074static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
J. Bruce Fields8b671b82009-02-22 14:51:34 -080076/* Locking: */
77
78/* Currently used for almost all code touching nfsv4 state: */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080079static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
J. Bruce Fields8b671b82009-02-22 14:51:34 -080081/*
82 * Currently used for the del_recall_lru and file hash table. In an
83 * effort to decrease the scope of the client_mutex, this spinlock may
84 * eventually cover more:
85 */
Benny Halevycdc97502014-05-30 09:09:30 -040086static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080087
Christoph Hellwigabf11352014-05-21 07:43:03 -070088static struct kmem_cache *openowner_slab;
89static struct kmem_cache *lockowner_slab;
90static struct kmem_cache *file_slab;
91static struct kmem_cache *stateid_slab;
92static struct kmem_cache *deleg_slab;
NeilBrowne60d4392005-06-23 22:03:01 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094void
95nfs4_lock_state(void)
96{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080097 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400100static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800101
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400102static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800103{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400104 return ses->se_flags & NFS4_SESSION_DEAD;
105}
106
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400107static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
108{
109 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400110 return nfserr_jukebox;
111 ses->se_flags |= NFS4_SESSION_DEAD;
112 return nfs_ok;
113}
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115void
116nfs4_unlock_state(void)
117{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800118 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
J. Bruce Fields221a6872013-04-01 22:23:49 -0400121static bool is_client_expired(struct nfs4_client *clp)
122{
123 return clp->cl_time == 0;
124}
125
126static __be32 mark_client_expired_locked(struct nfs4_client *clp)
127{
128 if (atomic_read(&clp->cl_refcount))
129 return nfserr_jukebox;
130 clp->cl_time = 0;
131 return nfs_ok;
132}
133
134static __be32 mark_client_expired(struct nfs4_client *clp)
135{
136 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
137 __be32 ret;
138
139 spin_lock(&nn->client_lock);
140 ret = mark_client_expired_locked(clp);
141 spin_unlock(&nn->client_lock);
142 return ret;
143}
144
145static __be32 get_client_locked(struct nfs4_client *clp)
146{
147 if (is_client_expired(clp))
148 return nfserr_expired;
149 atomic_inc(&clp->cl_refcount);
150 return nfs_ok;
151}
152
153/* must be called under the client_lock */
154static inline void
155renew_client_locked(struct nfs4_client *clp)
156{
157 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
158
159 if (is_client_expired(clp)) {
160 WARN_ON(1);
161 printk("%s: client (clientid %08x/%08x) already expired\n",
162 __func__,
163 clp->cl_clientid.cl_boot,
164 clp->cl_clientid.cl_id);
165 return;
166 }
167
168 dprintk("renewing client (clientid %08x/%08x)\n",
169 clp->cl_clientid.cl_boot,
170 clp->cl_clientid.cl_id);
171 list_move_tail(&clp->cl_lru, &nn->client_lru);
172 clp->cl_time = get_seconds();
173}
174
175static inline void
176renew_client(struct nfs4_client *clp)
177{
178 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
179
180 spin_lock(&nn->client_lock);
181 renew_client_locked(clp);
182 spin_unlock(&nn->client_lock);
183}
184
Fengguang Wuba138432013-04-16 22:14:15 -0400185static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400186{
187 if (!atomic_dec_and_test(&clp->cl_refcount))
188 return;
189 if (!is_client_expired(clp))
190 renew_client_locked(clp);
191}
192
Jeff Layton4b24ca72014-06-30 11:48:44 -0400193static void put_client_renew(struct nfs4_client *clp)
194{
195 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
196
Jeff Laytond6c249b2014-07-08 14:02:50 -0400197 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
198 return;
199 if (!is_client_expired(clp))
200 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400201 spin_unlock(&nn->client_lock);
202}
203
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400204static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
205{
206 __be32 status;
207
208 if (is_session_dead(ses))
209 return nfserr_badsession;
210 status = get_client_locked(ses->se_client);
211 if (status)
212 return status;
213 atomic_inc(&ses->se_ref);
214 return nfs_ok;
215}
216
217static void nfsd4_put_session_locked(struct nfsd4_session *ses)
218{
219 struct nfs4_client *clp = ses->se_client;
220
221 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
222 free_session(ses);
223 put_client_renew_locked(clp);
224}
225
226static void nfsd4_put_session(struct nfsd4_session *ses)
227{
228 struct nfs4_client *clp = ses->se_client;
229 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
230
231 spin_lock(&nn->client_lock);
232 nfsd4_put_session_locked(ses);
233 spin_unlock(&nn->client_lock);
234}
235
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237static inline u32
238opaque_hashval(const void *ptr, int nbytes)
239{
240 unsigned char *cptr = (unsigned char *) ptr;
241
242 u32 x = 0;
243 while (nbytes--) {
244 x *= 37;
245 x += *cptr++;
246 }
247 return x;
248}
249
J. Bruce Fields32513b42011-10-13 16:00:16 -0400250static void nfsd4_free_file(struct nfs4_file *f)
251{
252 kmem_cache_free(file_slab, f);
253}
254
NeilBrown13cd2182005-06-23 22:03:10 -0700255static inline void
256put_nfs4_file(struct nfs4_file *fi)
257{
Jeff Layton02e12152014-07-16 10:31:57 -0400258 might_lock(&state_lock);
259
Benny Halevycdc97502014-05-30 09:09:30 -0400260 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton89876f82013-04-02 09:01:59 -0400261 hlist_del(&fi->fi_hash);
Benny Halevycdc97502014-05-30 09:09:30 -0400262 spin_unlock(&state_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400263 nfsd4_free_file(fi);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800264 }
NeilBrown13cd2182005-06-23 22:03:10 -0700265}
266
267static inline void
268get_nfs4_file(struct nfs4_file *fi)
269{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800270 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700271}
272
Trond Myklebustde186432014-07-10 14:07:26 -0400273static struct file *
274__nfs4_get_fd(struct nfs4_file *f, int oflag)
275{
276 if (f->fi_fds[oflag])
277 return get_file(f->fi_fds[oflag]);
278 return NULL;
279}
280
281static struct file *
282find_writeable_file_locked(struct nfs4_file *f)
283{
284 struct file *ret;
285
286 lockdep_assert_held(&f->fi_lock);
287
288 ret = __nfs4_get_fd(f, O_WRONLY);
289 if (!ret)
290 ret = __nfs4_get_fd(f, O_RDWR);
291 return ret;
292}
293
294static struct file *
295find_writeable_file(struct nfs4_file *f)
296{
297 struct file *ret;
298
299 spin_lock(&f->fi_lock);
300 ret = find_writeable_file_locked(f);
301 spin_unlock(&f->fi_lock);
302
303 return ret;
304}
305
306static struct file *find_readable_file_locked(struct nfs4_file *f)
307{
308 struct file *ret;
309
310 lockdep_assert_held(&f->fi_lock);
311
312 ret = __nfs4_get_fd(f, O_RDONLY);
313 if (!ret)
314 ret = __nfs4_get_fd(f, O_RDWR);
315 return ret;
316}
317
318static struct file *
319find_readable_file(struct nfs4_file *f)
320{
321 struct file *ret;
322
323 spin_lock(&f->fi_lock);
324 ret = find_readable_file_locked(f);
325 spin_unlock(&f->fi_lock);
326
327 return ret;
328}
329
330static struct file *
331find_any_file(struct nfs4_file *f)
332{
333 struct file *ret;
334
335 spin_lock(&f->fi_lock);
336 ret = __nfs4_get_fd(f, O_RDWR);
337 if (!ret) {
338 ret = __nfs4_get_fd(f, O_WRONLY);
339 if (!ret)
340 ret = __nfs4_get_fd(f, O_RDONLY);
341 }
342 spin_unlock(&f->fi_lock);
343 return ret;
344}
345
Trond Myklebust02a35082014-07-25 07:34:22 -0400346static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800347unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700348
349/*
350 * Open owner state (share locks)
351 */
352
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500353/* hash tables for lock and open owners */
354#define OWNER_HASH_BITS 8
355#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
356#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700357
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500358static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400359{
360 unsigned int ret;
361
362 ret = opaque_hashval(ownername->data, ownername->len);
363 ret += clientid;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500364 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400365}
NeilBrownef0f3392006-04-10 22:55:41 -0700366
NeilBrownef0f3392006-04-10 22:55:41 -0700367/* hash table for nfs4_file */
368#define FILE_HASH_BITS 8
369#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800370
Trond Myklebustca943212014-07-23 16:17:39 -0400371static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400372{
Trond Myklebustca943212014-07-23 16:17:39 -0400373 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
374}
375
376static unsigned int file_hashval(struct knfsd_fh *fh)
377{
378 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
379}
380
381static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
382{
383 return fh1->fh_size == fh2->fh_size &&
384 !memcmp(fh1->fh_base.fh_pad,
385 fh2->fh_base.fh_pad,
386 fh1->fh_size);
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400387}
388
Jeff Layton89876f82013-04-02 09:01:59 -0400389static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700390
Jeff Layton12659652014-07-10 14:07:28 -0400391static void
392__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400393{
Jeff Layton7214e862014-07-10 14:07:33 -0400394 lockdep_assert_held(&fp->fi_lock);
395
Jeff Layton12659652014-07-10 14:07:28 -0400396 if (access & NFS4_SHARE_ACCESS_WRITE)
397 atomic_inc(&fp->fi_access[O_WRONLY]);
398 if (access & NFS4_SHARE_ACCESS_READ)
399 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400400}
401
Jeff Layton12659652014-07-10 14:07:28 -0400402static __be32
403nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400404{
Jeff Layton7214e862014-07-10 14:07:33 -0400405 lockdep_assert_held(&fp->fi_lock);
406
Jeff Layton12659652014-07-10 14:07:28 -0400407 /* Does this access mode make sense? */
408 if (access & ~NFS4_SHARE_ACCESS_BOTH)
409 return nfserr_inval;
410
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400411 /* Does it conflict with a deny mode already set? */
412 if ((access & fp->fi_share_deny) != 0)
413 return nfserr_share_denied;
414
Jeff Layton12659652014-07-10 14:07:28 -0400415 __nfs4_file_get_access(fp, access);
416 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400417}
418
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400419static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
420{
421 /* Common case is that there is no deny mode. */
422 if (deny) {
423 /* Does this deny mode make sense? */
424 if (deny & ~NFS4_SHARE_DENY_BOTH)
425 return nfserr_inval;
426
427 if ((deny & NFS4_SHARE_DENY_READ) &&
428 atomic_read(&fp->fi_access[O_RDONLY]))
429 return nfserr_share_denied;
430
431 if ((deny & NFS4_SHARE_DENY_WRITE) &&
432 atomic_read(&fp->fi_access[O_WRONLY]))
433 return nfserr_share_denied;
434 }
435 return nfs_ok;
436}
437
J. Bruce Fields998db522010-08-07 09:21:41 -0400438static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400439{
Trond Myklebustde186432014-07-10 14:07:26 -0400440 might_lock(&fp->fi_lock);
441
442 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
443 struct file *f1 = NULL;
444 struct file *f2 = NULL;
445
Jeff Layton6d338b52014-07-10 14:07:29 -0400446 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400447 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400448 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400449 spin_unlock(&fp->fi_lock);
450 if (f1)
451 fput(f1);
452 if (f2)
453 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400454 }
455}
456
Jeff Layton12659652014-07-10 14:07:28 -0400457static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400458{
Jeff Layton12659652014-07-10 14:07:28 -0400459 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
460
461 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400462 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400463 if (access & NFS4_SHARE_ACCESS_READ)
464 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400465}
466
Trond Myklebust60116952014-07-29 21:34:06 -0400467static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
468 struct kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400469{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500470 struct nfs4_stid *stid;
471 int new_id;
472
Trond Myklebustf8338832014-07-25 07:34:19 -0400473 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500474 if (!stid)
475 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400476
Jeff Layton4770d722014-07-29 21:34:10 -0400477 idr_preload(GFP_KERNEL);
478 spin_lock(&cl->cl_lock);
479 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
480 spin_unlock(&cl->cl_lock);
481 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700482 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500483 goto out_free;
484 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500485 stid->sc_stateid.si_opaque.so_id = new_id;
486 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
487 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400488 atomic_set(&stid->sc_count, 1);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500489
J. Bruce Fields996e0932011-10-17 11:14:48 -0400490 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500491 * It shouldn't be a problem to reuse an opaque stateid value.
492 * I don't think it is for 4.1. But with 4.0 I worry that, for
493 * example, a stray write retransmission could be accepted by
494 * the server when it should have been rejected. Therefore,
495 * adopt a trick from the sctp code to attempt to maximize the
496 * amount of time until an id is reused, by ensuring they always
497 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400498 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500499 return stid;
500out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800501 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500502 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400503}
504
Jeff Laytonb49e0842014-07-29 21:34:11 -0400505static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400506{
Trond Myklebust60116952014-07-29 21:34:06 -0400507 struct nfs4_stid *stid;
508 struct nfs4_ol_stateid *stp;
509
510 stid = nfs4_alloc_stid(clp, stateid_slab);
511 if (!stid)
512 return NULL;
513
514 stp = openlockstateid(stid);
515 stp->st_stid.sc_free = nfs4_free_ol_stateid;
516 return stp;
517}
518
519static void nfs4_free_deleg(struct nfs4_stid *stid)
520{
Trond Myklebust60116952014-07-29 21:34:06 -0400521 kmem_cache_free(deleg_slab, stid);
522 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400523}
524
NeilBrown6282cd52014-06-04 17:39:26 +1000525/*
526 * When we recall a delegation, we should be careful not to hand it
527 * out again straight away.
528 * To ensure this we keep a pair of bloom filters ('new' and 'old')
529 * in which the filehandles of recalled delegations are "stored".
530 * If a filehandle appear in either filter, a delegation is blocked.
531 * When a delegation is recalled, the filehandle is stored in the "new"
532 * filter.
533 * Every 30 seconds we swap the filters and clear the "new" one,
534 * unless both are empty of course.
535 *
536 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
537 * low 3 bytes as hash-table indices.
538 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400539 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000540 * is used to manage concurrent access. Testing does not need the lock
541 * except when swapping the two filters.
542 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400543static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000544static struct bloom_pair {
545 int entries, old_entries;
546 time_t swap_time;
547 int new; /* index into 'set' */
548 DECLARE_BITMAP(set[2], 256);
549} blocked_delegations;
550
551static int delegation_blocked(struct knfsd_fh *fh)
552{
553 u32 hash;
554 struct bloom_pair *bd = &blocked_delegations;
555
556 if (bd->entries == 0)
557 return 0;
558 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400559 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000560 if (seconds_since_boot() - bd->swap_time > 30) {
561 bd->entries -= bd->old_entries;
562 bd->old_entries = bd->entries;
563 memset(bd->set[bd->new], 0,
564 sizeof(bd->set[0]));
565 bd->new = 1-bd->new;
566 bd->swap_time = seconds_since_boot();
567 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400568 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000569 }
570 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
571 if (test_bit(hash&255, bd->set[0]) &&
572 test_bit((hash>>8)&255, bd->set[0]) &&
573 test_bit((hash>>16)&255, bd->set[0]))
574 return 1;
575
576 if (test_bit(hash&255, bd->set[1]) &&
577 test_bit((hash>>8)&255, bd->set[1]) &&
578 test_bit((hash>>16)&255, bd->set[1]))
579 return 1;
580
581 return 0;
582}
583
584static void block_delegations(struct knfsd_fh *fh)
585{
586 u32 hash;
587 struct bloom_pair *bd = &blocked_delegations;
588
589 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
590
Jeff Laytonf54fe962014-07-25 07:34:26 -0400591 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000592 __set_bit(hash&255, bd->set[bd->new]);
593 __set_bit((hash>>8)&255, bd->set[bd->new]);
594 __set_bit((hash>>16)&255, bd->set[bd->new]);
595 if (bd->entries == 0)
596 bd->swap_time = seconds_since_boot();
597 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400598 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601static struct nfs4_delegation *
Jeff Laytonf9416e22014-07-25 07:34:23 -0400602alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400605 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400608 n = atomic_long_inc_return(&num_delegations);
609 if (n < 0 || n > max_delegations)
610 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000611 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400612 goto out_dec;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400613 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700614 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400615 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400616
617 dp->dl_stid.sc_free = nfs4_free_deleg;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400618 /*
619 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400620 * meaning of seqid 0 isn't meaningful, really, but let's avoid
621 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400622 */
623 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700624 INIT_LIST_HEAD(&dp->dl_perfile);
625 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 INIT_LIST_HEAD(&dp->dl_recall_lru);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400627 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Jeff Layton02e12152014-07-16 10:31:57 -0400628 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400630out_dec:
631 atomic_long_dec(&num_delegations);
632 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
635void
Trond Myklebust60116952014-07-29 21:34:06 -0400636nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Trond Myklebust11b91642014-07-29 21:34:08 -0400638 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400639 struct nfs4_client *clp = s->sc_client;
640
Jeff Layton4770d722014-07-29 21:34:10 -0400641 might_lock(&clp->cl_lock);
642
643 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock))
Trond Myklebust60116952014-07-29 21:34:06 -0400644 return;
645 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400646 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400647 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400648 if (fp)
649 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500652static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Jeff Layton417c6622014-07-21 09:34:57 -0400654 lockdep_assert_held(&state_lock);
655
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -0500656 if (!fp->fi_lease)
657 return;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500658 if (atomic_dec_and_test(&fp->fi_delegees)) {
659 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
660 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400661 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500662 fp->fi_deleg_file = NULL;
663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400666static void unhash_stid(struct nfs4_stid *s)
667{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500668 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400669}
670
Benny Halevy931ee562014-05-30 09:09:27 -0400671static void
672hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
673{
Benny Halevycdc97502014-05-30 09:09:30 -0400674 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400675 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400676
Benny Halevy3fb87d12014-05-30 09:09:31 -0400677 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400678 list_add(&dp->dl_perfile, &fp->fi_delegations);
679 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
680}
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682static void
Jeff Layton42690672014-07-25 07:34:20 -0400683unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Trond Myklebust11b91642014-07-29 21:34:08 -0400685 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400686
Jeff Layton42690672014-07-25 07:34:20 -0400687 lockdep_assert_held(&state_lock);
688
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400689 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400690 /* Ensure that deleg break won't try to requeue it */
691 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400692 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400693 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400695 list_del_init(&dp->dl_perfile);
696 spin_unlock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400697 if (fp)
Trond Myklebustf8338832014-07-25 07:34:19 -0400698 nfs4_put_deleg_lease(fp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400699}
700
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400701static void destroy_delegation(struct nfs4_delegation *dp)
702{
Jeff Layton42690672014-07-25 07:34:20 -0400703 spin_lock(&state_lock);
704 unhash_delegation_locked(dp);
705 spin_unlock(&state_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400706 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400707}
708
709static void revoke_delegation(struct nfs4_delegation *dp)
710{
711 struct nfs4_client *clp = dp->dl_stid.sc_client;
712
Jeff Layton2d4a5322014-07-25 07:34:21 -0400713 WARN_ON(!list_empty(&dp->dl_recall_lru));
714
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400715 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400716 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400717 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400718 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400719 spin_lock(&clp->cl_lock);
720 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
721 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400722 }
723}
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725/*
726 * SETCLIENTID state
727 */
728
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400729static unsigned int clientid_hashval(u32 id)
730{
731 return id & CLIENT_HASH_MASK;
732}
733
734static unsigned int clientstr_hashval(const char *name)
735{
736 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
737}
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400740 * We store the NONE, READ, WRITE, and BOTH bits separately in the
741 * st_{access,deny}_bmap field of the stateid, in order to track not
742 * only what share bits are currently in force, but also what
743 * combinations of share bits previous opens have used. This allows us
744 * to enforce the recommendation of rfc 3530 14.2.19 that the server
745 * return an error if the client attempt to downgrade to a combination
746 * of share bits not explicable by closing some of its previous opens.
747 *
748 * XXX: This enforcement is actually incomplete, since we don't keep
749 * track of access/deny bit combinations; so, e.g., we allow:
750 *
751 * OPEN allow read, deny write
752 * OPEN allow both, deny none
753 * DOWNGRADE allow read, deny none
754 *
755 * which we should reject.
756 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400757static unsigned int
758bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400759 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400760 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400761
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400762 for (i = 1; i < 4; i++) {
763 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400764 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400765 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400766 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400767}
768
Jeff Layton82c5ff12012-05-11 09:45:13 -0400769/* set share access for a given stateid */
770static inline void
771set_access(u32 access, struct nfs4_ol_stateid *stp)
772{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400773 unsigned char mask = 1 << access;
774
775 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
776 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400777}
778
779/* clear share access for a given stateid */
780static inline void
781clear_access(u32 access, struct nfs4_ol_stateid *stp)
782{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400783 unsigned char mask = 1 << access;
784
785 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
786 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400787}
788
789/* test whether a given stateid has access */
790static inline bool
791test_access(u32 access, struct nfs4_ol_stateid *stp)
792{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400793 unsigned char mask = 1 << access;
794
795 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400796}
797
Jeff Laytonce0fc432012-05-11 09:45:14 -0400798/* set share deny for a given stateid */
799static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400800set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400801{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400802 unsigned char mask = 1 << deny;
803
804 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
805 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400806}
807
808/* clear share deny for a given stateid */
809static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400810clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400811{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400812 unsigned char mask = 1 << deny;
813
814 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
815 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400816}
817
818/* test whether a given stateid is denying specific access */
819static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400820test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400821{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400822 unsigned char mask = 1 << deny;
823
824 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400825}
826
827static int nfs4_access_to_omode(u32 access)
828{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400829 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400830 case NFS4_SHARE_ACCESS_READ:
831 return O_RDONLY;
832 case NFS4_SHARE_ACCESS_WRITE:
833 return O_WRONLY;
834 case NFS4_SHARE_ACCESS_BOTH:
835 return O_RDWR;
836 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500837 WARN_ON_ONCE(1);
838 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400839}
840
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400841/*
842 * A stateid that had a deny mode associated with it is being released
843 * or downgraded. Recalculate the deny mode on the file.
844 */
845static void
846recalculate_deny_mode(struct nfs4_file *fp)
847{
848 struct nfs4_ol_stateid *stp;
849
850 spin_lock(&fp->fi_lock);
851 fp->fi_share_deny = 0;
852 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
853 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
854 spin_unlock(&fp->fi_lock);
855}
856
857static void
858reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
859{
860 int i;
861 bool change = false;
862
863 for (i = 1; i < 4; i++) {
864 if ((i & deny) != i) {
865 change = true;
866 clear_deny(i, stp);
867 }
868 }
869
870 /* Recalculate per-file deny mode if there was a change */
871 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -0400872 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400873}
874
Jeff Layton82c5ff12012-05-11 09:45:13 -0400875/* release all access and file references for a given stateid */
876static void
877release_all_access(struct nfs4_ol_stateid *stp)
878{
879 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -0400880 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400881
882 if (fp && stp->st_deny_bmap != 0)
883 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400884
885 for (i = 1; i < 4; i++) {
886 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -0400887 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400888 clear_access(i, stp);
889 }
890}
891
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400892static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500893{
Trond Myklebust11b91642014-07-29 21:34:08 -0400894 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -0400895
Jeff Layton1c755dc2014-07-29 21:34:12 -0400896 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
897
Trond Myklebust1d31a252014-07-10 14:07:25 -0400898 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500899 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400900 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500901 list_del(&stp->st_perstateowner);
902}
903
Trond Myklebust60116952014-07-29 21:34:06 -0400904static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500905{
Trond Myklebust60116952014-07-29 21:34:06 -0400906 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400907
Trond Myklebust60116952014-07-29 21:34:06 -0400908 release_all_access(stp);
Trond Myklebust60116952014-07-29 21:34:06 -0400909 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500910}
911
Jeff Laytonb49e0842014-07-29 21:34:11 -0400912static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500913{
Jeff Laytonb49e0842014-07-29 21:34:11 -0400914 struct nfs4_ol_stateid *stp = openlockstateid(stid);
915 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500916 struct file *file;
917
Jeff Laytonb49e0842014-07-29 21:34:11 -0400918 file = find_any_file(stp->st_stid.sc_file);
919 if (file)
920 filp_close(file, (fl_owner_t)lo);
921 nfs4_free_ol_stateid(stid);
922}
923
924static void __release_lock_stateid(struct nfs4_ol_stateid *stp)
925{
Jeff Layton1c755dc2014-07-29 21:34:12 -0400926 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
927
928 spin_lock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400929 list_del(&stp->st_locks);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500930 unhash_generic_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400931 unhash_stid(&stp->st_stid);
Jeff Layton1c755dc2014-07-29 21:34:12 -0400932 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400933 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500934}
935
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400936static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500937{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400938 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500939
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400940 list_del(&lo->lo_owner.so_strhash);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400941 while (!list_empty(&lo->lo_owner.so_stateids)) {
942 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400943 struct nfs4_ol_stateid, st_perstateowner);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400944 __release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500945 }
946}
947
Trond Myklebust50cc6232014-04-18 14:44:03 -0400948static void nfs4_free_lockowner(struct nfs4_lockowner *lo)
949{
950 kfree(lo->lo_owner.so_owner.data);
951 kmem_cache_free(lockowner_slab, lo);
952}
953
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400954static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500955{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400956 unhash_lockowner(lo);
957 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500958}
959
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400960static void release_lockowner_if_empty(struct nfs4_lockowner *lo)
961{
962 if (list_empty(&lo->lo_owner.so_stateids))
963 release_lockowner(lo);
964}
965
966static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500967{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400968 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500969
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400970 lo = lockowner(stp->st_stateowner);
971 __release_lock_stateid(stp);
972 release_lockowner_if_empty(lo);
973}
974
975static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
Jeff Layton1c755dc2014-07-29 21:34:12 -0400976 __releases(&open_stp->st_stateowner->so_client->cl_lock)
977 __acquires(&open_stp->st_stateowner->so_client->cl_lock)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400978{
979 struct nfs4_ol_stateid *stp;
980
981 while (!list_empty(&open_stp->st_locks)) {
982 stp = list_entry(open_stp->st_locks.next,
983 struct nfs4_ol_stateid, st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -0400984 spin_unlock(&open_stp->st_stateowner->so_client->cl_lock);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400985 release_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -0400986 spin_lock(&open_stp->st_stateowner->so_client->cl_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500987 }
988}
989
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400990static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500991{
Jeff Layton1c755dc2014-07-29 21:34:12 -0400992 spin_lock(&stp->st_stateowner->so_client->cl_lock);
J. Bruce Fields22839632009-01-11 14:27:17 -0500993 unhash_generic_stateid(stp);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400994 release_open_stateid_locks(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -0400995 spin_unlock(&stp->st_stateowner->so_client->cl_lock);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400996}
997
998static void release_open_stateid(struct nfs4_ol_stateid *stp)
999{
1000 unhash_open_stateid(stp);
Trond Myklebust60116952014-07-29 21:34:06 -04001001 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields22839632009-01-11 14:27:17 -05001002}
1003
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001004static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001005{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001006 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001007
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001008 list_del(&oo->oo_owner.so_strhash);
1009 list_del(&oo->oo_perclient);
1010 while (!list_empty(&oo->oo_owner.so_stateids)) {
1011 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001012 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05001013 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001014 }
1015}
1016
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001017static void release_last_closed_stateid(struct nfs4_openowner *oo)
1018{
1019 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
1020
1021 if (s) {
Trond Myklebust60116952014-07-29 21:34:06 -04001022 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001023 oo->oo_last_closed_stid = NULL;
1024 }
1025}
1026
Trond Myklebust50cc6232014-04-18 14:44:03 -04001027static void nfs4_free_openowner(struct nfs4_openowner *oo)
1028{
1029 kfree(oo->oo_owner.so_owner.data);
1030 kmem_cache_free(openowner_slab, oo);
1031}
1032
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001033static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001034{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001035 unhash_openowner(oo);
1036 list_del(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001037 release_last_closed_stateid(oo);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001038 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001039}
1040
Marc Eshel5282fd72009-04-03 08:27:52 +03001041static inline int
1042hash_sessionid(struct nfs4_sessionid *sessionid)
1043{
1044 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1045
1046 return sid->sequence % SESSION_HASH_SIZE;
1047}
1048
Trond Myklebust8f199b82012-03-20 15:11:17 -04001049#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001050static inline void
1051dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1052{
1053 u32 *ptr = (u32 *)(&sessionid->data[0]);
1054 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1055}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001056#else
1057static inline void
1058dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1059{
1060}
1061#endif
1062
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001063/*
1064 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1065 * won't be used for replay.
1066 */
1067void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1068{
1069 struct nfs4_stateowner *so = cstate->replay_owner;
1070
1071 if (nfserr == nfserr_replay_me)
1072 return;
1073
1074 if (!seqid_mutating_err(ntohl(nfserr))) {
1075 cstate->replay_owner = NULL;
1076 return;
1077 }
1078 if (!so)
1079 return;
1080 if (so->so_is_open_owner)
1081 release_last_closed_stateid(openowner(so));
1082 so->so_seqid++;
1083 return;
1084}
Marc Eshel5282fd72009-04-03 08:27:52 +03001085
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001086static void
1087gen_sessionid(struct nfsd4_session *ses)
1088{
1089 struct nfs4_client *clp = ses->se_client;
1090 struct nfsd4_sessionid *sid;
1091
1092 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1093 sid->clientid = clp->cl_clientid;
1094 sid->sequence = current_sessionid++;
1095 sid->reserved = 0;
1096}
1097
1098/*
Andy Adamsona6496372009-08-28 08:45:01 -04001099 * The protocol defines ca_maxresponssize_cached to include the size of
1100 * the rpc header, but all we need to cache is the data starting after
1101 * the end of the initial SEQUENCE operation--the rest we regenerate
1102 * each time. Therefore we can advertise a ca_maxresponssize_cached
1103 * value that is the number of bytes in our cache plus a few additional
1104 * bytes. In order to stay on the safe side, and not promise more than
1105 * we can cache, those additional bytes must be the minimum possible: 24
1106 * bytes of rpc header (xid through accept state, with AUTH_NULL
1107 * verifier), 12 for the compound header (with zero-length tag), and 44
1108 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001109 */
Andy Adamsona6496372009-08-28 08:45:01 -04001110#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1111
Andy Adamson557ce262009-08-28 08:45:04 -04001112static void
1113free_session_slots(struct nfsd4_session *ses)
1114{
1115 int i;
1116
1117 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1118 kfree(ses->se_slots[i]);
1119}
1120
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001121/*
1122 * We don't actually need to cache the rpc and session headers, so we
1123 * can allocate a little less for each slot:
1124 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001125static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001126{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001127 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001128
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001129 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1130 size = 0;
1131 else
1132 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1133 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001134}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001135
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001136/*
1137 * XXX: If we run out of reserved DRC memory we could (up to a point)
1138 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001139 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001140 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001141static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001142{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001143 u32 slotsize = slot_bytes(ca);
1144 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001145 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001146
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001147 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001148 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1149 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001150 num = min_t(int, num, avail / slotsize);
1151 nfsd_drc_mem_used += num * slotsize;
1152 spin_unlock(&nfsd_drc_lock);
1153
1154 return num;
1155}
1156
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001157static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001158{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001159 int slotsize = slot_bytes(ca);
1160
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001161 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001162 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001163 spin_unlock(&nfsd_drc_lock);
1164}
1165
Kinglong Mee60810e52014-01-01 00:35:47 +08001166static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1167 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001168{
Kinglong Mee60810e52014-01-01 00:35:47 +08001169 int numslots = fattrs->maxreqs;
1170 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001171 struct nfsd4_session *new;
1172 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001173
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001174 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001175 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1176 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001177
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001178 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001179 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001180 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001181 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001182 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001183 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001184 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001185 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001186 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001187
1188 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1189 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1190
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001191 return new;
1192out_free:
1193 while (i--)
1194 kfree(new->se_slots[i]);
1195 kfree(new);
1196 return NULL;
1197}
1198
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001199static void free_conn(struct nfsd4_conn *c)
1200{
1201 svc_xprt_put(c->cn_xprt);
1202 kfree(c);
1203}
1204
1205static void nfsd4_conn_lost(struct svc_xpt_user *u)
1206{
1207 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1208 struct nfs4_client *clp = c->cn_session->se_client;
1209
1210 spin_lock(&clp->cl_lock);
1211 if (!list_empty(&c->cn_persession)) {
1212 list_del(&c->cn_persession);
1213 free_conn(c);
1214 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001215 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001216 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001217}
1218
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001219static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001220{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001221 struct nfsd4_conn *conn;
1222
1223 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1224 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001225 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001226 svc_xprt_get(rqstp->rq_xprt);
1227 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001228 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001229 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1230 return conn;
1231}
1232
J. Bruce Fields328ead22010-09-29 16:11:06 -04001233static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1234{
1235 conn->cn_session = ses;
1236 list_add(&conn->cn_persession, &ses->se_conns);
1237}
1238
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001239static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1240{
1241 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001242
1243 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001244 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001245 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001246}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001247
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001248static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001249{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001250 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001251 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001252}
1253
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001254static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001255{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001256 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001257
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001258 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001259 ret = nfsd4_register_conn(conn);
1260 if (ret)
1261 /* oops; xprt is already down: */
1262 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001263 /* We may have gained or lost a callback channel: */
1264 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001265}
1266
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001267static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001268{
1269 u32 dir = NFS4_CDFC4_FORE;
1270
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001271 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001272 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001273 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001274}
1275
1276/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001277static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001278{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001279 struct nfs4_client *clp = s->se_client;
1280 struct nfsd4_conn *c;
1281
1282 spin_lock(&clp->cl_lock);
1283 while (!list_empty(&s->se_conns)) {
1284 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1285 list_del_init(&c->cn_persession);
1286 spin_unlock(&clp->cl_lock);
1287
1288 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1289 free_conn(c);
1290
1291 spin_lock(&clp->cl_lock);
1292 }
1293 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001294}
1295
J. Bruce Fields1377b692012-09-11 21:42:40 -04001296static void __free_session(struct nfsd4_session *ses)
1297{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001298 free_session_slots(ses);
1299 kfree(ses);
1300}
1301
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001302static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001303{
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001304 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1305
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001306 lockdep_assert_held(&nn->client_lock);
1307 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001308 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001309 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001310}
Andy Adamson557ce262009-08-28 08:45:04 -04001311
Fengguang Wu135ae822012-11-10 07:20:25 -05001312static 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 -04001313{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001314 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001315 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001316
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001317 new->se_client = clp;
1318 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001319
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001320 INIT_LIST_HEAD(&new->se_conns);
1321
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001322 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001323 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001324 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001325 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001326 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001327 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001328 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001329 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001330 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001331 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001332 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001333 spin_unlock(&nn->client_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001334
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001335 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001336 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001337 /*
1338 * This is a little silly; with sessions there's no real
1339 * use for the callback address. Use the peer address
1340 * as a reasonable default for now, but consider fixing
1341 * the rpc client not to require an address in the
1342 * future:
1343 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001344 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1345 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001346 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001347}
1348
Benny Halevy9089f1b2010-05-12 00:12:26 +03001349/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001350static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001351__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001352{
1353 struct nfsd4_session *elem;
1354 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001355 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001356
1357 dump_sessionid(__func__, sessionid);
1358 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001359 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001360 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001361 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1362 NFS4_MAX_SESSIONID_LEN)) {
1363 return elem;
1364 }
1365 }
1366
1367 dprintk("%s: session not found\n", __func__);
1368 return NULL;
1369}
1370
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001371static struct nfsd4_session *
1372find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1373 __be32 *ret)
1374{
1375 struct nfsd4_session *session;
1376 __be32 status = nfserr_badsession;
1377
1378 session = __find_in_sessionid_hashtbl(sessionid, net);
1379 if (!session)
1380 goto out;
1381 status = nfsd4_get_session_locked(session);
1382 if (status)
1383 session = NULL;
1384out:
1385 *ret = status;
1386 return session;
1387}
1388
Benny Halevy9089f1b2010-05-12 00:12:26 +03001389/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001390static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001391unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001392{
1393 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001394 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001395 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001396 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001397}
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1400static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001401STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001403 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001405 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001406 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return 1;
1408}
1409
1410/*
1411 * XXX Should we use a slab cache ?
1412 * This type of memory management is somewhat inefficient, but we use it
1413 * anyway since SETCLIENTID is not a common operation.
1414 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001415static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
1417 struct nfs4_client *clp;
1418
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001419 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1420 if (clp == NULL)
1421 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001422 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001423 if (clp->cl_name.data == NULL) {
1424 kfree(clp);
1425 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001427 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001428 INIT_LIST_HEAD(&clp->cl_sessions);
1429 idr_init(&clp->cl_stateids);
1430 atomic_set(&clp->cl_refcount, 0);
1431 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1432 INIT_LIST_HEAD(&clp->cl_idhash);
1433 INIT_LIST_HEAD(&clp->cl_openowners);
1434 INIT_LIST_HEAD(&clp->cl_delegations);
1435 INIT_LIST_HEAD(&clp->cl_lru);
1436 INIT_LIST_HEAD(&clp->cl_callbacks);
1437 INIT_LIST_HEAD(&clp->cl_revoked);
1438 spin_lock_init(&clp->cl_lock);
1439 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return clp;
1441}
1442
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001443static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444free_client(struct nfs4_client *clp)
1445{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001446 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001447
1448 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001449 while (!list_empty(&clp->cl_sessions)) {
1450 struct nfsd4_session *ses;
1451 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1452 se_perclnt);
1453 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001454 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1455 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001456 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001457 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001458 free_svc_cred(&clp->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001460 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 kfree(clp);
1462}
1463
Benny Halevy84d38ac2010-05-12 00:13:16 +03001464/* must be called under the client_lock */
1465static inline void
1466unhash_client_locked(struct nfs4_client *clp)
1467{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001468 struct nfsd4_session *ses;
1469
Benny Halevy84d38ac2010-05-12 00:13:16 +03001470 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001471 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001472 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1473 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001474 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001475}
1476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001478destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001480 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001483 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001486 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001487 while (!list_empty(&clp->cl_delegations)) {
1488 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001489 unhash_delegation_locked(dp);
1490 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 }
Benny Halevycdc97502014-05-30 09:09:30 -04001492 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 while (!list_empty(&reaplist)) {
1494 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001495 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001496 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001498 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001499 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001500 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001501 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001502 }
NeilBrownea1da632005-06-23 22:04:17 -07001503 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001504 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1505 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001507 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001508 if (clp->cl_cb_conn.cb_xprt)
1509 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001510 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001511 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001512 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001513 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001514 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001515 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001516 unhash_client_locked(clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001517 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1518 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001519 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
1521
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001522static void expire_client(struct nfs4_client *clp)
1523{
1524 nfsd4_client_record_remove(clp);
1525 destroy_client(clp);
1526}
1527
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001528static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1529{
1530 memcpy(target->cl_verifier.data, source->data,
1531 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001534static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1535{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1537 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1538}
1539
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001540static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001541{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001542 if (source->cr_principal) {
1543 target->cr_principal =
1544 kstrdup(source->cr_principal, GFP_KERNEL);
1545 if (target->cr_principal == NULL)
1546 return -ENOMEM;
1547 } else
1548 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001549 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 target->cr_uid = source->cr_uid;
1551 target->cr_gid = source->cr_gid;
1552 target->cr_group_info = source->cr_group_info;
1553 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001554 target->cr_gss_mech = source->cr_gss_mech;
1555 if (source->cr_gss_mech)
1556 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001557 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558}
1559
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001560static long long
1561compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1562{
1563 long long res;
1564
1565 res = o1->len - o2->len;
1566 if (res)
1567 return res;
1568 return (long long)memcmp(o1->data, o2->data, o1->len);
1569}
1570
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001571static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001572{
NeilBrowna55370a2005-06-23 22:03:52 -07001573 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575
1576static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001577same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1578{
1579 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581
1582static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001583same_clid(clientid_t *cl1, clientid_t *cl2)
1584{
1585 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586}
1587
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001588static bool groups_equal(struct group_info *g1, struct group_info *g2)
1589{
1590 int i;
1591
1592 if (g1->ngroups != g2->ngroups)
1593 return false;
1594 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001595 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001596 return false;
1597 return true;
1598}
1599
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001600/*
1601 * RFC 3530 language requires clid_inuse be returned when the
1602 * "principal" associated with a requests differs from that previously
1603 * used. We use uid, gid's, and gss principal string as our best
1604 * approximation. We also don't want to allow non-gss use of a client
1605 * established using gss: in theory cr_principal should catch that
1606 * change, but in practice cr_principal can be null even in the gss case
1607 * since gssd doesn't always pass down a principal string.
1608 */
1609static bool is_gss_cred(struct svc_cred *cr)
1610{
1611 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1612 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1613}
1614
1615
Vivek Trivedi5559b502012-07-24 21:18:20 +05301616static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001617same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1618{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001619 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001620 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1621 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001622 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1623 return false;
1624 if (cr1->cr_principal == cr2->cr_principal)
1625 return true;
1626 if (!cr1->cr_principal || !cr2->cr_principal)
1627 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301628 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
1630
J. Bruce Fields57266a62013-04-13 14:27:29 -04001631static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1632{
1633 struct svc_cred *cr = &rqstp->rq_cred;
1634 u32 service;
1635
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001636 if (!cr->cr_gss_mech)
1637 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001638 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1639 return service == RPC_GSS_SVC_INTEGRITY ||
1640 service == RPC_GSS_SVC_PRIVACY;
1641}
1642
1643static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1644{
1645 struct svc_cred *cr = &rqstp->rq_cred;
1646
1647 if (!cl->cl_mach_cred)
1648 return true;
1649 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1650 return false;
1651 if (!svc_rqst_integrity_protected(rqstp))
1652 return false;
1653 if (!cr->cr_principal)
1654 return false;
1655 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1656}
1657
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001658static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001659{
1660 static u32 current_clientid = 1;
1661
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001662 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 clp->cl_clientid.cl_id = current_clientid++;
1664}
1665
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001666static void gen_confirm(struct nfs4_client *clp)
1667{
Chuck Leverab4684d2012-03-02 17:13:50 -05001668 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001669 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Jeff Laytonf4199922014-06-17 07:44:11 -04001671 /*
1672 * This is opaque to client, so no need to byte-swap. Use
1673 * __force to keep sparse happy
1674 */
1675 verf[0] = (__force __be32)get_seconds();
1676 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001677 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678}
1679
Jeff Layton4770d722014-07-29 21:34:10 -04001680static struct nfs4_stid *
1681find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001682{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001683 struct nfs4_stid *ret;
1684
1685 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1686 if (!ret || !ret->sc_type)
1687 return NULL;
1688 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001689}
1690
Jeff Layton4770d722014-07-29 21:34:10 -04001691static struct nfs4_stid *
1692find_stateid(struct nfs4_client *cl, stateid_t *t)
1693{
1694 struct nfs4_stid *ret;
1695
1696 spin_lock(&cl->cl_lock);
1697 ret = find_stateid_locked(cl, t);
1698 spin_unlock(&cl->cl_lock);
1699 return ret;
1700}
1701
1702static struct nfs4_stid *
1703find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001704{
1705 struct nfs4_stid *s;
1706
Jeff Layton4770d722014-07-29 21:34:10 -04001707 spin_lock(&cl->cl_lock);
1708 s = find_stateid_locked(cl, t);
1709 if (s != NULL && !(typemask & s->sc_type))
1710 s = NULL;
1711 spin_unlock(&cl->cl_lock);
1712 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001713}
1714
Jeff Layton2216d442012-11-12 15:00:57 -05001715static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001716 struct svc_rqst *rqstp, nfs4_verifier *verf)
1717{
1718 struct nfs4_client *clp;
1719 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001720 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001721 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001722 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001723
1724 clp = alloc_client(name);
1725 if (clp == NULL)
1726 return NULL;
1727
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001728 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1729 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001730 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001731 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001732 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001733 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001734 }
Jeff Layton02e12152014-07-16 10:31:57 -04001735 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001736 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001737 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001738 copy_verf(clp, verf);
1739 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001740 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001741 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001742 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001743 return clp;
1744}
1745
NeilBrownfd39ca92005-06-23 22:04:03 -07001746static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001747add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1748{
1749 struct rb_node **new = &(root->rb_node), *parent = NULL;
1750 struct nfs4_client *clp;
1751
1752 while (*new) {
1753 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1754 parent = *new;
1755
1756 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1757 new = &((*new)->rb_left);
1758 else
1759 new = &((*new)->rb_right);
1760 }
1761
1762 rb_link_node(&new_clp->cl_namenode, parent, new);
1763 rb_insert_color(&new_clp->cl_namenode, root);
1764}
1765
1766static struct nfs4_client *
1767find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1768{
1769 long long cmp;
1770 struct rb_node *node = root->rb_node;
1771 struct nfs4_client *clp;
1772
1773 while (node) {
1774 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1775 cmp = compare_blob(&clp->cl_name, name);
1776 if (cmp > 0)
1777 node = node->rb_left;
1778 else if (cmp < 0)
1779 node = node->rb_right;
1780 else
1781 return clp;
1782 }
1783 return NULL;
1784}
1785
1786static void
1787add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
1789 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001790 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001792 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001793 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001795 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001796 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797}
1798
NeilBrownfd39ca92005-06-23 22:04:03 -07001799static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800move_to_confirmed(struct nfs4_client *clp)
1801{
1802 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001803 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
1805 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001806 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001807 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001808 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001809 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 renew_client(clp);
1811}
1812
1813static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001814find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
1816 struct nfs4_client *clp;
1817 unsigned int idhashval = clientid_hashval(clid->cl_id);
1818
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001819 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001820 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001821 if ((bool)clp->cl_minorversion != sessions)
1822 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001823 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 }
1827 return NULL;
1828}
1829
1830static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001831find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1832{
1833 struct list_head *tbl = nn->conf_id_hashtbl;
1834
1835 return find_client_in_id_table(tbl, clid, sessions);
1836}
1837
1838static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001839find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001841 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001843 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
1845
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001846static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001847{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001848 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001849}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001850
NeilBrown28ce6052005-06-23 22:03:56 -07001851static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001852find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001853{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001854 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001855}
1856
1857static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001858find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001859{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001860 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001861}
1862
NeilBrownfd39ca92005-06-23 22:04:03 -07001863static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001864gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001866 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001867 struct sockaddr *sa = svc_addr(rqstp);
1868 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001869 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Jeff Layton7077ecb2009-08-14 12:57:58 -04001871 /* Currently, we only support tcp and tcp6 for the callback channel */
1872 if (se->se_callback_netid_len == 3 &&
1873 !memcmp(se->se_callback_netid_val, "tcp", 3))
1874 expected_family = AF_INET;
1875 else if (se->se_callback_netid_len == 4 &&
1876 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1877 expected_family = AF_INET6;
1878 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 goto out_err;
1880
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001881 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001882 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001883 (struct sockaddr *)&conn->cb_addr,
1884 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001885
J. Bruce Fields07263f12010-05-31 19:09:40 -04001886 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001888
J. Bruce Fields07263f12010-05-31 19:09:40 -04001889 if (conn->cb_addr.ss_family == AF_INET6)
1890 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001891
J. Bruce Fields07263f12010-05-31 19:09:40 -04001892 conn->cb_prog = se->se_callback_prog;
1893 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001894 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 return;
1896out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001897 conn->cb_addr.ss_family = AF_UNSPEC;
1898 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001899 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 "will not receive delegations\n",
1901 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 return;
1904}
1905
Andy Adamson074fe892009-04-03 08:28:15 +03001906/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04001907 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001908 */
Trond Myklebustb6076642014-06-30 11:48:35 -04001909static void
Andy Adamson074fe892009-04-03 08:28:15 +03001910nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1911{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001912 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04001913 struct nfsd4_slot *slot = resp->cstate.slot;
1914 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001915
Andy Adamson557ce262009-08-28 08:45:04 -04001916 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001917
Andy Adamson557ce262009-08-28 08:45:04 -04001918 slot->sl_opcnt = resp->opcnt;
1919 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001920
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001921 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001922 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001923 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001924 return;
1925 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001926 base = resp->cstate.data_offset;
1927 slot->sl_datalen = buf->len - base;
1928 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04001929 WARN("%s: sessions DRC could not cache compound\n", __func__);
1930 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001931}
1932
1933/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001934 * Encode the replay sequence operation from the slot values.
1935 * If cachethis is FALSE encode the uncached rep error on the next
1936 * operation which sets resp->p and increments resp->opcnt for
1937 * nfs4svc_encode_compoundres.
1938 *
Andy Adamson074fe892009-04-03 08:28:15 +03001939 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001940static __be32
1941nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1942 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001943{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001944 struct nfsd4_op *op;
1945 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001946
Andy Adamsonabfabf82009-07-23 19:02:18 -04001947 /* Encode the replayed sequence operation */
1948 op = &args->ops[resp->opcnt - 1];
1949 nfsd4_encode_operation(resp, op);
1950
1951 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05001952 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001953 op = &args->ops[resp->opcnt++];
1954 op->status = nfserr_retry_uncached_rep;
1955 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001956 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001957 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001958}
1959
1960/*
Andy Adamson557ce262009-08-28 08:45:04 -04001961 * The sequence operation is not cached because we can use the slot and
1962 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001963 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04001964static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001965nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1966 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001967{
Andy Adamson557ce262009-08-28 08:45:04 -04001968 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001969 struct xdr_stream *xdr = &resp->xdr;
1970 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03001971 __be32 status;
1972
Andy Adamson557ce262009-08-28 08:45:04 -04001973 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001974
Andy Adamsonabfabf82009-07-23 19:02:18 -04001975 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04001976 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04001977 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001978
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001979 p = xdr_reserve_space(xdr, slot->sl_datalen);
1980 if (!p) {
1981 WARN_ON_ONCE(1);
1982 return nfserr_serverfault;
1983 }
1984 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
1985 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03001986
Andy Adamson557ce262009-08-28 08:45:04 -04001987 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001988 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001989}
1990
Andy Adamson0733d212009-04-03 08:28:01 +03001991/*
1992 * Set the exchange_id flags returned by the server.
1993 */
1994static void
1995nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1996{
1997 /* pNFS is not supported */
1998 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1999
2000 /* Referrals are supported, Migration is not. */
2001 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2002
2003 /* set the wire flags to return to client. */
2004 clid->flags = new->cl_exchange_flags;
2005}
2006
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002007static bool client_has_state(struct nfs4_client *clp)
2008{
2009 /*
2010 * Note clp->cl_openowners check isn't quite right: there's no
2011 * need to count owners without stateid's.
2012 *
2013 * Also note we should probably be using this in 4.0 case too.
2014 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002015 return !list_empty(&clp->cl_openowners)
2016 || !list_empty(&clp->cl_delegations)
2017 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002018}
2019
Al Virob37ad282006-10-19 23:28:59 -07002020__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002021nfsd4_exchange_id(struct svc_rqst *rqstp,
2022 struct nfsd4_compound_state *cstate,
2023 struct nfsd4_exchange_id *exid)
2024{
Andy Adamson0733d212009-04-03 08:28:01 +03002025 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002026 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002027 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002028 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002029 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002030 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002031 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002032
Jeff Layton363168b2009-08-14 12:57:56 -04002033 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002034 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002035 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002036 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002037 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002038
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002039 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002040 return nfserr_inval;
2041
Andy Adamson0733d212009-04-03 08:28:01 +03002042 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002043 case SP4_MACH_CRED:
2044 if (!svc_rqst_integrity_protected(rqstp))
2045 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002046 case SP4_NONE:
2047 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002048 default: /* checked by xdr code */
2049 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002050 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002051 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002052 }
2053
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002054 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03002055 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002056 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002057 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002058 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2059 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2060
J. Bruce Fields136e6582012-05-12 20:37:23 -04002061 if (update) {
2062 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002063 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002064 goto out;
2065 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002066 if (!mach_creds_match(conf, rqstp)) {
2067 status = nfserr_wrong_cred;
2068 goto out;
2069 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002070 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002071 status = nfserr_perm;
2072 goto out;
2073 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002074 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002075 status = nfserr_not_same;
2076 goto out;
2077 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002078 /* case 6 */
2079 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2080 new = conf;
2081 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002082 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002083 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002084 if (client_has_state(conf)) {
2085 status = nfserr_clid_inuse;
2086 goto out;
2087 }
Andy Adamson0733d212009-04-03 08:28:01 +03002088 expire_client(conf);
2089 goto out_new;
2090 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002091 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002092 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002093 new = conf;
2094 goto out_copy;
2095 }
2096 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04002097 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002098 }
2099
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002100 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002101 status = nfserr_noent;
2102 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002103 }
2104
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002105 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002106 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03002107 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002108
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002109 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002110out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05002111 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03002112 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04002113 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03002114 goto out;
2115 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002116 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002117 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002118
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002119 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002120 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03002121out_copy:
2122 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2123 exid->clientid.cl_id = new->cl_clientid.cl_id;
2124
J. Bruce Fields778df3f2012-05-25 21:40:23 -04002125 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03002126 nfsd4_set_ex_flags(new, exid);
2127
2128 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04002129 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002130 status = nfs_ok;
2131
2132out:
2133 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03002134 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002135}
2136
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002137static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002138check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002139{
Andy Adamson88e588d2009-07-23 19:02:15 -04002140 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2141 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002142
2143 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002144 if (slot_inuse) {
2145 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002146 return nfserr_jukebox;
2147 else
2148 return nfserr_seq_misordered;
2149 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002150 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002151 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002152 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002153 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002154 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002155 return nfserr_seq_misordered;
2156}
2157
Andy Adamson49557cc2009-07-23 19:02:16 -04002158/*
2159 * Cache the create session result into the create session single DRC
2160 * slot cache by saving the xdr structure. sl_seqid has been set.
2161 * Do this for solo or embedded create session operations.
2162 */
2163static void
2164nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002165 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002166{
2167 slot->sl_status = nfserr;
2168 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2169}
2170
2171static __be32
2172nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2173 struct nfsd4_clid_slot *slot)
2174{
2175 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2176 return slot->sl_status;
2177}
2178
Mi Jinlong1b74c252011-07-14 14:50:17 +08002179#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2180 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2181 1 + /* MIN tag is length with zero, only length */ \
2182 3 + /* version, opcount, opcode */ \
2183 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2184 /* seqid, slotID, slotID, cache */ \
2185 4 ) * sizeof(__be32))
2186
2187#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2188 2 + /* verifier: AUTH_NULL, length 0 */\
2189 1 + /* status */ \
2190 1 + /* MIN tag is length with zero, only length */ \
2191 3 + /* opcount, opcode, opstatus*/ \
2192 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2193 /* seqid, slotID, slotID, slotID, status */ \
2194 5 ) * sizeof(__be32))
2195
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002196static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002197{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002198 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2199
J. Bruce Fields373cd402013-04-08 15:42:12 -04002200 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2201 return nfserr_toosmall;
2202 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2203 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002204 ca->headerpadsz = 0;
2205 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2206 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2207 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2208 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2209 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2210 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2211 /*
2212 * Note decreasing slot size below client's request may make it
2213 * difficult for client to function correctly, whereas
2214 * decreasing the number of slots will (just?) affect
2215 * performance. When short on memory we therefore prefer to
2216 * decrease number of slots instead of their size. Clients that
2217 * request larger slots than they need will get poor results:
2218 */
2219 ca->maxreqs = nfsd4_get_drc_mem(ca);
2220 if (!ca->maxreqs)
2221 return nfserr_jukebox;
2222
J. Bruce Fields373cd402013-04-08 15:42:12 -04002223 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002224}
2225
Kinglong Mee8a891632013-12-23 18:11:02 +08002226#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2227 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2228#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2229 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2230
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002231static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2232{
2233 ca->headerpadsz = 0;
2234
2235 /*
2236 * These RPC_MAX_HEADER macros are overkill, especially since we
2237 * don't even do gss on the backchannel yet. But this is still
2238 * less than 1k. Tighten up this estimate in the unlikely event
2239 * it turns out to be a problem for some client:
2240 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002241 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002242 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002243 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002244 return nfserr_toosmall;
2245 ca->maxresp_cached = 0;
2246 if (ca->maxops < 2)
2247 return nfserr_toosmall;
2248
2249 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002250}
2251
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002252static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2253{
2254 switch (cbs->flavor) {
2255 case RPC_AUTH_NULL:
2256 case RPC_AUTH_UNIX:
2257 return nfs_ok;
2258 default:
2259 /*
2260 * GSS case: the spec doesn't allow us to return this
2261 * error. But it also doesn't allow us not to support
2262 * GSS.
2263 * I'd rather this fail hard than return some error the
2264 * client might think it can already handle:
2265 */
2266 return nfserr_encr_alg_unsupp;
2267 }
2268}
2269
Andy Adamson069b6ad2009-04-03 08:27:58 +03002270__be32
2271nfsd4_create_session(struct svc_rqst *rqstp,
2272 struct nfsd4_compound_state *cstate,
2273 struct nfsd4_create_session *cr_ses)
2274{
Jeff Layton363168b2009-08-14 12:57:56 -04002275 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002276 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002277 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002278 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002279 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002280 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002281 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002282
Mi Jinlonga62573d2011-03-23 17:57:07 +08002283 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2284 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002285 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2286 if (status)
2287 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002288 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002289 if (status)
2290 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002291 status = check_backchannel_attrs(&cr_ses->back_channel);
2292 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002293 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002294 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002295 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002296 if (!new)
2297 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002298 conn = alloc_conn_from_crses(rqstp, cr_ses);
2299 if (!conn)
2300 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002301
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002302 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002303 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002304 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002305 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002306
2307 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002308 status = nfserr_wrong_cred;
2309 if (!mach_creds_match(conf, rqstp))
2310 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002311 cs_slot = &conf->cl_cs_slot;
2312 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002313 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002314 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002315 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002316 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002317 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002318 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002319 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002320 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002321 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002322 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002323 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002324 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002325 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002326 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002327 status = nfserr_wrong_cred;
2328 if (!mach_creds_match(unconf, rqstp))
2329 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002330 cs_slot = &unconf->cl_cs_slot;
2331 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002332 if (status) {
2333 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002334 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002335 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002336 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002337 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002338 if (old) {
2339 status = mark_client_expired(old);
2340 if (status)
2341 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002342 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002343 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002344 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002345 conf = unconf;
2346 } else {
2347 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002348 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002349 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002350 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002351 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002352 * We do not support RDMA or persistent sessions
2353 */
2354 cr_ses->flags &= ~SESSION4_PERSIST;
2355 cr_ses->flags &= ~SESSION4_RDMA;
2356
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002357 init_session(rqstp, new, conf, cr_ses);
2358 nfsd4_init_conn(rqstp, conn, new);
2359
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002360 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002361 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002362 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002363 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002364
Andy Adamson49557cc2009-07-23 19:02:16 -04002365 /* cache solo and embedded create sessions under the state lock */
2366 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002367 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002368 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002369out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08002370 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002371 free_conn(conn);
2372out_free_session:
2373 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002374out_release_drc_mem:
2375 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002376 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002377}
2378
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002379static __be32 nfsd4_map_bcts_dir(u32 *dir)
2380{
2381 switch (*dir) {
2382 case NFS4_CDFC4_FORE:
2383 case NFS4_CDFC4_BACK:
2384 return nfs_ok;
2385 case NFS4_CDFC4_FORE_OR_BOTH:
2386 case NFS4_CDFC4_BACK_OR_BOTH:
2387 *dir = NFS4_CDFC4_BOTH;
2388 return nfs_ok;
2389 };
2390 return nfserr_inval;
2391}
2392
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002393__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2394{
2395 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002396 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002397 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002398
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002399 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2400 if (status)
2401 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002402 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002403 session->se_cb_prog = bc->bc_cb_program;
2404 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002405 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002406
2407 nfsd4_probe_callback(session->se_client);
2408
2409 return nfs_ok;
2410}
2411
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002412__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2413 struct nfsd4_compound_state *cstate,
2414 struct nfsd4_bind_conn_to_session *bcts)
2415{
2416 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002417 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002418 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002419 struct net *net = SVC_NET(rqstp);
2420 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002421
2422 if (!nfsd4_last_compound_op(rqstp))
2423 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002424 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002425 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002426 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002427 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002428 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002429 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002430 status = nfserr_wrong_cred;
2431 if (!mach_creds_match(session->se_client, rqstp))
2432 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002433 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002434 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002435 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002436 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002437 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002438 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002439 goto out;
2440 nfsd4_init_conn(rqstp, conn, session);
2441 status = nfs_ok;
2442out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002443 nfsd4_put_session(session);
2444out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002445 nfs4_unlock_state();
2446 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002447}
2448
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002449static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2450{
2451 if (!session)
2452 return 0;
2453 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2454}
2455
Andy Adamson069b6ad2009-04-03 08:27:58 +03002456__be32
2457nfsd4_destroy_session(struct svc_rqst *r,
2458 struct nfsd4_compound_state *cstate,
2459 struct nfsd4_destroy_session *sessionid)
2460{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002461 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002462 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002463 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002464 struct net *net = SVC_NET(r);
2465 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002466
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002467 nfs4_lock_state();
2468 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002469 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002470 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002471 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002472 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002473 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002474 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002475 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002476 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002477 if (!ses)
2478 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002479 status = nfserr_wrong_cred;
2480 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002481 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002482 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002483 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002484 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002485 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002486 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002487
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002488 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002489
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002490 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002491 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002492out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002493 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002494out_client_lock:
2495 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002496out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002497 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002498 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002499}
2500
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002501static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002502{
2503 struct nfsd4_conn *c;
2504
2505 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002506 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002507 return c;
2508 }
2509 }
2510 return NULL;
2511}
2512
J. Bruce Fields57266a62013-04-13 14:27:29 -04002513static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002514{
2515 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002516 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002517 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002518 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002519
2520 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002521 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002522 if (c)
2523 goto out_free;
2524 status = nfserr_conn_not_bound_to_session;
2525 if (clp->cl_mach_cred)
2526 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002527 __nfsd4_hash_conn(new, ses);
2528 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002529 ret = nfsd4_register_conn(new);
2530 if (ret)
2531 /* oops; xprt is already down: */
2532 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002533 return nfs_ok;
2534out_free:
2535 spin_unlock(&clp->cl_lock);
2536 free_conn(new);
2537 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002538}
2539
Mi Jinlong868b89c2011-04-27 09:09:58 +08002540static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2541{
2542 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2543
2544 return args->opcnt > session->se_fchannel.maxops;
2545}
2546
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002547static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2548 struct nfsd4_session *session)
2549{
2550 struct xdr_buf *xb = &rqstp->rq_arg;
2551
2552 return xb->len > session->se_fchannel.maxreq_sz;
2553}
2554
Andy Adamson069b6ad2009-04-03 08:27:58 +03002555__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002556nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002557 struct nfsd4_compound_state *cstate,
2558 struct nfsd4_sequence *seq)
2559{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002560 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002561 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002562 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002563 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002564 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002565 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002566 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002567 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002568 struct net *net = SVC_NET(rqstp);
2569 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002570
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002571 if (resp->opcnt != 1)
2572 return nfserr_sequence_pos;
2573
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002574 /*
2575 * Will be either used or freed by nfsd4_sequence_check_conn
2576 * below.
2577 */
2578 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2579 if (!conn)
2580 return nfserr_jukebox;
2581
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002582 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002583 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002584 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002585 goto out_no_session;
2586 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002587
Mi Jinlong868b89c2011-04-27 09:09:58 +08002588 status = nfserr_too_many_ops;
2589 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002590 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002591
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002592 status = nfserr_req_too_big;
2593 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002594 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002595
Benny Halevyb85d4c02009-04-03 08:28:08 +03002596 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002597 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002598 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002599
Andy Adamson557ce262009-08-28 08:45:04 -04002600 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002601 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2602
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002603 /* We do not negotiate the number of slots yet, so set the
2604 * maxslots to the session maxreqs which is used to encode
2605 * sr_highest_slotid and the sr_target_slot id to maxslots */
2606 seq->maxslots = session->se_fchannel.maxreqs;
2607
J. Bruce Fields73e79482012-02-13 16:39:00 -05002608 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2609 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002610 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002611 status = nfserr_seq_misordered;
2612 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002613 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002614 cstate->slot = slot;
2615 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002616 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002617 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002618 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002619 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002620 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002621 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002622 }
2623 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002624 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002625
J. Bruce Fields57266a62013-04-13 14:27:29 -04002626 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002627 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002628 if (status)
2629 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002630
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002631 buflen = (seq->cachethis) ?
2632 session->se_fchannel.maxresp_cached :
2633 session->se_fchannel.maxresp_sz;
2634 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2635 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002636 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002637 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002638 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002639
2640 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002641 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002642 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002643 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002644 if (seq->cachethis)
2645 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002646 else
2647 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002648
2649 cstate->slot = slot;
2650 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002651 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002652
Benny Halevyb85d4c02009-04-03 08:28:08 +03002653out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002654 switch (clp->cl_cb_state) {
2655 case NFSD4_CB_DOWN:
2656 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2657 break;
2658 case NFSD4_CB_FAULT:
2659 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2660 break;
2661 default:
2662 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002663 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002664 if (!list_empty(&clp->cl_revoked))
2665 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002666out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002667 if (conn)
2668 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002669 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002670 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002671out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002672 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002673 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002674}
2675
Trond Myklebustb6076642014-06-30 11:48:35 -04002676void
2677nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2678{
2679 struct nfsd4_compound_state *cs = &resp->cstate;
2680
2681 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002682 if (cs->status != nfserr_replay_cache) {
2683 nfsd4_store_cache_entry(resp);
2684 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2685 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002686 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002687 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002688 } else if (cs->clp)
2689 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002690}
2691
Mi Jinlong345c2842011-10-20 17:51:39 +08002692__be32
2693nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2694{
2695 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002696 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002697 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002698
2699 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002700 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002701 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002702 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002703
2704 if (conf) {
2705 clp = conf;
2706
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002707 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002708 status = nfserr_clientid_busy;
2709 goto out;
2710 }
2711 } else if (unconf)
2712 clp = unconf;
2713 else {
2714 status = nfserr_stale_clientid;
2715 goto out;
2716 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002717 if (!mach_creds_match(clp, rqstp)) {
2718 status = nfserr_wrong_cred;
2719 goto out;
2720 }
Mi Jinlong345c2842011-10-20 17:51:39 +08002721 expire_client(clp);
2722out:
2723 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002724 return status;
2725}
2726
Andy Adamson069b6ad2009-04-03 08:27:58 +03002727__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002728nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2729{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002730 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002731
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002732 if (rc->rca_one_fs) {
2733 if (!cstate->current_fh.fh_dentry)
2734 return nfserr_nofilehandle;
2735 /*
2736 * We don't take advantage of the rca_one_fs case.
2737 * That's OK, it's optional, we can safely ignore it.
2738 */
2739 return nfs_ok;
2740 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002741
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002742 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002743 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002744 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2745 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002746 goto out;
2747
2748 status = nfserr_stale_clientid;
2749 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002750 /*
2751 * The following error isn't really legal.
2752 * But we only get here if the client just explicitly
2753 * destroyed the client. Surely it no longer cares what
2754 * error it gets back on an operation for the dead
2755 * client.
2756 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002757 goto out;
2758
2759 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002760 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002761out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002762 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002763 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002764}
2765
2766__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002767nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2768 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002770 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002772 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002773 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002774 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2775
J. Bruce Fields63db4632012-05-18 21:54:19 -04002776 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002778 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002779 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002780 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002782 if (clp_used_exchangeid(conf))
2783 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002784 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002785 char addr_str[INET6_ADDRSTRLEN];
2786 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2787 sizeof(addr_str));
2788 dprintk("NFSD: setclientid: string in use by client "
2789 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 goto out;
2791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002793 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002794 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002797 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002798 if (new == NULL)
2799 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002800 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002801 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002803 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002804 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002805 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002806 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002807 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2809 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2810 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2811 status = nfs_ok;
2812out:
2813 nfs4_unlock_state();
2814 return status;
2815}
2816
2817
Al Virob37ad282006-10-19 23:28:59 -07002818__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002819nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2820 struct nfsd4_compound_state *cstate,
2821 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822{
NeilBrown21ab45a2005-06-23 22:04:14 -07002823 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2825 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002826 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002827 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002829 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002832
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002833 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002834 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002835 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002836 * We try hard to give out unique clientid's, so if we get an
2837 * attempt to confirm the same clientid with a different cred,
2838 * there's a bug somewhere. Let's charitably assume it's our
2839 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002840 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002841 status = nfserr_serverfault;
2842 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2843 goto out;
2844 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2845 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002846 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002847 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2848 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002850 else /* case 4: client hasn't noticed we rebooted yet? */
2851 status = nfserr_stale_clientid;
2852 goto out;
2853 }
2854 status = nfs_ok;
2855 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002856 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2857 nfsd4_probe_callback(conf);
2858 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002859 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002860 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002861 if (conf) {
2862 status = mark_client_expired(conf);
2863 if (status)
2864 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002865 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002866 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002867 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002868 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 nfs4_unlock_state();
2872 return status;
2873}
2874
J. Bruce Fields32513b42011-10-13 16:00:16 -04002875static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002877 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2878}
2879
2880/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04002881static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04002882{
Trond Myklebustca943212014-07-23 16:17:39 -04002883 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
Trond Myklebust950e0112014-06-30 11:48:31 -04002885 lockdep_assert_held(&state_lock);
2886
J. Bruce Fields32513b42011-10-13 16:00:16 -04002887 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04002888 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002889 INIT_LIST_HEAD(&fp->fi_stateids);
2890 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04002891 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002892 fp->fi_had_conflict = false;
2893 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04002894 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002895 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2896 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04002897 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898}
2899
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002900void
NeilBrowne60d4392005-06-23 22:03:01 -07002901nfsd4_free_slabs(void)
2902{
Christoph Hellwigabf11352014-05-21 07:43:03 -07002903 kmem_cache_destroy(openowner_slab);
2904 kmem_cache_destroy(lockowner_slab);
2905 kmem_cache_destroy(file_slab);
2906 kmem_cache_destroy(stateid_slab);
2907 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002908}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
Bryan Schumaker72083392011-11-01 15:24:59 -04002910int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911nfsd4_init_slabs(void)
2912{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002913 openowner_slab = kmem_cache_create("nfsd4_openowners",
2914 sizeof(struct nfs4_openowner), 0, 0, NULL);
2915 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002916 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002917 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002918 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002919 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002920 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07002921 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002922 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002923 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002924 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07002925 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002926 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002927 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002928 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002929 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002930 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002931 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002932 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07002934
2935out_free_stateid_slab:
2936 kmem_cache_destroy(stateid_slab);
2937out_free_file_slab:
2938 kmem_cache_destroy(file_slab);
2939out_free_lockowner_slab:
2940 kmem_cache_destroy(lockowner_slab);
2941out_free_openowner_slab:
2942 kmem_cache_destroy(openowner_slab);
2943out:
NeilBrowne60d4392005-06-23 22:03:01 -07002944 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2945 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946}
2947
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002948static void init_nfs4_replay(struct nfs4_replay *rp)
2949{
2950 rp->rp_status = nfserr_serverfault;
2951 rp->rp_buflen = 0;
2952 rp->rp_buf = rp->rp_ibuf;
2953}
2954
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002955static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956{
2957 struct nfs4_stateowner *sop;
2958
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002959 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002960 if (!sop)
2961 return NULL;
2962
2963 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2964 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002965 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002966 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002968 sop->so_owner.len = owner->len;
2969
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002970 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002971 sop->so_client = clp;
2972 init_nfs4_replay(&sop->so_replay);
2973 return sop;
2974}
2975
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002976static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002977{
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002978 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2979
2980 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002981 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982}
2983
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002984static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04002985alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002986 struct nfsd4_compound_state *cstate)
2987{
Trond Myklebust13d6f662014-06-30 11:48:45 -04002988 struct nfs4_client *clp = cstate->clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002989 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002991 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2992 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002994 oo->oo_owner.so_is_open_owner = 1;
2995 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04002996 oo->oo_flags = NFS4_OO_NEW;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002997 if (nfsd4_has_session(cstate))
2998 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002999 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003000 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003001 INIT_LIST_HEAD(&oo->oo_close_lru);
3002 hash_openowner(oo, clp, strhashval);
3003 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004}
3005
J. Bruce Fields996e0932011-10-17 11:14:48 -04003006static 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 -04003007 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003009 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003010 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003011 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07003012 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003013 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 stp->st_access_bmap = 0;
3015 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003016 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003017 spin_lock(&oo->oo_owner.so_client->cl_lock);
3018 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003019 spin_lock(&fp->fi_lock);
3020 list_add(&stp->st_perfile, &fp->fi_stateids);
3021 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003022 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023}
3024
3025static void
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003026move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027{
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003028 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3029
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003030 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003032 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003033 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034}
3035
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04003037same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
3038 clientid_t *clid)
3039{
3040 return (sop->so_owner.len == owner->len) &&
3041 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
3042 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043}
3044
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003045static struct nfs4_openowner *
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003046find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
3047 bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048{
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003049 struct nfs4_stateowner *so;
3050 struct nfs4_openowner *oo;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003051 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003053 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003054 if (!so->so_is_open_owner)
3055 continue;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003056 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
3057 oo = openowner(so);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003058 clp = oo->oo_owner.so_client;
3059 if ((bool)clp->cl_minorversion != sessions)
3060 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003061 renew_client(oo->oo_owner.so_client);
3062 return oo;
3063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 }
3065 return NULL;
3066}
3067
3068/* search file_hashtbl[] for file */
3069static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003070find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071{
Trond Myklebustca943212014-07-23 16:17:39 -04003072 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 struct nfs4_file *fp;
3074
Trond Myklebust950e0112014-06-30 11:48:31 -04003075 lockdep_assert_held(&state_lock);
3076
Jeff Layton89876f82013-04-02 09:01:59 -04003077 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003078 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003079 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 }
3083 return NULL;
3084}
3085
Trond Myklebust950e0112014-06-30 11:48:31 -04003086static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003087find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003088{
3089 struct nfs4_file *fp;
3090
3091 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003092 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003093 spin_unlock(&state_lock);
3094 return fp;
3095}
3096
3097static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003098find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003099{
3100 struct nfs4_file *fp;
3101
3102 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003103 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003104 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003105 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003106 fp = new;
3107 }
3108 spin_unlock(&state_lock);
3109
3110 return fp;
3111}
3112
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003113/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 * Called to check deny when READ with all zero stateid or
3115 * WRITE with all zero or all one stateid
3116 */
Al Virob37ad282006-10-19 23:28:59 -07003117static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3119{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003121 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Trond Myklebustca943212014-07-23 16:17:39 -04003123 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003124 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003125 return ret;
3126 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003127 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003128 if (fp->fi_share_deny & deny_type)
3129 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003130 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003131 put_nfs4_file(fp);
3132 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133}
3134
Jeff Layton02e12152014-07-16 10:31:57 -04003135void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136{
Trond Myklebust11b91642014-07-29 21:34:08 -04003137 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3138 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003139
Trond Myklebust11b91642014-07-29 21:34:08 -04003140 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003141
Jeff Layton02e12152014-07-16 10:31:57 -04003142 /*
3143 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003144 * already holding inode->i_lock.
3145 *
Jeff Laytondff13992014-07-08 14:02:49 -04003146 * If the dl_time != 0, then we know that it has already been
3147 * queued for a lease break. Don't queue it again.
3148 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003149 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003150 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003151 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003152 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003153 }
Jeff Layton02e12152014-07-16 10:31:57 -04003154 spin_unlock(&state_lock);
3155}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
Jeff Layton02e12152014-07-16 10:31:57 -04003157static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3158{
3159 /*
3160 * We're assuming the state code never drops its reference
3161 * without first removing the lease. Since we're in this lease
3162 * callback (and since the lease code is serialized by the kernel
3163 * lock) we know the server hasn't removed the lease yet, we know
3164 * it's safe to take a reference.
3165 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003166 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003167 nfsd4_cb_recall(dp);
3168}
3169
Jeff Layton1c8c6012013-06-21 08:58:15 -04003170/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003171static void nfsd_break_deleg_cb(struct file_lock *fl)
3172{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003173 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3174 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003175
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003176 if (!fp) {
3177 WARN(1, "(%p)->fl_owner NULL\n", fl);
3178 return;
3179 }
3180 if (fp->fi_had_conflict) {
3181 WARN(1, "duplicate break on %p\n", fp);
3182 return;
3183 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003184 /*
3185 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003186 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003187 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003188 */
3189 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
Jeff Layton02e12152014-07-16 10:31:57 -04003191 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003192 fp->fi_had_conflict = true;
3193 /*
3194 * If there are no delegations on the list, then we can't count on this
3195 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3196 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3197 * true should keep any new delegations from being hashed.
3198 */
3199 if (list_empty(&fp->fi_delegations))
3200 fl->fl_break_time = jiffies;
3201 else
3202 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3203 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003204 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205}
3206
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207static
3208int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3209{
3210 if (arg & F_UNLCK)
3211 return lease_modify(onlist, arg);
3212 else
3213 return -EAGAIN;
3214}
3215
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003216static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003217 .lm_break = nfsd_break_deleg_cb,
3218 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219};
3220
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003221static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3222{
3223 if (nfsd4_has_session(cstate))
3224 return nfs_ok;
3225 if (seqid == so->so_seqid - 1)
3226 return nfserr_replay_me;
3227 if (seqid == so->so_seqid)
3228 return nfs_ok;
3229 return nfserr_bad_seqid;
3230}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Jeff Layton4b24ca72014-06-30 11:48:44 -04003232static __be32 lookup_clientid(clientid_t *clid,
3233 struct nfsd4_compound_state *cstate,
3234 struct nfsd_net *nn)
3235{
3236 struct nfs4_client *found;
3237
3238 if (cstate->clp) {
3239 found = cstate->clp;
3240 if (!same_clid(&found->cl_clientid, clid))
3241 return nfserr_stale_clientid;
3242 return nfs_ok;
3243 }
3244
3245 if (STALE_CLIENTID(clid, nn))
3246 return nfserr_stale_clientid;
3247
3248 /*
3249 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3250 * cached already then we know this is for is for v4.0 and "sessions"
3251 * will be false.
3252 */
3253 WARN_ON_ONCE(cstate->session);
3254 found = find_confirmed_client(clid, false, nn);
3255 if (!found)
3256 return nfserr_expired;
3257
3258 /* Cache the nfs4_client in cstate! */
3259 cstate->clp = found;
3260 atomic_inc(&found->cl_refcount);
3261 return nfs_ok;
3262}
3263
Al Virob37ad282006-10-19 23:28:59 -07003264__be32
Andy Adamson66689582009-04-03 08:28:45 +03003265nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003266 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 clientid_t *clientid = &open->op_clientid;
3269 struct nfs4_client *clp = NULL;
3270 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003271 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003272 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003274 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003276 /*
3277 * In case we need it later, after we've already created the
3278 * file and don't want to risk a further failure:
3279 */
3280 open->op_file = nfsd4_alloc_file();
3281 if (open->op_file == NULL)
3282 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Trond Myklebust2d91e892014-06-30 11:48:46 -04003284 status = lookup_clientid(clientid, cstate, nn);
3285 if (status)
3286 return status;
3287 clp = cstate->clp;
3288
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003289 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003290 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003291 open->op_openowner = oo;
3292 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003293 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003295 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003296 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003297 release_openowner(oo);
3298 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003299 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003300 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003301 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3302 if (status)
3303 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003304 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003305new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003306 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003307 if (oo == NULL)
3308 return nfserr_jukebox;
3309 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003310alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003311 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003312 if (!open->op_stp)
3313 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003314 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315}
3316
Al Virob37ad282006-10-19 23:28:59 -07003317static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003318nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3319{
3320 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3321 return nfserr_openmode;
3322 else
3323 return nfs_ok;
3324}
3325
Daniel Mackc47d8322011-05-16 16:38:14 +02003326static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003327{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003328 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3329}
3330
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003331static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003332{
3333 struct nfs4_stid *ret;
3334
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003335 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003336 if (!ret)
3337 return NULL;
3338 return delegstateid(ret);
3339}
3340
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003341static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3342{
3343 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3344 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3345}
3346
Al Virob37ad282006-10-19 23:28:59 -07003347static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003348nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003349 struct nfs4_delegation **dp)
3350{
3351 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003352 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07003353
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003354 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07003355 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003356 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003357 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07003358 status = nfs4_check_delegmode(*dp, flags);
3359 if (status)
3360 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003361out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003362 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003363 return nfs_ok;
3364 if (status)
3365 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003366 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003367 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003368}
3369
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003370static struct nfs4_ol_stateid *
3371nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003373 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003374 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375
Trond Myklebust1d31a252014-07-10 14:07:25 -04003376 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003377 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 /* ignore lock owners */
3379 if (local->st_stateowner->so_is_open_owner == 0)
3380 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003381 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003382 ret = local;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003383 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003386 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003387 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388}
3389
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003390static inline int nfs4_access_to_access(u32 nfs4_access)
3391{
3392 int flags = 0;
3393
3394 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3395 flags |= NFSD_MAY_READ;
3396 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3397 flags |= NFSD_MAY_WRITE;
3398 return flags;
3399}
3400
Al Virob37ad282006-10-19 23:28:59 -07003401static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3403 struct nfsd4_open *open)
3404{
3405 struct iattr iattr = {
3406 .ia_valid = ATTR_SIZE,
3407 .ia_size = 0,
3408 };
3409 if (!open->op_truncate)
3410 return 0;
3411 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003412 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3414}
3415
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003416static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003417 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3418 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003419{
Trond Myklebustde186432014-07-10 14:07:26 -04003420 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003421 __be32 status;
3422 int oflag = nfs4_access_to_omode(open->op_share_access);
3423 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003424 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003425
Trond Myklebustde186432014-07-10 14:07:26 -04003426 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003427
3428 /*
3429 * Are we trying to set a deny mode that would conflict with
3430 * current access?
3431 */
3432 status = nfs4_file_check_deny(fp, open->op_share_deny);
3433 if (status != nfs_ok) {
3434 spin_unlock(&fp->fi_lock);
3435 goto out;
3436 }
3437
3438 /* set access to the file */
3439 status = nfs4_file_get_access(fp, open->op_share_access);
3440 if (status != nfs_ok) {
3441 spin_unlock(&fp->fi_lock);
3442 goto out;
3443 }
3444
3445 /* Set access bits in stateid */
3446 old_access_bmap = stp->st_access_bmap;
3447 set_access(open->op_share_access, stp);
3448
3449 /* Set new deny mask */
3450 old_deny_bmap = stp->st_deny_bmap;
3451 set_deny(open->op_share_deny, stp);
3452 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3453
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003454 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003455 spin_unlock(&fp->fi_lock);
3456 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003457 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003458 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003459 spin_lock(&fp->fi_lock);
3460 if (!fp->fi_fds[oflag]) {
3461 fp->fi_fds[oflag] = filp;
3462 filp = NULL;
3463 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003464 }
Trond Myklebustde186432014-07-10 14:07:26 -04003465 spin_unlock(&fp->fi_lock);
3466 if (filp)
3467 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003468
3469 status = nfsd4_truncate(rqstp, cur_fh, open);
3470 if (status)
3471 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003472out:
3473 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003474out_put_access:
3475 stp->st_access_bmap = old_access_bmap;
3476 nfs4_file_put_access(fp, open->op_share_access);
3477 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3478 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003479}
3480
Al Virob37ad282006-10-19 23:28:59 -07003481static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003482nfs4_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 -07003483{
Al Virob37ad282006-10-19 23:28:59 -07003484 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003485 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003487 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003488 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003489
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003490 /* test and set deny mode */
3491 spin_lock(&fp->fi_lock);
3492 status = nfs4_file_check_deny(fp, open->op_share_deny);
3493 if (status == nfs_ok) {
3494 old_deny_bmap = stp->st_deny_bmap;
3495 set_deny(open->op_share_deny, stp);
3496 fp->fi_share_deny |=
3497 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3498 }
3499 spin_unlock(&fp->fi_lock);
3500
3501 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003504 status = nfsd4_truncate(rqstp, cur_fh, open);
3505 if (status != nfs_ok)
3506 reset_union_bmap_deny(old_deny_bmap, stp);
3507 return status;
3508}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003511nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003513 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514}
3515
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003516/* Should we give out recallable state?: */
3517static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3518{
3519 if (clp->cl_cb_state == NFSD4_CB_UP)
3520 return true;
3521 /*
3522 * In the sessions case, since we don't have to establish a
3523 * separate connection for callbacks, we assume it's OK
3524 * until we hear otherwise:
3525 */
3526 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3527}
3528
Jeff Laytond564fbe2014-07-16 10:31:58 -04003529static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003530{
3531 struct file_lock *fl;
3532
3533 fl = locks_alloc_lock();
3534 if (!fl)
3535 return NULL;
3536 locks_init_lock(fl);
3537 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003538 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003539 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3540 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003541 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003542 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003543 return fl;
3544}
3545
J. Bruce Fields99c41512013-05-21 16:21:25 -04003546static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003547{
Trond Myklebust11b91642014-07-29 21:34:08 -04003548 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003549 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003550 struct file *filp;
3551 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003552
Jeff Laytond564fbe2014-07-16 10:31:58 -04003553 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003554 if (!fl)
3555 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003556 filp = find_readable_file(fp);
3557 if (!filp) {
3558 /* We should always have a readable file here */
3559 WARN_ON_ONCE(1);
3560 return -EBADF;
3561 }
3562 fl->fl_file = filp;
3563 status = vfs_setlease(filp, fl->fl_type, &fl);
3564 if (status) {
3565 locks_free_lock(fl);
3566 goto out_fput;
3567 }
Benny Halevycdc97502014-05-30 09:09:30 -04003568 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003569 spin_lock(&fp->fi_lock);
3570 /* Did the lease get broken before we took the lock? */
3571 status = -EAGAIN;
3572 if (fp->fi_had_conflict)
3573 goto out_unlock;
3574 /* Race breaker */
3575 if (fp->fi_lease) {
3576 status = 0;
3577 atomic_inc(&fp->fi_delegees);
3578 hash_delegation_locked(dp, fp);
3579 goto out_unlock;
3580 }
3581 fp->fi_lease = fl;
3582 fp->fi_deleg_file = filp;
3583 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003584 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003585 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003586 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003587 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003588out_unlock:
3589 spin_unlock(&fp->fi_lock);
3590 spin_unlock(&state_lock);
3591out_fput:
3592 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003593 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003594}
3595
Jeff Layton0b266932014-07-25 07:34:25 -04003596static struct nfs4_delegation *
3597nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3598 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003599{
Jeff Layton0b266932014-07-25 07:34:25 -04003600 int status;
3601 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003602
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003603 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003604 return ERR_PTR(-EAGAIN);
3605
3606 dp = alloc_init_deleg(clp, fh);
3607 if (!dp)
3608 return ERR_PTR(-ENOMEM);
3609
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003610 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003611 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003612 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003613 dp->dl_stid.sc_file = fp;
Jeff Layton417c6622014-07-21 09:34:57 -04003614 if (!fp->fi_lease) {
3615 spin_unlock(&fp->fi_lock);
3616 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003617 status = nfs4_setlease(dp);
3618 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003619 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003620 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003621 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003622 status = -EAGAIN;
3623 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003624 }
Benny Halevy931ee562014-05-30 09:09:27 -04003625 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003626 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003627out_unlock:
3628 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003629 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003630out:
3631 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003632 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003633 return ERR_PTR(status);
3634 }
3635 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003636}
3637
Benny Halevy4aa89132012-02-21 14:16:44 -08003638static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3639{
3640 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3641 if (status == -EAGAIN)
3642 open->op_why_no_deleg = WND4_CONTENTION;
3643 else {
3644 open->op_why_no_deleg = WND4_RESOURCE;
3645 switch (open->op_deleg_want) {
3646 case NFS4_SHARE_WANT_READ_DELEG:
3647 case NFS4_SHARE_WANT_WRITE_DELEG:
3648 case NFS4_SHARE_WANT_ANY_DELEG:
3649 break;
3650 case NFS4_SHARE_WANT_CANCEL:
3651 open->op_why_no_deleg = WND4_CANCELLED;
3652 break;
3653 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003654 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003655 }
3656 }
3657}
3658
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659/*
3660 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003661 *
3662 * Note we don't support write delegations, and won't until the vfs has
3663 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 */
3665static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003666nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3667 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668{
3669 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003670 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3671 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003672 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003673 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003675 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003676 open->op_recall = 0;
3677 switch (open->op_claim_type) {
3678 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003679 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003680 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003681 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3682 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003683 break;
3684 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003685 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003686 /*
3687 * Let's not give out any delegations till everyone's
3688 * had the chance to reclaim theirs....
3689 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003690 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003691 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003692 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003693 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003694 /*
3695 * Also, if the file was opened for write or
3696 * create, there's a good chance the client's
3697 * about to write to it, resulting in an
3698 * immediate recall (since we don't support
3699 * write delegations):
3700 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003701 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003702 goto out_no_deleg;
3703 if (open->op_create == NFS4_OPEN_CREATE)
3704 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003705 break;
3706 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003707 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003708 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003709 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003710 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003711 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003713 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003715 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003716 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003717 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003718 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003719out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003720 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3721 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003722 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003723 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003724 open->op_recall = 1;
3725 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003726
3727 /* 4.1 client asking for a delegation? */
3728 if (open->op_deleg_want)
3729 nfsd4_open_deleg_none_ext(open, status);
3730 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731}
3732
Benny Halevye27f49c2012-02-21 14:16:54 -08003733static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3734 struct nfs4_delegation *dp)
3735{
3736 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3737 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3738 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3739 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3740 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3741 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3742 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3743 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3744 }
3745 /* Otherwise the client must be confused wanting a delegation
3746 * it already has, therefore we don't return
3747 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3748 */
3749}
3750
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751/*
3752 * called with nfs4_lock_state() held.
3753 */
Al Virob37ad282006-10-19 23:28:59 -07003754__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3756{
Andy Adamson66689582009-04-03 08:28:45 +03003757 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003758 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003760 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003761 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003762 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 /*
3765 * Lookup file; if found, lookup stateid and check open request,
3766 * and check for delegations in the process of being recalled.
3767 * If not found, create the nfs4_file struct
3768 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003769 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04003770 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04003771 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003772 if (status)
3773 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003774 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04003776 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003777 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003778 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003779 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003780 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 }
3782
3783 /*
3784 * OPEN the file, or upgrade an existing OPEN.
3785 * If truncate fails, the OPEN fails.
3786 */
3787 if (stp) {
3788 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003789 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 if (status)
3791 goto out;
3792 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003793 stp = open->op_stp;
3794 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003795 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003796 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3797 if (status) {
3798 release_open_stateid(stp);
3799 goto out;
3800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003802 update_stateid(&stp->st_stid.sc_stateid);
3803 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
Benny Halevyd24433c2012-02-16 20:57:17 +02003805 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02003806 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3807 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3808 open->op_why_no_deleg = WND4_NOT_WANTED;
3809 goto nodeleg;
3810 }
3811 }
3812
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 /*
3814 * Attempt to hand out a delegation. No error return, because the
3815 * OPEN succeeds even if we fail.
3816 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003817 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003818nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 status = nfs_ok;
3820
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003821 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003822 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003824 /* 4.1 client trying to upgrade/downgrade delegation? */
3825 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003826 open->op_deleg_want)
3827 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003828
NeilBrown13cd2182005-06-23 22:03:10 -07003829 if (fp)
3830 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003831 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003832 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 /*
3834 * To finish the open response, we just need to set the rflags.
3835 */
3836 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003837 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003838 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3840
3841 return status;
3842}
3843
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003844void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3845{
3846 if (open->op_openowner) {
3847 struct nfs4_openowner *oo = open->op_openowner;
3848
3849 if (!list_empty(&oo->oo_owner.so_stateids))
3850 list_del_init(&oo->oo_close_lru);
3851 if (oo->oo_flags & NFS4_OO_NEW) {
3852 if (status) {
3853 release_openowner(oo);
3854 open->op_openowner = NULL;
3855 } else
3856 oo->oo_flags &= ~NFS4_OO_NEW;
3857 }
3858 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003859 if (open->op_file)
3860 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003861 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04003862 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003863}
3864
Al Virob37ad282006-10-19 23:28:59 -07003865__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003866nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3867 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868{
3869 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003870 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003871 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872
3873 nfs4_lock_state();
3874 dprintk("process_renew(%08x/%08x): starting\n",
3875 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003876 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003877 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003879 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003881 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003882 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 goto out;
3884 status = nfs_ok;
3885out:
3886 nfs4_unlock_state();
3887 return status;
3888}
3889
NeilBrowna76b4312005-06-23 22:04:01 -07003890static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003891nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07003892{
Jeff Layton33dcc482012-04-10 11:08:48 -04003893 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003894 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04003895 return;
3896
NeilBrowna76b4312005-06-23 22:04:01 -07003897 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003898 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003899 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003900 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003901 /*
3902 * Now that every NFSv4 client has had the chance to recover and
3903 * to see the (possibly new, possibly shorter) lease time, we
3904 * can safely set the next grace time to the current lease time:
3905 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03003906 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003907}
3908
NeilBrownfd39ca92005-06-23 22:04:03 -07003909static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003910nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911{
3912 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003913 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 struct nfs4_delegation *dp;
3915 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003916 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003917 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918
3919 nfs4_lock_state();
3920
3921 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003922 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03003923 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003924 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03003925 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 clp = list_entry(pos, struct nfs4_client, cl_lru);
3927 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3928 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003929 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 break;
3931 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003932 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03003933 dprintk("NFSD: client in use (clientid %08x)\n",
3934 clp->cl_clientid.cl_id);
3935 continue;
3936 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003937 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003938 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003939 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03003940 list_for_each_safe(pos, next, &reaplist) {
3941 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 dprintk("NFSD: purging unused client (clientid %08x)\n",
3943 clp->cl_clientid.cl_id);
3944 expire_client(clp);
3945 }
Benny Halevycdc97502014-05-30 09:09:30 -04003946 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003947 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03003949 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3950 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003952 t = dp->dl_time - cutoff;
3953 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 break;
3955 }
Jeff Layton42690672014-07-25 07:34:20 -04003956 unhash_delegation_locked(dp);
3957 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 }
Benny Halevycdc97502014-05-30 09:09:30 -04003959 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04003960 while (!list_empty(&reaplist)) {
3961 dp = list_first_entry(&reaplist, struct nfs4_delegation,
3962 dl_recall_lru);
3963 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003964 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003966 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003967 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3968 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003969 t = oo->oo_time - cutoff;
3970 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 break;
3972 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003973 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04003975 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04003977 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978}
3979
Harvey Harrisona254b242008-02-20 12:49:00 -08003980static struct workqueue_struct *laundry_wq;
3981static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08003982
3983static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003984laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985{
3986 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003987 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3988 work);
3989 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3990 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003992 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003994 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995}
3996
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003997static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07003998{
Trond Myklebust11b91642014-07-29 21:34:08 -04003999 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004000 return nfserr_bad_stateid;
4001 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002}
4003
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004005access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004007 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4008 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4009 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010}
4011
4012static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004013access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004015 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4016 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017}
4018
4019static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004020__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021{
Al Virob37ad282006-10-19 23:28:59 -07004022 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
J. Bruce Fields02921912010-07-29 15:16:59 -04004024 /* For lock stateid's, we test the parent open, not the lock: */
4025 if (stp->st_openstp)
4026 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004027 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004029 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 goto out;
4031 status = nfs_ok;
4032out:
4033 return status;
4034}
4035
Al Virob37ad282006-10-19 23:28:59 -07004036static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004037check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004039 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004041 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004042 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 * conflicting state; so we must wait out the grace period. */
4044 return nfserr_grace;
4045 } else if (flags & WR_STATE)
4046 return nfs4_share_conflict(current_fh,
4047 NFS4_SHARE_DENY_WRITE);
4048 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4049 return nfs4_share_conflict(current_fh,
4050 NFS4_SHARE_DENY_READ);
4051}
4052
4053/*
4054 * Allow READ/WRITE during grace period on recovered state only for files
4055 * that are not able to provide mandatory locking.
4056 */
4057static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004058grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004060 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061}
4062
J. Bruce Fields81b82962011-08-23 11:03:29 -04004063/* Returns true iff a is later than b: */
4064static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4065{
Jim Rees1a9357f2013-05-17 17:33:00 -04004066 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004067}
4068
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004069static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004070{
Andy Adamson66689582009-04-03 08:28:45 +03004071 /*
4072 * When sessions are used the stateid generation number is ignored
4073 * when it is zero.
4074 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004075 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004076 return nfs_ok;
4077
4078 if (in->si_generation == ref->si_generation)
4079 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004080
J. Bruce Fields0836f582008-01-26 19:08:12 -05004081 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004082 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004083 return nfserr_bad_stateid;
4084 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004085 * However, we could see a stateid from the past, even from a
4086 * non-buggy client. For example, if the client sends a lock
4087 * while some IO is outstanding, the lock may bump si_generation
4088 * while the IO is still in flight. The client could avoid that
4089 * situation by waiting for responses on all the IO requests,
4090 * but better performance may result in retrying IO that
4091 * receives an old_stateid error if requests are rarely
4092 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004093 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004094 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004095}
4096
Chuck Lever7df302f2012-05-29 13:56:37 -04004097static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004098{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004099 struct nfs4_stid *s;
4100 struct nfs4_ol_stateid *ols;
4101 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004102
Chuck Lever7df302f2012-05-29 13:56:37 -04004103 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4104 return nfserr_bad_stateid;
4105 /* Client debugging aid. */
4106 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4107 char addr_str[INET6_ADDRSTRLEN];
4108 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4109 sizeof(addr_str));
4110 pr_warn_ratelimited("NFSD: client %s testing state ID "
4111 "with incorrect client ID\n", addr_str);
4112 return nfserr_bad_stateid;
4113 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004114 s = find_stateid(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004115 if (!s)
Chuck Lever7df302f2012-05-29 13:56:37 -04004116 return nfserr_bad_stateid;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004117 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004118 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004119 return status;
J. Bruce Fields23340032013-04-09 17:42:28 -04004120 switch (s->sc_type) {
4121 case NFS4_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004122 return nfs_ok;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004123 case NFS4_REVOKED_DELEG_STID:
4124 return nfserr_deleg_revoked;
J. Bruce Fields23340032013-04-09 17:42:28 -04004125 case NFS4_OPEN_STID:
4126 case NFS4_LOCK_STID:
4127 ols = openlockstateid(s);
4128 if (ols->st_stateowner->so_is_open_owner
4129 && !(openowner(ols->st_stateowner)->oo_flags
4130 & NFS4_OO_CONFIRMED))
4131 return nfserr_bad_stateid;
4132 return nfs_ok;
4133 default:
4134 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004135 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004136 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004137 case NFS4_CLOSED_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004138 return nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004139 }
Bryan Schumaker17456802011-07-13 10:50:48 -04004140}
4141
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004142static __be32
4143nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4144 stateid_t *stateid, unsigned char typemask,
4145 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004146{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004147 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004148
4149 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4150 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004151 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004152 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004153 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004154 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004155 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004156 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004157 if (status)
4158 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004159 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004160 if (!*s)
4161 return nfserr_bad_stateid;
4162 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004163}
4164
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165/*
4166* Checks for stateid operations
4167*/
Al Virob37ad282006-10-19 23:28:59 -07004168__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004169nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004170 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004172 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004173 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004175 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004177 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004178 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004179 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 if (filpp)
4182 *filpp = NULL;
4183
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004184 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 return nfserr_grace;
4186
4187 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004188 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189
Trond Myklebust14bcab12014-04-18 14:44:07 -04004190 nfs4_lock_state();
4191
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004192 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004193 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004194 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004195 if (status)
Trond Myklebust14bcab12014-04-18 14:44:07 -04004196 goto out;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004197 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4198 if (status)
4199 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004200 switch (s->sc_type) {
4201 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004202 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004203 status = nfs4_check_delegmode(dp, flags);
4204 if (status)
4205 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004206 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004207 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004208 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004209 WARN_ON_ONCE(1);
4210 status = nfserr_serverfault;
4211 goto out;
4212 }
Trond Myklebustde186432014-07-10 14:07:26 -04004213 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004214 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004215 break;
4216 case NFS4_OPEN_STID:
4217 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004218 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004219 status = nfs4_check_fh(current_fh, stp);
4220 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004222 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004223 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004225 status = nfs4_check_openmode(stp, flags);
4226 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004228 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004229 struct nfs4_file *fp = stp->st_stid.sc_file;
4230
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004231 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004232 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004233 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004234 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004235 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004236 break;
4237 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004238 status = nfserr_bad_stateid;
4239 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 }
4241 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004242 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004243 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244out:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004245 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 return status;
4247}
4248
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004249static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004250nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004251{
J. Bruce Fieldsa1b8ff42014-05-20 15:55:21 -04004252 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4253
Trond Myklebust11b91642014-07-29 21:34:08 -04004254 if (check_for_locks(stp->st_stid.sc_file, lo))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004255 return nfserr_locks_held;
Jeff Laytonc53530d2014-06-30 11:48:39 -04004256 release_lockowner_if_empty(lo);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004257 return nfs_ok;
4258}
4259
4260/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004261 * Test if the stateid is valid
4262 */
4263__be32
4264nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4265 struct nfsd4_test_stateid *test_stateid)
4266{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004267 struct nfsd4_test_stateid_id *stateid;
4268 struct nfs4_client *cl = cstate->session->se_client;
4269
4270 nfs4_lock_state();
4271 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004272 stateid->ts_id_status =
4273 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004274 nfs4_unlock_state();
4275
Bryan Schumaker17456802011-07-13 10:50:48 -04004276 return nfs_ok;
4277}
4278
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004279__be32
4280nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4281 struct nfsd4_free_stateid *free_stateid)
4282{
4283 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004284 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004285 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004286 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004287 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004288
4289 nfs4_lock_state();
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004290 s = find_stateid(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004291 if (!s)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004292 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004293 switch (s->sc_type) {
4294 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004295 ret = nfserr_locks_held;
4296 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004297 case NFS4_OPEN_STID:
4298 case NFS4_LOCK_STID:
4299 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4300 if (ret)
4301 goto out;
4302 if (s->sc_type == NFS4_LOCK_STID)
4303 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4304 else
4305 ret = nfserr_locks_held;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004306 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004307 case NFS4_REVOKED_DELEG_STID:
4308 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004309 spin_lock(&cl->cl_lock);
4310 list_del_init(&dp->dl_recall_lru);
4311 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004312 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004313 ret = nfs_ok;
4314 break;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004315 default:
4316 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004317 }
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004318out:
4319 nfs4_unlock_state();
4320 return ret;
4321}
4322
NeilBrown4c4cd222005-07-07 17:59:27 -07004323static inline int
4324setlkflg (int type)
4325{
4326 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4327 RD_STATE : WR_STATE;
4328}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004330static __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 -04004331{
4332 struct svc_fh *current_fh = &cstate->current_fh;
4333 struct nfs4_stateowner *sop = stp->st_stateowner;
4334 __be32 status;
4335
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004336 status = nfsd4_check_seqid(cstate, sop, seqid);
4337 if (status)
4338 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004339 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4340 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004341 /*
4342 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004343 * nfserr_replay_me from the previous step, and
4344 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004345 */
4346 return nfserr_bad_stateid;
4347 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4348 if (status)
4349 return status;
4350 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004351}
4352
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353/*
4354 * Checks for sequence id mutating operations.
4355 */
Al Virob37ad282006-10-19 23:28:59 -07004356static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004357nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004358 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004359 struct nfs4_ol_stateid **stpp,
4360 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004362 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004363 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004364 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004366 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4367 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004368
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004370 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004371 if (status)
4372 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004373 stp = openlockstateid(s);
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04004374 if (!nfsd4_has_session(cstate))
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004375 cstate->replay_owner = stp->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004377 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4378 if (!status)
4379 *stpp = stp;
4380 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004381}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004382
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004383static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4384 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004385{
4386 __be32 status;
4387 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004389 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004390 NFS4_OPEN_STID, stpp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004391 if (status)
4392 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004393 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004394 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07004395 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004396 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397}
4398
Al Virob37ad282006-10-19 23:28:59 -07004399__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004400nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004401 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402{
Al Virob37ad282006-10-19 23:28:59 -07004403 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004404 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004405 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004406 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407
Al Viroa6a9f182013-09-16 10:57:01 -04004408 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4409 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004411 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004412 if (status)
4413 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414
4415 nfs4_lock_state();
4416
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004417 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004418 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004419 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004420 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004421 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004422 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004423 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004424 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004425 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004426 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004427 update_stateid(&stp->st_stid.sc_stateid);
4428 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004429 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004430 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004431
Jeff Layton2a4317c2012-03-21 16:42:43 -04004432 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004433 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004435 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004436 if (!cstate->replay_owner)
4437 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 return status;
4439}
4440
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004441static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004443 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004444 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004445 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004446 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004447}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004448
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004449static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4450{
4451 switch (to_access) {
4452 case NFS4_SHARE_ACCESS_READ:
4453 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4454 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4455 break;
4456 case NFS4_SHARE_ACCESS_WRITE:
4457 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4458 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4459 break;
4460 case NFS4_SHARE_ACCESS_BOTH:
4461 break;
4462 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004463 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 }
4465}
4466
Al Virob37ad282006-10-19 23:28:59 -07004467__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004468nfsd4_open_downgrade(struct svc_rqst *rqstp,
4469 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004470 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471{
Al Virob37ad282006-10-19 23:28:59 -07004472 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004473 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004474 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
Al Viroa6a9f182013-09-16 10:57:01 -04004476 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4477 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004479 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004480 if (od->od_deleg_want)
4481 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4482 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483
4484 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004485 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004486 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004487 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004490 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004491 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 stp->st_access_bmap, od->od_share_access);
4493 goto out;
4494 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004495 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004496 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 stp->st_deny_bmap, od->od_share_deny);
4498 goto out;
4499 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004500 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501
Jeff Laytonce0fc432012-05-11 09:45:14 -04004502 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004504 update_stateid(&stp->st_stid.sc_stateid);
4505 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 status = nfs_ok;
4507out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004508 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004509 if (!cstate->replay_owner)
4510 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 return status;
4512}
4513
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004514static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4515{
Trond Myklebustacf92952014-06-30 11:48:37 -04004516 struct nfs4_client *clp = s->st_stid.sc_client;
4517 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4518
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004519 s->st_stid.sc_type = NFS4_CLOSED_STID;
Trond Myklebustacf92952014-06-30 11:48:37 -04004520 unhash_open_stateid(s);
4521
4522 if (clp->cl_minorversion) {
Trond Myklebustacf92952014-06-30 11:48:37 -04004523 if (list_empty(&oo->oo_owner.so_stateids))
4524 release_openowner(oo);
Trond Myklebust60116952014-07-29 21:34:06 -04004525 nfs4_put_stid(&s->st_stid);
Trond Myklebustacf92952014-06-30 11:48:37 -04004526 } else {
Trond Myklebust60116952014-07-29 21:34:06 -04004527 /*
4528 * In the 4.0 case we need to keep the owners around a
4529 * little while to handle CLOSE replay. We still do need
4530 * to release any file access that is held by them
4531 * before returning however.
4532 */
4533 release_all_access(s);
Trond Myklebust11b91642014-07-29 21:34:08 -04004534 if (s->st_stid.sc_file) {
4535 put_nfs4_file(s->st_stid.sc_file);
4536 s->st_stid.sc_file = NULL;
Trond Myklebustf8338832014-07-25 07:34:19 -04004537 }
Trond Myklebustacf92952014-06-30 11:48:37 -04004538 oo->oo_last_closed_stid = s;
Trond Myklebustacf92952014-06-30 11:48:37 -04004539 if (list_empty(&oo->oo_owner.so_stateids))
4540 move_to_close_lru(oo, clp->net);
4541 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004542}
4543
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544/*
4545 * nfs4_unlock_state() called after encode
4546 */
Al Virob37ad282006-10-19 23:28:59 -07004547__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004548nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004549 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550{
Al Virob37ad282006-10-19 23:28:59 -07004551 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004552 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004553 struct net *net = SVC_NET(rqstp);
4554 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
Al Viroa6a9f182013-09-16 10:57:01 -04004556 dprintk("NFSD: nfsd4_close on file %pd\n",
4557 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558
4559 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004560 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4561 &close->cl_stateid,
4562 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004563 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004564 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004565 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004567 update_stateid(&stp->st_stid.sc_stateid);
4568 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004570 nfsd4_close_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004572 if (!cstate->replay_owner)
4573 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 return status;
4575}
4576
Al Virob37ad282006-10-19 23:28:59 -07004577__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004578nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4579 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004581 struct nfs4_delegation *dp;
4582 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004583 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004584 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004585 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004587 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004588 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589
4590 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004591 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004592 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004593 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004594 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004595 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004596 if (status)
4597 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004598
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004599 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004601 nfs4_unlock_state();
4602
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 return status;
4604}
4605
4606
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608
Benny Halevy87df4de2008-12-15 19:42:03 +02004609static inline u64
4610end_offset(u64 start, u64 len)
4611{
4612 u64 end;
4613
4614 end = start + len;
4615 return end >= start ? end: NFS4_MAX_UINT64;
4616}
4617
4618/* last octet in a range */
4619static inline u64
4620last_byte_offset(u64 start, u64 len)
4621{
4622 u64 end;
4623
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004624 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004625 end = start + len;
4626 return end > start ? end - 1: NFS4_MAX_UINT64;
4627}
4628
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629/*
4630 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4631 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4632 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4633 * locking, this prevents us from being completely protocol-compliant. The
4634 * real solution to this problem is to start using unsigned file offsets in
4635 * the VFS, but this is a very deep change!
4636 */
4637static inline void
4638nfs4_transform_lock_offset(struct file_lock *lock)
4639{
4640 if (lock->fl_start < 0)
4641 lock->fl_start = OFFSET_MAX;
4642 if (lock->fl_end < 0)
4643 lock->fl_end = OFFSET_MAX;
4644}
4645
NeilBrownd5b90262006-04-10 22:55:22 -07004646/* Hack!: For now, we're defining this just so we can use a pointer to it
4647 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004648static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004649};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650
4651static inline void
4652nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4653{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004654 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655
NeilBrownd5b90262006-04-10 22:55:22 -07004656 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004657 lo = (struct nfs4_lockowner *) fl->fl_owner;
4658 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4659 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004660 if (!deny->ld_owner.data)
4661 /* We just don't care that much */
4662 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004663 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4664 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004665 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004666nevermind:
4667 deny->ld_owner.len = 0;
4668 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004669 deny->ld_clientid.cl_boot = 0;
4670 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 }
4672 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004673 deny->ld_length = NFS4_MAX_UINT64;
4674 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4676 deny->ld_type = NFS4_READ_LT;
4677 if (fl->fl_type != F_RDLCK)
4678 deny->ld_type = NFS4_WRITE_LT;
4679}
4680
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004681static struct nfs4_lockowner *
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004682find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4683 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684{
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004685 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4686 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004688 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4689 if (so->so_is_open_owner)
4690 continue;
4691 if (!same_owner_str(so, owner, clid))
4692 continue;
4693 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 }
4695 return NULL;
4696}
4697
4698/*
4699 * Alloc a lock owner structure.
4700 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004701 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004703 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004705static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004706alloc_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 -04004707 struct nfs4_lockowner *lo;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004708 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004710 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4711 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004713 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4714 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07004715 /* It is the openowner seqid that will be incremented in encode in the
4716 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004717 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004718 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004719 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720}
4721
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004722static struct nfs4_ol_stateid *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004723alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp,
4724 struct inode *inode,
4725 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726{
Jeff Laytonb49e0842014-07-29 21:34:11 -04004727 struct nfs4_stid *s;
Jeff Layton1c755dc2014-07-29 21:34:12 -04004728 struct nfs4_openowner *oo = openowner(open_stp->st_stateowner);
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004729 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004730 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731
Jeff Laytonb49e0842014-07-29 21:34:11 -04004732 s = nfs4_alloc_stid(clp, stateid_slab);
4733 if (s == NULL)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04004734 return NULL;
Jeff Laytonb49e0842014-07-29 21:34:11 -04004735 stp = openlockstateid(s);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004736 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004737 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07004738 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004739 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04004740 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004741 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004743 stp->st_openstp = open_stp;
Jeff Layton1c755dc2014-07-29 21:34:12 -04004744 spin_lock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004745 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04004746 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004747 spin_lock(&fp->fi_lock);
4748 list_add(&stp->st_perfile, &fp->fi_stateids);
4749 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04004750 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 return stp;
4752}
4753
Jeff Laytonc53530d2014-06-30 11:48:39 -04004754static struct nfs4_ol_stateid *
4755find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4756{
4757 struct nfs4_ol_stateid *lst;
4758
4759 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004760 if (lst->st_stid.sc_file == fp)
Jeff Laytonc53530d2014-06-30 11:48:39 -04004761 return lst;
4762 }
4763 return NULL;
4764}
4765
4766
NeilBrownfd39ca92005-06-23 22:04:03 -07004767static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768check_lock_length(u64 offset, u64 length)
4769{
Benny Halevy87df4de2008-12-15 19:42:03 +02004770 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 LOFF_OVERFLOW(offset, length)));
4772}
4773
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004774static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004775{
Trond Myklebust11b91642014-07-29 21:34:08 -04004776 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004777
Jeff Layton7214e862014-07-10 14:07:33 -04004778 lockdep_assert_held(&fp->fi_lock);
4779
Jeff Layton82c5ff12012-05-11 09:45:13 -04004780 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004781 return;
Jeff Layton12659652014-07-10 14:07:28 -04004782 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004783 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004784}
4785
J. Bruce Fields2355c592012-04-25 16:56:22 -04004786static __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 -04004787{
Trond Myklebust11b91642014-07-29 21:34:08 -04004788 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004789 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4790 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004791 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004792 struct nfs4_lockowner *lo;
4793 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004794 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004795
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004796 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004797 if (!lo) {
4798 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4799 &lock->v.new.owner);
4800 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4801 if (lo == NULL)
4802 return nfserr_jukebox;
4803 } else {
4804 /* with an existing lockowner, seqids must be the same */
4805 if (!cstate->minorversion &&
4806 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004807 return nfserr_bad_seqid;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004808 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004809
4810 *lst = find_lock_stateid(lo, fi);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004811 if (*lst == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004812 *lst = alloc_init_lock_stateid(lo, fi, inode, ost);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004813 if (*lst == NULL) {
4814 release_lockowner_if_empty(lo);
4815 return nfserr_jukebox;
4816 }
4817 *new = true;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004818 }
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004819 return nfs_ok;
4820}
4821
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822/*
4823 * LOCK operation
4824 */
Al Virob37ad282006-10-19 23:28:59 -07004825__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004826nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004827 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004829 struct nfs4_openowner *open_sop = NULL;
4830 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004831 struct nfs4_ol_stateid *lock_stp;
Jeff Layton7214e862014-07-10 14:07:33 -04004832 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004833 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004834 struct file_lock *file_lock = NULL;
4835 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004836 __be32 status = 0;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004837 bool new_state = false;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004838 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004839 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004840 struct net *net = SVC_NET(rqstp);
4841 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842
4843 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4844 (long long) lock->lk_offset,
4845 (long long) lock->lk_length);
4846
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 if (check_lock_length(lock->lk_offset, lock->lk_length))
4848 return nfserr_inval;
4849
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004850 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004851 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004852 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4853 return status;
4854 }
4855
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 nfs4_lock_state();
4857
4858 if (lock->lk_is_new) {
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004859 struct nfs4_ol_stateid *open_stp = NULL;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004860
4861 if (nfsd4_has_session(cstate))
4862 /* See rfc 5661 18.10.3: given clientid is ignored: */
4863 memcpy(&lock->v.new.clientid,
4864 &cstate->session->se_client->cl_clientid,
4865 sizeof(clientid_t));
4866
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004868 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004872 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 lock->lk_new_open_seqid,
4874 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004875 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07004876 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004878 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004879 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004880 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004881 &lock->v.new.clientid))
4882 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004883 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4884 &lock_stp, &new_state);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004885 } else
Benny Halevydd453df2009-04-03 08:28:41 +03004886 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004887 lock->lk_old_lock_seqid,
4888 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004889 NFS4_LOCK_STID, &lock_stp, nn);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004890 if (status)
4891 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004892 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004894 lkflg = setlkflg(lock->lk_type);
4895 status = nfs4_check_openmode(lock_stp, lkflg);
4896 if (status)
4897 goto out;
4898
NeilBrown0dd395d2005-07-07 17:59:15 -07004899 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004900 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004901 goto out;
4902 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004903 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004904 goto out;
4905
Jeff Layton21179d82012-08-21 08:03:32 -04004906 file_lock = locks_alloc_lock();
4907 if (!file_lock) {
4908 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4909 status = nfserr_jukebox;
4910 goto out;
4911 }
4912
Trond Myklebust11b91642014-07-29 21:34:08 -04004913 fp = lock_stp->st_stid.sc_file;
Jeff Layton21179d82012-08-21 08:03:32 -04004914 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 switch (lock->lk_type) {
4916 case NFS4_READ_LT:
4917 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04004918 spin_lock(&fp->fi_lock);
4919 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004920 if (filp)
4921 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04004922 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04004923 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004924 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 case NFS4_WRITE_LT:
4926 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04004927 spin_lock(&fp->fi_lock);
4928 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004929 if (filp)
4930 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04004931 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04004932 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004933 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 default:
4935 status = nfserr_inval;
4936 goto out;
4937 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004938 if (!filp) {
4939 status = nfserr_openmode;
4940 goto out;
4941 }
Jeff Layton21179d82012-08-21 08:03:32 -04004942 file_lock->fl_owner = (fl_owner_t)lock_sop;
4943 file_lock->fl_pid = current->tgid;
4944 file_lock->fl_file = filp;
4945 file_lock->fl_flags = FL_POSIX;
4946 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4947 file_lock->fl_start = lock->lk_offset;
4948 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4949 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950
Jeff Layton21179d82012-08-21 08:03:32 -04004951 conflock = locks_alloc_lock();
4952 if (!conflock) {
4953 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4954 status = nfserr_jukebox;
4955 goto out;
4956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
Jeff Layton21179d82012-08-21 08:03:32 -04004958 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004959 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004961 update_stateid(&lock_stp->st_stid.sc_stateid);
4962 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004964 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004965 break;
4966 case (EAGAIN): /* conflock holds conflicting lock */
4967 status = nfserr_denied;
4968 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04004969 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004970 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 case (EDEADLK):
4972 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004973 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004974 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004975 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004976 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004977 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979out:
Trond Myklebustde186432014-07-10 14:07:26 -04004980 if (filp)
4981 fput(filp);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004982 if (status && new_state)
Jeff Laytonc53530d2014-06-30 11:48:39 -04004983 release_lock_stateid(lock_stp);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004984 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004985 if (!cstate->replay_owner)
4986 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004987 if (file_lock)
4988 locks_free_lock(file_lock);
4989 if (conflock)
4990 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 return status;
4992}
4993
4994/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004995 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4996 * so we do a temporary open here just to get an open file to pass to
4997 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4998 * inode operation.)
4999 */
Al Viro04da6e92012-04-13 00:00:04 -04005000static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005001{
5002 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005003 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5004 if (!err) {
5005 err = nfserrno(vfs_test_lock(file, lock));
5006 nfsd_close(file);
5007 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005008 return err;
5009}
5010
5011/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 * LOCKT operation
5013 */
Al Virob37ad282006-10-19 23:28:59 -07005014__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005015nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5016 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017{
Jeff Layton21179d82012-08-21 08:03:32 -04005018 struct file_lock *file_lock = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005019 struct nfs4_lockowner *lo;
Al Virob37ad282006-10-19 23:28:59 -07005020 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005021 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005023 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 return nfserr_grace;
5025
5026 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5027 return nfserr_inval;
5028
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 nfs4_lock_state();
5030
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005031 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005032 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005033 if (status)
5034 goto out;
5035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005037 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039
Jeff Layton21179d82012-08-21 08:03:32 -04005040 file_lock = locks_alloc_lock();
5041 if (!file_lock) {
5042 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5043 status = nfserr_jukebox;
5044 goto out;
5045 }
5046 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 switch (lockt->lt_type) {
5048 case NFS4_READ_LT:
5049 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005050 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 break;
5052 case NFS4_WRITE_LT:
5053 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005054 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 break;
5056 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005057 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 status = nfserr_inval;
5059 goto out;
5060 }
5061
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005062 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005063 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005064 file_lock->fl_owner = (fl_owner_t)lo;
5065 file_lock->fl_pid = current->tgid;
5066 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067
Jeff Layton21179d82012-08-21 08:03:32 -04005068 file_lock->fl_start = lockt->lt_offset;
5069 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070
Jeff Layton21179d82012-08-21 08:03:32 -04005071 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072
Jeff Layton21179d82012-08-21 08:03:32 -04005073 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005074 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005075 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005076
Jeff Layton21179d82012-08-21 08:03:32 -04005077 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005079 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 }
5081out:
5082 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005083 if (file_lock)
5084 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 return status;
5086}
5087
Al Virob37ad282006-10-19 23:28:59 -07005088__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005089nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005090 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005092 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005094 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005095 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005096 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005097 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5098
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5100 (long long) locku->lu_offset,
5101 (long long) locku->lu_length);
5102
5103 if (check_lock_length(locku->lu_offset, locku->lu_length))
5104 return nfserr_inval;
5105
5106 nfs4_lock_state();
5107
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005108 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005109 &locku->lu_stateid, NFS4_LOCK_STID,
5110 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005111 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005113 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005114 if (!filp) {
5115 status = nfserr_lock_range;
5116 goto out;
5117 }
Jeff Layton21179d82012-08-21 08:03:32 -04005118 file_lock = locks_alloc_lock();
5119 if (!file_lock) {
5120 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5121 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005122 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005123 }
5124 locks_init_lock(file_lock);
5125 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005126 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005127 file_lock->fl_pid = current->tgid;
5128 file_lock->fl_file = filp;
5129 file_lock->fl_flags = FL_POSIX;
5130 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5131 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132
Jeff Layton21179d82012-08-21 08:03:32 -04005133 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5134 locku->lu_length);
5135 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136
Jeff Layton21179d82012-08-21 08:03:32 -04005137 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005138 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005139 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 goto out_nfserr;
5141 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005142 update_stateid(&stp->st_stid.sc_stateid);
5143 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005144fput:
5145 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005147 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields71c3bcd2011-09-27 21:42:29 -04005148 if (!cstate->replay_owner)
5149 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005150 if (file_lock)
5151 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 return status;
5153
5154out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005155 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005156 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157}
5158
5159/*
5160 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005161 * true: locks held by lockowner
5162 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005164static bool
5165check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166{
5167 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005168 int status = false;
5169 struct file *filp = find_any_file(fp);
5170 struct inode *inode;
5171
5172 if (!filp) {
5173 /* Any valid lock stateid should have some sort of access */
5174 WARN_ON_ONCE(1);
5175 return status;
5176 }
5177
5178 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179
Jeff Layton1c8c6012013-06-21 08:58:15 -04005180 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005182 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005183 status = true;
5184 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005187 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005188 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 return status;
5190}
5191
Al Virob37ad282006-10-19 23:28:59 -07005192__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005193nfsd4_release_lockowner(struct svc_rqst *rqstp,
5194 struct nfsd4_compound_state *cstate,
5195 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196{
5197 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Laytonfd449072014-06-30 11:48:41 -04005198 struct nfs4_stateowner *sop = NULL, *tmp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005199 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005200 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 struct xdr_netobj *owner = &rlockowner->rl_owner;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005202 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07005203 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005204 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205
5206 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5207 clid->cl_boot, clid->cl_id);
5208
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 nfs4_lock_state();
5210
Jeff Layton4b24ca72014-06-30 11:48:44 -04005211 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005212 if (status)
5213 goto out;
5214
NeilBrown3e9e3db2005-06-23 22:04:20 -07005215 status = nfserr_locks_held;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05005216
Jeff Laytonfd449072014-06-30 11:48:41 -04005217 /* Find the matching lock stateowner */
5218 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
5219 if (tmp->so_is_open_owner)
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005220 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005221 if (same_owner_str(tmp, owner, clid)) {
5222 sop = tmp;
5223 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07005225 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005226
5227 /* No matching owner found, maybe a replay? Just declare victory... */
5228 if (!sop) {
5229 status = nfs_ok;
5230 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005232
5233 lo = lockowner(sop);
5234 /* see if there are still any locks associated with it */
5235 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
Trond Myklebust11b91642014-07-29 21:34:08 -04005236 if (check_for_locks(stp->st_stid.sc_file, lo))
Jeff Laytonfd449072014-06-30 11:48:41 -04005237 goto out;
5238 }
5239
5240 status = nfs_ok;
5241 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242out:
5243 nfs4_unlock_state();
5244 return status;
5245}
5246
5247static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005248alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249{
NeilBrowna55370a2005-06-23 22:03:52 -07005250 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251}
5252
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005253bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005254nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005255{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005256 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005257
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005258 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005259 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005260}
5261
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262/*
5263 * failure => all reset bets are off, nfserr_no_grace...
5264 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005265struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005266nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267{
5268 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005269 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270
NeilBrowna55370a2005-06-23 22:03:52 -07005271 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5272 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005273 if (crp) {
5274 strhashval = clientstr_hashval(name);
5275 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005276 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005277 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005278 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005279 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005280 }
5281 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282}
5283
Jeff Layton2a4317c2012-03-21 16:42:43 -04005284void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005285nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005286{
5287 list_del(&crp->cr_strhash);
5288 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005289 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005290}
5291
5292void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005293nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294{
5295 struct nfs4_client_reclaim *crp = NULL;
5296 int i;
5297
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005299 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5300 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005302 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 }
5304 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005305 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306}
5307
5308/*
5309 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005310struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005311nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312{
5313 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 struct nfs4_client_reclaim *crp = NULL;
5315
Jeff Layton278c9312012-11-12 15:00:52 -05005316 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317
Jeff Layton278c9312012-11-12 15:00:52 -05005318 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005319 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005320 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 return crp;
5322 }
5323 }
5324 return NULL;
5325}
5326
5327/*
5328* Called from OPEN. Look for clientid in reclaim list.
5329*/
Al Virob37ad282006-10-19 23:28:59 -07005330__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005331nfs4_check_open_reclaim(clientid_t *clid,
5332 struct nfsd4_compound_state *cstate,
5333 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005335 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005336
5337 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005338 status = lookup_clientid(clid, cstate, nn);
5339 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005340 return nfserr_reclaim_bad;
5341
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005342 if (nfsd4_client_record_check(cstate->clp))
5343 return nfserr_reclaim_bad;
5344
5345 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346}
5347
Bryan Schumaker65178db2011-11-01 13:35:21 -04005348#ifdef CONFIG_NFSD_FAULT_INJECTION
5349
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005350u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5351{
J. Bruce Fields221a6872013-04-01 22:23:49 -04005352 if (mark_client_expired(clp))
5353 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005354 expire_client(clp);
5355 return 1;
5356}
5357
Bryan Schumaker184c1842012-11-29 11:40:44 -05005358u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5359{
5360 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005361 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005362 printk(KERN_INFO "NFS Client: %s\n", buf);
5363 return 1;
5364}
5365
5366static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5367 const char *type)
5368{
5369 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005370 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005371 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5372}
5373
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005374static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5375 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005376{
5377 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005378 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005379 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005380 u64 count = 0;
5381
5382 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005383 list_for_each_entry_safe(stp, st_next,
5384 &oop->oo_owner.so_stateids, st_perstateowner) {
5385 list_for_each_entry_safe(lst, lst_next,
5386 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005387 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005388 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005389 if (++count == max)
5390 return count;
5391 }
5392 }
5393 }
5394
5395 return count;
5396}
5397
5398u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5399{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005400 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005401}
5402
Bryan Schumaker184c1842012-11-29 11:40:44 -05005403u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5404{
5405 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5406 nfsd_print_count(clp, count, "locked files");
5407 return count;
5408}
5409
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005410static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5411{
5412 struct nfs4_openowner *oop, *next;
5413 u64 count = 0;
5414
5415 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5416 if (func)
5417 func(oop);
5418 if (++count == max)
5419 break;
5420 }
5421
5422 return count;
5423}
5424
5425u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5426{
5427 return nfsd_foreach_client_open(clp, max, release_openowner);
5428}
5429
Bryan Schumaker184c1842012-11-29 11:40:44 -05005430u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5431{
5432 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5433 nfsd_print_count(clp, count, "open files");
5434 return count;
5435}
5436
Bryan Schumaker269de302012-11-29 11:40:42 -05005437static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5438 struct list_head *victims)
5439{
5440 struct nfs4_delegation *dp, *next;
5441 u64 count = 0;
5442
Benny Halevycdc97502014-05-30 09:09:30 -04005443 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005444 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005445 if (victims) {
5446 /*
5447 * It's not safe to mess with delegations that have a
5448 * non-zero dl_time. They might have already been broken
5449 * and could be processed by the laundromat outside of
5450 * the state_lock. Just leave them be.
5451 */
5452 if (dp->dl_time != 0)
5453 continue;
5454
Jeff Layton42690672014-07-25 07:34:20 -04005455 unhash_delegation_locked(dp);
5456 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005457 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005458 if (++count == max)
5459 break;
5460 }
5461 return count;
5462}
5463
5464u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5465{
5466 struct nfs4_delegation *dp, *next;
5467 LIST_HEAD(victims);
5468 u64 count;
5469
Benny Halevycdc97502014-05-30 09:09:30 -04005470 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005471 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005472 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005473
Jeff Layton2d4a5322014-07-25 07:34:21 -04005474 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5475 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005476 revoke_delegation(dp);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005477 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005478
5479 return count;
5480}
5481
5482u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5483{
Jeff Laytondff13992014-07-08 14:02:49 -04005484 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005485 LIST_HEAD(victims);
5486 u64 count;
5487
Benny Halevycdc97502014-05-30 09:09:30 -04005488 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005489 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005490 while (!list_empty(&victims)) {
5491 dp = list_first_entry(&victims, struct nfs4_delegation,
5492 dl_recall_lru);
5493 list_del_init(&dp->dl_recall_lru);
5494 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005495 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005496 }
Benny Halevycdc97502014-05-30 09:09:30 -04005497 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005498
5499 return count;
5500}
5501
Bryan Schumaker184c1842012-11-29 11:40:44 -05005502u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5503{
5504 u64 count = 0;
5505
Benny Halevycdc97502014-05-30 09:09:30 -04005506 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005507 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005508 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005509
5510 nfsd_print_count(clp, count, "delegations");
5511 return count;
5512}
5513
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005514u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005515{
5516 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005517 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005518 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005519
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005520 if (!nfsd_netns_ready(nn))
5521 return 0;
5522
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005523 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005524 count += func(clp, max - count);
5525 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005526 break;
5527 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005528
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005529 return count;
5530}
5531
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005532struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5533{
5534 struct nfs4_client *clp;
5535 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5536
5537 if (!nfsd_netns_ready(nn))
5538 return NULL;
5539
5540 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5541 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5542 return clp;
5543 }
5544 return NULL;
5545}
5546
Bryan Schumaker65178db2011-11-01 13:35:21 -04005547#endif /* CONFIG_NFSD_FAULT_INJECTION */
5548
Meelap Shahc2f1a552007-07-17 04:04:39 -07005549/*
5550 * Since the lifetime of a delegation isn't limited to that of an open, a
5551 * client may quite reasonably hang on to a delegation as long as it has
5552 * the inode cached. This becomes an obvious problem the first time a
5553 * client's inode cache approaches the size of the server's total memory.
5554 *
5555 * For now we avoid this problem by imposing a hard limit on the number
5556 * of delegations, which varies according to the server's memory size.
5557 */
5558static void
5559set_max_delegations(void)
5560{
5561 /*
5562 * Allow at most 4 delegations per megabyte of RAM. Quick
5563 * estimates suggest that in the worst case (where every delegation
5564 * is for a different inode), a delegation could take about 1.5K,
5565 * giving a worst case usage of about 6% of memory.
5566 */
5567 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5568}
5569
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005570static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005571{
5572 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5573 int i;
5574
5575 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5576 CLIENT_HASH_SIZE, GFP_KERNEL);
5577 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005578 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005579 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5580 CLIENT_HASH_SIZE, GFP_KERNEL);
5581 if (!nn->unconf_id_hashtbl)
5582 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005583 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5584 OWNER_HASH_SIZE, GFP_KERNEL);
5585 if (!nn->ownerstr_hashtbl)
5586 goto err_ownerstr;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005587 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5588 SESSION_HASH_SIZE, GFP_KERNEL);
5589 if (!nn->sessionid_hashtbl)
5590 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005591
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005592 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005593 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005594 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005595 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005596 for (i = 0; i < OWNER_HASH_SIZE; i++)
5597 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005598 for (i = 0; i < SESSION_HASH_SIZE; i++)
5599 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005600 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005601 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005602 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005603 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005604 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005605 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005606
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005607 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005608 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005609
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005610 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005611
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005612err_sessionid:
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03005613 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005614err_ownerstr:
5615 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005616err_unconf_id:
5617 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005618err:
5619 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005620}
5621
5622static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005623nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005624{
5625 int i;
5626 struct nfs4_client *clp = NULL;
5627 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5628
5629 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5630 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5631 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5632 destroy_client(clp);
5633 }
5634 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005635
Kinglong Mee2b905632014-03-26 22:09:30 +08005636 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5637 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5638 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5639 destroy_client(clp);
5640 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005641 }
5642
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005643 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005644 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005645 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005646 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005647 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005648}
5649
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005650int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005651nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005652{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005653 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005654 int ret;
5655
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005656 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005657 if (ret)
5658 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005659 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005660 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005661 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005662 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005663 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005664 nn->nfsd4_grace, net);
5665 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005666 return 0;
5667}
5668
5669/* initialization to perform when the nfsd service is started: */
5670
5671int
5672nfs4_state_start(void)
5673{
5674 int ret;
5675
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005676 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005677 if (ret)
5678 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005679 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005680 if (laundry_wq == NULL) {
5681 ret = -ENOMEM;
5682 goto out_recovery;
5683 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005684 ret = nfsd4_create_callback_queue();
5685 if (ret)
5686 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005687
Meelap Shahc2f1a552007-07-17 04:04:39 -07005688 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005689
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005690 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005691
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005692out_free_laundry:
5693 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005694out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005695 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696}
5697
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005698void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005699nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005703 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005705 cancel_delayed_work_sync(&nn->laundromat_work);
5706 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005707
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005708 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04005710 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005711 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005713 unhash_delegation_locked(dp);
5714 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 }
Benny Halevycdc97502014-05-30 09:09:30 -04005716 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 list_for_each_safe(pos, next, &reaplist) {
5718 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005719 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04005720 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 }
5722
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005723 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005724 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005725 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726}
5727
5728void
5729nfs4_state_shutdown(void)
5730{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005731 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005732 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005734
5735static void
5736get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5737{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005738 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5739 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005740}
5741
5742static void
5743put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5744{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005745 if (cstate->minorversion) {
5746 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5747 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5748 }
5749}
5750
5751void
5752clear_current_stateid(struct nfsd4_compound_state *cstate)
5753{
5754 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005755}
5756
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005757/*
5758 * functions to set current state id
5759 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005760void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005761nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5762{
5763 put_stateid(cstate, &odp->od_stateid);
5764}
5765
5766void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005767nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5768{
5769 put_stateid(cstate, &open->op_stateid);
5770}
5771
5772void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005773nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5774{
5775 put_stateid(cstate, &close->cl_stateid);
5776}
5777
5778void
5779nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5780{
5781 put_stateid(cstate, &lock->lk_resp_stateid);
5782}
5783
5784/*
5785 * functions to consume current state id
5786 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005787
5788void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005789nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5790{
5791 get_stateid(cstate, &odp->od_stateid);
5792}
5793
5794void
5795nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5796{
5797 get_stateid(cstate, &drp->dr_stateid);
5798}
5799
5800void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005801nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5802{
5803 get_stateid(cstate, &fsp->fr_stateid);
5804}
5805
5806void
5807nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5808{
5809 get_stateid(cstate, &setattr->sa_stateid);
5810}
5811
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005812void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005813nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5814{
5815 get_stateid(cstate, &close->cl_stateid);
5816}
5817
5818void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005819nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005820{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005821 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005822}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01005823
5824void
5825nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5826{
5827 get_stateid(cstate, &read->rd_stateid);
5828}
5829
5830void
5831nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5832{
5833 get_stateid(cstate, &write->wr_stateid);
5834}