blob: 1ddf4da0023d8f42360e55cfaef1e20ff3ec9dfe [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 *
Jeff Layton4770d722014-07-29 21:34:10 -04001692find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001693{
1694 struct nfs4_stid *s;
1695
Jeff Layton4770d722014-07-29 21:34:10 -04001696 spin_lock(&cl->cl_lock);
1697 s = find_stateid_locked(cl, t);
1698 if (s != NULL && !(typemask & s->sc_type))
1699 s = NULL;
1700 spin_unlock(&cl->cl_lock);
1701 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001702}
1703
Jeff Layton2216d442012-11-12 15:00:57 -05001704static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001705 struct svc_rqst *rqstp, nfs4_verifier *verf)
1706{
1707 struct nfs4_client *clp;
1708 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001709 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001710 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001711 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001712
1713 clp = alloc_client(name);
1714 if (clp == NULL)
1715 return NULL;
1716
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001717 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1718 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001719 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001720 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001721 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001722 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001723 }
Jeff Layton02e12152014-07-16 10:31:57 -04001724 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001725 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001726 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001727 copy_verf(clp, verf);
1728 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001729 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001730 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001731 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001732 return clp;
1733}
1734
NeilBrownfd39ca92005-06-23 22:04:03 -07001735static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001736add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1737{
1738 struct rb_node **new = &(root->rb_node), *parent = NULL;
1739 struct nfs4_client *clp;
1740
1741 while (*new) {
1742 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1743 parent = *new;
1744
1745 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1746 new = &((*new)->rb_left);
1747 else
1748 new = &((*new)->rb_right);
1749 }
1750
1751 rb_link_node(&new_clp->cl_namenode, parent, new);
1752 rb_insert_color(&new_clp->cl_namenode, root);
1753}
1754
1755static struct nfs4_client *
1756find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1757{
1758 long long cmp;
1759 struct rb_node *node = root->rb_node;
1760 struct nfs4_client *clp;
1761
1762 while (node) {
1763 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1764 cmp = compare_blob(&clp->cl_name, name);
1765 if (cmp > 0)
1766 node = node->rb_left;
1767 else if (cmp < 0)
1768 node = node->rb_right;
1769 else
1770 return clp;
1771 }
1772 return NULL;
1773}
1774
1775static void
1776add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
1778 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001779 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001781 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001782 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001784 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001785 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
1787
NeilBrownfd39ca92005-06-23 22:04:03 -07001788static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789move_to_confirmed(struct nfs4_client *clp)
1790{
1791 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001792 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001795 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001796 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001797 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001798 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 renew_client(clp);
1800}
1801
1802static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001803find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
1805 struct nfs4_client *clp;
1806 unsigned int idhashval = clientid_hashval(clid->cl_id);
1807
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001808 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001809 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001810 if ((bool)clp->cl_minorversion != sessions)
1811 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001812 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 }
1816 return NULL;
1817}
1818
1819static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001820find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1821{
1822 struct list_head *tbl = nn->conf_id_hashtbl;
1823
1824 return find_client_in_id_table(tbl, clid, sessions);
1825}
1826
1827static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001828find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001830 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001832 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001835static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001836{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001837 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001838}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001839
NeilBrown28ce6052005-06-23 22:03:56 -07001840static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001841find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001842{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001843 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001844}
1845
1846static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001847find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001848{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001849 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001850}
1851
NeilBrownfd39ca92005-06-23 22:04:03 -07001852static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001853gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001855 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001856 struct sockaddr *sa = svc_addr(rqstp);
1857 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001858 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Jeff Layton7077ecb2009-08-14 12:57:58 -04001860 /* Currently, we only support tcp and tcp6 for the callback channel */
1861 if (se->se_callback_netid_len == 3 &&
1862 !memcmp(se->se_callback_netid_val, "tcp", 3))
1863 expected_family = AF_INET;
1864 else if (se->se_callback_netid_len == 4 &&
1865 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1866 expected_family = AF_INET6;
1867 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 goto out_err;
1869
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001870 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001871 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001872 (struct sockaddr *)&conn->cb_addr,
1873 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001874
J. Bruce Fields07263f12010-05-31 19:09:40 -04001875 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001877
J. Bruce Fields07263f12010-05-31 19:09:40 -04001878 if (conn->cb_addr.ss_family == AF_INET6)
1879 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001880
J. Bruce Fields07263f12010-05-31 19:09:40 -04001881 conn->cb_prog = se->se_callback_prog;
1882 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001883 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return;
1885out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001886 conn->cb_addr.ss_family = AF_UNSPEC;
1887 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001888 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 "will not receive delegations\n",
1890 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return;
1893}
1894
Andy Adamson074fe892009-04-03 08:28:15 +03001895/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04001896 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001897 */
Trond Myklebustb6076642014-06-30 11:48:35 -04001898static void
Andy Adamson074fe892009-04-03 08:28:15 +03001899nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1900{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001901 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04001902 struct nfsd4_slot *slot = resp->cstate.slot;
1903 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001904
Andy Adamson557ce262009-08-28 08:45:04 -04001905 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001906
Andy Adamson557ce262009-08-28 08:45:04 -04001907 slot->sl_opcnt = resp->opcnt;
1908 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001909
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001910 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001911 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001912 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001913 return;
1914 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001915 base = resp->cstate.data_offset;
1916 slot->sl_datalen = buf->len - base;
1917 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04001918 WARN("%s: sessions DRC could not cache compound\n", __func__);
1919 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001920}
1921
1922/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001923 * Encode the replay sequence operation from the slot values.
1924 * If cachethis is FALSE encode the uncached rep error on the next
1925 * operation which sets resp->p and increments resp->opcnt for
1926 * nfs4svc_encode_compoundres.
1927 *
Andy Adamson074fe892009-04-03 08:28:15 +03001928 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001929static __be32
1930nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1931 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001932{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001933 struct nfsd4_op *op;
1934 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001935
Andy Adamsonabfabf82009-07-23 19:02:18 -04001936 /* Encode the replayed sequence operation */
1937 op = &args->ops[resp->opcnt - 1];
1938 nfsd4_encode_operation(resp, op);
1939
1940 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05001941 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001942 op = &args->ops[resp->opcnt++];
1943 op->status = nfserr_retry_uncached_rep;
1944 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001945 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001946 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001947}
1948
1949/*
Andy Adamson557ce262009-08-28 08:45:04 -04001950 * The sequence operation is not cached because we can use the slot and
1951 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001952 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04001953static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001954nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1955 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001956{
Andy Adamson557ce262009-08-28 08:45:04 -04001957 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001958 struct xdr_stream *xdr = &resp->xdr;
1959 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03001960 __be32 status;
1961
Andy Adamson557ce262009-08-28 08:45:04 -04001962 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001963
Andy Adamsonabfabf82009-07-23 19:02:18 -04001964 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04001965 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04001966 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001967
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001968 p = xdr_reserve_space(xdr, slot->sl_datalen);
1969 if (!p) {
1970 WARN_ON_ONCE(1);
1971 return nfserr_serverfault;
1972 }
1973 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
1974 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03001975
Andy Adamson557ce262009-08-28 08:45:04 -04001976 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001977 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001978}
1979
Andy Adamson0733d212009-04-03 08:28:01 +03001980/*
1981 * Set the exchange_id flags returned by the server.
1982 */
1983static void
1984nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1985{
1986 /* pNFS is not supported */
1987 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1988
1989 /* Referrals are supported, Migration is not. */
1990 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1991
1992 /* set the wire flags to return to client. */
1993 clid->flags = new->cl_exchange_flags;
1994}
1995
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001996static bool client_has_state(struct nfs4_client *clp)
1997{
1998 /*
1999 * Note clp->cl_openowners check isn't quite right: there's no
2000 * need to count owners without stateid's.
2001 *
2002 * Also note we should probably be using this in 4.0 case too.
2003 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002004 return !list_empty(&clp->cl_openowners)
2005 || !list_empty(&clp->cl_delegations)
2006 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002007}
2008
Al Virob37ad282006-10-19 23:28:59 -07002009__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002010nfsd4_exchange_id(struct svc_rqst *rqstp,
2011 struct nfsd4_compound_state *cstate,
2012 struct nfsd4_exchange_id *exid)
2013{
Andy Adamson0733d212009-04-03 08:28:01 +03002014 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002015 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002016 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002017 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002018 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002019 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002020 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002021
Jeff Layton363168b2009-08-14 12:57:56 -04002022 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002023 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002024 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002025 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002026 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002027
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002028 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002029 return nfserr_inval;
2030
Andy Adamson0733d212009-04-03 08:28:01 +03002031 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002032 case SP4_MACH_CRED:
2033 if (!svc_rqst_integrity_protected(rqstp))
2034 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002035 case SP4_NONE:
2036 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002037 default: /* checked by xdr code */
2038 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002039 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002040 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002041 }
2042
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002043 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03002044 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002045 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002046 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002047 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2048 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2049
J. Bruce Fields136e6582012-05-12 20:37:23 -04002050 if (update) {
2051 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002052 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002053 goto out;
2054 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002055 if (!mach_creds_match(conf, rqstp)) {
2056 status = nfserr_wrong_cred;
2057 goto out;
2058 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002059 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002060 status = nfserr_perm;
2061 goto out;
2062 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002063 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002064 status = nfserr_not_same;
2065 goto out;
2066 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002067 /* case 6 */
2068 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2069 new = conf;
2070 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002071 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002072 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002073 if (client_has_state(conf)) {
2074 status = nfserr_clid_inuse;
2075 goto out;
2076 }
Andy Adamson0733d212009-04-03 08:28:01 +03002077 expire_client(conf);
2078 goto out_new;
2079 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002080 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002081 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002082 new = conf;
2083 goto out_copy;
2084 }
2085 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04002086 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002087 }
2088
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002089 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002090 status = nfserr_noent;
2091 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002092 }
2093
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002094 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002095 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03002096 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002097
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002098 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002099out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05002100 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03002101 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04002102 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03002103 goto out;
2104 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002105 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002106 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002107
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002108 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002109 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03002110out_copy:
2111 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2112 exid->clientid.cl_id = new->cl_clientid.cl_id;
2113
J. Bruce Fields778df3f2012-05-25 21:40:23 -04002114 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03002115 nfsd4_set_ex_flags(new, exid);
2116
2117 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04002118 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002119 status = nfs_ok;
2120
2121out:
2122 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03002123 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002124}
2125
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002126static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002127check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002128{
Andy Adamson88e588d2009-07-23 19:02:15 -04002129 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2130 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002131
2132 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002133 if (slot_inuse) {
2134 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002135 return nfserr_jukebox;
2136 else
2137 return nfserr_seq_misordered;
2138 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002139 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002140 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002141 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002142 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002143 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002144 return nfserr_seq_misordered;
2145}
2146
Andy Adamson49557cc2009-07-23 19:02:16 -04002147/*
2148 * Cache the create session result into the create session single DRC
2149 * slot cache by saving the xdr structure. sl_seqid has been set.
2150 * Do this for solo or embedded create session operations.
2151 */
2152static void
2153nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002154 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002155{
2156 slot->sl_status = nfserr;
2157 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2158}
2159
2160static __be32
2161nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2162 struct nfsd4_clid_slot *slot)
2163{
2164 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2165 return slot->sl_status;
2166}
2167
Mi Jinlong1b74c252011-07-14 14:50:17 +08002168#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2169 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2170 1 + /* MIN tag is length with zero, only length */ \
2171 3 + /* version, opcount, opcode */ \
2172 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2173 /* seqid, slotID, slotID, cache */ \
2174 4 ) * sizeof(__be32))
2175
2176#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2177 2 + /* verifier: AUTH_NULL, length 0 */\
2178 1 + /* status */ \
2179 1 + /* MIN tag is length with zero, only length */ \
2180 3 + /* opcount, opcode, opstatus*/ \
2181 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2182 /* seqid, slotID, slotID, slotID, status */ \
2183 5 ) * sizeof(__be32))
2184
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002185static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002186{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002187 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2188
J. Bruce Fields373cd402013-04-08 15:42:12 -04002189 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2190 return nfserr_toosmall;
2191 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2192 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002193 ca->headerpadsz = 0;
2194 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2195 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2196 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2197 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2198 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2199 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2200 /*
2201 * Note decreasing slot size below client's request may make it
2202 * difficult for client to function correctly, whereas
2203 * decreasing the number of slots will (just?) affect
2204 * performance. When short on memory we therefore prefer to
2205 * decrease number of slots instead of their size. Clients that
2206 * request larger slots than they need will get poor results:
2207 */
2208 ca->maxreqs = nfsd4_get_drc_mem(ca);
2209 if (!ca->maxreqs)
2210 return nfserr_jukebox;
2211
J. Bruce Fields373cd402013-04-08 15:42:12 -04002212 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002213}
2214
Kinglong Mee8a891632013-12-23 18:11:02 +08002215#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2216 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2217#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2218 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2219
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002220static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2221{
2222 ca->headerpadsz = 0;
2223
2224 /*
2225 * These RPC_MAX_HEADER macros are overkill, especially since we
2226 * don't even do gss on the backchannel yet. But this is still
2227 * less than 1k. Tighten up this estimate in the unlikely event
2228 * it turns out to be a problem for some client:
2229 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002230 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002231 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002232 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002233 return nfserr_toosmall;
2234 ca->maxresp_cached = 0;
2235 if (ca->maxops < 2)
2236 return nfserr_toosmall;
2237
2238 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002239}
2240
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002241static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2242{
2243 switch (cbs->flavor) {
2244 case RPC_AUTH_NULL:
2245 case RPC_AUTH_UNIX:
2246 return nfs_ok;
2247 default:
2248 /*
2249 * GSS case: the spec doesn't allow us to return this
2250 * error. But it also doesn't allow us not to support
2251 * GSS.
2252 * I'd rather this fail hard than return some error the
2253 * client might think it can already handle:
2254 */
2255 return nfserr_encr_alg_unsupp;
2256 }
2257}
2258
Andy Adamson069b6ad2009-04-03 08:27:58 +03002259__be32
2260nfsd4_create_session(struct svc_rqst *rqstp,
2261 struct nfsd4_compound_state *cstate,
2262 struct nfsd4_create_session *cr_ses)
2263{
Jeff Layton363168b2009-08-14 12:57:56 -04002264 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002265 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002266 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002267 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002268 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002269 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002270 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002271
Mi Jinlonga62573d2011-03-23 17:57:07 +08002272 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2273 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002274 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2275 if (status)
2276 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002277 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002278 if (status)
2279 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002280 status = check_backchannel_attrs(&cr_ses->back_channel);
2281 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002282 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002283 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002284 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002285 if (!new)
2286 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002287 conn = alloc_conn_from_crses(rqstp, cr_ses);
2288 if (!conn)
2289 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002290
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002291 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002292 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002293 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002294 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002295
2296 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002297 status = nfserr_wrong_cred;
2298 if (!mach_creds_match(conf, rqstp))
2299 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002300 cs_slot = &conf->cl_cs_slot;
2301 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002302 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002303 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002304 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002305 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002306 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002307 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002308 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002309 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002310 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002311 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002312 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002313 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002314 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002315 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002316 status = nfserr_wrong_cred;
2317 if (!mach_creds_match(unconf, rqstp))
2318 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002319 cs_slot = &unconf->cl_cs_slot;
2320 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002321 if (status) {
2322 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002323 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002324 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002325 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002326 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002327 if (old) {
2328 status = mark_client_expired(old);
2329 if (status)
2330 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002331 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002332 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002333 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002334 conf = unconf;
2335 } else {
2336 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002337 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002338 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002339 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002340 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002341 * We do not support RDMA or persistent sessions
2342 */
2343 cr_ses->flags &= ~SESSION4_PERSIST;
2344 cr_ses->flags &= ~SESSION4_RDMA;
2345
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002346 init_session(rqstp, new, conf, cr_ses);
2347 nfsd4_init_conn(rqstp, conn, new);
2348
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002349 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002350 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002351 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002352 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002353
Andy Adamson49557cc2009-07-23 19:02:16 -04002354 /* cache solo and embedded create sessions under the state lock */
2355 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002356 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002357 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002358out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08002359 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002360 free_conn(conn);
2361out_free_session:
2362 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002363out_release_drc_mem:
2364 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002365 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002366}
2367
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002368static __be32 nfsd4_map_bcts_dir(u32 *dir)
2369{
2370 switch (*dir) {
2371 case NFS4_CDFC4_FORE:
2372 case NFS4_CDFC4_BACK:
2373 return nfs_ok;
2374 case NFS4_CDFC4_FORE_OR_BOTH:
2375 case NFS4_CDFC4_BACK_OR_BOTH:
2376 *dir = NFS4_CDFC4_BOTH;
2377 return nfs_ok;
2378 };
2379 return nfserr_inval;
2380}
2381
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002382__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2383{
2384 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002385 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002386 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002387
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002388 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2389 if (status)
2390 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002391 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002392 session->se_cb_prog = bc->bc_cb_program;
2393 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002394 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002395
2396 nfsd4_probe_callback(session->se_client);
2397
2398 return nfs_ok;
2399}
2400
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002401__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2402 struct nfsd4_compound_state *cstate,
2403 struct nfsd4_bind_conn_to_session *bcts)
2404{
2405 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002406 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002407 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002408 struct net *net = SVC_NET(rqstp);
2409 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002410
2411 if (!nfsd4_last_compound_op(rqstp))
2412 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002413 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002414 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002415 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002416 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002417 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002418 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002419 status = nfserr_wrong_cred;
2420 if (!mach_creds_match(session->se_client, rqstp))
2421 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002422 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002423 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002424 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002425 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002426 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002427 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002428 goto out;
2429 nfsd4_init_conn(rqstp, conn, session);
2430 status = nfs_ok;
2431out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002432 nfsd4_put_session(session);
2433out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002434 nfs4_unlock_state();
2435 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002436}
2437
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002438static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2439{
2440 if (!session)
2441 return 0;
2442 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2443}
2444
Andy Adamson069b6ad2009-04-03 08:27:58 +03002445__be32
2446nfsd4_destroy_session(struct svc_rqst *r,
2447 struct nfsd4_compound_state *cstate,
2448 struct nfsd4_destroy_session *sessionid)
2449{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002450 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002451 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002452 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002453 struct net *net = SVC_NET(r);
2454 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002455
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002456 nfs4_lock_state();
2457 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002458 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002459 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002460 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002461 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002462 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002463 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002464 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002465 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002466 if (!ses)
2467 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002468 status = nfserr_wrong_cred;
2469 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002470 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002471 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002472 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002473 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002474 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002475 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002476
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002477 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002478
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002479 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002480 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002481out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002482 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002483out_client_lock:
2484 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002485out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002486 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002487 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002488}
2489
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002490static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002491{
2492 struct nfsd4_conn *c;
2493
2494 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002495 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002496 return c;
2497 }
2498 }
2499 return NULL;
2500}
2501
J. Bruce Fields57266a62013-04-13 14:27:29 -04002502static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002503{
2504 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002505 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002506 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002507 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002508
2509 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002510 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002511 if (c)
2512 goto out_free;
2513 status = nfserr_conn_not_bound_to_session;
2514 if (clp->cl_mach_cred)
2515 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002516 __nfsd4_hash_conn(new, ses);
2517 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002518 ret = nfsd4_register_conn(new);
2519 if (ret)
2520 /* oops; xprt is already down: */
2521 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002522 return nfs_ok;
2523out_free:
2524 spin_unlock(&clp->cl_lock);
2525 free_conn(new);
2526 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002527}
2528
Mi Jinlong868b89c2011-04-27 09:09:58 +08002529static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2530{
2531 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2532
2533 return args->opcnt > session->se_fchannel.maxops;
2534}
2535
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002536static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2537 struct nfsd4_session *session)
2538{
2539 struct xdr_buf *xb = &rqstp->rq_arg;
2540
2541 return xb->len > session->se_fchannel.maxreq_sz;
2542}
2543
Andy Adamson069b6ad2009-04-03 08:27:58 +03002544__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002545nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002546 struct nfsd4_compound_state *cstate,
2547 struct nfsd4_sequence *seq)
2548{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002549 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002550 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002551 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002552 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002553 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002554 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002555 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002556 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002557 struct net *net = SVC_NET(rqstp);
2558 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002559
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002560 if (resp->opcnt != 1)
2561 return nfserr_sequence_pos;
2562
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002563 /*
2564 * Will be either used or freed by nfsd4_sequence_check_conn
2565 * below.
2566 */
2567 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2568 if (!conn)
2569 return nfserr_jukebox;
2570
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002571 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002572 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002573 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002574 goto out_no_session;
2575 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002576
Mi Jinlong868b89c2011-04-27 09:09:58 +08002577 status = nfserr_too_many_ops;
2578 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002579 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002580
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002581 status = nfserr_req_too_big;
2582 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002583 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002584
Benny Halevyb85d4c02009-04-03 08:28:08 +03002585 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002586 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002587 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002588
Andy Adamson557ce262009-08-28 08:45:04 -04002589 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002590 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2591
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002592 /* We do not negotiate the number of slots yet, so set the
2593 * maxslots to the session maxreqs which is used to encode
2594 * sr_highest_slotid and the sr_target_slot id to maxslots */
2595 seq->maxslots = session->se_fchannel.maxreqs;
2596
J. Bruce Fields73e79482012-02-13 16:39:00 -05002597 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2598 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002599 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002600 status = nfserr_seq_misordered;
2601 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002602 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002603 cstate->slot = slot;
2604 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002605 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002606 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002607 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002608 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002609 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002610 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002611 }
2612 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002613 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002614
J. Bruce Fields57266a62013-04-13 14:27:29 -04002615 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002616 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002617 if (status)
2618 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002619
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002620 buflen = (seq->cachethis) ?
2621 session->se_fchannel.maxresp_cached :
2622 session->se_fchannel.maxresp_sz;
2623 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2624 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002625 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002626 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002627 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002628
2629 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002630 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002631 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002632 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002633 if (seq->cachethis)
2634 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002635 else
2636 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002637
2638 cstate->slot = slot;
2639 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002640 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002641
Benny Halevyb85d4c02009-04-03 08:28:08 +03002642out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002643 switch (clp->cl_cb_state) {
2644 case NFSD4_CB_DOWN:
2645 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2646 break;
2647 case NFSD4_CB_FAULT:
2648 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2649 break;
2650 default:
2651 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002652 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002653 if (!list_empty(&clp->cl_revoked))
2654 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002655out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002656 if (conn)
2657 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002658 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002659 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002660out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002661 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002662 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002663}
2664
Trond Myklebustb6076642014-06-30 11:48:35 -04002665void
2666nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2667{
2668 struct nfsd4_compound_state *cs = &resp->cstate;
2669
2670 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002671 if (cs->status != nfserr_replay_cache) {
2672 nfsd4_store_cache_entry(resp);
2673 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2674 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002675 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002676 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002677 } else if (cs->clp)
2678 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002679}
2680
Mi Jinlong345c2842011-10-20 17:51:39 +08002681__be32
2682nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2683{
2684 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002685 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002686 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002687
2688 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002689 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002690 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002691 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002692
2693 if (conf) {
2694 clp = conf;
2695
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002696 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002697 status = nfserr_clientid_busy;
2698 goto out;
2699 }
2700 } else if (unconf)
2701 clp = unconf;
2702 else {
2703 status = nfserr_stale_clientid;
2704 goto out;
2705 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002706 if (!mach_creds_match(clp, rqstp)) {
2707 status = nfserr_wrong_cred;
2708 goto out;
2709 }
Mi Jinlong345c2842011-10-20 17:51:39 +08002710 expire_client(clp);
2711out:
2712 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002713 return status;
2714}
2715
Andy Adamson069b6ad2009-04-03 08:27:58 +03002716__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002717nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2718{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002719 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002720
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002721 if (rc->rca_one_fs) {
2722 if (!cstate->current_fh.fh_dentry)
2723 return nfserr_nofilehandle;
2724 /*
2725 * We don't take advantage of the rca_one_fs case.
2726 * That's OK, it's optional, we can safely ignore it.
2727 */
2728 return nfs_ok;
2729 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002730
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002731 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002732 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002733 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2734 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002735 goto out;
2736
2737 status = nfserr_stale_clientid;
2738 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002739 /*
2740 * The following error isn't really legal.
2741 * But we only get here if the client just explicitly
2742 * destroyed the client. Surely it no longer cares what
2743 * error it gets back on an operation for the dead
2744 * client.
2745 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002746 goto out;
2747
2748 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002749 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002750out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002751 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002752 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002753}
2754
2755__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002756nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2757 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002759 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002761 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002762 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002763 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2764
J. Bruce Fields63db4632012-05-18 21:54:19 -04002765 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002767 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002768 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002769 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002771 if (clp_used_exchangeid(conf))
2772 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002773 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002774 char addr_str[INET6_ADDRSTRLEN];
2775 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2776 sizeof(addr_str));
2777 dprintk("NFSD: setclientid: string in use by client "
2778 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 goto out;
2780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002782 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002783 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002786 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002787 if (new == NULL)
2788 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002789 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002790 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002792 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002793 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002794 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002795 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002796 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2798 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2799 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2800 status = nfs_ok;
2801out:
2802 nfs4_unlock_state();
2803 return status;
2804}
2805
2806
Al Virob37ad282006-10-19 23:28:59 -07002807__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002808nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2809 struct nfsd4_compound_state *cstate,
2810 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811{
NeilBrown21ab45a2005-06-23 22:04:14 -07002812 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2814 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002815 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002816 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002818 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002821
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002822 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002823 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002824 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002825 * We try hard to give out unique clientid's, so if we get an
2826 * attempt to confirm the same clientid with a different cred,
2827 * there's a bug somewhere. Let's charitably assume it's our
2828 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002829 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002830 status = nfserr_serverfault;
2831 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2832 goto out;
2833 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2834 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002835 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002836 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2837 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002839 else /* case 4: client hasn't noticed we rebooted yet? */
2840 status = nfserr_stale_clientid;
2841 goto out;
2842 }
2843 status = nfs_ok;
2844 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002845 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2846 nfsd4_probe_callback(conf);
2847 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002848 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002849 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002850 if (conf) {
2851 status = mark_client_expired(conf);
2852 if (status)
2853 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002854 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002855 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002856 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002857 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 nfs4_unlock_state();
2861 return status;
2862}
2863
J. Bruce Fields32513b42011-10-13 16:00:16 -04002864static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002866 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2867}
2868
2869/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04002870static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04002871{
Trond Myklebustca943212014-07-23 16:17:39 -04002872 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
Trond Myklebust950e0112014-06-30 11:48:31 -04002874 lockdep_assert_held(&state_lock);
2875
J. Bruce Fields32513b42011-10-13 16:00:16 -04002876 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04002877 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002878 INIT_LIST_HEAD(&fp->fi_stateids);
2879 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04002880 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002881 fp->fi_had_conflict = false;
2882 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04002883 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002884 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2885 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04002886 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887}
2888
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002889void
NeilBrowne60d4392005-06-23 22:03:01 -07002890nfsd4_free_slabs(void)
2891{
Christoph Hellwigabf11352014-05-21 07:43:03 -07002892 kmem_cache_destroy(openowner_slab);
2893 kmem_cache_destroy(lockowner_slab);
2894 kmem_cache_destroy(file_slab);
2895 kmem_cache_destroy(stateid_slab);
2896 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002897}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
Bryan Schumaker72083392011-11-01 15:24:59 -04002899int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900nfsd4_init_slabs(void)
2901{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002902 openowner_slab = kmem_cache_create("nfsd4_openowners",
2903 sizeof(struct nfs4_openowner), 0, 0, NULL);
2904 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002905 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002906 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002907 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002908 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002909 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07002910 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002911 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002912 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002913 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07002914 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002915 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002916 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002917 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002918 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002919 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002920 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002921 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07002923
2924out_free_stateid_slab:
2925 kmem_cache_destroy(stateid_slab);
2926out_free_file_slab:
2927 kmem_cache_destroy(file_slab);
2928out_free_lockowner_slab:
2929 kmem_cache_destroy(lockowner_slab);
2930out_free_openowner_slab:
2931 kmem_cache_destroy(openowner_slab);
2932out:
NeilBrowne60d4392005-06-23 22:03:01 -07002933 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2934 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935}
2936
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002937static void init_nfs4_replay(struct nfs4_replay *rp)
2938{
2939 rp->rp_status = nfserr_serverfault;
2940 rp->rp_buflen = 0;
2941 rp->rp_buf = rp->rp_ibuf;
2942}
2943
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002944static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945{
2946 struct nfs4_stateowner *sop;
2947
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002948 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002949 if (!sop)
2950 return NULL;
2951
2952 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2953 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002954 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002955 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002957 sop->so_owner.len = owner->len;
2958
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002959 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002960 sop->so_client = clp;
2961 init_nfs4_replay(&sop->so_replay);
2962 return sop;
2963}
2964
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002965static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002966{
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002967 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2968
2969 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002970 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971}
2972
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002973static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04002974alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002975 struct nfsd4_compound_state *cstate)
2976{
Trond Myklebust13d6f662014-06-30 11:48:45 -04002977 struct nfs4_client *clp = cstate->clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002978 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002980 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2981 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002983 oo->oo_owner.so_is_open_owner = 1;
2984 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04002985 oo->oo_flags = NFS4_OO_NEW;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002986 if (nfsd4_has_session(cstate))
2987 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002988 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002989 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002990 INIT_LIST_HEAD(&oo->oo_close_lru);
2991 hash_openowner(oo, clp, strhashval);
2992 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993}
2994
J. Bruce Fields996e0932011-10-17 11:14:48 -04002995static 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 -04002996 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002998 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04002999 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003000 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07003001 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003002 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 stp->st_access_bmap = 0;
3004 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003005 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003006 spin_lock(&oo->oo_owner.so_client->cl_lock);
3007 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003008 spin_lock(&fp->fi_lock);
3009 list_add(&stp->st_perfile, &fp->fi_stateids);
3010 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003011 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012}
3013
3014static void
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003015move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016{
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003017 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3018
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003019 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003021 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003022 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023}
3024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04003026same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
3027 clientid_t *clid)
3028{
3029 return (sop->so_owner.len == owner->len) &&
3030 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
3031 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032}
3033
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003034static struct nfs4_openowner *
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003035find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
3036 bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037{
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003038 struct nfs4_stateowner *so;
3039 struct nfs4_openowner *oo;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003040 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003042 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003043 if (!so->so_is_open_owner)
3044 continue;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003045 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
3046 oo = openowner(so);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003047 clp = oo->oo_owner.so_client;
3048 if ((bool)clp->cl_minorversion != sessions)
3049 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003050 renew_client(oo->oo_owner.so_client);
3051 return oo;
3052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 }
3054 return NULL;
3055}
3056
3057/* search file_hashtbl[] for file */
3058static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003059find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060{
Trond Myklebustca943212014-07-23 16:17:39 -04003061 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 struct nfs4_file *fp;
3063
Trond Myklebust950e0112014-06-30 11:48:31 -04003064 lockdep_assert_held(&state_lock);
3065
Jeff Layton89876f82013-04-02 09:01:59 -04003066 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003067 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003068 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 }
3072 return NULL;
3073}
3074
Trond Myklebust950e0112014-06-30 11:48:31 -04003075static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003076find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003077{
3078 struct nfs4_file *fp;
3079
3080 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003081 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003082 spin_unlock(&state_lock);
3083 return fp;
3084}
3085
3086static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003087find_or_add_file(struct nfs4_file *new, 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 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003094 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003095 fp = new;
3096 }
3097 spin_unlock(&state_lock);
3098
3099 return fp;
3100}
3101
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003102/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 * Called to check deny when READ with all zero stateid or
3104 * WRITE with all zero or all one stateid
3105 */
Al Virob37ad282006-10-19 23:28:59 -07003106static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3108{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003110 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
Trond Myklebustca943212014-07-23 16:17:39 -04003112 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003113 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003114 return ret;
3115 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003116 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003117 if (fp->fi_share_deny & deny_type)
3118 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003119 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003120 put_nfs4_file(fp);
3121 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122}
3123
Jeff Layton02e12152014-07-16 10:31:57 -04003124void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125{
Trond Myklebust11b91642014-07-29 21:34:08 -04003126 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3127 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003128
Trond Myklebust11b91642014-07-29 21:34:08 -04003129 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003130
Jeff Layton02e12152014-07-16 10:31:57 -04003131 /*
3132 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003133 * already holding inode->i_lock.
3134 *
Jeff Laytondff13992014-07-08 14:02:49 -04003135 * If the dl_time != 0, then we know that it has already been
3136 * queued for a lease break. Don't queue it again.
3137 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003138 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003139 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003140 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003141 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003142 }
Jeff Layton02e12152014-07-16 10:31:57 -04003143 spin_unlock(&state_lock);
3144}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
Jeff Layton02e12152014-07-16 10:31:57 -04003146static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3147{
3148 /*
3149 * We're assuming the state code never drops its reference
3150 * without first removing the lease. Since we're in this lease
3151 * callback (and since the lease code is serialized by the kernel
3152 * lock) we know the server hasn't removed the lease yet, we know
3153 * it's safe to take a reference.
3154 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003155 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003156 nfsd4_cb_recall(dp);
3157}
3158
Jeff Layton1c8c6012013-06-21 08:58:15 -04003159/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003160static void nfsd_break_deleg_cb(struct file_lock *fl)
3161{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003162 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3163 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003164
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003165 if (!fp) {
3166 WARN(1, "(%p)->fl_owner NULL\n", fl);
3167 return;
3168 }
3169 if (fp->fi_had_conflict) {
3170 WARN(1, "duplicate break on %p\n", fp);
3171 return;
3172 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003173 /*
3174 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003175 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003176 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003177 */
3178 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Jeff Layton02e12152014-07-16 10:31:57 -04003180 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003181 fp->fi_had_conflict = true;
3182 /*
3183 * If there are no delegations on the list, then we can't count on this
3184 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3185 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3186 * true should keep any new delegations from being hashed.
3187 */
3188 if (list_empty(&fp->fi_delegations))
3189 fl->fl_break_time = jiffies;
3190 else
3191 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3192 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003193 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194}
3195
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196static
3197int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3198{
3199 if (arg & F_UNLCK)
3200 return lease_modify(onlist, arg);
3201 else
3202 return -EAGAIN;
3203}
3204
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003205static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003206 .lm_break = nfsd_break_deleg_cb,
3207 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208};
3209
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003210static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3211{
3212 if (nfsd4_has_session(cstate))
3213 return nfs_ok;
3214 if (seqid == so->so_seqid - 1)
3215 return nfserr_replay_me;
3216 if (seqid == so->so_seqid)
3217 return nfs_ok;
3218 return nfserr_bad_seqid;
3219}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
Jeff Layton4b24ca72014-06-30 11:48:44 -04003221static __be32 lookup_clientid(clientid_t *clid,
3222 struct nfsd4_compound_state *cstate,
3223 struct nfsd_net *nn)
3224{
3225 struct nfs4_client *found;
3226
3227 if (cstate->clp) {
3228 found = cstate->clp;
3229 if (!same_clid(&found->cl_clientid, clid))
3230 return nfserr_stale_clientid;
3231 return nfs_ok;
3232 }
3233
3234 if (STALE_CLIENTID(clid, nn))
3235 return nfserr_stale_clientid;
3236
3237 /*
3238 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3239 * cached already then we know this is for is for v4.0 and "sessions"
3240 * will be false.
3241 */
3242 WARN_ON_ONCE(cstate->session);
3243 found = find_confirmed_client(clid, false, nn);
3244 if (!found)
3245 return nfserr_expired;
3246
3247 /* Cache the nfs4_client in cstate! */
3248 cstate->clp = found;
3249 atomic_inc(&found->cl_refcount);
3250 return nfs_ok;
3251}
3252
Al Virob37ad282006-10-19 23:28:59 -07003253__be32
Andy Adamson66689582009-04-03 08:28:45 +03003254nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003255 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 clientid_t *clientid = &open->op_clientid;
3258 struct nfs4_client *clp = NULL;
3259 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003260 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003261 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003263 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003265 /*
3266 * In case we need it later, after we've already created the
3267 * file and don't want to risk a further failure:
3268 */
3269 open->op_file = nfsd4_alloc_file();
3270 if (open->op_file == NULL)
3271 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
Trond Myklebust2d91e892014-06-30 11:48:46 -04003273 status = lookup_clientid(clientid, cstate, nn);
3274 if (status)
3275 return status;
3276 clp = cstate->clp;
3277
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003278 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003279 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003280 open->op_openowner = oo;
3281 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003282 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003284 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003285 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003286 release_openowner(oo);
3287 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003288 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003289 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003290 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3291 if (status)
3292 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003293 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003294new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003295 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003296 if (oo == NULL)
3297 return nfserr_jukebox;
3298 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003299alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003300 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003301 if (!open->op_stp)
3302 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003303 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304}
3305
Al Virob37ad282006-10-19 23:28:59 -07003306static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003307nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3308{
3309 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3310 return nfserr_openmode;
3311 else
3312 return nfs_ok;
3313}
3314
Daniel Mackc47d8322011-05-16 16:38:14 +02003315static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003316{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003317 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3318}
3319
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003320static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003321{
3322 struct nfs4_stid *ret;
3323
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003324 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003325 if (!ret)
3326 return NULL;
3327 return delegstateid(ret);
3328}
3329
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003330static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3331{
3332 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3333 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3334}
3335
Al Virob37ad282006-10-19 23:28:59 -07003336static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003337nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003338 struct nfs4_delegation **dp)
3339{
3340 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003341 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07003342
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003343 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07003344 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003345 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003346 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07003347 status = nfs4_check_delegmode(*dp, flags);
3348 if (status)
3349 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003350out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003351 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003352 return nfs_ok;
3353 if (status)
3354 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003355 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003356 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003357}
3358
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003359static struct nfs4_ol_stateid *
3360nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003362 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003363 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364
Trond Myklebust1d31a252014-07-10 14:07:25 -04003365 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003366 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 /* ignore lock owners */
3368 if (local->st_stateowner->so_is_open_owner == 0)
3369 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003370 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003371 ret = local;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003372 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003375 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003376 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377}
3378
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003379static inline int nfs4_access_to_access(u32 nfs4_access)
3380{
3381 int flags = 0;
3382
3383 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3384 flags |= NFSD_MAY_READ;
3385 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3386 flags |= NFSD_MAY_WRITE;
3387 return flags;
3388}
3389
Al Virob37ad282006-10-19 23:28:59 -07003390static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3392 struct nfsd4_open *open)
3393{
3394 struct iattr iattr = {
3395 .ia_valid = ATTR_SIZE,
3396 .ia_size = 0,
3397 };
3398 if (!open->op_truncate)
3399 return 0;
3400 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003401 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3403}
3404
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003405static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003406 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3407 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003408{
Trond Myklebustde186432014-07-10 14:07:26 -04003409 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003410 __be32 status;
3411 int oflag = nfs4_access_to_omode(open->op_share_access);
3412 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003413 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003414
Trond Myklebustde186432014-07-10 14:07:26 -04003415 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003416
3417 /*
3418 * Are we trying to set a deny mode that would conflict with
3419 * current access?
3420 */
3421 status = nfs4_file_check_deny(fp, open->op_share_deny);
3422 if (status != nfs_ok) {
3423 spin_unlock(&fp->fi_lock);
3424 goto out;
3425 }
3426
3427 /* set access to the file */
3428 status = nfs4_file_get_access(fp, open->op_share_access);
3429 if (status != nfs_ok) {
3430 spin_unlock(&fp->fi_lock);
3431 goto out;
3432 }
3433
3434 /* Set access bits in stateid */
3435 old_access_bmap = stp->st_access_bmap;
3436 set_access(open->op_share_access, stp);
3437
3438 /* Set new deny mask */
3439 old_deny_bmap = stp->st_deny_bmap;
3440 set_deny(open->op_share_deny, stp);
3441 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3442
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003443 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003444 spin_unlock(&fp->fi_lock);
3445 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003446 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003447 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003448 spin_lock(&fp->fi_lock);
3449 if (!fp->fi_fds[oflag]) {
3450 fp->fi_fds[oflag] = filp;
3451 filp = NULL;
3452 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003453 }
Trond Myklebustde186432014-07-10 14:07:26 -04003454 spin_unlock(&fp->fi_lock);
3455 if (filp)
3456 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003457
3458 status = nfsd4_truncate(rqstp, cur_fh, open);
3459 if (status)
3460 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003461out:
3462 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003463out_put_access:
3464 stp->st_access_bmap = old_access_bmap;
3465 nfs4_file_put_access(fp, open->op_share_access);
3466 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3467 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003468}
3469
Al Virob37ad282006-10-19 23:28:59 -07003470static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003471nfs4_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 -07003472{
Al Virob37ad282006-10-19 23:28:59 -07003473 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003474 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003476 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003477 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003478
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003479 /* test and set deny mode */
3480 spin_lock(&fp->fi_lock);
3481 status = nfs4_file_check_deny(fp, open->op_share_deny);
3482 if (status == nfs_ok) {
3483 old_deny_bmap = stp->st_deny_bmap;
3484 set_deny(open->op_share_deny, stp);
3485 fp->fi_share_deny |=
3486 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3487 }
3488 spin_unlock(&fp->fi_lock);
3489
3490 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003493 status = nfsd4_truncate(rqstp, cur_fh, open);
3494 if (status != nfs_ok)
3495 reset_union_bmap_deny(old_deny_bmap, stp);
3496 return status;
3497}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003500nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003502 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503}
3504
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003505/* Should we give out recallable state?: */
3506static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3507{
3508 if (clp->cl_cb_state == NFSD4_CB_UP)
3509 return true;
3510 /*
3511 * In the sessions case, since we don't have to establish a
3512 * separate connection for callbacks, we assume it's OK
3513 * until we hear otherwise:
3514 */
3515 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3516}
3517
Jeff Laytond564fbe2014-07-16 10:31:58 -04003518static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003519{
3520 struct file_lock *fl;
3521
3522 fl = locks_alloc_lock();
3523 if (!fl)
3524 return NULL;
3525 locks_init_lock(fl);
3526 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003527 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003528 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3529 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003530 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003531 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003532 return fl;
3533}
3534
J. Bruce Fields99c41512013-05-21 16:21:25 -04003535static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003536{
Trond Myklebust11b91642014-07-29 21:34:08 -04003537 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003538 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003539 struct file *filp;
3540 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003541
Jeff Laytond564fbe2014-07-16 10:31:58 -04003542 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003543 if (!fl)
3544 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003545 filp = find_readable_file(fp);
3546 if (!filp) {
3547 /* We should always have a readable file here */
3548 WARN_ON_ONCE(1);
3549 return -EBADF;
3550 }
3551 fl->fl_file = filp;
3552 status = vfs_setlease(filp, fl->fl_type, &fl);
3553 if (status) {
3554 locks_free_lock(fl);
3555 goto out_fput;
3556 }
Benny Halevycdc97502014-05-30 09:09:30 -04003557 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003558 spin_lock(&fp->fi_lock);
3559 /* Did the lease get broken before we took the lock? */
3560 status = -EAGAIN;
3561 if (fp->fi_had_conflict)
3562 goto out_unlock;
3563 /* Race breaker */
3564 if (fp->fi_lease) {
3565 status = 0;
3566 atomic_inc(&fp->fi_delegees);
3567 hash_delegation_locked(dp, fp);
3568 goto out_unlock;
3569 }
3570 fp->fi_lease = fl;
3571 fp->fi_deleg_file = filp;
3572 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003573 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003574 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003575 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003576 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003577out_unlock:
3578 spin_unlock(&fp->fi_lock);
3579 spin_unlock(&state_lock);
3580out_fput:
3581 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003582 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003583}
3584
Jeff Layton0b266932014-07-25 07:34:25 -04003585static struct nfs4_delegation *
3586nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3587 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003588{
Jeff Layton0b266932014-07-25 07:34:25 -04003589 int status;
3590 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003591
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003592 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003593 return ERR_PTR(-EAGAIN);
3594
3595 dp = alloc_init_deleg(clp, fh);
3596 if (!dp)
3597 return ERR_PTR(-ENOMEM);
3598
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003599 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003600 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003601 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003602 dp->dl_stid.sc_file = fp;
Jeff Layton417c6622014-07-21 09:34:57 -04003603 if (!fp->fi_lease) {
3604 spin_unlock(&fp->fi_lock);
3605 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003606 status = nfs4_setlease(dp);
3607 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003608 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003609 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003610 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003611 status = -EAGAIN;
3612 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003613 }
Benny Halevy931ee562014-05-30 09:09:27 -04003614 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003615 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003616out_unlock:
3617 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003618 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003619out:
3620 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003621 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003622 return ERR_PTR(status);
3623 }
3624 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003625}
3626
Benny Halevy4aa89132012-02-21 14:16:44 -08003627static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3628{
3629 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3630 if (status == -EAGAIN)
3631 open->op_why_no_deleg = WND4_CONTENTION;
3632 else {
3633 open->op_why_no_deleg = WND4_RESOURCE;
3634 switch (open->op_deleg_want) {
3635 case NFS4_SHARE_WANT_READ_DELEG:
3636 case NFS4_SHARE_WANT_WRITE_DELEG:
3637 case NFS4_SHARE_WANT_ANY_DELEG:
3638 break;
3639 case NFS4_SHARE_WANT_CANCEL:
3640 open->op_why_no_deleg = WND4_CANCELLED;
3641 break;
3642 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003643 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003644 }
3645 }
3646}
3647
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648/*
3649 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003650 *
3651 * Note we don't support write delegations, and won't until the vfs has
3652 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 */
3654static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003655nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3656 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657{
3658 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003659 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3660 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003661 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003662 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003664 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003665 open->op_recall = 0;
3666 switch (open->op_claim_type) {
3667 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003668 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003669 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003670 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3671 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003672 break;
3673 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003674 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003675 /*
3676 * Let's not give out any delegations till everyone's
3677 * had the chance to reclaim theirs....
3678 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003679 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003680 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003681 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003682 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003683 /*
3684 * Also, if the file was opened for write or
3685 * create, there's a good chance the client's
3686 * about to write to it, resulting in an
3687 * immediate recall (since we don't support
3688 * write delegations):
3689 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003690 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003691 goto out_no_deleg;
3692 if (open->op_create == NFS4_OPEN_CREATE)
3693 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003694 break;
3695 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003696 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003697 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003698 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003699 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003700 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003702 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003704 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003705 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003706 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003707 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003708out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003709 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3710 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003711 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003712 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003713 open->op_recall = 1;
3714 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003715
3716 /* 4.1 client asking for a delegation? */
3717 if (open->op_deleg_want)
3718 nfsd4_open_deleg_none_ext(open, status);
3719 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720}
3721
Benny Halevye27f49c2012-02-21 14:16:54 -08003722static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3723 struct nfs4_delegation *dp)
3724{
3725 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3726 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3727 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3728 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3729 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3730 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3731 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3732 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3733 }
3734 /* Otherwise the client must be confused wanting a delegation
3735 * it already has, therefore we don't return
3736 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3737 */
3738}
3739
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740/*
3741 * called with nfs4_lock_state() held.
3742 */
Al Virob37ad282006-10-19 23:28:59 -07003743__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3745{
Andy Adamson66689582009-04-03 08:28:45 +03003746 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003747 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003749 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003750 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003751 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 /*
3754 * Lookup file; if found, lookup stateid and check open request,
3755 * and check for delegations in the process of being recalled.
3756 * If not found, create the nfs4_file struct
3757 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003758 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04003759 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04003760 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003761 if (status)
3762 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003763 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04003765 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003766 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003767 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003768 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003769 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 }
3771
3772 /*
3773 * OPEN the file, or upgrade an existing OPEN.
3774 * If truncate fails, the OPEN fails.
3775 */
3776 if (stp) {
3777 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003778 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 if (status)
3780 goto out;
3781 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003782 stp = open->op_stp;
3783 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003784 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003785 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3786 if (status) {
3787 release_open_stateid(stp);
3788 goto out;
3789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003791 update_stateid(&stp->st_stid.sc_stateid);
3792 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793
Benny Halevyd24433c2012-02-16 20:57:17 +02003794 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02003795 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3796 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3797 open->op_why_no_deleg = WND4_NOT_WANTED;
3798 goto nodeleg;
3799 }
3800 }
3801
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 /*
3803 * Attempt to hand out a delegation. No error return, because the
3804 * OPEN succeeds even if we fail.
3805 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003806 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003807nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 status = nfs_ok;
3809
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003810 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003811 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003813 /* 4.1 client trying to upgrade/downgrade delegation? */
3814 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003815 open->op_deleg_want)
3816 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003817
NeilBrown13cd2182005-06-23 22:03:10 -07003818 if (fp)
3819 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003820 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003821 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 /*
3823 * To finish the open response, we just need to set the rflags.
3824 */
3825 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003826 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003827 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3829
3830 return status;
3831}
3832
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003833void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3834{
3835 if (open->op_openowner) {
3836 struct nfs4_openowner *oo = open->op_openowner;
3837
3838 if (!list_empty(&oo->oo_owner.so_stateids))
3839 list_del_init(&oo->oo_close_lru);
3840 if (oo->oo_flags & NFS4_OO_NEW) {
3841 if (status) {
3842 release_openowner(oo);
3843 open->op_openowner = NULL;
3844 } else
3845 oo->oo_flags &= ~NFS4_OO_NEW;
3846 }
3847 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003848 if (open->op_file)
3849 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003850 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04003851 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003852}
3853
Al Virob37ad282006-10-19 23:28:59 -07003854__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003855nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3856 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857{
3858 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003859 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003860 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861
3862 nfs4_lock_state();
3863 dprintk("process_renew(%08x/%08x): starting\n",
3864 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003865 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003866 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003868 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003870 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003871 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 goto out;
3873 status = nfs_ok;
3874out:
3875 nfs4_unlock_state();
3876 return status;
3877}
3878
NeilBrowna76b4312005-06-23 22:04:01 -07003879static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003880nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07003881{
Jeff Layton33dcc482012-04-10 11:08:48 -04003882 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003883 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04003884 return;
3885
NeilBrowna76b4312005-06-23 22:04:01 -07003886 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003887 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003888 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003889 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003890 /*
3891 * Now that every NFSv4 client has had the chance to recover and
3892 * to see the (possibly new, possibly shorter) lease time, we
3893 * can safely set the next grace time to the current lease time:
3894 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03003895 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003896}
3897
NeilBrownfd39ca92005-06-23 22:04:03 -07003898static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003899nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900{
3901 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003902 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 struct nfs4_delegation *dp;
3904 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003905 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003906 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907
3908 nfs4_lock_state();
3909
3910 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003911 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03003912 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003913 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03003914 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 clp = list_entry(pos, struct nfs4_client, cl_lru);
3916 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3917 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003918 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 break;
3920 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003921 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03003922 dprintk("NFSD: client in use (clientid %08x)\n",
3923 clp->cl_clientid.cl_id);
3924 continue;
3925 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003926 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003927 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003928 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03003929 list_for_each_safe(pos, next, &reaplist) {
3930 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 dprintk("NFSD: purging unused client (clientid %08x)\n",
3932 clp->cl_clientid.cl_id);
3933 expire_client(clp);
3934 }
Benny Halevycdc97502014-05-30 09:09:30 -04003935 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003936 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03003938 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3939 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003941 t = dp->dl_time - cutoff;
3942 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 break;
3944 }
Jeff Layton42690672014-07-25 07:34:20 -04003945 unhash_delegation_locked(dp);
3946 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 }
Benny Halevycdc97502014-05-30 09:09:30 -04003948 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04003949 while (!list_empty(&reaplist)) {
3950 dp = list_first_entry(&reaplist, struct nfs4_delegation,
3951 dl_recall_lru);
3952 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003953 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003955 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003956 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3957 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003958 t = oo->oo_time - cutoff;
3959 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 break;
3961 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003962 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04003964 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04003966 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967}
3968
Harvey Harrisona254b242008-02-20 12:49:00 -08003969static struct workqueue_struct *laundry_wq;
3970static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08003971
3972static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003973laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974{
3975 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003976 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3977 work);
3978 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3979 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003981 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003983 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984}
3985
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003986static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07003987{
Trond Myklebust11b91642014-07-29 21:34:08 -04003988 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003989 return nfserr_bad_stateid;
3990 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991}
3992
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003994access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003996 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3997 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3998 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999}
4000
4001static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004002access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004004 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4005 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006}
4007
4008static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004009__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010{
Al Virob37ad282006-10-19 23:28:59 -07004011 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012
J. Bruce Fields02921912010-07-29 15:16:59 -04004013 /* For lock stateid's, we test the parent open, not the lock: */
4014 if (stp->st_openstp)
4015 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004016 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004018 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 goto out;
4020 status = nfs_ok;
4021out:
4022 return status;
4023}
4024
Al Virob37ad282006-10-19 23:28:59 -07004025static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004026check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004028 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004030 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004031 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 * conflicting state; so we must wait out the grace period. */
4033 return nfserr_grace;
4034 } else if (flags & WR_STATE)
4035 return nfs4_share_conflict(current_fh,
4036 NFS4_SHARE_DENY_WRITE);
4037 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4038 return nfs4_share_conflict(current_fh,
4039 NFS4_SHARE_DENY_READ);
4040}
4041
4042/*
4043 * Allow READ/WRITE during grace period on recovered state only for files
4044 * that are not able to provide mandatory locking.
4045 */
4046static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004047grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004049 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050}
4051
J. Bruce Fields81b82962011-08-23 11:03:29 -04004052/* Returns true iff a is later than b: */
4053static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4054{
Jim Rees1a9357f2013-05-17 17:33:00 -04004055 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004056}
4057
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004058static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004059{
Andy Adamson66689582009-04-03 08:28:45 +03004060 /*
4061 * When sessions are used the stateid generation number is ignored
4062 * when it is zero.
4063 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004064 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004065 return nfs_ok;
4066
4067 if (in->si_generation == ref->si_generation)
4068 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004069
J. Bruce Fields0836f582008-01-26 19:08:12 -05004070 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004071 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004072 return nfserr_bad_stateid;
4073 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004074 * However, we could see a stateid from the past, even from a
4075 * non-buggy client. For example, if the client sends a lock
4076 * while some IO is outstanding, the lock may bump si_generation
4077 * while the IO is still in flight. The client could avoid that
4078 * situation by waiting for responses on all the IO requests,
4079 * but better performance may result in retrying IO that
4080 * receives an old_stateid error if requests are rarely
4081 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004082 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004083 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004084}
4085
Chuck Lever7df302f2012-05-29 13:56:37 -04004086static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004087{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004088 struct nfs4_stid *s;
4089 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004090 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004091
Chuck Lever7df302f2012-05-29 13:56:37 -04004092 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004093 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004094 /* Client debugging aid. */
4095 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4096 char addr_str[INET6_ADDRSTRLEN];
4097 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4098 sizeof(addr_str));
4099 pr_warn_ratelimited("NFSD: client %s testing state ID "
4100 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004101 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004102 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004103 spin_lock(&cl->cl_lock);
4104 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004105 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004106 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004107 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004108 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004109 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004110 switch (s->sc_type) {
4111 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004112 status = nfs_ok;
4113 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004114 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004115 status = nfserr_deleg_revoked;
4116 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004117 case NFS4_OPEN_STID:
4118 case NFS4_LOCK_STID:
4119 ols = openlockstateid(s);
4120 if (ols->st_stateowner->so_is_open_owner
4121 && !(openowner(ols->st_stateowner)->oo_flags
4122 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004123 status = nfserr_bad_stateid;
4124 else
4125 status = nfs_ok;
4126 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004127 default:
4128 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004129 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004130 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004131 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004132 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004133 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004134out_unlock:
4135 spin_unlock(&cl->cl_lock);
4136 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004137}
4138
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004139static __be32
4140nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4141 stateid_t *stateid, unsigned char typemask,
4142 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004143{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004144 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004145
4146 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4147 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004148 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004149 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004150 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004151 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004152 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004153 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004154 if (status)
4155 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004156 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004157 if (!*s)
4158 return nfserr_bad_stateid;
4159 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004160}
4161
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162/*
4163* Checks for stateid operations
4164*/
Al Virob37ad282006-10-19 23:28:59 -07004165__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004166nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004167 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004169 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004170 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004172 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004174 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004175 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004176 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 if (filpp)
4179 *filpp = NULL;
4180
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004181 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 return nfserr_grace;
4183
4184 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004185 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186
Trond Myklebust14bcab12014-04-18 14:44:07 -04004187 nfs4_lock_state();
4188
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004189 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004190 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004191 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004192 if (status)
Trond Myklebust14bcab12014-04-18 14:44:07 -04004193 goto out;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004194 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4195 if (status)
4196 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004197 switch (s->sc_type) {
4198 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004199 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004200 status = nfs4_check_delegmode(dp, flags);
4201 if (status)
4202 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004203 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004204 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004205 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004206 WARN_ON_ONCE(1);
4207 status = nfserr_serverfault;
4208 goto out;
4209 }
Trond Myklebustde186432014-07-10 14:07:26 -04004210 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004211 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004212 break;
4213 case NFS4_OPEN_STID:
4214 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004215 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004216 status = nfs4_check_fh(current_fh, stp);
4217 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004219 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004220 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004222 status = nfs4_check_openmode(stp, flags);
4223 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004225 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004226 struct nfs4_file *fp = stp->st_stid.sc_file;
4227
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004228 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004229 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004230 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004231 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004232 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004233 break;
4234 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004235 status = nfserr_bad_stateid;
4236 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 }
4238 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004239 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004240 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241out:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004242 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 return status;
4244}
4245
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004246static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004247nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004248{
J. Bruce Fieldsa1b8ff42014-05-20 15:55:21 -04004249 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4250
Trond Myklebust11b91642014-07-29 21:34:08 -04004251 if (check_for_locks(stp->st_stid.sc_file, lo))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004252 return nfserr_locks_held;
Jeff Laytonc53530d2014-06-30 11:48:39 -04004253 release_lockowner_if_empty(lo);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004254 return nfs_ok;
4255}
4256
4257/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004258 * Test if the stateid is valid
4259 */
4260__be32
4261nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4262 struct nfsd4_test_stateid *test_stateid)
4263{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004264 struct nfsd4_test_stateid_id *stateid;
4265 struct nfs4_client *cl = cstate->session->se_client;
4266
4267 nfs4_lock_state();
4268 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004269 stateid->ts_id_status =
4270 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004271 nfs4_unlock_state();
4272
Bryan Schumaker17456802011-07-13 10:50:48 -04004273 return nfs_ok;
4274}
4275
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004276__be32
4277nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4278 struct nfsd4_free_stateid *free_stateid)
4279{
4280 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004281 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004282 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004283 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004284 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004285
4286 nfs4_lock_state();
Jeff Layton1af71cc2014-07-29 21:34:14 -04004287 spin_lock(&cl->cl_lock);
4288 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004289 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004290 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004291 switch (s->sc_type) {
4292 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004293 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004294 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004295 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004296 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4297 if (ret)
4298 break;
4299 ret = nfserr_locks_held;
4300 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004301 case NFS4_LOCK_STID:
4302 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4303 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004304 break;
4305 spin_unlock(&cl->cl_lock);
4306 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4307 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004308 case NFS4_REVOKED_DELEG_STID:
4309 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004310 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;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004314 goto out;
4315 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004316 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004317out_unlock:
4318 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004319out:
4320 nfs4_unlock_state();
4321 return ret;
4322}
4323
NeilBrown4c4cd222005-07-07 17:59:27 -07004324static inline int
4325setlkflg (int type)
4326{
4327 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4328 RD_STATE : WR_STATE;
4329}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004331static __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 -04004332{
4333 struct svc_fh *current_fh = &cstate->current_fh;
4334 struct nfs4_stateowner *sop = stp->st_stateowner;
4335 __be32 status;
4336
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004337 status = nfsd4_check_seqid(cstate, sop, seqid);
4338 if (status)
4339 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004340 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4341 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004342 /*
4343 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004344 * nfserr_replay_me from the previous step, and
4345 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004346 */
4347 return nfserr_bad_stateid;
4348 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4349 if (status)
4350 return status;
4351 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004352}
4353
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354/*
4355 * Checks for sequence id mutating operations.
4356 */
Al Virob37ad282006-10-19 23:28:59 -07004357static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004358nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004359 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004360 struct nfs4_ol_stateid **stpp,
4361 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004363 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004364 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004365 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004367 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4368 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004369
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004371 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004372 if (status)
4373 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004374 stp = openlockstateid(s);
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04004375 if (!nfsd4_has_session(cstate))
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004376 cstate->replay_owner = stp->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004378 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4379 if (!status)
4380 *stpp = stp;
4381 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004382}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004383
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004384static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4385 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004386{
4387 __be32 status;
4388 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004390 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004391 NFS4_OPEN_STID, stpp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004392 if (status)
4393 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004394 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004395 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07004396 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004397 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398}
4399
Al Virob37ad282006-10-19 23:28:59 -07004400__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004401nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004402 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403{
Al Virob37ad282006-10-19 23:28:59 -07004404 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004405 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004406 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004407 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408
Al Viroa6a9f182013-09-16 10:57:01 -04004409 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4410 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004412 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004413 if (status)
4414 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415
4416 nfs4_lock_state();
4417
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004418 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004419 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004420 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004421 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004422 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004423 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004424 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004425 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004426 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004427 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004428 update_stateid(&stp->st_stid.sc_stateid);
4429 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004430 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004431 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004432
Jeff Layton2a4317c2012-03-21 16:42:43 -04004433 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004434 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004436 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004437 if (!cstate->replay_owner)
4438 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 return status;
4440}
4441
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004442static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004444 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004445 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004446 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004447 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004448}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004449
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004450static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4451{
4452 switch (to_access) {
4453 case NFS4_SHARE_ACCESS_READ:
4454 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4455 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4456 break;
4457 case NFS4_SHARE_ACCESS_WRITE:
4458 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4459 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4460 break;
4461 case NFS4_SHARE_ACCESS_BOTH:
4462 break;
4463 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004464 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 }
4466}
4467
Al Virob37ad282006-10-19 23:28:59 -07004468__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004469nfsd4_open_downgrade(struct svc_rqst *rqstp,
4470 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004471 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472{
Al Virob37ad282006-10-19 23:28:59 -07004473 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004474 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004475 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476
Al Viroa6a9f182013-09-16 10:57:01 -04004477 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4478 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004480 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004481 if (od->od_deleg_want)
4482 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4483 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484
4485 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004486 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004487 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004488 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004491 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004492 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 stp->st_access_bmap, od->od_share_access);
4494 goto out;
4495 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004496 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004497 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 stp->st_deny_bmap, od->od_share_deny);
4499 goto out;
4500 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004501 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
Jeff Laytonce0fc432012-05-11 09:45:14 -04004503 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004505 update_stateid(&stp->st_stid.sc_stateid);
4506 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 status = nfs_ok;
4508out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004509 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004510 if (!cstate->replay_owner)
4511 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 return status;
4513}
4514
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004515static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4516{
Trond Myklebustacf92952014-06-30 11:48:37 -04004517 struct nfs4_client *clp = s->st_stid.sc_client;
4518 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4519
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004520 s->st_stid.sc_type = NFS4_CLOSED_STID;
Trond Myklebustacf92952014-06-30 11:48:37 -04004521 unhash_open_stateid(s);
4522
4523 if (clp->cl_minorversion) {
Trond Myklebustacf92952014-06-30 11:48:37 -04004524 if (list_empty(&oo->oo_owner.so_stateids))
4525 release_openowner(oo);
Trond Myklebust60116952014-07-29 21:34:06 -04004526 nfs4_put_stid(&s->st_stid);
Trond Myklebustacf92952014-06-30 11:48:37 -04004527 } else {
Trond Myklebust60116952014-07-29 21:34:06 -04004528 /*
4529 * In the 4.0 case we need to keep the owners around a
4530 * little while to handle CLOSE replay. We still do need
4531 * to release any file access that is held by them
4532 * before returning however.
4533 */
4534 release_all_access(s);
Trond Myklebust11b91642014-07-29 21:34:08 -04004535 if (s->st_stid.sc_file) {
4536 put_nfs4_file(s->st_stid.sc_file);
4537 s->st_stid.sc_file = NULL;
Trond Myklebustf8338832014-07-25 07:34:19 -04004538 }
Trond Myklebustacf92952014-06-30 11:48:37 -04004539 oo->oo_last_closed_stid = s;
Trond Myklebustacf92952014-06-30 11:48:37 -04004540 if (list_empty(&oo->oo_owner.so_stateids))
4541 move_to_close_lru(oo, clp->net);
4542 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004543}
4544
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545/*
4546 * nfs4_unlock_state() called after encode
4547 */
Al Virob37ad282006-10-19 23:28:59 -07004548__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004549nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004550 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551{
Al Virob37ad282006-10-19 23:28:59 -07004552 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004553 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004554 struct net *net = SVC_NET(rqstp);
4555 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556
Al Viroa6a9f182013-09-16 10:57:01 -04004557 dprintk("NFSD: nfsd4_close on file %pd\n",
4558 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
4560 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004561 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4562 &close->cl_stateid,
4563 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004564 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004565 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004566 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004568 update_stateid(&stp->st_stid.sc_stateid);
4569 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004571 nfsd4_close_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004573 if (!cstate->replay_owner)
4574 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 return status;
4576}
4577
Al Virob37ad282006-10-19 23:28:59 -07004578__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004579nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4580 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004582 struct nfs4_delegation *dp;
4583 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004584 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004585 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004586 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004588 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004589 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590
4591 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004592 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004593 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004594 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004595 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004596 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004597 if (status)
4598 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004599
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004600 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004602 nfs4_unlock_state();
4603
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 return status;
4605}
4606
4607
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609
Benny Halevy87df4de2008-12-15 19:42:03 +02004610static inline u64
4611end_offset(u64 start, u64 len)
4612{
4613 u64 end;
4614
4615 end = start + len;
4616 return end >= start ? end: NFS4_MAX_UINT64;
4617}
4618
4619/* last octet in a range */
4620static inline u64
4621last_byte_offset(u64 start, u64 len)
4622{
4623 u64 end;
4624
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004625 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004626 end = start + len;
4627 return end > start ? end - 1: NFS4_MAX_UINT64;
4628}
4629
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630/*
4631 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4632 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4633 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4634 * locking, this prevents us from being completely protocol-compliant. The
4635 * real solution to this problem is to start using unsigned file offsets in
4636 * the VFS, but this is a very deep change!
4637 */
4638static inline void
4639nfs4_transform_lock_offset(struct file_lock *lock)
4640{
4641 if (lock->fl_start < 0)
4642 lock->fl_start = OFFSET_MAX;
4643 if (lock->fl_end < 0)
4644 lock->fl_end = OFFSET_MAX;
4645}
4646
NeilBrownd5b90262006-04-10 22:55:22 -07004647/* Hack!: For now, we're defining this just so we can use a pointer to it
4648 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004649static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004650};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651
4652static inline void
4653nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4654{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004655 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656
NeilBrownd5b90262006-04-10 22:55:22 -07004657 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004658 lo = (struct nfs4_lockowner *) fl->fl_owner;
4659 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4660 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004661 if (!deny->ld_owner.data)
4662 /* We just don't care that much */
4663 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004664 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4665 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004666 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004667nevermind:
4668 deny->ld_owner.len = 0;
4669 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004670 deny->ld_clientid.cl_boot = 0;
4671 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 }
4673 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004674 deny->ld_length = NFS4_MAX_UINT64;
4675 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4677 deny->ld_type = NFS4_READ_LT;
4678 if (fl->fl_type != F_RDLCK)
4679 deny->ld_type = NFS4_WRITE_LT;
4680}
4681
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004682static struct nfs4_lockowner *
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004683find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4684 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685{
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004686 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4687 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004689 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4690 if (so->so_is_open_owner)
4691 continue;
4692 if (!same_owner_str(so, owner, clid))
4693 continue;
4694 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 }
4696 return NULL;
4697}
4698
4699/*
4700 * Alloc a lock owner structure.
4701 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004702 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004704 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004706static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004707alloc_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 -04004708 struct nfs4_lockowner *lo;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004709 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004711 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4712 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004714 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4715 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07004716 /* It is the openowner seqid that will be incremented in encode in the
4717 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004718 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004719 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004720 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721}
4722
Jeff Layton356a95e2014-07-29 21:34:13 -04004723static void
4724init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
4725 struct nfs4_file *fp, struct inode *inode,
4726 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004728 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729
Jeff Layton356a95e2014-07-29 21:34:13 -04004730 lockdep_assert_held(&clp->cl_lock);
4731
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004732 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004733 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004734 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07004735 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004736 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04004737 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004738 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004740 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004741 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04004742 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004743 spin_lock(&fp->fi_lock);
4744 list_add(&stp->st_perfile, &fp->fi_stateids);
4745 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746}
4747
Jeff Laytonc53530d2014-06-30 11:48:39 -04004748static struct nfs4_ol_stateid *
4749find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4750{
4751 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04004752 struct nfs4_client *clp = lo->lo_owner.so_client;
4753
4754 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004755
4756 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004757 if (lst->st_stid.sc_file == fp) {
4758 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004759 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004760 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004761 }
4762 return NULL;
4763}
4764
Jeff Layton356a95e2014-07-29 21:34:13 -04004765static struct nfs4_ol_stateid *
4766find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
4767 struct inode *inode, struct nfs4_ol_stateid *ost,
4768 bool *new)
4769{
4770 struct nfs4_stid *ns = NULL;
4771 struct nfs4_ol_stateid *lst;
4772 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4773 struct nfs4_client *clp = oo->oo_owner.so_client;
4774
4775 spin_lock(&clp->cl_lock);
4776 lst = find_lock_stateid(lo, fi);
4777 if (lst == NULL) {
4778 spin_unlock(&clp->cl_lock);
4779 ns = nfs4_alloc_stid(clp, stateid_slab);
4780 if (ns == NULL)
4781 return NULL;
4782
4783 spin_lock(&clp->cl_lock);
4784 lst = find_lock_stateid(lo, fi);
4785 if (likely(!lst)) {
4786 lst = openlockstateid(ns);
4787 init_lock_stateid(lst, lo, fi, inode, ost);
4788 ns = NULL;
4789 *new = true;
4790 }
4791 }
4792 spin_unlock(&clp->cl_lock);
4793 if (ns)
4794 nfs4_put_stid(ns);
4795 return lst;
4796}
Jeff Laytonc53530d2014-06-30 11:48:39 -04004797
NeilBrownfd39ca92005-06-23 22:04:03 -07004798static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799check_lock_length(u64 offset, u64 length)
4800{
Benny Halevy87df4de2008-12-15 19:42:03 +02004801 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 LOFF_OVERFLOW(offset, length)));
4803}
4804
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004805static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004806{
Trond Myklebust11b91642014-07-29 21:34:08 -04004807 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004808
Jeff Layton7214e862014-07-10 14:07:33 -04004809 lockdep_assert_held(&fp->fi_lock);
4810
Jeff Layton82c5ff12012-05-11 09:45:13 -04004811 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004812 return;
Jeff Layton12659652014-07-10 14:07:28 -04004813 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004814 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004815}
4816
Jeff Layton356a95e2014-07-29 21:34:13 -04004817static __be32
4818lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
4819 struct nfs4_ol_stateid *ost,
4820 struct nfsd4_lock *lock,
4821 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004822{
Trond Myklebust11b91642014-07-29 21:34:08 -04004823 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004824 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4825 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004826 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004827 struct nfs4_lockowner *lo;
4828 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004829 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004830
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004831 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004832 if (!lo) {
4833 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4834 &lock->v.new.owner);
4835 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4836 if (lo == NULL)
4837 return nfserr_jukebox;
4838 } else {
4839 /* with an existing lockowner, seqids must be the same */
4840 if (!cstate->minorversion &&
4841 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004842 return nfserr_bad_seqid;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004843 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004844
Jeff Layton356a95e2014-07-29 21:34:13 -04004845 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004846 if (*lst == NULL) {
Jeff Layton356a95e2014-07-29 21:34:13 -04004847 release_lockowner_if_empty(lo);
4848 return nfserr_jukebox;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004849 }
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004850 return nfs_ok;
4851}
4852
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853/*
4854 * LOCK operation
4855 */
Al Virob37ad282006-10-19 23:28:59 -07004856__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004857nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004858 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004860 struct nfs4_openowner *open_sop = NULL;
4861 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004862 struct nfs4_ol_stateid *lock_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04004863 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004864 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004865 struct file_lock *file_lock = NULL;
4866 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004867 __be32 status = 0;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004868 bool new_state = false;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004869 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004870 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004871 struct net *net = SVC_NET(rqstp);
4872 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873
4874 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4875 (long long) lock->lk_offset,
4876 (long long) lock->lk_length);
4877
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 if (check_lock_length(lock->lk_offset, lock->lk_length))
4879 return nfserr_inval;
4880
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004881 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004882 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004883 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4884 return status;
4885 }
4886
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 nfs4_lock_state();
4888
4889 if (lock->lk_is_new) {
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004890 struct nfs4_ol_stateid *open_stp = NULL;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004891
4892 if (nfsd4_has_session(cstate))
4893 /* See rfc 5661 18.10.3: given clientid is ignored: */
4894 memcpy(&lock->v.new.clientid,
4895 &cstate->session->se_client->cl_clientid,
4896 sizeof(clientid_t));
4897
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004899 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004903 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 lock->lk_new_open_seqid,
4905 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004906 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07004907 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004909 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004910 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004911 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004912 &lock->v.new.clientid))
4913 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004914 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4915 &lock_stp, &new_state);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004916 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03004917 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004918 lock->lk_old_lock_seqid,
4919 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004920 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004921 /* FIXME: move into nfs4_preprocess_seqid_op */
4922 if (!status)
4923 atomic_inc(&lock_stp->st_stid.sc_count);
4924 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004925 if (status)
4926 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004927 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004929 lkflg = setlkflg(lock->lk_type);
4930 status = nfs4_check_openmode(lock_stp, lkflg);
4931 if (status)
4932 goto out;
4933
NeilBrown0dd395d2005-07-07 17:59:15 -07004934 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004935 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004936 goto out;
4937 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004938 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004939 goto out;
4940
Jeff Layton21179d82012-08-21 08:03:32 -04004941 file_lock = locks_alloc_lock();
4942 if (!file_lock) {
4943 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4944 status = nfserr_jukebox;
4945 goto out;
4946 }
4947
Trond Myklebust11b91642014-07-29 21:34:08 -04004948 fp = lock_stp->st_stid.sc_file;
Jeff Layton21179d82012-08-21 08:03:32 -04004949 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 switch (lock->lk_type) {
4951 case NFS4_READ_LT:
4952 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04004953 spin_lock(&fp->fi_lock);
4954 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004955 if (filp)
4956 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04004957 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04004958 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004959 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 case NFS4_WRITE_LT:
4961 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04004962 spin_lock(&fp->fi_lock);
4963 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004964 if (filp)
4965 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04004966 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04004967 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004968 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 default:
4970 status = nfserr_inval;
4971 goto out;
4972 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004973 if (!filp) {
4974 status = nfserr_openmode;
4975 goto out;
4976 }
Jeff Layton21179d82012-08-21 08:03:32 -04004977 file_lock->fl_owner = (fl_owner_t)lock_sop;
4978 file_lock->fl_pid = current->tgid;
4979 file_lock->fl_file = filp;
4980 file_lock->fl_flags = FL_POSIX;
4981 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4982 file_lock->fl_start = lock->lk_offset;
4983 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4984 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985
Jeff Layton21179d82012-08-21 08:03:32 -04004986 conflock = locks_alloc_lock();
4987 if (!conflock) {
4988 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4989 status = nfserr_jukebox;
4990 goto out;
4991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992
Jeff Layton21179d82012-08-21 08:03:32 -04004993 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004994 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004996 update_stateid(&lock_stp->st_stid.sc_stateid);
4997 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004999 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005000 break;
5001 case (EAGAIN): /* conflock holds conflicting lock */
5002 status = nfserr_denied;
5003 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005004 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005005 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 case (EDEADLK):
5007 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005008 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005009 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005010 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005011 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005012 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014out:
Trond Myklebustde186432014-07-10 14:07:26 -04005015 if (filp)
5016 fput(filp);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005017 if (lock_stp)
5018 nfs4_put_stid(&lock_stp->st_stid);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005019 if (status && new_state)
Jeff Laytonc53530d2014-06-30 11:48:39 -04005020 release_lock_stateid(lock_stp);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005021 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04005022 if (!cstate->replay_owner)
5023 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005024 if (file_lock)
5025 locks_free_lock(file_lock);
5026 if (conflock)
5027 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 return status;
5029}
5030
5031/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005032 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5033 * so we do a temporary open here just to get an open file to pass to
5034 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5035 * inode operation.)
5036 */
Al Viro04da6e92012-04-13 00:00:04 -04005037static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005038{
5039 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005040 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5041 if (!err) {
5042 err = nfserrno(vfs_test_lock(file, lock));
5043 nfsd_close(file);
5044 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005045 return err;
5046}
5047
5048/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 * LOCKT operation
5050 */
Al Virob37ad282006-10-19 23:28:59 -07005051__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005052nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5053 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054{
Jeff Layton21179d82012-08-21 08:03:32 -04005055 struct file_lock *file_lock = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005056 struct nfs4_lockowner *lo;
Al Virob37ad282006-10-19 23:28:59 -07005057 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005058 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005060 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 return nfserr_grace;
5062
5063 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5064 return nfserr_inval;
5065
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 nfs4_lock_state();
5067
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005068 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005069 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005070 if (status)
5071 goto out;
5072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005074 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076
Jeff Layton21179d82012-08-21 08:03:32 -04005077 file_lock = locks_alloc_lock();
5078 if (!file_lock) {
5079 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5080 status = nfserr_jukebox;
5081 goto out;
5082 }
5083 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 switch (lockt->lt_type) {
5085 case NFS4_READ_LT:
5086 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005087 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 break;
5089 case NFS4_WRITE_LT:
5090 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005091 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 break;
5093 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005094 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 status = nfserr_inval;
5096 goto out;
5097 }
5098
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005099 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005100 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005101 file_lock->fl_owner = (fl_owner_t)lo;
5102 file_lock->fl_pid = current->tgid;
5103 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104
Jeff Layton21179d82012-08-21 08:03:32 -04005105 file_lock->fl_start = lockt->lt_offset;
5106 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
Jeff Layton21179d82012-08-21 08:03:32 -04005108 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109
Jeff Layton21179d82012-08-21 08:03:32 -04005110 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005111 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005112 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005113
Jeff Layton21179d82012-08-21 08:03:32 -04005114 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005116 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 }
5118out:
5119 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005120 if (file_lock)
5121 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 return status;
5123}
5124
Al Virob37ad282006-10-19 23:28:59 -07005125__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005126nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005127 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005129 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005131 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005132 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005133 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005134 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5135
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5137 (long long) locku->lu_offset,
5138 (long long) locku->lu_length);
5139
5140 if (check_lock_length(locku->lu_offset, locku->lu_length))
5141 return nfserr_inval;
5142
5143 nfs4_lock_state();
5144
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005145 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005146 &locku->lu_stateid, NFS4_LOCK_STID,
5147 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005148 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005150 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005151 if (!filp) {
5152 status = nfserr_lock_range;
5153 goto out;
5154 }
Jeff Layton21179d82012-08-21 08:03:32 -04005155 file_lock = locks_alloc_lock();
5156 if (!file_lock) {
5157 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5158 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005159 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005160 }
5161 locks_init_lock(file_lock);
5162 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005163 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005164 file_lock->fl_pid = current->tgid;
5165 file_lock->fl_file = filp;
5166 file_lock->fl_flags = FL_POSIX;
5167 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5168 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169
Jeff Layton21179d82012-08-21 08:03:32 -04005170 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5171 locku->lu_length);
5172 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173
Jeff Layton21179d82012-08-21 08:03:32 -04005174 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005175 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005176 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 goto out_nfserr;
5178 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005179 update_stateid(&stp->st_stid.sc_stateid);
5180 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005181fput:
5182 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005184 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields71c3bcd2011-09-27 21:42:29 -04005185 if (!cstate->replay_owner)
5186 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005187 if (file_lock)
5188 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 return status;
5190
5191out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005192 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005193 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194}
5195
5196/*
5197 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005198 * true: locks held by lockowner
5199 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005201static bool
5202check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203{
5204 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005205 int status = false;
5206 struct file *filp = find_any_file(fp);
5207 struct inode *inode;
5208
5209 if (!filp) {
5210 /* Any valid lock stateid should have some sort of access */
5211 WARN_ON_ONCE(1);
5212 return status;
5213 }
5214
5215 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216
Jeff Layton1c8c6012013-06-21 08:58:15 -04005217 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005219 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005220 status = true;
5221 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005224 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005225 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 return status;
5227}
5228
Al Virob37ad282006-10-19 23:28:59 -07005229__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005230nfsd4_release_lockowner(struct svc_rqst *rqstp,
5231 struct nfsd4_compound_state *cstate,
5232 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233{
5234 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Laytonfd449072014-06-30 11:48:41 -04005235 struct nfs4_stateowner *sop = NULL, *tmp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005236 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005237 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 struct xdr_netobj *owner = &rlockowner->rl_owner;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005239 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07005240 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005241 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242
5243 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5244 clid->cl_boot, clid->cl_id);
5245
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 nfs4_lock_state();
5247
Jeff Layton4b24ca72014-06-30 11:48:44 -04005248 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005249 if (status)
5250 goto out;
5251
NeilBrown3e9e3db2005-06-23 22:04:20 -07005252 status = nfserr_locks_held;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05005253
Jeff Laytonfd449072014-06-30 11:48:41 -04005254 /* Find the matching lock stateowner */
5255 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
5256 if (tmp->so_is_open_owner)
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005257 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005258 if (same_owner_str(tmp, owner, clid)) {
5259 sop = tmp;
5260 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07005262 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005263
5264 /* No matching owner found, maybe a replay? Just declare victory... */
5265 if (!sop) {
5266 status = nfs_ok;
5267 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005269
5270 lo = lockowner(sop);
5271 /* see if there are still any locks associated with it */
5272 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
Trond Myklebust11b91642014-07-29 21:34:08 -04005273 if (check_for_locks(stp->st_stid.sc_file, lo))
Jeff Laytonfd449072014-06-30 11:48:41 -04005274 goto out;
5275 }
5276
5277 status = nfs_ok;
5278 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279out:
5280 nfs4_unlock_state();
5281 return status;
5282}
5283
5284static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005285alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286{
NeilBrowna55370a2005-06-23 22:03:52 -07005287 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288}
5289
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005290bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005291nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005292{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005293 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005294
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005295 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005296 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005297}
5298
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299/*
5300 * failure => all reset bets are off, nfserr_no_grace...
5301 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005302struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005303nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304{
5305 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005306 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307
NeilBrowna55370a2005-06-23 22:03:52 -07005308 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5309 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005310 if (crp) {
5311 strhashval = clientstr_hashval(name);
5312 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005313 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005314 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005315 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005316 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005317 }
5318 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319}
5320
Jeff Layton2a4317c2012-03-21 16:42:43 -04005321void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005322nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005323{
5324 list_del(&crp->cr_strhash);
5325 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005326 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005327}
5328
5329void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005330nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331{
5332 struct nfs4_client_reclaim *crp = NULL;
5333 int i;
5334
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005336 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5337 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005339 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 }
5341 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005342 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343}
5344
5345/*
5346 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005347struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005348nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349{
5350 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 struct nfs4_client_reclaim *crp = NULL;
5352
Jeff Layton278c9312012-11-12 15:00:52 -05005353 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354
Jeff Layton278c9312012-11-12 15:00:52 -05005355 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005356 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005357 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 return crp;
5359 }
5360 }
5361 return NULL;
5362}
5363
5364/*
5365* Called from OPEN. Look for clientid in reclaim list.
5366*/
Al Virob37ad282006-10-19 23:28:59 -07005367__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005368nfs4_check_open_reclaim(clientid_t *clid,
5369 struct nfsd4_compound_state *cstate,
5370 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005372 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005373
5374 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005375 status = lookup_clientid(clid, cstate, nn);
5376 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005377 return nfserr_reclaim_bad;
5378
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005379 if (nfsd4_client_record_check(cstate->clp))
5380 return nfserr_reclaim_bad;
5381
5382 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383}
5384
Bryan Schumaker65178db2011-11-01 13:35:21 -04005385#ifdef CONFIG_NFSD_FAULT_INJECTION
5386
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005387u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5388{
J. Bruce Fields221a6872013-04-01 22:23:49 -04005389 if (mark_client_expired(clp))
5390 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005391 expire_client(clp);
5392 return 1;
5393}
5394
Bryan Schumaker184c1842012-11-29 11:40:44 -05005395u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5396{
5397 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005398 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005399 printk(KERN_INFO "NFS Client: %s\n", buf);
5400 return 1;
5401}
5402
5403static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5404 const char *type)
5405{
5406 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005407 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005408 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5409}
5410
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005411static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5412 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005413{
5414 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005415 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005416 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005417 u64 count = 0;
5418
5419 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005420 list_for_each_entry_safe(stp, st_next,
5421 &oop->oo_owner.so_stateids, st_perstateowner) {
5422 list_for_each_entry_safe(lst, lst_next,
5423 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005424 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005425 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005426 if (++count == max)
5427 return count;
5428 }
5429 }
5430 }
5431
5432 return count;
5433}
5434
5435u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5436{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005437 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005438}
5439
Bryan Schumaker184c1842012-11-29 11:40:44 -05005440u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5441{
5442 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5443 nfsd_print_count(clp, count, "locked files");
5444 return count;
5445}
5446
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005447static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5448{
5449 struct nfs4_openowner *oop, *next;
5450 u64 count = 0;
5451
5452 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5453 if (func)
5454 func(oop);
5455 if (++count == max)
5456 break;
5457 }
5458
5459 return count;
5460}
5461
5462u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5463{
5464 return nfsd_foreach_client_open(clp, max, release_openowner);
5465}
5466
Bryan Schumaker184c1842012-11-29 11:40:44 -05005467u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5468{
5469 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5470 nfsd_print_count(clp, count, "open files");
5471 return count;
5472}
5473
Bryan Schumaker269de302012-11-29 11:40:42 -05005474static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5475 struct list_head *victims)
5476{
5477 struct nfs4_delegation *dp, *next;
5478 u64 count = 0;
5479
Benny Halevycdc97502014-05-30 09:09:30 -04005480 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005481 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005482 if (victims) {
5483 /*
5484 * It's not safe to mess with delegations that have a
5485 * non-zero dl_time. They might have already been broken
5486 * and could be processed by the laundromat outside of
5487 * the state_lock. Just leave them be.
5488 */
5489 if (dp->dl_time != 0)
5490 continue;
5491
Jeff Layton42690672014-07-25 07:34:20 -04005492 unhash_delegation_locked(dp);
5493 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005494 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005495 if (++count == max)
5496 break;
5497 }
5498 return count;
5499}
5500
5501u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5502{
5503 struct nfs4_delegation *dp, *next;
5504 LIST_HEAD(victims);
5505 u64 count;
5506
Benny Halevycdc97502014-05-30 09:09:30 -04005507 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005508 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005509 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005510
Jeff Layton2d4a5322014-07-25 07:34:21 -04005511 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5512 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005513 revoke_delegation(dp);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005514 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005515
5516 return count;
5517}
5518
5519u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5520{
Jeff Laytondff13992014-07-08 14:02:49 -04005521 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005522 LIST_HEAD(victims);
5523 u64 count;
5524
Benny Halevycdc97502014-05-30 09:09:30 -04005525 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005526 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005527 while (!list_empty(&victims)) {
5528 dp = list_first_entry(&victims, struct nfs4_delegation,
5529 dl_recall_lru);
5530 list_del_init(&dp->dl_recall_lru);
5531 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005532 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005533 }
Benny Halevycdc97502014-05-30 09:09:30 -04005534 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005535
5536 return count;
5537}
5538
Bryan Schumaker184c1842012-11-29 11:40:44 -05005539u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5540{
5541 u64 count = 0;
5542
Benny Halevycdc97502014-05-30 09:09:30 -04005543 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005544 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005545 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005546
5547 nfsd_print_count(clp, count, "delegations");
5548 return count;
5549}
5550
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005551u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005552{
5553 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005554 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005555 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005556
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005557 if (!nfsd_netns_ready(nn))
5558 return 0;
5559
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005560 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005561 count += func(clp, max - count);
5562 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005563 break;
5564 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005565
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005566 return count;
5567}
5568
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005569struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5570{
5571 struct nfs4_client *clp;
5572 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5573
5574 if (!nfsd_netns_ready(nn))
5575 return NULL;
5576
5577 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5578 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5579 return clp;
5580 }
5581 return NULL;
5582}
5583
Bryan Schumaker65178db2011-11-01 13:35:21 -04005584#endif /* CONFIG_NFSD_FAULT_INJECTION */
5585
Meelap Shahc2f1a552007-07-17 04:04:39 -07005586/*
5587 * Since the lifetime of a delegation isn't limited to that of an open, a
5588 * client may quite reasonably hang on to a delegation as long as it has
5589 * the inode cached. This becomes an obvious problem the first time a
5590 * client's inode cache approaches the size of the server's total memory.
5591 *
5592 * For now we avoid this problem by imposing a hard limit on the number
5593 * of delegations, which varies according to the server's memory size.
5594 */
5595static void
5596set_max_delegations(void)
5597{
5598 /*
5599 * Allow at most 4 delegations per megabyte of RAM. Quick
5600 * estimates suggest that in the worst case (where every delegation
5601 * is for a different inode), a delegation could take about 1.5K,
5602 * giving a worst case usage of about 6% of memory.
5603 */
5604 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5605}
5606
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005607static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005608{
5609 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5610 int i;
5611
5612 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5613 CLIENT_HASH_SIZE, GFP_KERNEL);
5614 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005615 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005616 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5617 CLIENT_HASH_SIZE, GFP_KERNEL);
5618 if (!nn->unconf_id_hashtbl)
5619 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005620 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5621 OWNER_HASH_SIZE, GFP_KERNEL);
5622 if (!nn->ownerstr_hashtbl)
5623 goto err_ownerstr;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005624 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5625 SESSION_HASH_SIZE, GFP_KERNEL);
5626 if (!nn->sessionid_hashtbl)
5627 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005628
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005629 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005630 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005631 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005632 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005633 for (i = 0; i < OWNER_HASH_SIZE; i++)
5634 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005635 for (i = 0; i < SESSION_HASH_SIZE; i++)
5636 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005637 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005638 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005639 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005640 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005641 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005642 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005643
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005644 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005645 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005646
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005647 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005648
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005649err_sessionid:
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03005650 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005651err_ownerstr:
5652 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005653err_unconf_id:
5654 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005655err:
5656 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005657}
5658
5659static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005660nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005661{
5662 int i;
5663 struct nfs4_client *clp = NULL;
5664 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5665
5666 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5667 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5668 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5669 destroy_client(clp);
5670 }
5671 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005672
Kinglong Mee2b905632014-03-26 22:09:30 +08005673 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5674 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5675 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5676 destroy_client(clp);
5677 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005678 }
5679
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005680 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005681 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005682 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005683 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005684 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005685}
5686
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005687int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005688nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005689{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005690 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005691 int ret;
5692
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005693 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005694 if (ret)
5695 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005696 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005697 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005698 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005699 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005700 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005701 nn->nfsd4_grace, net);
5702 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005703 return 0;
5704}
5705
5706/* initialization to perform when the nfsd service is started: */
5707
5708int
5709nfs4_state_start(void)
5710{
5711 int ret;
5712
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005713 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005714 if (ret)
5715 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005716 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005717 if (laundry_wq == NULL) {
5718 ret = -ENOMEM;
5719 goto out_recovery;
5720 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005721 ret = nfsd4_create_callback_queue();
5722 if (ret)
5723 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005724
Meelap Shahc2f1a552007-07-17 04:04:39 -07005725 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005726
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005727 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005728
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005729out_free_laundry:
5730 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005731out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005732 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733}
5734
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005735void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005736nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005740 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005742 cancel_delayed_work_sync(&nn->laundromat_work);
5743 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005744
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005745 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04005747 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005748 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005750 unhash_delegation_locked(dp);
5751 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 }
Benny Halevycdc97502014-05-30 09:09:30 -04005753 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 list_for_each_safe(pos, next, &reaplist) {
5755 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005756 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04005757 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 }
5759
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005760 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005761 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005762 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763}
5764
5765void
5766nfs4_state_shutdown(void)
5767{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005768 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005769 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005771
5772static void
5773get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5774{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005775 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5776 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005777}
5778
5779static void
5780put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5781{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005782 if (cstate->minorversion) {
5783 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5784 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5785 }
5786}
5787
5788void
5789clear_current_stateid(struct nfsd4_compound_state *cstate)
5790{
5791 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005792}
5793
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005794/*
5795 * functions to set current state id
5796 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005797void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005798nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5799{
5800 put_stateid(cstate, &odp->od_stateid);
5801}
5802
5803void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005804nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5805{
5806 put_stateid(cstate, &open->op_stateid);
5807}
5808
5809void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005810nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5811{
5812 put_stateid(cstate, &close->cl_stateid);
5813}
5814
5815void
5816nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5817{
5818 put_stateid(cstate, &lock->lk_resp_stateid);
5819}
5820
5821/*
5822 * functions to consume current state id
5823 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005824
5825void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005826nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5827{
5828 get_stateid(cstate, &odp->od_stateid);
5829}
5830
5831void
5832nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5833{
5834 get_stateid(cstate, &drp->dr_stateid);
5835}
5836
5837void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005838nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5839{
5840 get_stateid(cstate, &fsp->fr_stateid);
5841}
5842
5843void
5844nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5845{
5846 get_stateid(cstate, &setattr->sa_stateid);
5847}
5848
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005849void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005850nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5851{
5852 get_stateid(cstate, &close->cl_stateid);
5853}
5854
5855void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005856nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005857{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005858 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005859}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01005860
5861void
5862nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5863{
5864 get_stateid(cstate, &read->rd_stateid);
5865}
5866
5867void
5868nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5869{
5870 get_stateid(cstate, &write->wr_stateid);
5871}