blob: 8ce5894133aa6e2b34235278aca04377f1836258 [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
896 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500897 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400898 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500899 list_del(&stp->st_perstateowner);
900}
901
Trond Myklebust60116952014-07-29 21:34:06 -0400902static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500903{
Trond Myklebust60116952014-07-29 21:34:06 -0400904 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400905
Trond Myklebust60116952014-07-29 21:34:06 -0400906 release_all_access(stp);
Trond Myklebust60116952014-07-29 21:34:06 -0400907 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500908}
909
Jeff Laytonb49e0842014-07-29 21:34:11 -0400910static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500911{
Jeff Laytonb49e0842014-07-29 21:34:11 -0400912 struct nfs4_ol_stateid *stp = openlockstateid(stid);
913 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500914 struct file *file;
915
Jeff Laytonb49e0842014-07-29 21:34:11 -0400916 file = find_any_file(stp->st_stid.sc_file);
917 if (file)
918 filp_close(file, (fl_owner_t)lo);
919 nfs4_free_ol_stateid(stid);
920}
921
922static void __release_lock_stateid(struct nfs4_ol_stateid *stp)
923{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400924 list_del(&stp->st_locks);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500925 unhash_generic_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400926 unhash_stid(&stp->st_stid);
Trond Myklebust60116952014-07-29 21:34:06 -0400927 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500928}
929
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400930static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500931{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400932 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500933
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400934 list_del(&lo->lo_owner.so_strhash);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400935 while (!list_empty(&lo->lo_owner.so_stateids)) {
936 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400937 struct nfs4_ol_stateid, st_perstateowner);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400938 __release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500939 }
940}
941
Trond Myklebust50cc6232014-04-18 14:44:03 -0400942static void nfs4_free_lockowner(struct nfs4_lockowner *lo)
943{
944 kfree(lo->lo_owner.so_owner.data);
945 kmem_cache_free(lockowner_slab, lo);
946}
947
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400948static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500949{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400950 unhash_lockowner(lo);
951 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500952}
953
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400954static void release_lockowner_if_empty(struct nfs4_lockowner *lo)
955{
956 if (list_empty(&lo->lo_owner.so_stateids))
957 release_lockowner(lo);
958}
959
960static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500961{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400962 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500963
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400964 lo = lockowner(stp->st_stateowner);
965 __release_lock_stateid(stp);
966 release_lockowner_if_empty(lo);
967}
968
969static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
970{
971 struct nfs4_ol_stateid *stp;
972
973 while (!list_empty(&open_stp->st_locks)) {
974 stp = list_entry(open_stp->st_locks.next,
975 struct nfs4_ol_stateid, st_locks);
976 release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500977 }
978}
979
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400980static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500981{
982 unhash_generic_stateid(stp);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400983 release_open_stateid_locks(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400984}
985
986static void release_open_stateid(struct nfs4_ol_stateid *stp)
987{
988 unhash_open_stateid(stp);
Trond Myklebust60116952014-07-29 21:34:06 -0400989 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields22839632009-01-11 14:27:17 -0500990}
991
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400992static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500993{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400994 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500995
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400996 list_del(&oo->oo_owner.so_strhash);
997 list_del(&oo->oo_perclient);
998 while (!list_empty(&oo->oo_owner.so_stateids)) {
999 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001000 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05001001 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001002 }
1003}
1004
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001005static void release_last_closed_stateid(struct nfs4_openowner *oo)
1006{
1007 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
1008
1009 if (s) {
Trond Myklebust60116952014-07-29 21:34:06 -04001010 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001011 oo->oo_last_closed_stid = NULL;
1012 }
1013}
1014
Trond Myklebust50cc6232014-04-18 14:44:03 -04001015static void nfs4_free_openowner(struct nfs4_openowner *oo)
1016{
1017 kfree(oo->oo_owner.so_owner.data);
1018 kmem_cache_free(openowner_slab, oo);
1019}
1020
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001021static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001022{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001023 unhash_openowner(oo);
1024 list_del(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001025 release_last_closed_stateid(oo);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001026 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001027}
1028
Marc Eshel5282fd72009-04-03 08:27:52 +03001029static inline int
1030hash_sessionid(struct nfs4_sessionid *sessionid)
1031{
1032 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1033
1034 return sid->sequence % SESSION_HASH_SIZE;
1035}
1036
Trond Myklebust8f199b82012-03-20 15:11:17 -04001037#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001038static inline void
1039dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1040{
1041 u32 *ptr = (u32 *)(&sessionid->data[0]);
1042 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1043}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001044#else
1045static inline void
1046dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1047{
1048}
1049#endif
1050
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001051/*
1052 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1053 * won't be used for replay.
1054 */
1055void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1056{
1057 struct nfs4_stateowner *so = cstate->replay_owner;
1058
1059 if (nfserr == nfserr_replay_me)
1060 return;
1061
1062 if (!seqid_mutating_err(ntohl(nfserr))) {
1063 cstate->replay_owner = NULL;
1064 return;
1065 }
1066 if (!so)
1067 return;
1068 if (so->so_is_open_owner)
1069 release_last_closed_stateid(openowner(so));
1070 so->so_seqid++;
1071 return;
1072}
Marc Eshel5282fd72009-04-03 08:27:52 +03001073
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001074static void
1075gen_sessionid(struct nfsd4_session *ses)
1076{
1077 struct nfs4_client *clp = ses->se_client;
1078 struct nfsd4_sessionid *sid;
1079
1080 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1081 sid->clientid = clp->cl_clientid;
1082 sid->sequence = current_sessionid++;
1083 sid->reserved = 0;
1084}
1085
1086/*
Andy Adamsona649637c72009-08-28 08:45:01 -04001087 * The protocol defines ca_maxresponssize_cached to include the size of
1088 * the rpc header, but all we need to cache is the data starting after
1089 * the end of the initial SEQUENCE operation--the rest we regenerate
1090 * each time. Therefore we can advertise a ca_maxresponssize_cached
1091 * value that is the number of bytes in our cache plus a few additional
1092 * bytes. In order to stay on the safe side, and not promise more than
1093 * we can cache, those additional bytes must be the minimum possible: 24
1094 * bytes of rpc header (xid through accept state, with AUTH_NULL
1095 * verifier), 12 for the compound header (with zero-length tag), and 44
1096 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001097 */
Andy Adamsona649637c72009-08-28 08:45:01 -04001098#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1099
Andy Adamson557ce262009-08-28 08:45:04 -04001100static void
1101free_session_slots(struct nfsd4_session *ses)
1102{
1103 int i;
1104
1105 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1106 kfree(ses->se_slots[i]);
1107}
1108
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001109/*
1110 * We don't actually need to cache the rpc and session headers, so we
1111 * can allocate a little less for each slot:
1112 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001113static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001114{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001115 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001116
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001117 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1118 size = 0;
1119 else
1120 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1121 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001122}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001123
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001124/*
1125 * XXX: If we run out of reserved DRC memory we could (up to a point)
1126 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001127 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001128 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001129static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001130{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001131 u32 slotsize = slot_bytes(ca);
1132 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001133 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001134
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001135 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001136 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1137 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001138 num = min_t(int, num, avail / slotsize);
1139 nfsd_drc_mem_used += num * slotsize;
1140 spin_unlock(&nfsd_drc_lock);
1141
1142 return num;
1143}
1144
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001145static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001146{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001147 int slotsize = slot_bytes(ca);
1148
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001149 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001150 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001151 spin_unlock(&nfsd_drc_lock);
1152}
1153
Kinglong Mee60810e52014-01-01 00:35:47 +08001154static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1155 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001156{
Kinglong Mee60810e52014-01-01 00:35:47 +08001157 int numslots = fattrs->maxreqs;
1158 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001159 struct nfsd4_session *new;
1160 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001161
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001162 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001163 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1164 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001165
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001166 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001167 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001168 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001169 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001170 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001171 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001172 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001173 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001174 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001175
1176 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1177 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1178
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001179 return new;
1180out_free:
1181 while (i--)
1182 kfree(new->se_slots[i]);
1183 kfree(new);
1184 return NULL;
1185}
1186
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001187static void free_conn(struct nfsd4_conn *c)
1188{
1189 svc_xprt_put(c->cn_xprt);
1190 kfree(c);
1191}
1192
1193static void nfsd4_conn_lost(struct svc_xpt_user *u)
1194{
1195 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1196 struct nfs4_client *clp = c->cn_session->se_client;
1197
1198 spin_lock(&clp->cl_lock);
1199 if (!list_empty(&c->cn_persession)) {
1200 list_del(&c->cn_persession);
1201 free_conn(c);
1202 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001203 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001204 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001205}
1206
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001207static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001208{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001209 struct nfsd4_conn *conn;
1210
1211 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1212 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001213 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001214 svc_xprt_get(rqstp->rq_xprt);
1215 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001216 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001217 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1218 return conn;
1219}
1220
J. Bruce Fields328ead22010-09-29 16:11:06 -04001221static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1222{
1223 conn->cn_session = ses;
1224 list_add(&conn->cn_persession, &ses->se_conns);
1225}
1226
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001227static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1228{
1229 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001230
1231 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001232 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001233 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001234}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001235
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001236static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001237{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001238 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001239 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001240}
1241
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001242static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001243{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001244 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001245
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001246 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001247 ret = nfsd4_register_conn(conn);
1248 if (ret)
1249 /* oops; xprt is already down: */
1250 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001251 /* We may have gained or lost a callback channel: */
1252 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001253}
1254
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001255static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001256{
1257 u32 dir = NFS4_CDFC4_FORE;
1258
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001259 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001260 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001261 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001262}
1263
1264/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001265static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001266{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001267 struct nfs4_client *clp = s->se_client;
1268 struct nfsd4_conn *c;
1269
1270 spin_lock(&clp->cl_lock);
1271 while (!list_empty(&s->se_conns)) {
1272 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1273 list_del_init(&c->cn_persession);
1274 spin_unlock(&clp->cl_lock);
1275
1276 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1277 free_conn(c);
1278
1279 spin_lock(&clp->cl_lock);
1280 }
1281 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001282}
1283
J. Bruce Fields1377b692012-09-11 21:42:40 -04001284static void __free_session(struct nfsd4_session *ses)
1285{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001286 free_session_slots(ses);
1287 kfree(ses);
1288}
1289
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001290static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001291{
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001292 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1293
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001294 lockdep_assert_held(&nn->client_lock);
1295 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001296 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001297 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001298}
Andy Adamson557ce262009-08-28 08:45:04 -04001299
Fengguang Wu135ae822012-11-10 07:20:25 -05001300static 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 -04001301{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001302 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001303 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001304
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001305 new->se_client = clp;
1306 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001307
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001308 INIT_LIST_HEAD(&new->se_conns);
1309
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001310 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001311 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001312 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001313 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001314 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001315 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001316 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001317 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001318 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001319 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001320 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001321 spin_unlock(&nn->client_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001322
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001323 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001324 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001325 /*
1326 * This is a little silly; with sessions there's no real
1327 * use for the callback address. Use the peer address
1328 * as a reasonable default for now, but consider fixing
1329 * the rpc client not to require an address in the
1330 * future:
1331 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001332 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1333 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001334 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001335}
1336
Benny Halevy9089f1b2010-05-12 00:12:26 +03001337/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001338static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001339__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001340{
1341 struct nfsd4_session *elem;
1342 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001343 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001344
1345 dump_sessionid(__func__, sessionid);
1346 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001347 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001348 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001349 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1350 NFS4_MAX_SESSIONID_LEN)) {
1351 return elem;
1352 }
1353 }
1354
1355 dprintk("%s: session not found\n", __func__);
1356 return NULL;
1357}
1358
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001359static struct nfsd4_session *
1360find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1361 __be32 *ret)
1362{
1363 struct nfsd4_session *session;
1364 __be32 status = nfserr_badsession;
1365
1366 session = __find_in_sessionid_hashtbl(sessionid, net);
1367 if (!session)
1368 goto out;
1369 status = nfsd4_get_session_locked(session);
1370 if (status)
1371 session = NULL;
1372out:
1373 *ret = status;
1374 return session;
1375}
1376
Benny Halevy9089f1b2010-05-12 00:12:26 +03001377/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001378static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001379unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001380{
1381 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001382 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001383 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001384 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001385}
1386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1388static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001389STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001391 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001393 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001394 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return 1;
1396}
1397
1398/*
1399 * XXX Should we use a slab cache ?
1400 * This type of memory management is somewhat inefficient, but we use it
1401 * anyway since SETCLIENTID is not a common operation.
1402 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001403static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 struct nfs4_client *clp;
1406
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001407 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1408 if (clp == NULL)
1409 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001410 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001411 if (clp->cl_name.data == NULL) {
1412 kfree(clp);
1413 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001415 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001416 INIT_LIST_HEAD(&clp->cl_sessions);
1417 idr_init(&clp->cl_stateids);
1418 atomic_set(&clp->cl_refcount, 0);
1419 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1420 INIT_LIST_HEAD(&clp->cl_idhash);
1421 INIT_LIST_HEAD(&clp->cl_openowners);
1422 INIT_LIST_HEAD(&clp->cl_delegations);
1423 INIT_LIST_HEAD(&clp->cl_lru);
1424 INIT_LIST_HEAD(&clp->cl_callbacks);
1425 INIT_LIST_HEAD(&clp->cl_revoked);
1426 spin_lock_init(&clp->cl_lock);
1427 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return clp;
1429}
1430
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001431static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432free_client(struct nfs4_client *clp)
1433{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001434 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001435
1436 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001437 while (!list_empty(&clp->cl_sessions)) {
1438 struct nfsd4_session *ses;
1439 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1440 se_perclnt);
1441 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001442 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1443 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001444 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001445 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001446 free_svc_cred(&clp->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001448 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 kfree(clp);
1450}
1451
Benny Halevy84d38ac2010-05-12 00:13:16 +03001452/* must be called under the client_lock */
1453static inline void
1454unhash_client_locked(struct nfs4_client *clp)
1455{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001456 struct nfsd4_session *ses;
1457
Benny Halevy84d38ac2010-05-12 00:13:16 +03001458 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001459 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001460 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1461 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001462 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001463}
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001466destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001468 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001471 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001474 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001475 while (!list_empty(&clp->cl_delegations)) {
1476 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001477 unhash_delegation_locked(dp);
1478 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 }
Benny Halevycdc97502014-05-30 09:09:30 -04001480 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 while (!list_empty(&reaplist)) {
1482 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001483 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001484 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001486 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001487 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001488 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001489 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001490 }
NeilBrownea1da632005-06-23 22:04:17 -07001491 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001492 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1493 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001495 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001496 if (clp->cl_cb_conn.cb_xprt)
1497 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001498 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001499 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001500 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001501 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001502 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001503 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001504 unhash_client_locked(clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001505 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1506 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001507 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508}
1509
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001510static void expire_client(struct nfs4_client *clp)
1511{
1512 nfsd4_client_record_remove(clp);
1513 destroy_client(clp);
1514}
1515
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001516static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1517{
1518 memcpy(target->cl_verifier.data, source->data,
1519 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
1521
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001522static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1523{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1525 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1526}
1527
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001528static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001529{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001530 if (source->cr_principal) {
1531 target->cr_principal =
1532 kstrdup(source->cr_principal, GFP_KERNEL);
1533 if (target->cr_principal == NULL)
1534 return -ENOMEM;
1535 } else
1536 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001537 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 target->cr_uid = source->cr_uid;
1539 target->cr_gid = source->cr_gid;
1540 target->cr_group_info = source->cr_group_info;
1541 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001542 target->cr_gss_mech = source->cr_gss_mech;
1543 if (source->cr_gss_mech)
1544 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001548static long long
1549compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1550{
1551 long long res;
1552
1553 res = o1->len - o2->len;
1554 if (res)
1555 return res;
1556 return (long long)memcmp(o1->data, o2->data, o1->len);
1557}
1558
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001559static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001560{
NeilBrowna55370a2005-06-23 22:03:52 -07001561 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
1564static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001565same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1566{
1567 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
1570static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001571same_clid(clientid_t *cl1, clientid_t *cl2)
1572{
1573 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001576static bool groups_equal(struct group_info *g1, struct group_info *g2)
1577{
1578 int i;
1579
1580 if (g1->ngroups != g2->ngroups)
1581 return false;
1582 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001583 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001584 return false;
1585 return true;
1586}
1587
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001588/*
1589 * RFC 3530 language requires clid_inuse be returned when the
1590 * "principal" associated with a requests differs from that previously
1591 * used. We use uid, gid's, and gss principal string as our best
1592 * approximation. We also don't want to allow non-gss use of a client
1593 * established using gss: in theory cr_principal should catch that
1594 * change, but in practice cr_principal can be null even in the gss case
1595 * since gssd doesn't always pass down a principal string.
1596 */
1597static bool is_gss_cred(struct svc_cred *cr)
1598{
1599 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1600 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1601}
1602
1603
Vivek Trivedi5559b502012-07-24 21:18:20 +05301604static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001605same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1606{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001607 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001608 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1609 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001610 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1611 return false;
1612 if (cr1->cr_principal == cr2->cr_principal)
1613 return true;
1614 if (!cr1->cr_principal || !cr2->cr_principal)
1615 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301616 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
J. Bruce Fields57266a62013-04-13 14:27:29 -04001619static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1620{
1621 struct svc_cred *cr = &rqstp->rq_cred;
1622 u32 service;
1623
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001624 if (!cr->cr_gss_mech)
1625 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001626 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1627 return service == RPC_GSS_SVC_INTEGRITY ||
1628 service == RPC_GSS_SVC_PRIVACY;
1629}
1630
1631static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1632{
1633 struct svc_cred *cr = &rqstp->rq_cred;
1634
1635 if (!cl->cl_mach_cred)
1636 return true;
1637 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1638 return false;
1639 if (!svc_rqst_integrity_protected(rqstp))
1640 return false;
1641 if (!cr->cr_principal)
1642 return false;
1643 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1644}
1645
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001646static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001647{
1648 static u32 current_clientid = 1;
1649
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001650 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 clp->cl_clientid.cl_id = current_clientid++;
1652}
1653
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001654static void gen_confirm(struct nfs4_client *clp)
1655{
Chuck Leverab4684d2012-03-02 17:13:50 -05001656 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001657 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Jeff Laytonf4199922014-06-17 07:44:11 -04001659 /*
1660 * This is opaque to client, so no need to byte-swap. Use
1661 * __force to keep sparse happy
1662 */
1663 verf[0] = (__force __be32)get_seconds();
1664 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001665 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666}
1667
Jeff Layton4770d722014-07-29 21:34:10 -04001668static struct nfs4_stid *
1669find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001670{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001671 struct nfs4_stid *ret;
1672
1673 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1674 if (!ret || !ret->sc_type)
1675 return NULL;
1676 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001677}
1678
Jeff Layton4770d722014-07-29 21:34:10 -04001679static struct nfs4_stid *
1680find_stateid(struct nfs4_client *cl, stateid_t *t)
1681{
1682 struct nfs4_stid *ret;
1683
1684 spin_lock(&cl->cl_lock);
1685 ret = find_stateid_locked(cl, t);
1686 spin_unlock(&cl->cl_lock);
1687 return ret;
1688}
1689
1690static struct nfs4_stid *
1691find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001692{
1693 struct nfs4_stid *s;
1694
Jeff Layton4770d722014-07-29 21:34:10 -04001695 spin_lock(&cl->cl_lock);
1696 s = find_stateid_locked(cl, t);
1697 if (s != NULL && !(typemask & s->sc_type))
1698 s = NULL;
1699 spin_unlock(&cl->cl_lock);
1700 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001701}
1702
Jeff Layton2216d442012-11-12 15:00:57 -05001703static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001704 struct svc_rqst *rqstp, nfs4_verifier *verf)
1705{
1706 struct nfs4_client *clp;
1707 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001708 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001709 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001710 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001711
1712 clp = alloc_client(name);
1713 if (clp == NULL)
1714 return NULL;
1715
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001716 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1717 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001718 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001719 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001720 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001721 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001722 }
Jeff Layton02e12152014-07-16 10:31:57 -04001723 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001724 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001725 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001726 copy_verf(clp, verf);
1727 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001728 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001729 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001730 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001731 return clp;
1732}
1733
NeilBrownfd39ca92005-06-23 22:04:03 -07001734static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001735add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1736{
1737 struct rb_node **new = &(root->rb_node), *parent = NULL;
1738 struct nfs4_client *clp;
1739
1740 while (*new) {
1741 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1742 parent = *new;
1743
1744 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1745 new = &((*new)->rb_left);
1746 else
1747 new = &((*new)->rb_right);
1748 }
1749
1750 rb_link_node(&new_clp->cl_namenode, parent, new);
1751 rb_insert_color(&new_clp->cl_namenode, root);
1752}
1753
1754static struct nfs4_client *
1755find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1756{
1757 long long cmp;
1758 struct rb_node *node = root->rb_node;
1759 struct nfs4_client *clp;
1760
1761 while (node) {
1762 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1763 cmp = compare_blob(&clp->cl_name, name);
1764 if (cmp > 0)
1765 node = node->rb_left;
1766 else if (cmp < 0)
1767 node = node->rb_right;
1768 else
1769 return clp;
1770 }
1771 return NULL;
1772}
1773
1774static void
1775add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
1777 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001778 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001780 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001781 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001783 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001784 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
NeilBrownfd39ca92005-06-23 22:04:03 -07001787static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788move_to_confirmed(struct nfs4_client *clp)
1789{
1790 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001791 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001794 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001795 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001796 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001797 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 renew_client(clp);
1799}
1800
1801static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001802find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
1804 struct nfs4_client *clp;
1805 unsigned int idhashval = clientid_hashval(clid->cl_id);
1806
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001807 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001808 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001809 if ((bool)clp->cl_minorversion != sessions)
1810 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001811 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 }
1815 return NULL;
1816}
1817
1818static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001819find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1820{
1821 struct list_head *tbl = nn->conf_id_hashtbl;
1822
1823 return find_client_in_id_table(tbl, clid, sessions);
1824}
1825
1826static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001827find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001829 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001831 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832}
1833
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001834static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001835{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001836 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001837}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001838
NeilBrown28ce6052005-06-23 22:03:56 -07001839static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001840find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001841{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001842 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001843}
1844
1845static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001846find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001847{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001848 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001849}
1850
NeilBrownfd39ca92005-06-23 22:04:03 -07001851static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001852gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001854 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001855 struct sockaddr *sa = svc_addr(rqstp);
1856 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001857 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Jeff Layton7077ecb2009-08-14 12:57:58 -04001859 /* Currently, we only support tcp and tcp6 for the callback channel */
1860 if (se->se_callback_netid_len == 3 &&
1861 !memcmp(se->se_callback_netid_val, "tcp", 3))
1862 expected_family = AF_INET;
1863 else if (se->se_callback_netid_len == 4 &&
1864 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1865 expected_family = AF_INET6;
1866 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 goto out_err;
1868
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001869 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001870 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001871 (struct sockaddr *)&conn->cb_addr,
1872 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001873
J. Bruce Fields07263f12010-05-31 19:09:40 -04001874 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001876
J. Bruce Fields07263f12010-05-31 19:09:40 -04001877 if (conn->cb_addr.ss_family == AF_INET6)
1878 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001879
J. Bruce Fields07263f12010-05-31 19:09:40 -04001880 conn->cb_prog = se->se_callback_prog;
1881 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001882 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 return;
1884out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001885 conn->cb_addr.ss_family = AF_UNSPEC;
1886 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001887 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 "will not receive delegations\n",
1889 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 return;
1892}
1893
Andy Adamson074fe892009-04-03 08:28:15 +03001894/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04001895 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001896 */
Trond Myklebustb6076642014-06-30 11:48:35 -04001897static void
Andy Adamson074fe892009-04-03 08:28:15 +03001898nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1899{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001900 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04001901 struct nfsd4_slot *slot = resp->cstate.slot;
1902 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001903
Andy Adamson557ce262009-08-28 08:45:04 -04001904 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001905
Andy Adamson557ce262009-08-28 08:45:04 -04001906 slot->sl_opcnt = resp->opcnt;
1907 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001908
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001909 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001910 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001911 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001912 return;
1913 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001914 base = resp->cstate.data_offset;
1915 slot->sl_datalen = buf->len - base;
1916 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04001917 WARN("%s: sessions DRC could not cache compound\n", __func__);
1918 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001919}
1920
1921/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001922 * Encode the replay sequence operation from the slot values.
1923 * If cachethis is FALSE encode the uncached rep error on the next
1924 * operation which sets resp->p and increments resp->opcnt for
1925 * nfs4svc_encode_compoundres.
1926 *
Andy Adamson074fe892009-04-03 08:28:15 +03001927 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001928static __be32
1929nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1930 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001931{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001932 struct nfsd4_op *op;
1933 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001934
Andy Adamsonabfabf82009-07-23 19:02:18 -04001935 /* Encode the replayed sequence operation */
1936 op = &args->ops[resp->opcnt - 1];
1937 nfsd4_encode_operation(resp, op);
1938
1939 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05001940 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001941 op = &args->ops[resp->opcnt++];
1942 op->status = nfserr_retry_uncached_rep;
1943 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001944 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001945 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001946}
1947
1948/*
Andy Adamson557ce262009-08-28 08:45:04 -04001949 * The sequence operation is not cached because we can use the slot and
1950 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001951 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04001952static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001953nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1954 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001955{
Andy Adamson557ce262009-08-28 08:45:04 -04001956 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001957 struct xdr_stream *xdr = &resp->xdr;
1958 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03001959 __be32 status;
1960
Andy Adamson557ce262009-08-28 08:45:04 -04001961 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001962
Andy Adamsonabfabf82009-07-23 19:02:18 -04001963 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04001964 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04001965 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001966
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001967 p = xdr_reserve_space(xdr, slot->sl_datalen);
1968 if (!p) {
1969 WARN_ON_ONCE(1);
1970 return nfserr_serverfault;
1971 }
1972 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
1973 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03001974
Andy Adamson557ce262009-08-28 08:45:04 -04001975 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001976 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001977}
1978
Andy Adamson0733d212009-04-03 08:28:01 +03001979/*
1980 * Set the exchange_id flags returned by the server.
1981 */
1982static void
1983nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1984{
1985 /* pNFS is not supported */
1986 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1987
1988 /* Referrals are supported, Migration is not. */
1989 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1990
1991 /* set the wire flags to return to client. */
1992 clid->flags = new->cl_exchange_flags;
1993}
1994
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001995static bool client_has_state(struct nfs4_client *clp)
1996{
1997 /*
1998 * Note clp->cl_openowners check isn't quite right: there's no
1999 * need to count owners without stateid's.
2000 *
2001 * Also note we should probably be using this in 4.0 case too.
2002 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002003 return !list_empty(&clp->cl_openowners)
2004 || !list_empty(&clp->cl_delegations)
2005 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002006}
2007
Al Virob37ad282006-10-19 23:28:59 -07002008__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002009nfsd4_exchange_id(struct svc_rqst *rqstp,
2010 struct nfsd4_compound_state *cstate,
2011 struct nfsd4_exchange_id *exid)
2012{
Andy Adamson0733d212009-04-03 08:28:01 +03002013 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002014 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002015 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002016 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002017 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002018 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002019 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002020
Jeff Layton363168b2009-08-14 12:57:56 -04002021 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002022 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002023 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002024 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002025 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002026
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002027 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002028 return nfserr_inval;
2029
Andy Adamson0733d212009-04-03 08:28:01 +03002030 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002031 case SP4_MACH_CRED:
2032 if (!svc_rqst_integrity_protected(rqstp))
2033 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002034 case SP4_NONE:
2035 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002036 default: /* checked by xdr code */
2037 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002038 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002039 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002040 }
2041
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002042 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03002043 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002044 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002045 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002046 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2047 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2048
J. Bruce Fields136e6582012-05-12 20:37:23 -04002049 if (update) {
2050 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002051 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002052 goto out;
2053 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002054 if (!mach_creds_match(conf, rqstp)) {
2055 status = nfserr_wrong_cred;
2056 goto out;
2057 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002058 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002059 status = nfserr_perm;
2060 goto out;
2061 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002062 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002063 status = nfserr_not_same;
2064 goto out;
2065 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002066 /* case 6 */
2067 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2068 new = conf;
2069 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002070 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002071 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002072 if (client_has_state(conf)) {
2073 status = nfserr_clid_inuse;
2074 goto out;
2075 }
Andy Adamson0733d212009-04-03 08:28:01 +03002076 expire_client(conf);
2077 goto out_new;
2078 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002079 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002080 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002081 new = conf;
2082 goto out_copy;
2083 }
2084 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04002085 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002086 }
2087
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002088 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002089 status = nfserr_noent;
2090 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002091 }
2092
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002093 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002094 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03002095 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002096
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002097 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002098out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05002099 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03002100 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04002101 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03002102 goto out;
2103 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002104 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002105 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002106
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002107 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002108 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03002109out_copy:
2110 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2111 exid->clientid.cl_id = new->cl_clientid.cl_id;
2112
J. Bruce Fields778df3f2012-05-25 21:40:23 -04002113 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03002114 nfsd4_set_ex_flags(new, exid);
2115
2116 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04002117 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002118 status = nfs_ok;
2119
2120out:
2121 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03002122 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002123}
2124
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002125static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002126check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002127{
Andy Adamson88e588d2009-07-23 19:02:15 -04002128 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2129 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002130
2131 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002132 if (slot_inuse) {
2133 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002134 return nfserr_jukebox;
2135 else
2136 return nfserr_seq_misordered;
2137 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002138 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002139 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002140 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002141 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002142 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002143 return nfserr_seq_misordered;
2144}
2145
Andy Adamson49557cc2009-07-23 19:02:16 -04002146/*
2147 * Cache the create session result into the create session single DRC
2148 * slot cache by saving the xdr structure. sl_seqid has been set.
2149 * Do this for solo or embedded create session operations.
2150 */
2151static void
2152nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002153 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002154{
2155 slot->sl_status = nfserr;
2156 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2157}
2158
2159static __be32
2160nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2161 struct nfsd4_clid_slot *slot)
2162{
2163 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2164 return slot->sl_status;
2165}
2166
Mi Jinlong1b74c252011-07-14 14:50:17 +08002167#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2168 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2169 1 + /* MIN tag is length with zero, only length */ \
2170 3 + /* version, opcount, opcode */ \
2171 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2172 /* seqid, slotID, slotID, cache */ \
2173 4 ) * sizeof(__be32))
2174
2175#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2176 2 + /* verifier: AUTH_NULL, length 0 */\
2177 1 + /* status */ \
2178 1 + /* MIN tag is length with zero, only length */ \
2179 3 + /* opcount, opcode, opstatus*/ \
2180 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2181 /* seqid, slotID, slotID, slotID, status */ \
2182 5 ) * sizeof(__be32))
2183
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002184static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002185{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002186 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2187
J. Bruce Fields373cd402013-04-08 15:42:12 -04002188 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2189 return nfserr_toosmall;
2190 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2191 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002192 ca->headerpadsz = 0;
2193 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2194 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2195 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2196 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2197 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2198 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2199 /*
2200 * Note decreasing slot size below client's request may make it
2201 * difficult for client to function correctly, whereas
2202 * decreasing the number of slots will (just?) affect
2203 * performance. When short on memory we therefore prefer to
2204 * decrease number of slots instead of their size. Clients that
2205 * request larger slots than they need will get poor results:
2206 */
2207 ca->maxreqs = nfsd4_get_drc_mem(ca);
2208 if (!ca->maxreqs)
2209 return nfserr_jukebox;
2210
J. Bruce Fields373cd402013-04-08 15:42:12 -04002211 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002212}
2213
Kinglong Mee8a891632013-12-23 18:11:02 +08002214#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2215 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2216#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2217 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2218
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002219static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2220{
2221 ca->headerpadsz = 0;
2222
2223 /*
2224 * These RPC_MAX_HEADER macros are overkill, especially since we
2225 * don't even do gss on the backchannel yet. But this is still
2226 * less than 1k. Tighten up this estimate in the unlikely event
2227 * it turns out to be a problem for some client:
2228 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002229 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002230 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002231 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002232 return nfserr_toosmall;
2233 ca->maxresp_cached = 0;
2234 if (ca->maxops < 2)
2235 return nfserr_toosmall;
2236
2237 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002238}
2239
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002240static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2241{
2242 switch (cbs->flavor) {
2243 case RPC_AUTH_NULL:
2244 case RPC_AUTH_UNIX:
2245 return nfs_ok;
2246 default:
2247 /*
2248 * GSS case: the spec doesn't allow us to return this
2249 * error. But it also doesn't allow us not to support
2250 * GSS.
2251 * I'd rather this fail hard than return some error the
2252 * client might think it can already handle:
2253 */
2254 return nfserr_encr_alg_unsupp;
2255 }
2256}
2257
Andy Adamson069b6ad2009-04-03 08:27:58 +03002258__be32
2259nfsd4_create_session(struct svc_rqst *rqstp,
2260 struct nfsd4_compound_state *cstate,
2261 struct nfsd4_create_session *cr_ses)
2262{
Jeff Layton363168b2009-08-14 12:57:56 -04002263 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002264 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002265 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002266 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002267 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002268 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002269 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002270
Mi Jinlonga62573d2011-03-23 17:57:07 +08002271 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2272 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002273 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2274 if (status)
2275 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002276 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002277 if (status)
2278 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002279 status = check_backchannel_attrs(&cr_ses->back_channel);
2280 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002281 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002282 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002283 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002284 if (!new)
2285 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002286 conn = alloc_conn_from_crses(rqstp, cr_ses);
2287 if (!conn)
2288 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002289
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002290 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002291 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002292 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002293 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002294
2295 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002296 status = nfserr_wrong_cred;
2297 if (!mach_creds_match(conf, rqstp))
2298 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002299 cs_slot = &conf->cl_cs_slot;
2300 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002301 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002302 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002303 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002304 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002305 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002306 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002307 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002308 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002309 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002310 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002311 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002312 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002313 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002314 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002315 status = nfserr_wrong_cred;
2316 if (!mach_creds_match(unconf, rqstp))
2317 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002318 cs_slot = &unconf->cl_cs_slot;
2319 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002320 if (status) {
2321 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002322 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002323 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002324 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002325 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002326 if (old) {
2327 status = mark_client_expired(old);
2328 if (status)
2329 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002330 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002331 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002332 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002333 conf = unconf;
2334 } else {
2335 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002336 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002337 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002338 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002339 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002340 * We do not support RDMA or persistent sessions
2341 */
2342 cr_ses->flags &= ~SESSION4_PERSIST;
2343 cr_ses->flags &= ~SESSION4_RDMA;
2344
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002345 init_session(rqstp, new, conf, cr_ses);
2346 nfsd4_init_conn(rqstp, conn, new);
2347
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002348 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002349 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002350 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002351 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002352
Andy Adamson49557cc2009-07-23 19:02:16 -04002353 /* cache solo and embedded create sessions under the state lock */
2354 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002355 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002356 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002357out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08002358 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002359 free_conn(conn);
2360out_free_session:
2361 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002362out_release_drc_mem:
2363 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002364 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002365}
2366
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002367static __be32 nfsd4_map_bcts_dir(u32 *dir)
2368{
2369 switch (*dir) {
2370 case NFS4_CDFC4_FORE:
2371 case NFS4_CDFC4_BACK:
2372 return nfs_ok;
2373 case NFS4_CDFC4_FORE_OR_BOTH:
2374 case NFS4_CDFC4_BACK_OR_BOTH:
2375 *dir = NFS4_CDFC4_BOTH;
2376 return nfs_ok;
2377 };
2378 return nfserr_inval;
2379}
2380
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002381__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2382{
2383 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002384 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002385 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002386
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002387 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2388 if (status)
2389 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002390 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002391 session->se_cb_prog = bc->bc_cb_program;
2392 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002393 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002394
2395 nfsd4_probe_callback(session->se_client);
2396
2397 return nfs_ok;
2398}
2399
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002400__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2401 struct nfsd4_compound_state *cstate,
2402 struct nfsd4_bind_conn_to_session *bcts)
2403{
2404 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002405 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002406 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002407 struct net *net = SVC_NET(rqstp);
2408 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002409
2410 if (!nfsd4_last_compound_op(rqstp))
2411 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002412 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002413 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002414 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002415 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002416 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002417 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002418 status = nfserr_wrong_cred;
2419 if (!mach_creds_match(session->se_client, rqstp))
2420 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002421 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002422 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002423 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002424 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002425 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002426 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002427 goto out;
2428 nfsd4_init_conn(rqstp, conn, session);
2429 status = nfs_ok;
2430out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002431 nfsd4_put_session(session);
2432out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002433 nfs4_unlock_state();
2434 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002435}
2436
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002437static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2438{
2439 if (!session)
2440 return 0;
2441 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2442}
2443
Andy Adamson069b6ad2009-04-03 08:27:58 +03002444__be32
2445nfsd4_destroy_session(struct svc_rqst *r,
2446 struct nfsd4_compound_state *cstate,
2447 struct nfsd4_destroy_session *sessionid)
2448{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002449 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002450 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002451 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002452 struct net *net = SVC_NET(r);
2453 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002454
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002455 nfs4_lock_state();
2456 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002457 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002458 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002459 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002460 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002461 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002462 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002463 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002464 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002465 if (!ses)
2466 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002467 status = nfserr_wrong_cred;
2468 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002469 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002470 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002471 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002472 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002473 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002474 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002475
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002476 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002477
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002478 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002479 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002480out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002481 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002482out_client_lock:
2483 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002484out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002485 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002486 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002487}
2488
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002489static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002490{
2491 struct nfsd4_conn *c;
2492
2493 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002494 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002495 return c;
2496 }
2497 }
2498 return NULL;
2499}
2500
J. Bruce Fields57266a62013-04-13 14:27:29 -04002501static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002502{
2503 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002504 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002505 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002506 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002507
2508 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002509 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002510 if (c)
2511 goto out_free;
2512 status = nfserr_conn_not_bound_to_session;
2513 if (clp->cl_mach_cred)
2514 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002515 __nfsd4_hash_conn(new, ses);
2516 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002517 ret = nfsd4_register_conn(new);
2518 if (ret)
2519 /* oops; xprt is already down: */
2520 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002521 return nfs_ok;
2522out_free:
2523 spin_unlock(&clp->cl_lock);
2524 free_conn(new);
2525 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002526}
2527
Mi Jinlong868b89c2011-04-27 09:09:58 +08002528static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2529{
2530 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2531
2532 return args->opcnt > session->se_fchannel.maxops;
2533}
2534
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002535static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2536 struct nfsd4_session *session)
2537{
2538 struct xdr_buf *xb = &rqstp->rq_arg;
2539
2540 return xb->len > session->se_fchannel.maxreq_sz;
2541}
2542
Andy Adamson069b6ad2009-04-03 08:27:58 +03002543__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002544nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002545 struct nfsd4_compound_state *cstate,
2546 struct nfsd4_sequence *seq)
2547{
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002548 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002549 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002550 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002551 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002552 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002553 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002554 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002555 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002556 struct net *net = SVC_NET(rqstp);
2557 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002558
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002559 if (resp->opcnt != 1)
2560 return nfserr_sequence_pos;
2561
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002562 /*
2563 * Will be either used or freed by nfsd4_sequence_check_conn
2564 * below.
2565 */
2566 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2567 if (!conn)
2568 return nfserr_jukebox;
2569
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002570 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002571 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002572 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002573 goto out_no_session;
2574 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002575
Mi Jinlong868b89c2011-04-27 09:09:58 +08002576 status = nfserr_too_many_ops;
2577 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002578 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002579
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002580 status = nfserr_req_too_big;
2581 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002582 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002583
Benny Halevyb85d4c02009-04-03 08:28:08 +03002584 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002585 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002586 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002587
Andy Adamson557ce262009-08-28 08:45:04 -04002588 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002589 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2590
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002591 /* We do not negotiate the number of slots yet, so set the
2592 * maxslots to the session maxreqs which is used to encode
2593 * sr_highest_slotid and the sr_target_slot id to maxslots */
2594 seq->maxslots = session->se_fchannel.maxreqs;
2595
J. Bruce Fields73e79482012-02-13 16:39:00 -05002596 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2597 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002598 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002599 status = nfserr_seq_misordered;
2600 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002601 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002602 cstate->slot = slot;
2603 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002604 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002605 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002606 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002607 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002608 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002609 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002610 }
2611 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002612 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002613
J. Bruce Fields57266a62013-04-13 14:27:29 -04002614 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002615 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002616 if (status)
2617 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002618
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002619 buflen = (seq->cachethis) ?
2620 session->se_fchannel.maxresp_cached :
2621 session->se_fchannel.maxresp_sz;
2622 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2623 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002624 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002625 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002626 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002627
2628 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002629 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002630 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002631 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002632 if (seq->cachethis)
2633 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002634 else
2635 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002636
2637 cstate->slot = slot;
2638 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002639 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002640
Benny Halevyb85d4c02009-04-03 08:28:08 +03002641out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002642 switch (clp->cl_cb_state) {
2643 case NFSD4_CB_DOWN:
2644 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2645 break;
2646 case NFSD4_CB_FAULT:
2647 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2648 break;
2649 default:
2650 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002651 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002652 if (!list_empty(&clp->cl_revoked))
2653 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002654out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002655 if (conn)
2656 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002657 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002658 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002659out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002660 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002661 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002662}
2663
Trond Myklebustb6076642014-06-30 11:48:35 -04002664void
2665nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2666{
2667 struct nfsd4_compound_state *cs = &resp->cstate;
2668
2669 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002670 if (cs->status != nfserr_replay_cache) {
2671 nfsd4_store_cache_entry(resp);
2672 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2673 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002674 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002675 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002676 } else if (cs->clp)
2677 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002678}
2679
Mi Jinlong345c2842011-10-20 17:51:39 +08002680__be32
2681nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2682{
2683 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002684 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002685 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002686
2687 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002688 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002689 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002690 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002691
2692 if (conf) {
2693 clp = conf;
2694
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002695 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002696 status = nfserr_clientid_busy;
2697 goto out;
2698 }
2699 } else if (unconf)
2700 clp = unconf;
2701 else {
2702 status = nfserr_stale_clientid;
2703 goto out;
2704 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002705 if (!mach_creds_match(clp, rqstp)) {
2706 status = nfserr_wrong_cred;
2707 goto out;
2708 }
Mi Jinlong345c2842011-10-20 17:51:39 +08002709 expire_client(clp);
2710out:
2711 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002712 return status;
2713}
2714
Andy Adamson069b6ad2009-04-03 08:27:58 +03002715__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002716nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2717{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002718 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002719
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002720 if (rc->rca_one_fs) {
2721 if (!cstate->current_fh.fh_dentry)
2722 return nfserr_nofilehandle;
2723 /*
2724 * We don't take advantage of the rca_one_fs case.
2725 * That's OK, it's optional, we can safely ignore it.
2726 */
2727 return nfs_ok;
2728 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002729
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002730 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002731 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002732 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2733 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002734 goto out;
2735
2736 status = nfserr_stale_clientid;
2737 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002738 /*
2739 * The following error isn't really legal.
2740 * But we only get here if the client just explicitly
2741 * destroyed the client. Surely it no longer cares what
2742 * error it gets back on an operation for the dead
2743 * client.
2744 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002745 goto out;
2746
2747 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002748 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002749out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002750 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002751 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002752}
2753
2754__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002755nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2756 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002758 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002760 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002761 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002762 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2763
J. Bruce Fields63db4632012-05-18 21:54:19 -04002764 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002766 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002767 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002768 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002770 if (clp_used_exchangeid(conf))
2771 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002772 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002773 char addr_str[INET6_ADDRSTRLEN];
2774 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2775 sizeof(addr_str));
2776 dprintk("NFSD: setclientid: string in use by client "
2777 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 goto out;
2779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002781 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002782 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002785 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002786 if (new == NULL)
2787 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002788 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002789 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002791 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002792 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002793 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002794 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002795 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2797 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2798 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2799 status = nfs_ok;
2800out:
2801 nfs4_unlock_state();
2802 return status;
2803}
2804
2805
Al Virob37ad282006-10-19 23:28:59 -07002806__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002807nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2808 struct nfsd4_compound_state *cstate,
2809 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810{
NeilBrown21ab45a2005-06-23 22:04:14 -07002811 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2813 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002814 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002815 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002817 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002820
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002821 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002822 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002823 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002824 * We try hard to give out unique clientid's, so if we get an
2825 * attempt to confirm the same clientid with a different cred,
2826 * there's a bug somewhere. Let's charitably assume it's our
2827 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002828 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002829 status = nfserr_serverfault;
2830 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2831 goto out;
2832 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2833 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002834 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002835 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2836 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002838 else /* case 4: client hasn't noticed we rebooted yet? */
2839 status = nfserr_stale_clientid;
2840 goto out;
2841 }
2842 status = nfs_ok;
2843 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002844 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2845 nfsd4_probe_callback(conf);
2846 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002847 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002848 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002849 if (conf) {
2850 status = mark_client_expired(conf);
2851 if (status)
2852 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002853 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002854 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002855 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002856 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 nfs4_unlock_state();
2860 return status;
2861}
2862
J. Bruce Fields32513b42011-10-13 16:00:16 -04002863static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002865 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2866}
2867
2868/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04002869static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04002870{
Trond Myklebustca943212014-07-23 16:17:39 -04002871 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Trond Myklebust950e0112014-06-30 11:48:31 -04002873 lockdep_assert_held(&state_lock);
2874
J. Bruce Fields32513b42011-10-13 16:00:16 -04002875 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04002876 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002877 INIT_LIST_HEAD(&fp->fi_stateids);
2878 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04002879 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002880 fp->fi_had_conflict = false;
2881 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04002882 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002883 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2884 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04002885 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886}
2887
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002888void
NeilBrowne60d4392005-06-23 22:03:01 -07002889nfsd4_free_slabs(void)
2890{
Christoph Hellwigabf11352014-05-21 07:43:03 -07002891 kmem_cache_destroy(openowner_slab);
2892 kmem_cache_destroy(lockowner_slab);
2893 kmem_cache_destroy(file_slab);
2894 kmem_cache_destroy(stateid_slab);
2895 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002896}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
Bryan Schumaker72083392011-11-01 15:24:59 -04002898int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899nfsd4_init_slabs(void)
2900{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002901 openowner_slab = kmem_cache_create("nfsd4_openowners",
2902 sizeof(struct nfs4_openowner), 0, 0, NULL);
2903 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002904 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002905 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002906 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002907 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002908 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07002909 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002910 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002911 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002912 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07002913 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002914 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002915 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002916 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002917 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002918 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002919 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002920 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07002922
2923out_free_stateid_slab:
2924 kmem_cache_destroy(stateid_slab);
2925out_free_file_slab:
2926 kmem_cache_destroy(file_slab);
2927out_free_lockowner_slab:
2928 kmem_cache_destroy(lockowner_slab);
2929out_free_openowner_slab:
2930 kmem_cache_destroy(openowner_slab);
2931out:
NeilBrowne60d4392005-06-23 22:03:01 -07002932 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2933 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934}
2935
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002936static void init_nfs4_replay(struct nfs4_replay *rp)
2937{
2938 rp->rp_status = nfserr_serverfault;
2939 rp->rp_buflen = 0;
2940 rp->rp_buf = rp->rp_ibuf;
2941}
2942
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002943static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944{
2945 struct nfs4_stateowner *sop;
2946
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002947 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002948 if (!sop)
2949 return NULL;
2950
2951 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2952 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002953 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002954 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002956 sop->so_owner.len = owner->len;
2957
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002958 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002959 sop->so_client = clp;
2960 init_nfs4_replay(&sop->so_replay);
2961 return sop;
2962}
2963
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002964static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002965{
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002966 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2967
2968 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002969 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970}
2971
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002972static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04002973alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002974 struct nfsd4_compound_state *cstate)
2975{
Trond Myklebust13d6f662014-06-30 11:48:45 -04002976 struct nfs4_client *clp = cstate->clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002977 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002979 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2980 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002982 oo->oo_owner.so_is_open_owner = 1;
2983 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04002984 oo->oo_flags = NFS4_OO_NEW;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002985 if (nfsd4_has_session(cstate))
2986 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002987 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002988 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002989 INIT_LIST_HEAD(&oo->oo_close_lru);
2990 hash_openowner(oo, clp, strhashval);
2991 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992}
2993
J. Bruce Fields996e0932011-10-17 11:14:48 -04002994static 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 -04002995 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002997 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04002998 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002999 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
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;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003006 spin_lock(&fp->fi_lock);
3007 list_add(&stp->st_perfile, &fp->fi_stateids);
3008 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009}
3010
3011static void
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003012move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013{
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003014 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3015
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003016 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003018 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003019 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020}
3021
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04003023same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
3024 clientid_t *clid)
3025{
3026 return (sop->so_owner.len == owner->len) &&
3027 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
3028 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029}
3030
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003031static struct nfs4_openowner *
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003032find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
3033 bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034{
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003035 struct nfs4_stateowner *so;
3036 struct nfs4_openowner *oo;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003037 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003039 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003040 if (!so->so_is_open_owner)
3041 continue;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003042 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
3043 oo = openowner(so);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003044 clp = oo->oo_owner.so_client;
3045 if ((bool)clp->cl_minorversion != sessions)
3046 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003047 renew_client(oo->oo_owner.so_client);
3048 return oo;
3049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 }
3051 return NULL;
3052}
3053
3054/* search file_hashtbl[] for file */
3055static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003056find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
Trond Myklebustca943212014-07-23 16:17:39 -04003058 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 struct nfs4_file *fp;
3060
Trond Myklebust950e0112014-06-30 11:48:31 -04003061 lockdep_assert_held(&state_lock);
3062
Jeff Layton89876f82013-04-02 09:01:59 -04003063 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003064 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003065 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 }
3069 return NULL;
3070}
3071
Trond Myklebust950e0112014-06-30 11:48:31 -04003072static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003073find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003074{
3075 struct nfs4_file *fp;
3076
3077 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003078 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003079 spin_unlock(&state_lock);
3080 return fp;
3081}
3082
3083static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003084find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003085{
3086 struct nfs4_file *fp;
3087
3088 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003089 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003090 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003091 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003092 fp = new;
3093 }
3094 spin_unlock(&state_lock);
3095
3096 return fp;
3097}
3098
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003099/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 * Called to check deny when READ with all zero stateid or
3101 * WRITE with all zero or all one stateid
3102 */
Al Virob37ad282006-10-19 23:28:59 -07003103static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3105{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003107 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Trond Myklebustca943212014-07-23 16:17:39 -04003109 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003110 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003111 return ret;
3112 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003113 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003114 if (fp->fi_share_deny & deny_type)
3115 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003116 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003117 put_nfs4_file(fp);
3118 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119}
3120
Jeff Layton02e12152014-07-16 10:31:57 -04003121void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122{
Trond Myklebust11b91642014-07-29 21:34:08 -04003123 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3124 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003125
Trond Myklebust11b91642014-07-29 21:34:08 -04003126 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003127
Jeff Layton02e12152014-07-16 10:31:57 -04003128 /*
3129 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003130 * already holding inode->i_lock.
3131 *
Jeff Laytondff13992014-07-08 14:02:49 -04003132 * If the dl_time != 0, then we know that it has already been
3133 * queued for a lease break. Don't queue it again.
3134 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003135 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003136 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003137 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003138 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003139 }
Jeff Layton02e12152014-07-16 10:31:57 -04003140 spin_unlock(&state_lock);
3141}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
Jeff Layton02e12152014-07-16 10:31:57 -04003143static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3144{
3145 /*
3146 * We're assuming the state code never drops its reference
3147 * without first removing the lease. Since we're in this lease
3148 * callback (and since the lease code is serialized by the kernel
3149 * lock) we know the server hasn't removed the lease yet, we know
3150 * it's safe to take a reference.
3151 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003152 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003153 nfsd4_cb_recall(dp);
3154}
3155
Jeff Layton1c8c6012013-06-21 08:58:15 -04003156/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003157static void nfsd_break_deleg_cb(struct file_lock *fl)
3158{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003159 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3160 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003161
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003162 if (!fp) {
3163 WARN(1, "(%p)->fl_owner NULL\n", fl);
3164 return;
3165 }
3166 if (fp->fi_had_conflict) {
3167 WARN(1, "duplicate break on %p\n", fp);
3168 return;
3169 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003170 /*
3171 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003172 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003173 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003174 */
3175 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
Jeff Layton02e12152014-07-16 10:31:57 -04003177 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003178 fp->fi_had_conflict = true;
3179 /*
3180 * If there are no delegations on the list, then we can't count on this
3181 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3182 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3183 * true should keep any new delegations from being hashed.
3184 */
3185 if (list_empty(&fp->fi_delegations))
3186 fl->fl_break_time = jiffies;
3187 else
3188 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3189 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003190 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191}
3192
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193static
3194int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3195{
3196 if (arg & F_UNLCK)
3197 return lease_modify(onlist, arg);
3198 else
3199 return -EAGAIN;
3200}
3201
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003202static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003203 .lm_break = nfsd_break_deleg_cb,
3204 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205};
3206
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003207static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3208{
3209 if (nfsd4_has_session(cstate))
3210 return nfs_ok;
3211 if (seqid == so->so_seqid - 1)
3212 return nfserr_replay_me;
3213 if (seqid == so->so_seqid)
3214 return nfs_ok;
3215 return nfserr_bad_seqid;
3216}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Jeff Layton4b24ca72014-06-30 11:48:44 -04003218static __be32 lookup_clientid(clientid_t *clid,
3219 struct nfsd4_compound_state *cstate,
3220 struct nfsd_net *nn)
3221{
3222 struct nfs4_client *found;
3223
3224 if (cstate->clp) {
3225 found = cstate->clp;
3226 if (!same_clid(&found->cl_clientid, clid))
3227 return nfserr_stale_clientid;
3228 return nfs_ok;
3229 }
3230
3231 if (STALE_CLIENTID(clid, nn))
3232 return nfserr_stale_clientid;
3233
3234 /*
3235 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3236 * cached already then we know this is for is for v4.0 and "sessions"
3237 * will be false.
3238 */
3239 WARN_ON_ONCE(cstate->session);
3240 found = find_confirmed_client(clid, false, nn);
3241 if (!found)
3242 return nfserr_expired;
3243
3244 /* Cache the nfs4_client in cstate! */
3245 cstate->clp = found;
3246 atomic_inc(&found->cl_refcount);
3247 return nfs_ok;
3248}
3249
Al Virob37ad282006-10-19 23:28:59 -07003250__be32
Andy Adamson66689582009-04-03 08:28:45 +03003251nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003252 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 clientid_t *clientid = &open->op_clientid;
3255 struct nfs4_client *clp = NULL;
3256 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003257 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003258 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003260 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003262 /*
3263 * In case we need it later, after we've already created the
3264 * file and don't want to risk a further failure:
3265 */
3266 open->op_file = nfsd4_alloc_file();
3267 if (open->op_file == NULL)
3268 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
Trond Myklebust2d91e892014-06-30 11:48:46 -04003270 status = lookup_clientid(clientid, cstate, nn);
3271 if (status)
3272 return status;
3273 clp = cstate->clp;
3274
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003275 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003276 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003277 open->op_openowner = oo;
3278 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003279 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003281 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003282 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003283 release_openowner(oo);
3284 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003285 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003286 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003287 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3288 if (status)
3289 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003290 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003291new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003292 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003293 if (oo == NULL)
3294 return nfserr_jukebox;
3295 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003296alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003297 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003298 if (!open->op_stp)
3299 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003300 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301}
3302
Al Virob37ad282006-10-19 23:28:59 -07003303static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003304nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3305{
3306 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3307 return nfserr_openmode;
3308 else
3309 return nfs_ok;
3310}
3311
Daniel Mackc47d8322011-05-16 16:38:14 +02003312static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003313{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003314 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3315}
3316
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003317static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003318{
3319 struct nfs4_stid *ret;
3320
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003321 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003322 if (!ret)
3323 return NULL;
3324 return delegstateid(ret);
3325}
3326
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003327static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3328{
3329 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3330 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3331}
3332
Al Virob37ad282006-10-19 23:28:59 -07003333static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003334nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003335 struct nfs4_delegation **dp)
3336{
3337 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003338 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07003339
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003340 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07003341 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003342 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003343 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07003344 status = nfs4_check_delegmode(*dp, flags);
3345 if (status)
3346 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003347out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003348 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003349 return nfs_ok;
3350 if (status)
3351 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003352 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003353 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003354}
3355
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003356static struct nfs4_ol_stateid *
3357nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003359 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003360 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361
Trond Myklebust1d31a252014-07-10 14:07:25 -04003362 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003363 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 /* ignore lock owners */
3365 if (local->st_stateowner->so_is_open_owner == 0)
3366 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003367 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003368 ret = local;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003369 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003372 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003373 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374}
3375
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003376static inline int nfs4_access_to_access(u32 nfs4_access)
3377{
3378 int flags = 0;
3379
3380 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3381 flags |= NFSD_MAY_READ;
3382 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3383 flags |= NFSD_MAY_WRITE;
3384 return flags;
3385}
3386
Al Virob37ad282006-10-19 23:28:59 -07003387static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3389 struct nfsd4_open *open)
3390{
3391 struct iattr iattr = {
3392 .ia_valid = ATTR_SIZE,
3393 .ia_size = 0,
3394 };
3395 if (!open->op_truncate)
3396 return 0;
3397 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003398 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3400}
3401
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003402static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003403 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3404 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003405{
Trond Myklebustde186432014-07-10 14:07:26 -04003406 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003407 __be32 status;
3408 int oflag = nfs4_access_to_omode(open->op_share_access);
3409 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003410 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003411
Trond Myklebustde186432014-07-10 14:07:26 -04003412 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003413
3414 /*
3415 * Are we trying to set a deny mode that would conflict with
3416 * current access?
3417 */
3418 status = nfs4_file_check_deny(fp, open->op_share_deny);
3419 if (status != nfs_ok) {
3420 spin_unlock(&fp->fi_lock);
3421 goto out;
3422 }
3423
3424 /* set access to the file */
3425 status = nfs4_file_get_access(fp, open->op_share_access);
3426 if (status != nfs_ok) {
3427 spin_unlock(&fp->fi_lock);
3428 goto out;
3429 }
3430
3431 /* Set access bits in stateid */
3432 old_access_bmap = stp->st_access_bmap;
3433 set_access(open->op_share_access, stp);
3434
3435 /* Set new deny mask */
3436 old_deny_bmap = stp->st_deny_bmap;
3437 set_deny(open->op_share_deny, stp);
3438 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3439
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003440 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003441 spin_unlock(&fp->fi_lock);
3442 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003443 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003444 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003445 spin_lock(&fp->fi_lock);
3446 if (!fp->fi_fds[oflag]) {
3447 fp->fi_fds[oflag] = filp;
3448 filp = NULL;
3449 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003450 }
Trond Myklebustde186432014-07-10 14:07:26 -04003451 spin_unlock(&fp->fi_lock);
3452 if (filp)
3453 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003454
3455 status = nfsd4_truncate(rqstp, cur_fh, open);
3456 if (status)
3457 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003458out:
3459 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003460out_put_access:
3461 stp->st_access_bmap = old_access_bmap;
3462 nfs4_file_put_access(fp, open->op_share_access);
3463 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3464 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003465}
3466
Al Virob37ad282006-10-19 23:28:59 -07003467static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003468nfs4_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 -07003469{
Al Virob37ad282006-10-19 23:28:59 -07003470 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003471 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003473 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003474 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003475
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003476 /* test and set deny mode */
3477 spin_lock(&fp->fi_lock);
3478 status = nfs4_file_check_deny(fp, open->op_share_deny);
3479 if (status == nfs_ok) {
3480 old_deny_bmap = stp->st_deny_bmap;
3481 set_deny(open->op_share_deny, stp);
3482 fp->fi_share_deny |=
3483 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3484 }
3485 spin_unlock(&fp->fi_lock);
3486
3487 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003490 status = nfsd4_truncate(rqstp, cur_fh, open);
3491 if (status != nfs_ok)
3492 reset_union_bmap_deny(old_deny_bmap, stp);
3493 return status;
3494}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003497nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003499 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500}
3501
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003502/* Should we give out recallable state?: */
3503static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3504{
3505 if (clp->cl_cb_state == NFSD4_CB_UP)
3506 return true;
3507 /*
3508 * In the sessions case, since we don't have to establish a
3509 * separate connection for callbacks, we assume it's OK
3510 * until we hear otherwise:
3511 */
3512 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3513}
3514
Jeff Laytond564fbe2014-07-16 10:31:58 -04003515static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003516{
3517 struct file_lock *fl;
3518
3519 fl = locks_alloc_lock();
3520 if (!fl)
3521 return NULL;
3522 locks_init_lock(fl);
3523 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003524 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003525 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3526 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003527 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003528 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003529 return fl;
3530}
3531
J. Bruce Fields99c41512013-05-21 16:21:25 -04003532static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003533{
Trond Myklebust11b91642014-07-29 21:34:08 -04003534 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003535 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003536 struct file *filp;
3537 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003538
Jeff Laytond564fbe2014-07-16 10:31:58 -04003539 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003540 if (!fl)
3541 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003542 filp = find_readable_file(fp);
3543 if (!filp) {
3544 /* We should always have a readable file here */
3545 WARN_ON_ONCE(1);
3546 return -EBADF;
3547 }
3548 fl->fl_file = filp;
3549 status = vfs_setlease(filp, fl->fl_type, &fl);
3550 if (status) {
3551 locks_free_lock(fl);
3552 goto out_fput;
3553 }
Benny Halevycdc97502014-05-30 09:09:30 -04003554 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003555 spin_lock(&fp->fi_lock);
3556 /* Did the lease get broken before we took the lock? */
3557 status = -EAGAIN;
3558 if (fp->fi_had_conflict)
3559 goto out_unlock;
3560 /* Race breaker */
3561 if (fp->fi_lease) {
3562 status = 0;
3563 atomic_inc(&fp->fi_delegees);
3564 hash_delegation_locked(dp, fp);
3565 goto out_unlock;
3566 }
3567 fp->fi_lease = fl;
3568 fp->fi_deleg_file = filp;
3569 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003570 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003571 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003572 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003573 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003574out_unlock:
3575 spin_unlock(&fp->fi_lock);
3576 spin_unlock(&state_lock);
3577out_fput:
3578 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003579 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003580}
3581
Jeff Layton0b266932014-07-25 07:34:25 -04003582static struct nfs4_delegation *
3583nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3584 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003585{
Jeff Layton0b266932014-07-25 07:34:25 -04003586 int status;
3587 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003588
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003589 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003590 return ERR_PTR(-EAGAIN);
3591
3592 dp = alloc_init_deleg(clp, fh);
3593 if (!dp)
3594 return ERR_PTR(-ENOMEM);
3595
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003596 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003597 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003598 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003599 dp->dl_stid.sc_file = fp;
Jeff Layton417c6622014-07-21 09:34:57 -04003600 if (!fp->fi_lease) {
3601 spin_unlock(&fp->fi_lock);
3602 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003603 status = nfs4_setlease(dp);
3604 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003605 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003606 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003607 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003608 status = -EAGAIN;
3609 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003610 }
Benny Halevy931ee562014-05-30 09:09:27 -04003611 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003612 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003613out_unlock:
3614 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003615 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003616out:
3617 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003618 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003619 return ERR_PTR(status);
3620 }
3621 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003622}
3623
Benny Halevy4aa89132012-02-21 14:16:44 -08003624static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3625{
3626 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3627 if (status == -EAGAIN)
3628 open->op_why_no_deleg = WND4_CONTENTION;
3629 else {
3630 open->op_why_no_deleg = WND4_RESOURCE;
3631 switch (open->op_deleg_want) {
3632 case NFS4_SHARE_WANT_READ_DELEG:
3633 case NFS4_SHARE_WANT_WRITE_DELEG:
3634 case NFS4_SHARE_WANT_ANY_DELEG:
3635 break;
3636 case NFS4_SHARE_WANT_CANCEL:
3637 open->op_why_no_deleg = WND4_CANCELLED;
3638 break;
3639 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003640 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003641 }
3642 }
3643}
3644
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645/*
3646 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003647 *
3648 * Note we don't support write delegations, and won't until the vfs has
3649 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 */
3651static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003652nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3653 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654{
3655 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003656 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3657 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003658 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003659 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003661 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003662 open->op_recall = 0;
3663 switch (open->op_claim_type) {
3664 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003665 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003666 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003667 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3668 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003669 break;
3670 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003671 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003672 /*
3673 * Let's not give out any delegations till everyone's
3674 * had the chance to reclaim theirs....
3675 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003676 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003677 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003678 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003679 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003680 /*
3681 * Also, if the file was opened for write or
3682 * create, there's a good chance the client's
3683 * about to write to it, resulting in an
3684 * immediate recall (since we don't support
3685 * write delegations):
3686 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003687 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003688 goto out_no_deleg;
3689 if (open->op_create == NFS4_OPEN_CREATE)
3690 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003691 break;
3692 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003693 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003694 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003695 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003696 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003697 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003699 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003701 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003702 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003703 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003704 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003705out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003706 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3707 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003708 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003709 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003710 open->op_recall = 1;
3711 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003712
3713 /* 4.1 client asking for a delegation? */
3714 if (open->op_deleg_want)
3715 nfsd4_open_deleg_none_ext(open, status);
3716 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717}
3718
Benny Halevye27f49c2012-02-21 14:16:54 -08003719static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3720 struct nfs4_delegation *dp)
3721{
3722 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3723 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3724 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3725 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3726 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3727 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3728 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3729 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3730 }
3731 /* Otherwise the client must be confused wanting a delegation
3732 * it already has, therefore we don't return
3733 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3734 */
3735}
3736
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737/*
3738 * called with nfs4_lock_state() held.
3739 */
Al Virob37ad282006-10-19 23:28:59 -07003740__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3742{
Andy Adamson66689582009-04-03 08:28:45 +03003743 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003744 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003746 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003747 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003748 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 /*
3751 * Lookup file; if found, lookup stateid and check open request,
3752 * and check for delegations in the process of being recalled.
3753 * If not found, create the nfs4_file struct
3754 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003755 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04003756 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04003757 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003758 if (status)
3759 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003760 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04003762 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003763 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003764 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003765 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003766 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 }
3768
3769 /*
3770 * OPEN the file, or upgrade an existing OPEN.
3771 * If truncate fails, the OPEN fails.
3772 */
3773 if (stp) {
3774 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003775 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 if (status)
3777 goto out;
3778 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003779 stp = open->op_stp;
3780 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003781 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003782 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3783 if (status) {
3784 release_open_stateid(stp);
3785 goto out;
3786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003788 update_stateid(&stp->st_stid.sc_stateid);
3789 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790
Benny Halevyd24433c2012-02-16 20:57:17 +02003791 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02003792 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3793 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3794 open->op_why_no_deleg = WND4_NOT_WANTED;
3795 goto nodeleg;
3796 }
3797 }
3798
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 /*
3800 * Attempt to hand out a delegation. No error return, because the
3801 * OPEN succeeds even if we fail.
3802 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003803 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003804nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 status = nfs_ok;
3806
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003807 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003808 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003810 /* 4.1 client trying to upgrade/downgrade delegation? */
3811 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003812 open->op_deleg_want)
3813 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003814
NeilBrown13cd2182005-06-23 22:03:10 -07003815 if (fp)
3816 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003817 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003818 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 /*
3820 * To finish the open response, we just need to set the rflags.
3821 */
3822 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003823 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003824 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3826
3827 return status;
3828}
3829
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003830void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3831{
3832 if (open->op_openowner) {
3833 struct nfs4_openowner *oo = open->op_openowner;
3834
3835 if (!list_empty(&oo->oo_owner.so_stateids))
3836 list_del_init(&oo->oo_close_lru);
3837 if (oo->oo_flags & NFS4_OO_NEW) {
3838 if (status) {
3839 release_openowner(oo);
3840 open->op_openowner = NULL;
3841 } else
3842 oo->oo_flags &= ~NFS4_OO_NEW;
3843 }
3844 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003845 if (open->op_file)
3846 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003847 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04003848 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003849}
3850
Al Virob37ad282006-10-19 23:28:59 -07003851__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003852nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3853 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854{
3855 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003856 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003857 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858
3859 nfs4_lock_state();
3860 dprintk("process_renew(%08x/%08x): starting\n",
3861 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003862 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003863 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003865 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003867 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003868 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 goto out;
3870 status = nfs_ok;
3871out:
3872 nfs4_unlock_state();
3873 return status;
3874}
3875
NeilBrowna76b4312005-06-23 22:04:01 -07003876static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003877nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07003878{
Jeff Layton33dcc482012-04-10 11:08:48 -04003879 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003880 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04003881 return;
3882
NeilBrowna76b4312005-06-23 22:04:01 -07003883 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003884 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003885 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003886 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003887 /*
3888 * Now that every NFSv4 client has had the chance to recover and
3889 * to see the (possibly new, possibly shorter) lease time, we
3890 * can safely set the next grace time to the current lease time:
3891 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03003892 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003893}
3894
NeilBrownfd39ca92005-06-23 22:04:03 -07003895static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003896nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897{
3898 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003899 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 struct nfs4_delegation *dp;
3901 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003902 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003903 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904
3905 nfs4_lock_state();
3906
3907 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003908 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03003909 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003910 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03003911 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 clp = list_entry(pos, struct nfs4_client, cl_lru);
3913 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3914 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003915 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 break;
3917 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003918 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03003919 dprintk("NFSD: client in use (clientid %08x)\n",
3920 clp->cl_clientid.cl_id);
3921 continue;
3922 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003923 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003924 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003925 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03003926 list_for_each_safe(pos, next, &reaplist) {
3927 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 dprintk("NFSD: purging unused client (clientid %08x)\n",
3929 clp->cl_clientid.cl_id);
3930 expire_client(clp);
3931 }
Benny Halevycdc97502014-05-30 09:09:30 -04003932 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003933 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03003935 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3936 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003938 t = dp->dl_time - cutoff;
3939 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 break;
3941 }
Jeff Layton42690672014-07-25 07:34:20 -04003942 unhash_delegation_locked(dp);
3943 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 }
Benny Halevycdc97502014-05-30 09:09:30 -04003945 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04003946 while (!list_empty(&reaplist)) {
3947 dp = list_first_entry(&reaplist, struct nfs4_delegation,
3948 dl_recall_lru);
3949 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003950 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003952 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003953 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3954 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003955 t = oo->oo_time - cutoff;
3956 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 break;
3958 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003959 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04003961 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04003963 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964}
3965
Harvey Harrisona254b242008-02-20 12:49:00 -08003966static struct workqueue_struct *laundry_wq;
3967static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08003968
3969static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003970laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971{
3972 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003973 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3974 work);
3975 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3976 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003978 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003980 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981}
3982
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003983static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07003984{
Trond Myklebust11b91642014-07-29 21:34:08 -04003985 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003986 return nfserr_bad_stateid;
3987 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988}
3989
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003991access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003993 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3994 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3995 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996}
3997
3998static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003999access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004001 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4002 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003}
4004
4005static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004006__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007{
Al Virob37ad282006-10-19 23:28:59 -07004008 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009
J. Bruce Fields02921912010-07-29 15:16:59 -04004010 /* For lock stateid's, we test the parent open, not the lock: */
4011 if (stp->st_openstp)
4012 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004013 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004015 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 goto out;
4017 status = nfs_ok;
4018out:
4019 return status;
4020}
4021
Al Virob37ad282006-10-19 23:28:59 -07004022static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004023check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004025 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004027 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004028 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 * conflicting state; so we must wait out the grace period. */
4030 return nfserr_grace;
4031 } else if (flags & WR_STATE)
4032 return nfs4_share_conflict(current_fh,
4033 NFS4_SHARE_DENY_WRITE);
4034 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4035 return nfs4_share_conflict(current_fh,
4036 NFS4_SHARE_DENY_READ);
4037}
4038
4039/*
4040 * Allow READ/WRITE during grace period on recovered state only for files
4041 * that are not able to provide mandatory locking.
4042 */
4043static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004044grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004046 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047}
4048
J. Bruce Fields81b82962011-08-23 11:03:29 -04004049/* Returns true iff a is later than b: */
4050static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4051{
Jim Rees1a9357f2013-05-17 17:33:00 -04004052 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004053}
4054
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004055static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004056{
Andy Adamson66689582009-04-03 08:28:45 +03004057 /*
4058 * When sessions are used the stateid generation number is ignored
4059 * when it is zero.
4060 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004061 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004062 return nfs_ok;
4063
4064 if (in->si_generation == ref->si_generation)
4065 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004066
J. Bruce Fields0836f582008-01-26 19:08:12 -05004067 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004068 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004069 return nfserr_bad_stateid;
4070 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004071 * However, we could see a stateid from the past, even from a
4072 * non-buggy client. For example, if the client sends a lock
4073 * while some IO is outstanding, the lock may bump si_generation
4074 * while the IO is still in flight. The client could avoid that
4075 * situation by waiting for responses on all the IO requests,
4076 * but better performance may result in retrying IO that
4077 * receives an old_stateid error if requests are rarely
4078 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004079 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004080 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004081}
4082
Chuck Lever7df302f2012-05-29 13:56:37 -04004083static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004084{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004085 struct nfs4_stid *s;
4086 struct nfs4_ol_stateid *ols;
4087 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004088
Chuck Lever7df302f2012-05-29 13:56:37 -04004089 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4090 return nfserr_bad_stateid;
4091 /* Client debugging aid. */
4092 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4093 char addr_str[INET6_ADDRSTRLEN];
4094 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4095 sizeof(addr_str));
4096 pr_warn_ratelimited("NFSD: client %s testing state ID "
4097 "with incorrect client ID\n", addr_str);
4098 return nfserr_bad_stateid;
4099 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004100 s = find_stateid(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004101 if (!s)
Chuck Lever7df302f2012-05-29 13:56:37 -04004102 return nfserr_bad_stateid;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004103 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004104 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004105 return status;
J. Bruce Fields23340032013-04-09 17:42:28 -04004106 switch (s->sc_type) {
4107 case NFS4_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004108 return nfs_ok;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004109 case NFS4_REVOKED_DELEG_STID:
4110 return nfserr_deleg_revoked;
J. Bruce Fields23340032013-04-09 17:42:28 -04004111 case NFS4_OPEN_STID:
4112 case NFS4_LOCK_STID:
4113 ols = openlockstateid(s);
4114 if (ols->st_stateowner->so_is_open_owner
4115 && !(openowner(ols->st_stateowner)->oo_flags
4116 & NFS4_OO_CONFIRMED))
4117 return nfserr_bad_stateid;
4118 return nfs_ok;
4119 default:
4120 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004121 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004122 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004123 case NFS4_CLOSED_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004124 return nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004125 }
Bryan Schumaker17456802011-07-13 10:50:48 -04004126}
4127
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004128static __be32
4129nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4130 stateid_t *stateid, unsigned char typemask,
4131 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004132{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004133 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004134
4135 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4136 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004137 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004138 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004139 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004140 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004141 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004142 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004143 if (status)
4144 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004145 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004146 if (!*s)
4147 return nfserr_bad_stateid;
4148 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004149}
4150
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151/*
4152* Checks for stateid operations
4153*/
Al Virob37ad282006-10-19 23:28:59 -07004154__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004155nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004156 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004158 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004159 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004161 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004163 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004164 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004165 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 if (filpp)
4168 *filpp = NULL;
4169
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004170 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 return nfserr_grace;
4172
4173 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004174 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
Trond Myklebust14bcab12014-04-18 14:44:07 -04004176 nfs4_lock_state();
4177
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004178 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004179 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004180 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004181 if (status)
Trond Myklebust14bcab12014-04-18 14:44:07 -04004182 goto out;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004183 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4184 if (status)
4185 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004186 switch (s->sc_type) {
4187 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004188 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004189 status = nfs4_check_delegmode(dp, flags);
4190 if (status)
4191 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004192 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004193 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004194 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004195 WARN_ON_ONCE(1);
4196 status = nfserr_serverfault;
4197 goto out;
4198 }
Trond Myklebustde186432014-07-10 14:07:26 -04004199 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004200 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004201 break;
4202 case NFS4_OPEN_STID:
4203 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004204 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004205 status = nfs4_check_fh(current_fh, stp);
4206 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004208 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004209 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004211 status = nfs4_check_openmode(stp, flags);
4212 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004214 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004215 struct nfs4_file *fp = stp->st_stid.sc_file;
4216
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004217 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004218 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004219 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004220 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004221 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004222 break;
4223 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004224 status = nfserr_bad_stateid;
4225 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 }
4227 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004228 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004229 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230out:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004231 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 return status;
4233}
4234
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004235static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004236nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004237{
J. Bruce Fieldsa1b8ff42014-05-20 15:55:21 -04004238 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4239
Trond Myklebust11b91642014-07-29 21:34:08 -04004240 if (check_for_locks(stp->st_stid.sc_file, lo))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004241 return nfserr_locks_held;
Jeff Laytonc53530d2014-06-30 11:48:39 -04004242 release_lockowner_if_empty(lo);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004243 return nfs_ok;
4244}
4245
4246/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004247 * Test if the stateid is valid
4248 */
4249__be32
4250nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4251 struct nfsd4_test_stateid *test_stateid)
4252{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004253 struct nfsd4_test_stateid_id *stateid;
4254 struct nfs4_client *cl = cstate->session->se_client;
4255
4256 nfs4_lock_state();
4257 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004258 stateid->ts_id_status =
4259 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004260 nfs4_unlock_state();
4261
Bryan Schumaker17456802011-07-13 10:50:48 -04004262 return nfs_ok;
4263}
4264
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004265__be32
4266nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4267 struct nfsd4_free_stateid *free_stateid)
4268{
4269 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004270 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004271 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004272 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004273 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004274
4275 nfs4_lock_state();
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004276 s = find_stateid(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004277 if (!s)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004278 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004279 switch (s->sc_type) {
4280 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004281 ret = nfserr_locks_held;
4282 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004283 case NFS4_OPEN_STID:
4284 case NFS4_LOCK_STID:
4285 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4286 if (ret)
4287 goto out;
4288 if (s->sc_type == NFS4_LOCK_STID)
4289 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4290 else
4291 ret = nfserr_locks_held;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004292 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004293 case NFS4_REVOKED_DELEG_STID:
4294 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004295 spin_lock(&cl->cl_lock);
4296 list_del_init(&dp->dl_recall_lru);
4297 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004298 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004299 ret = nfs_ok;
4300 break;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004301 default:
4302 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004303 }
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004304out:
4305 nfs4_unlock_state();
4306 return ret;
4307}
4308
NeilBrown4c4cd222005-07-07 17:59:27 -07004309static inline int
4310setlkflg (int type)
4311{
4312 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4313 RD_STATE : WR_STATE;
4314}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004316static __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 -04004317{
4318 struct svc_fh *current_fh = &cstate->current_fh;
4319 struct nfs4_stateowner *sop = stp->st_stateowner;
4320 __be32 status;
4321
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004322 status = nfsd4_check_seqid(cstate, sop, seqid);
4323 if (status)
4324 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004325 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4326 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004327 /*
4328 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004329 * nfserr_replay_me from the previous step, and
4330 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004331 */
4332 return nfserr_bad_stateid;
4333 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4334 if (status)
4335 return status;
4336 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004337}
4338
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339/*
4340 * Checks for sequence id mutating operations.
4341 */
Al Virob37ad282006-10-19 23:28:59 -07004342static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004343nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004344 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004345 struct nfs4_ol_stateid **stpp,
4346 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004348 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004349 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004350 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004352 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4353 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004354
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004356 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004357 if (status)
4358 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004359 stp = openlockstateid(s);
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04004360 if (!nfsd4_has_session(cstate))
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004361 cstate->replay_owner = stp->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004363 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4364 if (!status)
4365 *stpp = stp;
4366 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004367}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004368
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004369static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4370 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004371{
4372 __be32 status;
4373 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004375 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004376 NFS4_OPEN_STID, stpp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004377 if (status)
4378 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004379 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004380 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07004381 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004382 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383}
4384
Al Virob37ad282006-10-19 23:28:59 -07004385__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004386nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004387 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388{
Al Virob37ad282006-10-19 23:28:59 -07004389 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004390 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004391 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004392 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393
Al Viroa6a9f182013-09-16 10:57:01 -04004394 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4395 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004397 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004398 if (status)
4399 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
4401 nfs4_lock_state();
4402
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004403 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004404 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004405 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004406 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004407 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004408 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004409 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004410 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004411 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004412 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004413 update_stateid(&stp->st_stid.sc_stateid);
4414 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004415 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004416 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004417
Jeff Layton2a4317c2012-03-21 16:42:43 -04004418 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004419 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004421 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004422 if (!cstate->replay_owner)
4423 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 return status;
4425}
4426
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004427static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004429 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004430 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004431 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004432 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004433}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004434
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004435static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4436{
4437 switch (to_access) {
4438 case NFS4_SHARE_ACCESS_READ:
4439 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4440 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4441 break;
4442 case NFS4_SHARE_ACCESS_WRITE:
4443 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4444 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4445 break;
4446 case NFS4_SHARE_ACCESS_BOTH:
4447 break;
4448 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004449 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 }
4451}
4452
Al Virob37ad282006-10-19 23:28:59 -07004453__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004454nfsd4_open_downgrade(struct svc_rqst *rqstp,
4455 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004456 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457{
Al Virob37ad282006-10-19 23:28:59 -07004458 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004459 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004460 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461
Al Viroa6a9f182013-09-16 10:57:01 -04004462 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4463 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004465 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004466 if (od->od_deleg_want)
4467 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4468 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469
4470 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004471 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004472 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004473 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004476 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004477 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 stp->st_access_bmap, od->od_share_access);
4479 goto out;
4480 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004481 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004482 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 stp->st_deny_bmap, od->od_share_deny);
4484 goto out;
4485 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004486 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487
Jeff Laytonce0fc432012-05-11 09:45:14 -04004488 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004490 update_stateid(&stp->st_stid.sc_stateid);
4491 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 status = nfs_ok;
4493out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004494 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004495 if (!cstate->replay_owner)
4496 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 return status;
4498}
4499
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004500static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4501{
Trond Myklebustacf92952014-06-30 11:48:37 -04004502 struct nfs4_client *clp = s->st_stid.sc_client;
4503 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4504
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004505 s->st_stid.sc_type = NFS4_CLOSED_STID;
Trond Myklebustacf92952014-06-30 11:48:37 -04004506 unhash_open_stateid(s);
4507
4508 if (clp->cl_minorversion) {
Trond Myklebustacf92952014-06-30 11:48:37 -04004509 if (list_empty(&oo->oo_owner.so_stateids))
4510 release_openowner(oo);
Trond Myklebust60116952014-07-29 21:34:06 -04004511 nfs4_put_stid(&s->st_stid);
Trond Myklebustacf92952014-06-30 11:48:37 -04004512 } else {
Trond Myklebust60116952014-07-29 21:34:06 -04004513 /*
4514 * In the 4.0 case we need to keep the owners around a
4515 * little while to handle CLOSE replay. We still do need
4516 * to release any file access that is held by them
4517 * before returning however.
4518 */
4519 release_all_access(s);
Trond Myklebust11b91642014-07-29 21:34:08 -04004520 if (s->st_stid.sc_file) {
4521 put_nfs4_file(s->st_stid.sc_file);
4522 s->st_stid.sc_file = NULL;
Trond Myklebustf8338832014-07-25 07:34:19 -04004523 }
Trond Myklebustacf92952014-06-30 11:48:37 -04004524 oo->oo_last_closed_stid = s;
Trond Myklebustacf92952014-06-30 11:48:37 -04004525 if (list_empty(&oo->oo_owner.so_stateids))
4526 move_to_close_lru(oo, clp->net);
4527 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004528}
4529
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530/*
4531 * nfs4_unlock_state() called after encode
4532 */
Al Virob37ad282006-10-19 23:28:59 -07004533__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004534nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004535 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536{
Al Virob37ad282006-10-19 23:28:59 -07004537 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004538 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004539 struct net *net = SVC_NET(rqstp);
4540 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541
Al Viroa6a9f182013-09-16 10:57:01 -04004542 dprintk("NFSD: nfsd4_close on file %pd\n",
4543 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
4545 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004546 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4547 &close->cl_stateid,
4548 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004549 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004550 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004551 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004553 update_stateid(&stp->st_stid.sc_stateid);
4554 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004556 nfsd4_close_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004558 if (!cstate->replay_owner)
4559 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 return status;
4561}
4562
Al Virob37ad282006-10-19 23:28:59 -07004563__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004564nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4565 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004567 struct nfs4_delegation *dp;
4568 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004569 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004570 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004571 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004573 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004574 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575
4576 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004577 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004578 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004579 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004580 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004581 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004582 if (status)
4583 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004584
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004585 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004587 nfs4_unlock_state();
4588
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 return status;
4590}
4591
4592
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594
Benny Halevy87df4de2008-12-15 19:42:03 +02004595static inline u64
4596end_offset(u64 start, u64 len)
4597{
4598 u64 end;
4599
4600 end = start + len;
4601 return end >= start ? end: NFS4_MAX_UINT64;
4602}
4603
4604/* last octet in a range */
4605static inline u64
4606last_byte_offset(u64 start, u64 len)
4607{
4608 u64 end;
4609
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004610 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004611 end = start + len;
4612 return end > start ? end - 1: NFS4_MAX_UINT64;
4613}
4614
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615/*
4616 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4617 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4618 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4619 * locking, this prevents us from being completely protocol-compliant. The
4620 * real solution to this problem is to start using unsigned file offsets in
4621 * the VFS, but this is a very deep change!
4622 */
4623static inline void
4624nfs4_transform_lock_offset(struct file_lock *lock)
4625{
4626 if (lock->fl_start < 0)
4627 lock->fl_start = OFFSET_MAX;
4628 if (lock->fl_end < 0)
4629 lock->fl_end = OFFSET_MAX;
4630}
4631
NeilBrownd5b90262006-04-10 22:55:22 -07004632/* Hack!: For now, we're defining this just so we can use a pointer to it
4633 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004634static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004635};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636
4637static inline void
4638nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4639{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004640 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641
NeilBrownd5b90262006-04-10 22:55:22 -07004642 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004643 lo = (struct nfs4_lockowner *) fl->fl_owner;
4644 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4645 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004646 if (!deny->ld_owner.data)
4647 /* We just don't care that much */
4648 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004649 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4650 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004651 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004652nevermind:
4653 deny->ld_owner.len = 0;
4654 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004655 deny->ld_clientid.cl_boot = 0;
4656 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 }
4658 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004659 deny->ld_length = NFS4_MAX_UINT64;
4660 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4662 deny->ld_type = NFS4_READ_LT;
4663 if (fl->fl_type != F_RDLCK)
4664 deny->ld_type = NFS4_WRITE_LT;
4665}
4666
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004667static struct nfs4_lockowner *
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004668find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4669 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670{
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004671 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4672 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004674 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4675 if (so->so_is_open_owner)
4676 continue;
4677 if (!same_owner_str(so, owner, clid))
4678 continue;
4679 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 }
4681 return NULL;
4682}
4683
4684/*
4685 * Alloc a lock owner structure.
4686 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004687 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004689 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004691static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004692alloc_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 -04004693 struct nfs4_lockowner *lo;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004694 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004696 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4697 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004699 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4700 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07004701 /* It is the openowner seqid that will be incremented in encode in the
4702 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004703 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004704 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004705 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706}
4707
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004708static struct nfs4_ol_stateid *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004709alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp,
4710 struct inode *inode,
4711 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712{
Jeff Laytonb49e0842014-07-29 21:34:11 -04004713 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004714 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004715 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716
Jeff Laytonb49e0842014-07-29 21:34:11 -04004717 s = nfs4_alloc_stid(clp, stateid_slab);
4718 if (s == NULL)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04004719 return NULL;
Jeff Laytonb49e0842014-07-29 21:34:11 -04004720 stp = openlockstateid(s);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004721 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004722 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4723 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07004724 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004725 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04004726 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004727 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004729 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004730 list_add(&stp->st_locks, &open_stp->st_locks);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004731 spin_lock(&fp->fi_lock);
4732 list_add(&stp->st_perfile, &fp->fi_stateids);
4733 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 return stp;
4735}
4736
Jeff Laytonc53530d2014-06-30 11:48:39 -04004737static struct nfs4_ol_stateid *
4738find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4739{
4740 struct nfs4_ol_stateid *lst;
4741
4742 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004743 if (lst->st_stid.sc_file == fp)
Jeff Laytonc53530d2014-06-30 11:48:39 -04004744 return lst;
4745 }
4746 return NULL;
4747}
4748
4749
NeilBrownfd39ca92005-06-23 22:04:03 -07004750static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751check_lock_length(u64 offset, u64 length)
4752{
Benny Halevy87df4de2008-12-15 19:42:03 +02004753 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 LOFF_OVERFLOW(offset, length)));
4755}
4756
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004757static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004758{
Trond Myklebust11b91642014-07-29 21:34:08 -04004759 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004760
Jeff Layton7214e862014-07-10 14:07:33 -04004761 lockdep_assert_held(&fp->fi_lock);
4762
Jeff Layton82c5ff12012-05-11 09:45:13 -04004763 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004764 return;
Jeff Layton12659652014-07-10 14:07:28 -04004765 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004766 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004767}
4768
J. Bruce Fields2355c592012-04-25 16:56:22 -04004769static __be32 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, struct nfs4_ol_stateid *ost, struct nfsd4_lock *lock, struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004770{
Trond Myklebust11b91642014-07-29 21:34:08 -04004771 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004772 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4773 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004774 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004775 struct nfs4_lockowner *lo;
4776 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004777 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004778
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004779 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004780 if (!lo) {
4781 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4782 &lock->v.new.owner);
4783 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4784 if (lo == NULL)
4785 return nfserr_jukebox;
4786 } else {
4787 /* with an existing lockowner, seqids must be the same */
4788 if (!cstate->minorversion &&
4789 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004790 return nfserr_bad_seqid;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004791 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004792
4793 *lst = find_lock_stateid(lo, fi);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004794 if (*lst == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004795 *lst = alloc_init_lock_stateid(lo, fi, inode, ost);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004796 if (*lst == NULL) {
4797 release_lockowner_if_empty(lo);
4798 return nfserr_jukebox;
4799 }
4800 *new = true;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004801 }
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004802 return nfs_ok;
4803}
4804
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805/*
4806 * LOCK operation
4807 */
Al Virob37ad282006-10-19 23:28:59 -07004808__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004809nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004810 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004812 struct nfs4_openowner *open_sop = NULL;
4813 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004814 struct nfs4_ol_stateid *lock_stp;
Jeff Layton7214e862014-07-10 14:07:33 -04004815 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004816 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004817 struct file_lock *file_lock = NULL;
4818 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004819 __be32 status = 0;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004820 bool new_state = false;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004821 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004822 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004823 struct net *net = SVC_NET(rqstp);
4824 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
4826 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4827 (long long) lock->lk_offset,
4828 (long long) lock->lk_length);
4829
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 if (check_lock_length(lock->lk_offset, lock->lk_length))
4831 return nfserr_inval;
4832
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004833 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004834 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004835 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4836 return status;
4837 }
4838
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 nfs4_lock_state();
4840
4841 if (lock->lk_is_new) {
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004842 struct nfs4_ol_stateid *open_stp = NULL;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004843
4844 if (nfsd4_has_session(cstate))
4845 /* See rfc 5661 18.10.3: given clientid is ignored: */
4846 memcpy(&lock->v.new.clientid,
4847 &cstate->session->se_client->cl_clientid,
4848 sizeof(clientid_t));
4849
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004851 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004855 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 lock->lk_new_open_seqid,
4857 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004858 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07004859 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004861 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004862 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004863 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004864 &lock->v.new.clientid))
4865 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004866 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4867 &lock_stp, &new_state);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004868 } else
Benny Halevydd453df2009-04-03 08:28:41 +03004869 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004870 lock->lk_old_lock_seqid,
4871 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004872 NFS4_LOCK_STID, &lock_stp, nn);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004873 if (status)
4874 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004875 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004877 lkflg = setlkflg(lock->lk_type);
4878 status = nfs4_check_openmode(lock_stp, lkflg);
4879 if (status)
4880 goto out;
4881
NeilBrown0dd395d2005-07-07 17:59:15 -07004882 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004883 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004884 goto out;
4885 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004886 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004887 goto out;
4888
Jeff Layton21179d82012-08-21 08:03:32 -04004889 file_lock = locks_alloc_lock();
4890 if (!file_lock) {
4891 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4892 status = nfserr_jukebox;
4893 goto out;
4894 }
4895
Trond Myklebust11b91642014-07-29 21:34:08 -04004896 fp = lock_stp->st_stid.sc_file;
Jeff Layton21179d82012-08-21 08:03:32 -04004897 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 switch (lock->lk_type) {
4899 case NFS4_READ_LT:
4900 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04004901 spin_lock(&fp->fi_lock);
4902 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004903 if (filp)
4904 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04004905 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04004906 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004907 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 case NFS4_WRITE_LT:
4909 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04004910 spin_lock(&fp->fi_lock);
4911 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004912 if (filp)
4913 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04004914 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04004915 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004916 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 default:
4918 status = nfserr_inval;
4919 goto out;
4920 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004921 if (!filp) {
4922 status = nfserr_openmode;
4923 goto out;
4924 }
Jeff Layton21179d82012-08-21 08:03:32 -04004925 file_lock->fl_owner = (fl_owner_t)lock_sop;
4926 file_lock->fl_pid = current->tgid;
4927 file_lock->fl_file = filp;
4928 file_lock->fl_flags = FL_POSIX;
4929 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4930 file_lock->fl_start = lock->lk_offset;
4931 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4932 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933
Jeff Layton21179d82012-08-21 08:03:32 -04004934 conflock = locks_alloc_lock();
4935 if (!conflock) {
4936 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4937 status = nfserr_jukebox;
4938 goto out;
4939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940
Jeff Layton21179d82012-08-21 08:03:32 -04004941 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004942 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004944 update_stateid(&lock_stp->st_stid.sc_stateid);
4945 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004947 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004948 break;
4949 case (EAGAIN): /* conflock holds conflicting lock */
4950 status = nfserr_denied;
4951 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04004952 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004953 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 case (EDEADLK):
4955 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004956 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004957 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004958 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004959 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004960 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962out:
Trond Myklebustde186432014-07-10 14:07:26 -04004963 if (filp)
4964 fput(filp);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004965 if (status && new_state)
Jeff Laytonc53530d2014-06-30 11:48:39 -04004966 release_lock_stateid(lock_stp);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004967 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004968 if (!cstate->replay_owner)
4969 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004970 if (file_lock)
4971 locks_free_lock(file_lock);
4972 if (conflock)
4973 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 return status;
4975}
4976
4977/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004978 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4979 * so we do a temporary open here just to get an open file to pass to
4980 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4981 * inode operation.)
4982 */
Al Viro04da6e92012-04-13 00:00:04 -04004983static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004984{
4985 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04004986 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4987 if (!err) {
4988 err = nfserrno(vfs_test_lock(file, lock));
4989 nfsd_close(file);
4990 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004991 return err;
4992}
4993
4994/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 * LOCKT operation
4996 */
Al Virob37ad282006-10-19 23:28:59 -07004997__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004998nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4999 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000{
Jeff Layton21179d82012-08-21 08:03:32 -04005001 struct file_lock *file_lock = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005002 struct nfs4_lockowner *lo;
Al Virob37ad282006-10-19 23:28:59 -07005003 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005004 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005006 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 return nfserr_grace;
5008
5009 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5010 return nfserr_inval;
5011
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 nfs4_lock_state();
5013
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005014 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005015 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005016 if (status)
5017 goto out;
5018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005020 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022
Jeff Layton21179d82012-08-21 08:03:32 -04005023 file_lock = locks_alloc_lock();
5024 if (!file_lock) {
5025 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5026 status = nfserr_jukebox;
5027 goto out;
5028 }
5029 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 switch (lockt->lt_type) {
5031 case NFS4_READ_LT:
5032 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005033 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 break;
5035 case NFS4_WRITE_LT:
5036 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005037 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 break;
5039 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005040 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 status = nfserr_inval;
5042 goto out;
5043 }
5044
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005045 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005046 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005047 file_lock->fl_owner = (fl_owner_t)lo;
5048 file_lock->fl_pid = current->tgid;
5049 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050
Jeff Layton21179d82012-08-21 08:03:32 -04005051 file_lock->fl_start = lockt->lt_offset;
5052 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053
Jeff Layton21179d82012-08-21 08:03:32 -04005054 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055
Jeff Layton21179d82012-08-21 08:03:32 -04005056 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005057 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005058 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005059
Jeff Layton21179d82012-08-21 08:03:32 -04005060 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005062 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 }
5064out:
5065 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005066 if (file_lock)
5067 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 return status;
5069}
5070
Al Virob37ad282006-10-19 23:28:59 -07005071__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005072nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005073 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005075 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005077 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005078 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005079 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005080 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5081
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5083 (long long) locku->lu_offset,
5084 (long long) locku->lu_length);
5085
5086 if (check_lock_length(locku->lu_offset, locku->lu_length))
5087 return nfserr_inval;
5088
5089 nfs4_lock_state();
5090
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005091 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005092 &locku->lu_stateid, NFS4_LOCK_STID,
5093 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005094 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005096 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005097 if (!filp) {
5098 status = nfserr_lock_range;
5099 goto out;
5100 }
Jeff Layton21179d82012-08-21 08:03:32 -04005101 file_lock = locks_alloc_lock();
5102 if (!file_lock) {
5103 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5104 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005105 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005106 }
5107 locks_init_lock(file_lock);
5108 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005109 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005110 file_lock->fl_pid = current->tgid;
5111 file_lock->fl_file = filp;
5112 file_lock->fl_flags = FL_POSIX;
5113 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5114 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115
Jeff Layton21179d82012-08-21 08:03:32 -04005116 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5117 locku->lu_length);
5118 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119
Jeff Layton21179d82012-08-21 08:03:32 -04005120 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005121 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005122 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 goto out_nfserr;
5124 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005125 update_stateid(&stp->st_stid.sc_stateid);
5126 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005127fput:
5128 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005130 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields71c3bcd2011-09-27 21:42:29 -04005131 if (!cstate->replay_owner)
5132 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005133 if (file_lock)
5134 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 return status;
5136
5137out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005138 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005139 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140}
5141
5142/*
5143 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005144 * true: locks held by lockowner
5145 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005147static bool
5148check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149{
5150 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005151 int status = false;
5152 struct file *filp = find_any_file(fp);
5153 struct inode *inode;
5154
5155 if (!filp) {
5156 /* Any valid lock stateid should have some sort of access */
5157 WARN_ON_ONCE(1);
5158 return status;
5159 }
5160
5161 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162
Jeff Layton1c8c6012013-06-21 08:58:15 -04005163 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005165 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005166 status = true;
5167 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005170 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005171 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 return status;
5173}
5174
Al Virob37ad282006-10-19 23:28:59 -07005175__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005176nfsd4_release_lockowner(struct svc_rqst *rqstp,
5177 struct nfsd4_compound_state *cstate,
5178 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179{
5180 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Laytonfd449072014-06-30 11:48:41 -04005181 struct nfs4_stateowner *sop = NULL, *tmp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005182 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005183 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 struct xdr_netobj *owner = &rlockowner->rl_owner;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005185 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07005186 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005187 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188
5189 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5190 clid->cl_boot, clid->cl_id);
5191
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 nfs4_lock_state();
5193
Jeff Layton4b24ca72014-06-30 11:48:44 -04005194 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005195 if (status)
5196 goto out;
5197
NeilBrown3e9e3db2005-06-23 22:04:20 -07005198 status = nfserr_locks_held;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05005199
Jeff Laytonfd449072014-06-30 11:48:41 -04005200 /* Find the matching lock stateowner */
5201 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
5202 if (tmp->so_is_open_owner)
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005203 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005204 if (same_owner_str(tmp, owner, clid)) {
5205 sop = tmp;
5206 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07005208 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005209
5210 /* No matching owner found, maybe a replay? Just declare victory... */
5211 if (!sop) {
5212 status = nfs_ok;
5213 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005215
5216 lo = lockowner(sop);
5217 /* see if there are still any locks associated with it */
5218 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
Trond Myklebust11b91642014-07-29 21:34:08 -04005219 if (check_for_locks(stp->st_stid.sc_file, lo))
Jeff Laytonfd449072014-06-30 11:48:41 -04005220 goto out;
5221 }
5222
5223 status = nfs_ok;
5224 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225out:
5226 nfs4_unlock_state();
5227 return status;
5228}
5229
5230static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005231alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232{
NeilBrowna55370a2005-06-23 22:03:52 -07005233 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234}
5235
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005236bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005237nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005238{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005239 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005240
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005241 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005242 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005243}
5244
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245/*
5246 * failure => all reset bets are off, nfserr_no_grace...
5247 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005248struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005249nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250{
5251 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005252 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253
NeilBrowna55370a2005-06-23 22:03:52 -07005254 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5255 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005256 if (crp) {
5257 strhashval = clientstr_hashval(name);
5258 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005259 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005260 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005261 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005262 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005263 }
5264 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265}
5266
Jeff Layton2a4317c2012-03-21 16:42:43 -04005267void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005268nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005269{
5270 list_del(&crp->cr_strhash);
5271 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005272 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005273}
5274
5275void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005276nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277{
5278 struct nfs4_client_reclaim *crp = NULL;
5279 int i;
5280
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005282 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5283 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005285 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 }
5287 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005288 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289}
5290
5291/*
5292 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005293struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005294nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295{
5296 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 struct nfs4_client_reclaim *crp = NULL;
5298
Jeff Layton278c9312012-11-12 15:00:52 -05005299 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300
Jeff Layton278c9312012-11-12 15:00:52 -05005301 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005302 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005303 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 return crp;
5305 }
5306 }
5307 return NULL;
5308}
5309
5310/*
5311* Called from OPEN. Look for clientid in reclaim list.
5312*/
Al Virob37ad282006-10-19 23:28:59 -07005313__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005314nfs4_check_open_reclaim(clientid_t *clid,
5315 struct nfsd4_compound_state *cstate,
5316 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005318 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005319
5320 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005321 status = lookup_clientid(clid, cstate, nn);
5322 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005323 return nfserr_reclaim_bad;
5324
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005325 if (nfsd4_client_record_check(cstate->clp))
5326 return nfserr_reclaim_bad;
5327
5328 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329}
5330
Bryan Schumaker65178db2011-11-01 13:35:21 -04005331#ifdef CONFIG_NFSD_FAULT_INJECTION
5332
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005333u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5334{
J. Bruce Fields221a6872013-04-01 22:23:49 -04005335 if (mark_client_expired(clp))
5336 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005337 expire_client(clp);
5338 return 1;
5339}
5340
Bryan Schumaker184c1842012-11-29 11:40:44 -05005341u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5342{
5343 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005344 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005345 printk(KERN_INFO "NFS Client: %s\n", buf);
5346 return 1;
5347}
5348
5349static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5350 const char *type)
5351{
5352 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005353 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005354 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5355}
5356
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005357static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5358 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005359{
5360 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005361 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005362 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005363 u64 count = 0;
5364
5365 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005366 list_for_each_entry_safe(stp, st_next,
5367 &oop->oo_owner.so_stateids, st_perstateowner) {
5368 list_for_each_entry_safe(lst, lst_next,
5369 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005370 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005371 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005372 if (++count == max)
5373 return count;
5374 }
5375 }
5376 }
5377
5378 return count;
5379}
5380
5381u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5382{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005383 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005384}
5385
Bryan Schumaker184c1842012-11-29 11:40:44 -05005386u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5387{
5388 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5389 nfsd_print_count(clp, count, "locked files");
5390 return count;
5391}
5392
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005393static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5394{
5395 struct nfs4_openowner *oop, *next;
5396 u64 count = 0;
5397
5398 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5399 if (func)
5400 func(oop);
5401 if (++count == max)
5402 break;
5403 }
5404
5405 return count;
5406}
5407
5408u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5409{
5410 return nfsd_foreach_client_open(clp, max, release_openowner);
5411}
5412
Bryan Schumaker184c1842012-11-29 11:40:44 -05005413u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5414{
5415 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5416 nfsd_print_count(clp, count, "open files");
5417 return count;
5418}
5419
Bryan Schumaker269de302012-11-29 11:40:42 -05005420static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5421 struct list_head *victims)
5422{
5423 struct nfs4_delegation *dp, *next;
5424 u64 count = 0;
5425
Benny Halevycdc97502014-05-30 09:09:30 -04005426 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005427 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005428 if (victims) {
5429 /*
5430 * It's not safe to mess with delegations that have a
5431 * non-zero dl_time. They might have already been broken
5432 * and could be processed by the laundromat outside of
5433 * the state_lock. Just leave them be.
5434 */
5435 if (dp->dl_time != 0)
5436 continue;
5437
Jeff Layton42690672014-07-25 07:34:20 -04005438 unhash_delegation_locked(dp);
5439 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005440 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005441 if (++count == max)
5442 break;
5443 }
5444 return count;
5445}
5446
5447u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5448{
5449 struct nfs4_delegation *dp, *next;
5450 LIST_HEAD(victims);
5451 u64 count;
5452
Benny Halevycdc97502014-05-30 09:09:30 -04005453 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005454 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005455 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005456
Jeff Layton2d4a5322014-07-25 07:34:21 -04005457 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5458 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005459 revoke_delegation(dp);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005460 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005461
5462 return count;
5463}
5464
5465u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5466{
Jeff Laytondff13992014-07-08 14:02:49 -04005467 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005468 LIST_HEAD(victims);
5469 u64 count;
5470
Benny Halevycdc97502014-05-30 09:09:30 -04005471 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005472 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005473 while (!list_empty(&victims)) {
5474 dp = list_first_entry(&victims, struct nfs4_delegation,
5475 dl_recall_lru);
5476 list_del_init(&dp->dl_recall_lru);
5477 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005478 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005479 }
Benny Halevycdc97502014-05-30 09:09:30 -04005480 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005481
5482 return count;
5483}
5484
Bryan Schumaker184c1842012-11-29 11:40:44 -05005485u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5486{
5487 u64 count = 0;
5488
Benny Halevycdc97502014-05-30 09:09:30 -04005489 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005490 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005491 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005492
5493 nfsd_print_count(clp, count, "delegations");
5494 return count;
5495}
5496
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005497u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005498{
5499 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005500 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005501 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005502
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005503 if (!nfsd_netns_ready(nn))
5504 return 0;
5505
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005506 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005507 count += func(clp, max - count);
5508 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005509 break;
5510 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005511
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005512 return count;
5513}
5514
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005515struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5516{
5517 struct nfs4_client *clp;
5518 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5519
5520 if (!nfsd_netns_ready(nn))
5521 return NULL;
5522
5523 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5524 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5525 return clp;
5526 }
5527 return NULL;
5528}
5529
Bryan Schumaker65178db2011-11-01 13:35:21 -04005530#endif /* CONFIG_NFSD_FAULT_INJECTION */
5531
Meelap Shahc2f1a552007-07-17 04:04:39 -07005532/*
5533 * Since the lifetime of a delegation isn't limited to that of an open, a
5534 * client may quite reasonably hang on to a delegation as long as it has
5535 * the inode cached. This becomes an obvious problem the first time a
5536 * client's inode cache approaches the size of the server's total memory.
5537 *
5538 * For now we avoid this problem by imposing a hard limit on the number
5539 * of delegations, which varies according to the server's memory size.
5540 */
5541static void
5542set_max_delegations(void)
5543{
5544 /*
5545 * Allow at most 4 delegations per megabyte of RAM. Quick
5546 * estimates suggest that in the worst case (where every delegation
5547 * is for a different inode), a delegation could take about 1.5K,
5548 * giving a worst case usage of about 6% of memory.
5549 */
5550 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5551}
5552
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005553static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005554{
5555 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5556 int i;
5557
5558 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5559 CLIENT_HASH_SIZE, GFP_KERNEL);
5560 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005561 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005562 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5563 CLIENT_HASH_SIZE, GFP_KERNEL);
5564 if (!nn->unconf_id_hashtbl)
5565 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005566 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5567 OWNER_HASH_SIZE, GFP_KERNEL);
5568 if (!nn->ownerstr_hashtbl)
5569 goto err_ownerstr;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005570 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5571 SESSION_HASH_SIZE, GFP_KERNEL);
5572 if (!nn->sessionid_hashtbl)
5573 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005574
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005575 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005576 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005577 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005578 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005579 for (i = 0; i < OWNER_HASH_SIZE; i++)
5580 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005581 for (i = 0; i < SESSION_HASH_SIZE; i++)
5582 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005583 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005584 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005585 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005586 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005587 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005588 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005589
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005590 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005591 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005592
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005593 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005594
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005595err_sessionid:
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03005596 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005597err_ownerstr:
5598 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005599err_unconf_id:
5600 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005601err:
5602 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005603}
5604
5605static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005606nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005607{
5608 int i;
5609 struct nfs4_client *clp = NULL;
5610 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5611
5612 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5613 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5614 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5615 destroy_client(clp);
5616 }
5617 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005618
Kinglong Mee2b905632014-03-26 22:09:30 +08005619 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5620 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5621 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5622 destroy_client(clp);
5623 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005624 }
5625
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005626 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005627 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005628 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005629 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005630 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005631}
5632
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005633int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005634nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005635{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005636 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005637 int ret;
5638
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005639 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005640 if (ret)
5641 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005642 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005643 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005644 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005645 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005646 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005647 nn->nfsd4_grace, net);
5648 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005649 return 0;
5650}
5651
5652/* initialization to perform when the nfsd service is started: */
5653
5654int
5655nfs4_state_start(void)
5656{
5657 int ret;
5658
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005659 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005660 if (ret)
5661 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005662 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005663 if (laundry_wq == NULL) {
5664 ret = -ENOMEM;
5665 goto out_recovery;
5666 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005667 ret = nfsd4_create_callback_queue();
5668 if (ret)
5669 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005670
Meelap Shahc2f1a552007-07-17 04:04:39 -07005671 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005672
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005673 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005674
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005675out_free_laundry:
5676 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005677out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005678 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679}
5680
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005681void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005682nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005686 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005688 cancel_delayed_work_sync(&nn->laundromat_work);
5689 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005690
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005691 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04005693 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005694 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005696 unhash_delegation_locked(dp);
5697 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 }
Benny Halevycdc97502014-05-30 09:09:30 -04005699 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 list_for_each_safe(pos, next, &reaplist) {
5701 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005702 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04005703 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 }
5705
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005706 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005707 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005708 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709}
5710
5711void
5712nfs4_state_shutdown(void)
5713{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005714 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005715 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005717
5718static void
5719get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5720{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005721 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5722 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005723}
5724
5725static void
5726put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5727{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005728 if (cstate->minorversion) {
5729 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5730 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5731 }
5732}
5733
5734void
5735clear_current_stateid(struct nfsd4_compound_state *cstate)
5736{
5737 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005738}
5739
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005740/*
5741 * functions to set current state id
5742 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005743void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005744nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5745{
5746 put_stateid(cstate, &odp->od_stateid);
5747}
5748
5749void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005750nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5751{
5752 put_stateid(cstate, &open->op_stateid);
5753}
5754
5755void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005756nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5757{
5758 put_stateid(cstate, &close->cl_stateid);
5759}
5760
5761void
5762nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5763{
5764 put_stateid(cstate, &lock->lk_resp_stateid);
5765}
5766
5767/*
5768 * functions to consume current state id
5769 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005770
5771void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005772nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5773{
5774 get_stateid(cstate, &odp->od_stateid);
5775}
5776
5777void
5778nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5779{
5780 get_stateid(cstate, &drp->dr_stateid);
5781}
5782
5783void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005784nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5785{
5786 get_stateid(cstate, &fsp->fr_stateid);
5787}
5788
5789void
5790nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5791{
5792 get_stateid(cstate, &setattr->sa_stateid);
5793}
5794
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005795void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005796nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5797{
5798 get_stateid(cstate, &close->cl_stateid);
5799}
5800
5801void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005802nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005803{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005804 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005805}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01005806
5807void
5808nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5809{
5810 get_stateid(cstate, &read->rd_stateid);
5811}
5812
5813void
5814nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5815{
5816 get_stateid(cstate, &write->wr_stateid);
5817}