blob: 5a93e5fafd4a30fae087b4cd58c3d45a442a6132 [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
Trond Myklebust67cb1272014-07-29 21:34:17 -0400677 atomic_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400678 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400679 list_add(&dp->dl_perfile, &fp->fi_delegations);
680 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
681}
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683static void
Jeff Layton42690672014-07-25 07:34:20 -0400684unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Trond Myklebust11b91642014-07-29 21:34:08 -0400686 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400687
Jeff Layton42690672014-07-25 07:34:20 -0400688 lockdep_assert_held(&state_lock);
689
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400690 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400691 /* Ensure that deleg break won't try to requeue it */
692 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400693 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400694 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400696 list_del_init(&dp->dl_perfile);
697 spin_unlock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400698 if (fp)
Trond Myklebustf8338832014-07-25 07:34:19 -0400699 nfs4_put_deleg_lease(fp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400700}
701
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400702static void destroy_delegation(struct nfs4_delegation *dp)
703{
Jeff Layton42690672014-07-25 07:34:20 -0400704 spin_lock(&state_lock);
705 unhash_delegation_locked(dp);
706 spin_unlock(&state_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400707 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400708}
709
710static void revoke_delegation(struct nfs4_delegation *dp)
711{
712 struct nfs4_client *clp = dp->dl_stid.sc_client;
713
Jeff Layton2d4a5322014-07-25 07:34:21 -0400714 WARN_ON(!list_empty(&dp->dl_recall_lru));
715
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400716 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400717 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400718 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400719 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400720 spin_lock(&clp->cl_lock);
721 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
722 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400723 }
724}
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726/*
727 * SETCLIENTID state
728 */
729
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400730static unsigned int clientid_hashval(u32 id)
731{
732 return id & CLIENT_HASH_MASK;
733}
734
735static unsigned int clientstr_hashval(const char *name)
736{
737 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
738}
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400741 * We store the NONE, READ, WRITE, and BOTH bits separately in the
742 * st_{access,deny}_bmap field of the stateid, in order to track not
743 * only what share bits are currently in force, but also what
744 * combinations of share bits previous opens have used. This allows us
745 * to enforce the recommendation of rfc 3530 14.2.19 that the server
746 * return an error if the client attempt to downgrade to a combination
747 * of share bits not explicable by closing some of its previous opens.
748 *
749 * XXX: This enforcement is actually incomplete, since we don't keep
750 * track of access/deny bit combinations; so, e.g., we allow:
751 *
752 * OPEN allow read, deny write
753 * OPEN allow both, deny none
754 * DOWNGRADE allow read, deny none
755 *
756 * which we should reject.
757 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400758static unsigned int
759bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400760 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400761 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400762
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400763 for (i = 1; i < 4; i++) {
764 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400765 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400766 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400767 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400768}
769
Jeff Layton82c5ff12012-05-11 09:45:13 -0400770/* set share access for a given stateid */
771static inline void
772set_access(u32 access, struct nfs4_ol_stateid *stp)
773{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400774 unsigned char mask = 1 << access;
775
776 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
777 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400778}
779
780/* clear share access for a given stateid */
781static inline void
782clear_access(u32 access, struct nfs4_ol_stateid *stp)
783{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400784 unsigned char mask = 1 << access;
785
786 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
787 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400788}
789
790/* test whether a given stateid has access */
791static inline bool
792test_access(u32 access, struct nfs4_ol_stateid *stp)
793{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400794 unsigned char mask = 1 << access;
795
796 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400797}
798
Jeff Laytonce0fc432012-05-11 09:45:14 -0400799/* set share deny for a given stateid */
800static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400801set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400802{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400803 unsigned char mask = 1 << deny;
804
805 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
806 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400807}
808
809/* clear share deny for a given stateid */
810static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400811clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400812{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400813 unsigned char mask = 1 << deny;
814
815 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
816 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400817}
818
819/* test whether a given stateid is denying specific access */
820static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400821test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400822{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400823 unsigned char mask = 1 << deny;
824
825 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400826}
827
828static int nfs4_access_to_omode(u32 access)
829{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400830 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400831 case NFS4_SHARE_ACCESS_READ:
832 return O_RDONLY;
833 case NFS4_SHARE_ACCESS_WRITE:
834 return O_WRONLY;
835 case NFS4_SHARE_ACCESS_BOTH:
836 return O_RDWR;
837 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500838 WARN_ON_ONCE(1);
839 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400840}
841
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400842/*
843 * A stateid that had a deny mode associated with it is being released
844 * or downgraded. Recalculate the deny mode on the file.
845 */
846static void
847recalculate_deny_mode(struct nfs4_file *fp)
848{
849 struct nfs4_ol_stateid *stp;
850
851 spin_lock(&fp->fi_lock);
852 fp->fi_share_deny = 0;
853 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
854 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
855 spin_unlock(&fp->fi_lock);
856}
857
858static void
859reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
860{
861 int i;
862 bool change = false;
863
864 for (i = 1; i < 4; i++) {
865 if ((i & deny) != i) {
866 change = true;
867 clear_deny(i, stp);
868 }
869 }
870
871 /* Recalculate per-file deny mode if there was a change */
872 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -0400873 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400874}
875
Jeff Layton82c5ff12012-05-11 09:45:13 -0400876/* release all access and file references for a given stateid */
877static void
878release_all_access(struct nfs4_ol_stateid *stp)
879{
880 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -0400881 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400882
883 if (fp && stp->st_deny_bmap != 0)
884 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400885
886 for (i = 1; i < 4; i++) {
887 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -0400888 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400889 clear_access(i, stp);
890 }
891}
892
Jeff Layton6b180f02014-07-29 21:34:26 -0400893static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
894{
895 if (!atomic_dec_and_test(&sop->so_count))
896 return;
897 kfree(sop->so_owner.data);
898 sop->so_ops->so_free(sop);
899}
900
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400901static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500902{
Trond Myklebust11b91642014-07-29 21:34:08 -0400903 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -0400904
Jeff Layton1c755dc2014-07-29 21:34:12 -0400905 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
906
Trond Myklebust1d31a252014-07-10 14:07:25 -0400907 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500908 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400909 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500910 list_del(&stp->st_perstateowner);
911}
912
Trond Myklebust60116952014-07-29 21:34:06 -0400913static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500914{
Trond Myklebust60116952014-07-29 21:34:06 -0400915 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400916
Trond Myklebust60116952014-07-29 21:34:06 -0400917 release_all_access(stp);
Trond Myklebust60116952014-07-29 21:34:06 -0400918 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500919}
920
Jeff Laytonb49e0842014-07-29 21:34:11 -0400921static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500922{
Jeff Laytonb49e0842014-07-29 21:34:11 -0400923 struct nfs4_ol_stateid *stp = openlockstateid(stid);
924 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500925 struct file *file;
926
Jeff Laytonb49e0842014-07-29 21:34:11 -0400927 file = find_any_file(stp->st_stid.sc_file);
928 if (file)
929 filp_close(file, (fl_owner_t)lo);
930 nfs4_free_ol_stateid(stid);
931}
932
933static void __release_lock_stateid(struct nfs4_ol_stateid *stp)
934{
Jeff Layton1c755dc2014-07-29 21:34:12 -0400935 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
936
937 spin_lock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400938 list_del(&stp->st_locks);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500939 unhash_generic_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400940 unhash_stid(&stp->st_stid);
Jeff Layton1c755dc2014-07-29 21:34:12 -0400941 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400942 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500943}
944
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400945static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500946{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400947 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500948
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400949 list_del(&lo->lo_owner.so_strhash);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400950 while (!list_empty(&lo->lo_owner.so_stateids)) {
951 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400952 struct nfs4_ol_stateid, st_perstateowner);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400953 __release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500954 }
955}
956
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400957static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500958{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400959 unhash_lockowner(lo);
Jeff Layton6b180f02014-07-29 21:34:26 -0400960 nfs4_put_stateowner(&lo->lo_owner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500961}
962
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400963static void release_lockowner_if_empty(struct nfs4_lockowner *lo)
964{
965 if (list_empty(&lo->lo_owner.so_stateids))
966 release_lockowner(lo);
967}
968
969static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500970{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400971 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500972
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400973 lo = lockowner(stp->st_stateowner);
974 __release_lock_stateid(stp);
975 release_lockowner_if_empty(lo);
976}
977
978static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
Jeff Layton1c755dc2014-07-29 21:34:12 -0400979 __releases(&open_stp->st_stateowner->so_client->cl_lock)
980 __acquires(&open_stp->st_stateowner->so_client->cl_lock)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400981{
982 struct nfs4_ol_stateid *stp;
983
984 while (!list_empty(&open_stp->st_locks)) {
985 stp = list_entry(open_stp->st_locks.next,
986 struct nfs4_ol_stateid, st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -0400987 spin_unlock(&open_stp->st_stateowner->so_client->cl_lock);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400988 release_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -0400989 spin_lock(&open_stp->st_stateowner->so_client->cl_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500990 }
991}
992
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400993static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500994{
Jeff Layton1c755dc2014-07-29 21:34:12 -0400995 spin_lock(&stp->st_stateowner->so_client->cl_lock);
J. Bruce Fields22839632009-01-11 14:27:17 -0500996 unhash_generic_stateid(stp);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400997 release_open_stateid_locks(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -0400998 spin_unlock(&stp->st_stateowner->so_client->cl_lock);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400999}
1000
1001static void release_open_stateid(struct nfs4_ol_stateid *stp)
1002{
1003 unhash_open_stateid(stp);
Trond Myklebust60116952014-07-29 21:34:06 -04001004 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields22839632009-01-11 14:27:17 -05001005}
1006
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001007static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001008{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001009 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001010
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001011 list_del(&oo->oo_owner.so_strhash);
1012 list_del(&oo->oo_perclient);
1013 while (!list_empty(&oo->oo_owner.so_stateids)) {
1014 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001015 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05001016 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001017 }
1018}
1019
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001020static void release_last_closed_stateid(struct nfs4_openowner *oo)
1021{
1022 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
1023
1024 if (s) {
Trond Myklebust60116952014-07-29 21:34:06 -04001025 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001026 oo->oo_last_closed_stid = NULL;
1027 }
1028}
1029
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001030static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001031{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001032 unhash_openowner(oo);
1033 list_del(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001034 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001035 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001036}
1037
Marc Eshel5282fd72009-04-03 08:27:52 +03001038static inline int
1039hash_sessionid(struct nfs4_sessionid *sessionid)
1040{
1041 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1042
1043 return sid->sequence % SESSION_HASH_SIZE;
1044}
1045
Trond Myklebust8f199b82012-03-20 15:11:17 -04001046#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001047static inline void
1048dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1049{
1050 u32 *ptr = (u32 *)(&sessionid->data[0]);
1051 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1052}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001053#else
1054static inline void
1055dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1056{
1057}
1058#endif
1059
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001060/*
1061 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1062 * won't be used for replay.
1063 */
1064void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1065{
1066 struct nfs4_stateowner *so = cstate->replay_owner;
1067
1068 if (nfserr == nfserr_replay_me)
1069 return;
1070
1071 if (!seqid_mutating_err(ntohl(nfserr))) {
1072 cstate->replay_owner = NULL;
1073 return;
1074 }
1075 if (!so)
1076 return;
1077 if (so->so_is_open_owner)
1078 release_last_closed_stateid(openowner(so));
1079 so->so_seqid++;
1080 return;
1081}
Marc Eshel5282fd72009-04-03 08:27:52 +03001082
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001083static void
1084gen_sessionid(struct nfsd4_session *ses)
1085{
1086 struct nfs4_client *clp = ses->se_client;
1087 struct nfsd4_sessionid *sid;
1088
1089 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1090 sid->clientid = clp->cl_clientid;
1091 sid->sequence = current_sessionid++;
1092 sid->reserved = 0;
1093}
1094
1095/*
Andy Adamsona6496372009-08-28 08:45:01 -04001096 * The protocol defines ca_maxresponssize_cached to include the size of
1097 * the rpc header, but all we need to cache is the data starting after
1098 * the end of the initial SEQUENCE operation--the rest we regenerate
1099 * each time. Therefore we can advertise a ca_maxresponssize_cached
1100 * value that is the number of bytes in our cache plus a few additional
1101 * bytes. In order to stay on the safe side, and not promise more than
1102 * we can cache, those additional bytes must be the minimum possible: 24
1103 * bytes of rpc header (xid through accept state, with AUTH_NULL
1104 * verifier), 12 for the compound header (with zero-length tag), and 44
1105 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001106 */
Andy Adamsona6496372009-08-28 08:45:01 -04001107#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1108
Andy Adamson557ce262009-08-28 08:45:04 -04001109static void
1110free_session_slots(struct nfsd4_session *ses)
1111{
1112 int i;
1113
1114 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1115 kfree(ses->se_slots[i]);
1116}
1117
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001118/*
1119 * We don't actually need to cache the rpc and session headers, so we
1120 * can allocate a little less for each slot:
1121 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001122static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001123{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001124 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001125
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001126 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1127 size = 0;
1128 else
1129 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1130 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001131}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001132
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001133/*
1134 * XXX: If we run out of reserved DRC memory we could (up to a point)
1135 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001136 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001137 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001138static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001139{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001140 u32 slotsize = slot_bytes(ca);
1141 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001142 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001143
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001144 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001145 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1146 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001147 num = min_t(int, num, avail / slotsize);
1148 nfsd_drc_mem_used += num * slotsize;
1149 spin_unlock(&nfsd_drc_lock);
1150
1151 return num;
1152}
1153
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001154static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001155{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001156 int slotsize = slot_bytes(ca);
1157
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001158 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001159 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001160 spin_unlock(&nfsd_drc_lock);
1161}
1162
Kinglong Mee60810e52014-01-01 00:35:47 +08001163static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1164 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001165{
Kinglong Mee60810e52014-01-01 00:35:47 +08001166 int numslots = fattrs->maxreqs;
1167 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001168 struct nfsd4_session *new;
1169 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001170
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001171 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001172 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1173 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001174
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001175 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001176 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001177 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001178 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001179 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001180 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001181 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001182 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001183 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001184
1185 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1186 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1187
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001188 return new;
1189out_free:
1190 while (i--)
1191 kfree(new->se_slots[i]);
1192 kfree(new);
1193 return NULL;
1194}
1195
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001196static void free_conn(struct nfsd4_conn *c)
1197{
1198 svc_xprt_put(c->cn_xprt);
1199 kfree(c);
1200}
1201
1202static void nfsd4_conn_lost(struct svc_xpt_user *u)
1203{
1204 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1205 struct nfs4_client *clp = c->cn_session->se_client;
1206
1207 spin_lock(&clp->cl_lock);
1208 if (!list_empty(&c->cn_persession)) {
1209 list_del(&c->cn_persession);
1210 free_conn(c);
1211 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001212 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001213 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001214}
1215
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001216static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001217{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001218 struct nfsd4_conn *conn;
1219
1220 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1221 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001222 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001223 svc_xprt_get(rqstp->rq_xprt);
1224 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001225 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001226 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1227 return conn;
1228}
1229
J. Bruce Fields328ead22010-09-29 16:11:06 -04001230static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1231{
1232 conn->cn_session = ses;
1233 list_add(&conn->cn_persession, &ses->se_conns);
1234}
1235
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001236static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1237{
1238 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001239
1240 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001241 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001242 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001243}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001244
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001245static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001246{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001247 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001248 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001249}
1250
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001251static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001252{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001253 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001254
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001255 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001256 ret = nfsd4_register_conn(conn);
1257 if (ret)
1258 /* oops; xprt is already down: */
1259 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001260 /* We may have gained or lost a callback channel: */
1261 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001262}
1263
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001264static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001265{
1266 u32 dir = NFS4_CDFC4_FORE;
1267
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001268 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001269 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001270 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001271}
1272
1273/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001274static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001275{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001276 struct nfs4_client *clp = s->se_client;
1277 struct nfsd4_conn *c;
1278
1279 spin_lock(&clp->cl_lock);
1280 while (!list_empty(&s->se_conns)) {
1281 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1282 list_del_init(&c->cn_persession);
1283 spin_unlock(&clp->cl_lock);
1284
1285 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1286 free_conn(c);
1287
1288 spin_lock(&clp->cl_lock);
1289 }
1290 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001291}
1292
J. Bruce Fields1377b692012-09-11 21:42:40 -04001293static void __free_session(struct nfsd4_session *ses)
1294{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001295 free_session_slots(ses);
1296 kfree(ses);
1297}
1298
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001299static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001300{
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001301 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1302
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001303 lockdep_assert_held(&nn->client_lock);
1304 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001305 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001306 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001307}
Andy Adamson557ce262009-08-28 08:45:04 -04001308
Fengguang Wu135ae822012-11-10 07:20:25 -05001309static 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 -04001310{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001311 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001312 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001313
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001314 new->se_client = clp;
1315 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001316
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001317 INIT_LIST_HEAD(&new->se_conns);
1318
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001319 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001320 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001321 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001322 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001323 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001324 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001325 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001326 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001327 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001328 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001329 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001330 spin_unlock(&nn->client_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001331
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001332 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001333 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001334 /*
1335 * This is a little silly; with sessions there's no real
1336 * use for the callback address. Use the peer address
1337 * as a reasonable default for now, but consider fixing
1338 * the rpc client not to require an address in the
1339 * future:
1340 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001341 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1342 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001343 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001344}
1345
Benny Halevy9089f1b2010-05-12 00:12:26 +03001346/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001347static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001348__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001349{
1350 struct nfsd4_session *elem;
1351 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001352 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001353
1354 dump_sessionid(__func__, sessionid);
1355 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001356 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001357 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001358 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1359 NFS4_MAX_SESSIONID_LEN)) {
1360 return elem;
1361 }
1362 }
1363
1364 dprintk("%s: session not found\n", __func__);
1365 return NULL;
1366}
1367
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001368static struct nfsd4_session *
1369find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1370 __be32 *ret)
1371{
1372 struct nfsd4_session *session;
1373 __be32 status = nfserr_badsession;
1374
1375 session = __find_in_sessionid_hashtbl(sessionid, net);
1376 if (!session)
1377 goto out;
1378 status = nfsd4_get_session_locked(session);
1379 if (status)
1380 session = NULL;
1381out:
1382 *ret = status;
1383 return session;
1384}
1385
Benny Halevy9089f1b2010-05-12 00:12:26 +03001386/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001387static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001388unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001389{
1390 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001391 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001392 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001393 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001394}
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1397static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001398STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001400 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001402 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001403 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 return 1;
1405}
1406
1407/*
1408 * XXX Should we use a slab cache ?
1409 * This type of memory management is somewhat inefficient, but we use it
1410 * anyway since SETCLIENTID is not a common operation.
1411 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001412static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
1414 struct nfs4_client *clp;
1415
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001416 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1417 if (clp == NULL)
1418 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001419 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001420 if (clp->cl_name.data == NULL) {
1421 kfree(clp);
1422 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001424 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001425 INIT_LIST_HEAD(&clp->cl_sessions);
1426 idr_init(&clp->cl_stateids);
1427 atomic_set(&clp->cl_refcount, 0);
1428 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1429 INIT_LIST_HEAD(&clp->cl_idhash);
1430 INIT_LIST_HEAD(&clp->cl_openowners);
1431 INIT_LIST_HEAD(&clp->cl_delegations);
1432 INIT_LIST_HEAD(&clp->cl_lru);
1433 INIT_LIST_HEAD(&clp->cl_callbacks);
1434 INIT_LIST_HEAD(&clp->cl_revoked);
1435 spin_lock_init(&clp->cl_lock);
1436 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 return clp;
1438}
1439
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001440static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441free_client(struct nfs4_client *clp)
1442{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001443 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001444
1445 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001446 while (!list_empty(&clp->cl_sessions)) {
1447 struct nfsd4_session *ses;
1448 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1449 se_perclnt);
1450 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001451 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1452 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001453 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001454 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001455 free_svc_cred(&clp->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001457 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 kfree(clp);
1459}
1460
Benny Halevy84d38ac2010-05-12 00:13:16 +03001461/* must be called under the client_lock */
1462static inline void
1463unhash_client_locked(struct nfs4_client *clp)
1464{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001465 struct nfsd4_session *ses;
1466
Benny Halevy84d38ac2010-05-12 00:13:16 +03001467 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001468 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001469 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1470 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001471 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001472}
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001475destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001477 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001480 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001483 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001484 while (!list_empty(&clp->cl_delegations)) {
1485 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001486 unhash_delegation_locked(dp);
1487 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 }
Benny Halevycdc97502014-05-30 09:09:30 -04001489 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 while (!list_empty(&reaplist)) {
1491 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001492 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001493 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001495 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001496 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001497 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001498 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001499 }
NeilBrownea1da632005-06-23 22:04:17 -07001500 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001501 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1502 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001504 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001505 if (clp->cl_cb_conn.cb_xprt)
1506 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001507 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001508 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001509 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001510 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001511 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001512 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001513 unhash_client_locked(clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001514 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1515 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001516 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001519static void expire_client(struct nfs4_client *clp)
1520{
1521 nfsd4_client_record_remove(clp);
1522 destroy_client(clp);
1523}
1524
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001525static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1526{
1527 memcpy(target->cl_verifier.data, source->data,
1528 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001531static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1532{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1534 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1535}
1536
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001537static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001538{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001539 if (source->cr_principal) {
1540 target->cr_principal =
1541 kstrdup(source->cr_principal, GFP_KERNEL);
1542 if (target->cr_principal == NULL)
1543 return -ENOMEM;
1544 } else
1545 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001546 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 target->cr_uid = source->cr_uid;
1548 target->cr_gid = source->cr_gid;
1549 target->cr_group_info = source->cr_group_info;
1550 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001551 target->cr_gss_mech = source->cr_gss_mech;
1552 if (source->cr_gss_mech)
1553 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001554 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
1556
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001557static long long
1558compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1559{
1560 long long res;
1561
1562 res = o1->len - o2->len;
1563 if (res)
1564 return res;
1565 return (long long)memcmp(o1->data, o2->data, o1->len);
1566}
1567
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001568static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001569{
NeilBrowna55370a2005-06-23 22:03:52 -07001570 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
1572
1573static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001574same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1575{
1576 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
1579static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001580same_clid(clientid_t *cl1, clientid_t *cl2)
1581{
1582 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
1584
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001585static bool groups_equal(struct group_info *g1, struct group_info *g2)
1586{
1587 int i;
1588
1589 if (g1->ngroups != g2->ngroups)
1590 return false;
1591 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001592 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001593 return false;
1594 return true;
1595}
1596
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001597/*
1598 * RFC 3530 language requires clid_inuse be returned when the
1599 * "principal" associated with a requests differs from that previously
1600 * used. We use uid, gid's, and gss principal string as our best
1601 * approximation. We also don't want to allow non-gss use of a client
1602 * established using gss: in theory cr_principal should catch that
1603 * change, but in practice cr_principal can be null even in the gss case
1604 * since gssd doesn't always pass down a principal string.
1605 */
1606static bool is_gss_cred(struct svc_cred *cr)
1607{
1608 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1609 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1610}
1611
1612
Vivek Trivedi5559b502012-07-24 21:18:20 +05301613static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001614same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1615{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001616 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001617 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1618 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001619 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1620 return false;
1621 if (cr1->cr_principal == cr2->cr_principal)
1622 return true;
1623 if (!cr1->cr_principal || !cr2->cr_principal)
1624 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301625 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
1627
J. Bruce Fields57266a62013-04-13 14:27:29 -04001628static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1629{
1630 struct svc_cred *cr = &rqstp->rq_cred;
1631 u32 service;
1632
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001633 if (!cr->cr_gss_mech)
1634 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001635 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1636 return service == RPC_GSS_SVC_INTEGRITY ||
1637 service == RPC_GSS_SVC_PRIVACY;
1638}
1639
1640static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1641{
1642 struct svc_cred *cr = &rqstp->rq_cred;
1643
1644 if (!cl->cl_mach_cred)
1645 return true;
1646 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1647 return false;
1648 if (!svc_rqst_integrity_protected(rqstp))
1649 return false;
1650 if (!cr->cr_principal)
1651 return false;
1652 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1653}
1654
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001655static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001656{
1657 static u32 current_clientid = 1;
1658
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001659 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 clp->cl_clientid.cl_id = current_clientid++;
1661}
1662
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001663static void gen_confirm(struct nfs4_client *clp)
1664{
Chuck Leverab4684d2012-03-02 17:13:50 -05001665 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001666 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Jeff Laytonf4199922014-06-17 07:44:11 -04001668 /*
1669 * This is opaque to client, so no need to byte-swap. Use
1670 * __force to keep sparse happy
1671 */
1672 verf[0] = (__force __be32)get_seconds();
1673 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001674 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675}
1676
Jeff Layton4770d722014-07-29 21:34:10 -04001677static struct nfs4_stid *
1678find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001679{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001680 struct nfs4_stid *ret;
1681
1682 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1683 if (!ret || !ret->sc_type)
1684 return NULL;
1685 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001686}
1687
Jeff Layton4770d722014-07-29 21:34:10 -04001688static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001689find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001690{
1691 struct nfs4_stid *s;
1692
Jeff Layton4770d722014-07-29 21:34:10 -04001693 spin_lock(&cl->cl_lock);
1694 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001695 if (s != NULL) {
1696 if (typemask & s->sc_type)
1697 atomic_inc(&s->sc_count);
1698 else
1699 s = NULL;
1700 }
Jeff Layton4770d722014-07-29 21:34:10 -04001701 spin_unlock(&cl->cl_lock);
1702 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001703}
1704
Jeff Layton2216d442012-11-12 15:00:57 -05001705static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001706 struct svc_rqst *rqstp, nfs4_verifier *verf)
1707{
1708 struct nfs4_client *clp;
1709 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001710 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001711 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001712 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001713
1714 clp = alloc_client(name);
1715 if (clp == NULL)
1716 return NULL;
1717
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001718 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1719 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001720 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001721 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001722 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001723 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001724 }
Jeff Layton02e12152014-07-16 10:31:57 -04001725 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001726 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001727 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001728 copy_verf(clp, verf);
1729 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001730 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001731 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001732 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001733 return clp;
1734}
1735
NeilBrownfd39ca92005-06-23 22:04:03 -07001736static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001737add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1738{
1739 struct rb_node **new = &(root->rb_node), *parent = NULL;
1740 struct nfs4_client *clp;
1741
1742 while (*new) {
1743 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1744 parent = *new;
1745
1746 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1747 new = &((*new)->rb_left);
1748 else
1749 new = &((*new)->rb_right);
1750 }
1751
1752 rb_link_node(&new_clp->cl_namenode, parent, new);
1753 rb_insert_color(&new_clp->cl_namenode, root);
1754}
1755
1756static struct nfs4_client *
1757find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1758{
1759 long long cmp;
1760 struct rb_node *node = root->rb_node;
1761 struct nfs4_client *clp;
1762
1763 while (node) {
1764 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1765 cmp = compare_blob(&clp->cl_name, name);
1766 if (cmp > 0)
1767 node = node->rb_left;
1768 else if (cmp < 0)
1769 node = node->rb_right;
1770 else
1771 return clp;
1772 }
1773 return NULL;
1774}
1775
1776static void
1777add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
1779 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001780 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001782 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001783 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001785 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001786 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787}
1788
NeilBrownfd39ca92005-06-23 22:04:03 -07001789static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790move_to_confirmed(struct nfs4_client *clp)
1791{
1792 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001793 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
1795 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001796 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001797 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001798 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001799 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 renew_client(clp);
1801}
1802
1803static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001804find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
1806 struct nfs4_client *clp;
1807 unsigned int idhashval = clientid_hashval(clid->cl_id);
1808
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001809 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001810 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001811 if ((bool)clp->cl_minorversion != sessions)
1812 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001813 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
1817 return NULL;
1818}
1819
1820static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001821find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1822{
1823 struct list_head *tbl = nn->conf_id_hashtbl;
1824
1825 return find_client_in_id_table(tbl, clid, sessions);
1826}
1827
1828static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001829find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001831 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001833 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
1835
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001836static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001837{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001838 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001839}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001840
NeilBrown28ce6052005-06-23 22:03:56 -07001841static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001842find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001843{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001844 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001845}
1846
1847static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001848find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001849{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001850 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001851}
1852
NeilBrownfd39ca92005-06-23 22:04:03 -07001853static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001854gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001856 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001857 struct sockaddr *sa = svc_addr(rqstp);
1858 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001859 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Jeff Layton7077ecb2009-08-14 12:57:58 -04001861 /* Currently, we only support tcp and tcp6 for the callback channel */
1862 if (se->se_callback_netid_len == 3 &&
1863 !memcmp(se->se_callback_netid_val, "tcp", 3))
1864 expected_family = AF_INET;
1865 else if (se->se_callback_netid_len == 4 &&
1866 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1867 expected_family = AF_INET6;
1868 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 goto out_err;
1870
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001871 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001872 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001873 (struct sockaddr *)&conn->cb_addr,
1874 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001875
J. Bruce Fields07263f12010-05-31 19:09:40 -04001876 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001878
J. Bruce Fields07263f12010-05-31 19:09:40 -04001879 if (conn->cb_addr.ss_family == AF_INET6)
1880 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001881
J. Bruce Fields07263f12010-05-31 19:09:40 -04001882 conn->cb_prog = se->se_callback_prog;
1883 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001884 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 return;
1886out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001887 conn->cb_addr.ss_family = AF_UNSPEC;
1888 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001889 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 "will not receive delegations\n",
1891 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 return;
1894}
1895
Andy Adamson074fe892009-04-03 08:28:15 +03001896/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04001897 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001898 */
Trond Myklebustb6076642014-06-30 11:48:35 -04001899static void
Andy Adamson074fe892009-04-03 08:28:15 +03001900nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1901{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001902 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04001903 struct nfsd4_slot *slot = resp->cstate.slot;
1904 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001905
Andy Adamson557ce262009-08-28 08:45:04 -04001906 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001907
Andy Adamson557ce262009-08-28 08:45:04 -04001908 slot->sl_opcnt = resp->opcnt;
1909 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001910
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001911 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001912 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001913 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001914 return;
1915 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001916 base = resp->cstate.data_offset;
1917 slot->sl_datalen = buf->len - base;
1918 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04001919 WARN("%s: sessions DRC could not cache compound\n", __func__);
1920 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001921}
1922
1923/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001924 * Encode the replay sequence operation from the slot values.
1925 * If cachethis is FALSE encode the uncached rep error on the next
1926 * operation which sets resp->p and increments resp->opcnt for
1927 * nfs4svc_encode_compoundres.
1928 *
Andy Adamson074fe892009-04-03 08:28:15 +03001929 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001930static __be32
1931nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1932 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001933{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001934 struct nfsd4_op *op;
1935 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001936
Andy Adamsonabfabf82009-07-23 19:02:18 -04001937 /* Encode the replayed sequence operation */
1938 op = &args->ops[resp->opcnt - 1];
1939 nfsd4_encode_operation(resp, op);
1940
1941 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05001942 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001943 op = &args->ops[resp->opcnt++];
1944 op->status = nfserr_retry_uncached_rep;
1945 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001946 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001947 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001948}
1949
1950/*
Andy Adamson557ce262009-08-28 08:45:04 -04001951 * The sequence operation is not cached because we can use the slot and
1952 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001953 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04001954static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001955nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1956 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001957{
Andy Adamson557ce262009-08-28 08:45:04 -04001958 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001959 struct xdr_stream *xdr = &resp->xdr;
1960 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03001961 __be32 status;
1962
Andy Adamson557ce262009-08-28 08:45:04 -04001963 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001964
Andy Adamsonabfabf82009-07-23 19:02:18 -04001965 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04001966 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04001967 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001968
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001969 p = xdr_reserve_space(xdr, slot->sl_datalen);
1970 if (!p) {
1971 WARN_ON_ONCE(1);
1972 return nfserr_serverfault;
1973 }
1974 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
1975 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03001976
Andy Adamson557ce262009-08-28 08:45:04 -04001977 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001978 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001979}
1980
Andy Adamson0733d212009-04-03 08:28:01 +03001981/*
1982 * Set the exchange_id flags returned by the server.
1983 */
1984static void
1985nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1986{
1987 /* pNFS is not supported */
1988 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1989
1990 /* Referrals are supported, Migration is not. */
1991 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1992
1993 /* set the wire flags to return to client. */
1994 clid->flags = new->cl_exchange_flags;
1995}
1996
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001997static bool client_has_state(struct nfs4_client *clp)
1998{
1999 /*
2000 * Note clp->cl_openowners check isn't quite right: there's no
2001 * need to count owners without stateid's.
2002 *
2003 * Also note we should probably be using this in 4.0 case too.
2004 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002005 return !list_empty(&clp->cl_openowners)
2006 || !list_empty(&clp->cl_delegations)
2007 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002008}
2009
Al Virob37ad282006-10-19 23:28:59 -07002010__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002011nfsd4_exchange_id(struct svc_rqst *rqstp,
2012 struct nfsd4_compound_state *cstate,
2013 struct nfsd4_exchange_id *exid)
2014{
Andy Adamson0733d212009-04-03 08:28:01 +03002015 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002016 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002017 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002018 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002019 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002020 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002021 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002022
Jeff Layton363168b2009-08-14 12:57:56 -04002023 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002024 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002025 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002026 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002027 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002028
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002029 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002030 return nfserr_inval;
2031
Andy Adamson0733d212009-04-03 08:28:01 +03002032 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002033 case SP4_MACH_CRED:
2034 if (!svc_rqst_integrity_protected(rqstp))
2035 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002036 case SP4_NONE:
2037 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002038 default: /* checked by xdr code */
2039 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002040 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002041 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002042 }
2043
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002044 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03002045 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002046 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002047 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002048 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2049 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2050
J. Bruce Fields136e6582012-05-12 20:37:23 -04002051 if (update) {
2052 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002053 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002054 goto out;
2055 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002056 if (!mach_creds_match(conf, rqstp)) {
2057 status = nfserr_wrong_cred;
2058 goto out;
2059 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002060 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002061 status = nfserr_perm;
2062 goto out;
2063 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002064 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002065 status = nfserr_not_same;
2066 goto out;
2067 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002068 /* case 6 */
2069 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2070 new = conf;
2071 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002072 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002073 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002074 if (client_has_state(conf)) {
2075 status = nfserr_clid_inuse;
2076 goto out;
2077 }
Andy Adamson0733d212009-04-03 08:28:01 +03002078 expire_client(conf);
2079 goto out_new;
2080 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002081 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002082 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002083 new = conf;
2084 goto out_copy;
2085 }
2086 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04002087 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002088 }
2089
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002090 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002091 status = nfserr_noent;
2092 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002093 }
2094
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002095 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002096 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03002097 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002098
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002099 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002100out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05002101 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03002102 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04002103 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03002104 goto out;
2105 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002106 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002107 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002108
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002109 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002110 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03002111out_copy:
2112 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2113 exid->clientid.cl_id = new->cl_clientid.cl_id;
2114
J. Bruce Fields778df3f2012-05-25 21:40:23 -04002115 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03002116 nfsd4_set_ex_flags(new, exid);
2117
2118 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04002119 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002120 status = nfs_ok;
2121
2122out:
2123 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03002124 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002125}
2126
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002127static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002128check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002129{
Andy Adamson88e588d2009-07-23 19:02:15 -04002130 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2131 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002132
2133 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002134 if (slot_inuse) {
2135 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002136 return nfserr_jukebox;
2137 else
2138 return nfserr_seq_misordered;
2139 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002140 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002141 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002142 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002143 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002144 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002145 return nfserr_seq_misordered;
2146}
2147
Andy Adamson49557cc2009-07-23 19:02:16 -04002148/*
2149 * Cache the create session result into the create session single DRC
2150 * slot cache by saving the xdr structure. sl_seqid has been set.
2151 * Do this for solo or embedded create session operations.
2152 */
2153static void
2154nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002155 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002156{
2157 slot->sl_status = nfserr;
2158 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2159}
2160
2161static __be32
2162nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2163 struct nfsd4_clid_slot *slot)
2164{
2165 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2166 return slot->sl_status;
2167}
2168
Mi Jinlong1b74c252011-07-14 14:50:17 +08002169#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2170 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2171 1 + /* MIN tag is length with zero, only length */ \
2172 3 + /* version, opcount, opcode */ \
2173 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2174 /* seqid, slotID, slotID, cache */ \
2175 4 ) * sizeof(__be32))
2176
2177#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2178 2 + /* verifier: AUTH_NULL, length 0 */\
2179 1 + /* status */ \
2180 1 + /* MIN tag is length with zero, only length */ \
2181 3 + /* opcount, opcode, opstatus*/ \
2182 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2183 /* seqid, slotID, slotID, slotID, status */ \
2184 5 ) * sizeof(__be32))
2185
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002186static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002187{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002188 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2189
J. Bruce Fields373cd402013-04-08 15:42:12 -04002190 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2191 return nfserr_toosmall;
2192 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2193 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002194 ca->headerpadsz = 0;
2195 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2196 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2197 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2198 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2199 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2200 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2201 /*
2202 * Note decreasing slot size below client's request may make it
2203 * difficult for client to function correctly, whereas
2204 * decreasing the number of slots will (just?) affect
2205 * performance. When short on memory we therefore prefer to
2206 * decrease number of slots instead of their size. Clients that
2207 * request larger slots than they need will get poor results:
2208 */
2209 ca->maxreqs = nfsd4_get_drc_mem(ca);
2210 if (!ca->maxreqs)
2211 return nfserr_jukebox;
2212
J. Bruce Fields373cd402013-04-08 15:42:12 -04002213 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002214}
2215
Kinglong Mee8a891632013-12-23 18:11:02 +08002216#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2217 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2218#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2219 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2220
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002221static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2222{
2223 ca->headerpadsz = 0;
2224
2225 /*
2226 * These RPC_MAX_HEADER macros are overkill, especially since we
2227 * don't even do gss on the backchannel yet. But this is still
2228 * less than 1k. Tighten up this estimate in the unlikely event
2229 * it turns out to be a problem for some client:
2230 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002231 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002232 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002233 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002234 return nfserr_toosmall;
2235 ca->maxresp_cached = 0;
2236 if (ca->maxops < 2)
2237 return nfserr_toosmall;
2238
2239 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002240}
2241
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002242static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2243{
2244 switch (cbs->flavor) {
2245 case RPC_AUTH_NULL:
2246 case RPC_AUTH_UNIX:
2247 return nfs_ok;
2248 default:
2249 /*
2250 * GSS case: the spec doesn't allow us to return this
2251 * error. But it also doesn't allow us not to support
2252 * GSS.
2253 * I'd rather this fail hard than return some error the
2254 * client might think it can already handle:
2255 */
2256 return nfserr_encr_alg_unsupp;
2257 }
2258}
2259
Andy Adamson069b6ad2009-04-03 08:27:58 +03002260__be32
2261nfsd4_create_session(struct svc_rqst *rqstp,
2262 struct nfsd4_compound_state *cstate,
2263 struct nfsd4_create_session *cr_ses)
2264{
Jeff Layton363168b2009-08-14 12:57:56 -04002265 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002266 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002267 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002268 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002269 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002270 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002271 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002272
Mi Jinlonga62573d2011-03-23 17:57:07 +08002273 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2274 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002275 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2276 if (status)
2277 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002278 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002279 if (status)
2280 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002281 status = check_backchannel_attrs(&cr_ses->back_channel);
2282 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002283 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002284 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002285 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002286 if (!new)
2287 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002288 conn = alloc_conn_from_crses(rqstp, cr_ses);
2289 if (!conn)
2290 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002291
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002292 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002293 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002294 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002295 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002296
2297 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002298 status = nfserr_wrong_cred;
2299 if (!mach_creds_match(conf, rqstp))
2300 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002301 cs_slot = &conf->cl_cs_slot;
2302 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002303 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002304 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002305 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002306 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002307 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002308 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002309 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002310 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002311 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002312 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002313 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002314 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002315 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002316 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002317 status = nfserr_wrong_cred;
2318 if (!mach_creds_match(unconf, rqstp))
2319 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002320 cs_slot = &unconf->cl_cs_slot;
2321 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002322 if (status) {
2323 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002324 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002325 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002326 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002327 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002328 if (old) {
2329 status = mark_client_expired(old);
2330 if (status)
2331 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002332 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002333 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002334 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002335 conf = unconf;
2336 } else {
2337 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002338 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002339 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002340 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002341 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002342 * We do not support RDMA or persistent sessions
2343 */
2344 cr_ses->flags &= ~SESSION4_PERSIST;
2345 cr_ses->flags &= ~SESSION4_RDMA;
2346
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002347 init_session(rqstp, new, conf, cr_ses);
2348 nfsd4_init_conn(rqstp, conn, new);
2349
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002350 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002351 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002352 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002353 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002354
Andy Adamson49557cc2009-07-23 19:02:16 -04002355 /* cache solo and embedded create sessions under the state lock */
2356 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002357 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002358 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002359out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08002360 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002361 free_conn(conn);
2362out_free_session:
2363 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002364out_release_drc_mem:
2365 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002366 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002367}
2368
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002369static __be32 nfsd4_map_bcts_dir(u32 *dir)
2370{
2371 switch (*dir) {
2372 case NFS4_CDFC4_FORE:
2373 case NFS4_CDFC4_BACK:
2374 return nfs_ok;
2375 case NFS4_CDFC4_FORE_OR_BOTH:
2376 case NFS4_CDFC4_BACK_OR_BOTH:
2377 *dir = NFS4_CDFC4_BOTH;
2378 return nfs_ok;
2379 };
2380 return nfserr_inval;
2381}
2382
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002383__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2384{
2385 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002386 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002387 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002388
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002389 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2390 if (status)
2391 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002392 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002393 session->se_cb_prog = bc->bc_cb_program;
2394 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002395 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002396
2397 nfsd4_probe_callback(session->se_client);
2398
2399 return nfs_ok;
2400}
2401
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002402__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2403 struct nfsd4_compound_state *cstate,
2404 struct nfsd4_bind_conn_to_session *bcts)
2405{
2406 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002407 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002408 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002409 struct net *net = SVC_NET(rqstp);
2410 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002411
2412 if (!nfsd4_last_compound_op(rqstp))
2413 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002414 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002415 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002416 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002417 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002418 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002419 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002420 status = nfserr_wrong_cred;
2421 if (!mach_creds_match(session->se_client, rqstp))
2422 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002423 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002424 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002425 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002426 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002427 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002428 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002429 goto out;
2430 nfsd4_init_conn(rqstp, conn, session);
2431 status = nfs_ok;
2432out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002433 nfsd4_put_session(session);
2434out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002435 nfs4_unlock_state();
2436 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002437}
2438
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002439static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2440{
2441 if (!session)
2442 return 0;
2443 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2444}
2445
Andy Adamson069b6ad2009-04-03 08:27:58 +03002446__be32
2447nfsd4_destroy_session(struct svc_rqst *r,
2448 struct nfsd4_compound_state *cstate,
2449 struct nfsd4_destroy_session *sessionid)
2450{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002451 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002452 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002453 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002454 struct net *net = SVC_NET(r);
2455 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002456
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002457 nfs4_lock_state();
2458 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002459 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002460 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002461 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002462 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002463 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002464 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002465 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002466 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002467 if (!ses)
2468 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002469 status = nfserr_wrong_cred;
2470 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002471 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002472 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002473 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002474 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002475 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002476 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002477
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002478 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002479
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002480 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002481 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002482out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002483 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002484out_client_lock:
2485 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002486out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002487 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002488 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002489}
2490
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002491static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002492{
2493 struct nfsd4_conn *c;
2494
2495 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002496 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002497 return c;
2498 }
2499 }
2500 return NULL;
2501}
2502
J. Bruce Fields57266a62013-04-13 14:27:29 -04002503static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002504{
2505 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002506 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002507 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002508 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002509
2510 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002511 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002512 if (c)
2513 goto out_free;
2514 status = nfserr_conn_not_bound_to_session;
2515 if (clp->cl_mach_cred)
2516 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002517 __nfsd4_hash_conn(new, ses);
2518 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002519 ret = nfsd4_register_conn(new);
2520 if (ret)
2521 /* oops; xprt is already down: */
2522 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002523 return nfs_ok;
2524out_free:
2525 spin_unlock(&clp->cl_lock);
2526 free_conn(new);
2527 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002528}
2529
Mi Jinlong868b89c2011-04-27 09:09:58 +08002530static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2531{
2532 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2533
2534 return args->opcnt > session->se_fchannel.maxops;
2535}
2536
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002537static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2538 struct nfsd4_session *session)
2539{
2540 struct xdr_buf *xb = &rqstp->rq_arg;
2541
2542 return xb->len > session->se_fchannel.maxreq_sz;
2543}
2544
Andy Adamson069b6ad2009-04-03 08:27:58 +03002545__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002546nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002547 struct nfsd4_compound_state *cstate,
2548 struct nfsd4_sequence *seq)
2549{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002550 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002551 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002552 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002553 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002554 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002555 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002556 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002557 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002558 struct net *net = SVC_NET(rqstp);
2559 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002560
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002561 if (resp->opcnt != 1)
2562 return nfserr_sequence_pos;
2563
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002564 /*
2565 * Will be either used or freed by nfsd4_sequence_check_conn
2566 * below.
2567 */
2568 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2569 if (!conn)
2570 return nfserr_jukebox;
2571
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002572 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002573 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002574 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002575 goto out_no_session;
2576 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002577
Mi Jinlong868b89c2011-04-27 09:09:58 +08002578 status = nfserr_too_many_ops;
2579 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002580 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002581
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002582 status = nfserr_req_too_big;
2583 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002584 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002585
Benny Halevyb85d4c02009-04-03 08:28:08 +03002586 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002587 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002588 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002589
Andy Adamson557ce262009-08-28 08:45:04 -04002590 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002591 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2592
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002593 /* We do not negotiate the number of slots yet, so set the
2594 * maxslots to the session maxreqs which is used to encode
2595 * sr_highest_slotid and the sr_target_slot id to maxslots */
2596 seq->maxslots = session->se_fchannel.maxreqs;
2597
J. Bruce Fields73e79482012-02-13 16:39:00 -05002598 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2599 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002600 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002601 status = nfserr_seq_misordered;
2602 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002603 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002604 cstate->slot = slot;
2605 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002606 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002607 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002608 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002609 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002610 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002611 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002612 }
2613 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002614 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002615
J. Bruce Fields57266a62013-04-13 14:27:29 -04002616 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002617 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002618 if (status)
2619 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002620
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002621 buflen = (seq->cachethis) ?
2622 session->se_fchannel.maxresp_cached :
2623 session->se_fchannel.maxresp_sz;
2624 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2625 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002626 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002627 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002628 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002629
2630 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002631 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002632 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002633 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002634 if (seq->cachethis)
2635 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002636 else
2637 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002638
2639 cstate->slot = slot;
2640 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002641 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002642
Benny Halevyb85d4c02009-04-03 08:28:08 +03002643out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002644 switch (clp->cl_cb_state) {
2645 case NFSD4_CB_DOWN:
2646 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2647 break;
2648 case NFSD4_CB_FAULT:
2649 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2650 break;
2651 default:
2652 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002653 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002654 if (!list_empty(&clp->cl_revoked))
2655 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002656out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002657 if (conn)
2658 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002659 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002660 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002661out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002662 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002663 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002664}
2665
Trond Myklebustb6076642014-06-30 11:48:35 -04002666void
2667nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2668{
2669 struct nfsd4_compound_state *cs = &resp->cstate;
2670
2671 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002672 if (cs->status != nfserr_replay_cache) {
2673 nfsd4_store_cache_entry(resp);
2674 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2675 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002676 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002677 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002678 } else if (cs->clp)
2679 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002680}
2681
Mi Jinlong345c2842011-10-20 17:51:39 +08002682__be32
2683nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2684{
2685 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002686 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002687 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002688
2689 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002690 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002691 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002692 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002693
2694 if (conf) {
2695 clp = conf;
2696
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002697 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002698 status = nfserr_clientid_busy;
2699 goto out;
2700 }
2701 } else if (unconf)
2702 clp = unconf;
2703 else {
2704 status = nfserr_stale_clientid;
2705 goto out;
2706 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002707 if (!mach_creds_match(clp, rqstp)) {
2708 status = nfserr_wrong_cred;
2709 goto out;
2710 }
Mi Jinlong345c2842011-10-20 17:51:39 +08002711 expire_client(clp);
2712out:
2713 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002714 return status;
2715}
2716
Andy Adamson069b6ad2009-04-03 08:27:58 +03002717__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002718nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2719{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002720 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002721
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002722 if (rc->rca_one_fs) {
2723 if (!cstate->current_fh.fh_dentry)
2724 return nfserr_nofilehandle;
2725 /*
2726 * We don't take advantage of the rca_one_fs case.
2727 * That's OK, it's optional, we can safely ignore it.
2728 */
2729 return nfs_ok;
2730 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002731
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002732 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002733 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002734 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2735 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002736 goto out;
2737
2738 status = nfserr_stale_clientid;
2739 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002740 /*
2741 * The following error isn't really legal.
2742 * But we only get here if the client just explicitly
2743 * destroyed the client. Surely it no longer cares what
2744 * error it gets back on an operation for the dead
2745 * client.
2746 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002747 goto out;
2748
2749 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002750 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002751out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002752 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002753 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002754}
2755
2756__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002757nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2758 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002760 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002762 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002763 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002764 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2765
J. Bruce Fields63db4632012-05-18 21:54:19 -04002766 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002768 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002769 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002770 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002772 if (clp_used_exchangeid(conf))
2773 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002774 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002775 char addr_str[INET6_ADDRSTRLEN];
2776 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2777 sizeof(addr_str));
2778 dprintk("NFSD: setclientid: string in use by client "
2779 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 goto out;
2781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002783 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002784 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002787 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002788 if (new == NULL)
2789 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002790 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002791 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002793 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002794 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002795 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002796 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002797 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2799 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2800 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2801 status = nfs_ok;
2802out:
2803 nfs4_unlock_state();
2804 return status;
2805}
2806
2807
Al Virob37ad282006-10-19 23:28:59 -07002808__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002809nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2810 struct nfsd4_compound_state *cstate,
2811 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812{
NeilBrown21ab45a2005-06-23 22:04:14 -07002813 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2815 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002816 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002817 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002819 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002822
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002823 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002824 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002825 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002826 * We try hard to give out unique clientid's, so if we get an
2827 * attempt to confirm the same clientid with a different cred,
2828 * there's a bug somewhere. Let's charitably assume it's our
2829 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002830 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002831 status = nfserr_serverfault;
2832 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2833 goto out;
2834 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2835 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002836 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002837 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2838 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002840 else /* case 4: client hasn't noticed we rebooted yet? */
2841 status = nfserr_stale_clientid;
2842 goto out;
2843 }
2844 status = nfs_ok;
2845 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002846 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2847 nfsd4_probe_callback(conf);
2848 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002849 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002850 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002851 if (conf) {
2852 status = mark_client_expired(conf);
2853 if (status)
2854 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002855 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002856 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002857 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002858 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 nfs4_unlock_state();
2862 return status;
2863}
2864
J. Bruce Fields32513b42011-10-13 16:00:16 -04002865static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002867 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2868}
2869
2870/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04002871static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04002872{
Trond Myklebustca943212014-07-23 16:17:39 -04002873 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Trond Myklebust950e0112014-06-30 11:48:31 -04002875 lockdep_assert_held(&state_lock);
2876
J. Bruce Fields32513b42011-10-13 16:00:16 -04002877 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04002878 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002879 INIT_LIST_HEAD(&fp->fi_stateids);
2880 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04002881 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002882 fp->fi_had_conflict = false;
2883 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04002884 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002885 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2886 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04002887 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888}
2889
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002890void
NeilBrowne60d4392005-06-23 22:03:01 -07002891nfsd4_free_slabs(void)
2892{
Christoph Hellwigabf11352014-05-21 07:43:03 -07002893 kmem_cache_destroy(openowner_slab);
2894 kmem_cache_destroy(lockowner_slab);
2895 kmem_cache_destroy(file_slab);
2896 kmem_cache_destroy(stateid_slab);
2897 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002898}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
Bryan Schumaker72083392011-11-01 15:24:59 -04002900int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901nfsd4_init_slabs(void)
2902{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002903 openowner_slab = kmem_cache_create("nfsd4_openowners",
2904 sizeof(struct nfs4_openowner), 0, 0, NULL);
2905 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002906 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002907 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002908 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002909 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002910 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07002911 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002912 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002913 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002914 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07002915 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002916 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002917 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002918 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002919 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002920 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002921 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002922 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07002924
2925out_free_stateid_slab:
2926 kmem_cache_destroy(stateid_slab);
2927out_free_file_slab:
2928 kmem_cache_destroy(file_slab);
2929out_free_lockowner_slab:
2930 kmem_cache_destroy(lockowner_slab);
2931out_free_openowner_slab:
2932 kmem_cache_destroy(openowner_slab);
2933out:
NeilBrowne60d4392005-06-23 22:03:01 -07002934 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2935 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936}
2937
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002938static void init_nfs4_replay(struct nfs4_replay *rp)
2939{
2940 rp->rp_status = nfserr_serverfault;
2941 rp->rp_buflen = 0;
2942 rp->rp_buf = rp->rp_ibuf;
2943}
2944
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002945static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946{
2947 struct nfs4_stateowner *sop;
2948
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002949 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002950 if (!sop)
2951 return NULL;
2952
2953 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2954 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002955 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002956 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002958 sop->so_owner.len = owner->len;
2959
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002960 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002961 sop->so_client = clp;
2962 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04002963 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002964 return sop;
2965}
2966
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002967static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002968{
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002969 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2970
2971 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002972 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973}
2974
Jeff Layton6b180f02014-07-29 21:34:26 -04002975static void nfs4_free_openowner(struct nfs4_stateowner *so)
2976{
2977 struct nfs4_openowner *oo = openowner(so);
2978
2979 kmem_cache_free(openowner_slab, oo);
2980}
2981
2982static const struct nfs4_stateowner_operations openowner_ops = {
2983 .so_free = nfs4_free_openowner,
2984};
2985
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002986static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04002987alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002988 struct nfsd4_compound_state *cstate)
2989{
Trond Myklebust13d6f662014-06-30 11:48:45 -04002990 struct nfs4_client *clp = cstate->clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002991 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002993 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2994 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04002996 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002997 oo->oo_owner.so_is_open_owner = 1;
2998 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04002999 oo->oo_flags = NFS4_OO_NEW;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003000 if (nfsd4_has_session(cstate))
3001 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003002 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003003 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003004 INIT_LIST_HEAD(&oo->oo_close_lru);
3005 hash_openowner(oo, clp, strhashval);
3006 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007}
3008
J. Bruce Fields996e0932011-10-17 11:14:48 -04003009static 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 -04003010 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003012 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003013 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003014 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003015 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07003016 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003017 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 stp->st_access_bmap = 0;
3019 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003020 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003021 spin_lock(&oo->oo_owner.so_client->cl_lock);
3022 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003023 spin_lock(&fp->fi_lock);
3024 list_add(&stp->st_perfile, &fp->fi_stateids);
3025 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003026 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027}
3028
3029static void
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003030move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003032 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3033
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003034 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003036 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003037 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038}
3039
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04003041same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
3042 clientid_t *clid)
3043{
3044 return (sop->so_owner.len == owner->len) &&
3045 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
3046 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047}
3048
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003049static struct nfs4_openowner *
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003050find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
3051 bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052{
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003053 struct nfs4_stateowner *so;
3054 struct nfs4_openowner *oo;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003055 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003057 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003058 if (!so->so_is_open_owner)
3059 continue;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003060 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
3061 oo = openowner(so);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003062 clp = oo->oo_owner.so_client;
3063 if ((bool)clp->cl_minorversion != sessions)
3064 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003065 renew_client(oo->oo_owner.so_client);
3066 return oo;
3067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 }
3069 return NULL;
3070}
3071
3072/* search file_hashtbl[] for file */
3073static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003074find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075{
Trond Myklebustca943212014-07-23 16:17:39 -04003076 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 struct nfs4_file *fp;
3078
Trond Myklebust950e0112014-06-30 11:48:31 -04003079 lockdep_assert_held(&state_lock);
3080
Jeff Layton89876f82013-04-02 09:01:59 -04003081 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003082 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003083 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 }
3087 return NULL;
3088}
3089
Trond Myklebust950e0112014-06-30 11:48:31 -04003090static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003091find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003092{
3093 struct nfs4_file *fp;
3094
3095 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003096 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003097 spin_unlock(&state_lock);
3098 return fp;
3099}
3100
3101static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003102find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003103{
3104 struct nfs4_file *fp;
3105
3106 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003107 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003108 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003109 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003110 fp = new;
3111 }
3112 spin_unlock(&state_lock);
3113
3114 return fp;
3115}
3116
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003117/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 * Called to check deny when READ with all zero stateid or
3119 * WRITE with all zero or all one stateid
3120 */
Al Virob37ad282006-10-19 23:28:59 -07003121static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3123{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003125 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Trond Myklebustca943212014-07-23 16:17:39 -04003127 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003128 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003129 return ret;
3130 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003131 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003132 if (fp->fi_share_deny & deny_type)
3133 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003134 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003135 put_nfs4_file(fp);
3136 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137}
3138
Jeff Layton02e12152014-07-16 10:31:57 -04003139void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140{
Trond Myklebust11b91642014-07-29 21:34:08 -04003141 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3142 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003143
Trond Myklebust11b91642014-07-29 21:34:08 -04003144 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003145
Jeff Layton02e12152014-07-16 10:31:57 -04003146 /*
3147 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003148 * already holding inode->i_lock.
3149 *
Jeff Laytondff13992014-07-08 14:02:49 -04003150 * If the dl_time != 0, then we know that it has already been
3151 * queued for a lease break. Don't queue it again.
3152 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003153 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003154 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003155 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003156 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003157 }
Jeff Layton02e12152014-07-16 10:31:57 -04003158 spin_unlock(&state_lock);
3159}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
Jeff Layton02e12152014-07-16 10:31:57 -04003161static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3162{
3163 /*
3164 * We're assuming the state code never drops its reference
3165 * without first removing the lease. Since we're in this lease
3166 * callback (and since the lease code is serialized by the kernel
3167 * lock) we know the server hasn't removed the lease yet, we know
3168 * it's safe to take a reference.
3169 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003170 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003171 nfsd4_cb_recall(dp);
3172}
3173
Jeff Layton1c8c6012013-06-21 08:58:15 -04003174/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003175static void nfsd_break_deleg_cb(struct file_lock *fl)
3176{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003177 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3178 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003179
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003180 if (!fp) {
3181 WARN(1, "(%p)->fl_owner NULL\n", fl);
3182 return;
3183 }
3184 if (fp->fi_had_conflict) {
3185 WARN(1, "duplicate break on %p\n", fp);
3186 return;
3187 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003188 /*
3189 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003190 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003191 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003192 */
3193 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
Jeff Layton02e12152014-07-16 10:31:57 -04003195 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003196 fp->fi_had_conflict = true;
3197 /*
3198 * If there are no delegations on the list, then we can't count on this
3199 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3200 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3201 * true should keep any new delegations from being hashed.
3202 */
3203 if (list_empty(&fp->fi_delegations))
3204 fl->fl_break_time = jiffies;
3205 else
3206 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3207 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003208 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209}
3210
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211static
3212int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3213{
3214 if (arg & F_UNLCK)
3215 return lease_modify(onlist, arg);
3216 else
3217 return -EAGAIN;
3218}
3219
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003220static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003221 .lm_break = nfsd_break_deleg_cb,
3222 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223};
3224
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003225static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3226{
3227 if (nfsd4_has_session(cstate))
3228 return nfs_ok;
3229 if (seqid == so->so_seqid - 1)
3230 return nfserr_replay_me;
3231 if (seqid == so->so_seqid)
3232 return nfs_ok;
3233 return nfserr_bad_seqid;
3234}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Jeff Layton4b24ca72014-06-30 11:48:44 -04003236static __be32 lookup_clientid(clientid_t *clid,
3237 struct nfsd4_compound_state *cstate,
3238 struct nfsd_net *nn)
3239{
3240 struct nfs4_client *found;
3241
3242 if (cstate->clp) {
3243 found = cstate->clp;
3244 if (!same_clid(&found->cl_clientid, clid))
3245 return nfserr_stale_clientid;
3246 return nfs_ok;
3247 }
3248
3249 if (STALE_CLIENTID(clid, nn))
3250 return nfserr_stale_clientid;
3251
3252 /*
3253 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3254 * cached already then we know this is for is for v4.0 and "sessions"
3255 * will be false.
3256 */
3257 WARN_ON_ONCE(cstate->session);
3258 found = find_confirmed_client(clid, false, nn);
3259 if (!found)
3260 return nfserr_expired;
3261
3262 /* Cache the nfs4_client in cstate! */
3263 cstate->clp = found;
3264 atomic_inc(&found->cl_refcount);
3265 return nfs_ok;
3266}
3267
Al Virob37ad282006-10-19 23:28:59 -07003268__be32
Andy Adamson66689582009-04-03 08:28:45 +03003269nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003270 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 clientid_t *clientid = &open->op_clientid;
3273 struct nfs4_client *clp = NULL;
3274 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003275 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003276 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003278 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003280 /*
3281 * In case we need it later, after we've already created the
3282 * file and don't want to risk a further failure:
3283 */
3284 open->op_file = nfsd4_alloc_file();
3285 if (open->op_file == NULL)
3286 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
Trond Myklebust2d91e892014-06-30 11:48:46 -04003288 status = lookup_clientid(clientid, cstate, nn);
3289 if (status)
3290 return status;
3291 clp = cstate->clp;
3292
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003293 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003294 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003295 open->op_openowner = oo;
3296 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003297 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003299 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003300 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003301 release_openowner(oo);
3302 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003303 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003304 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003305 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3306 if (status)
3307 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003308 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003309new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003310 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003311 if (oo == NULL)
3312 return nfserr_jukebox;
3313 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003314alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003315 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003316 if (!open->op_stp)
3317 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003318 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319}
3320
Al Virob37ad282006-10-19 23:28:59 -07003321static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003322nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3323{
3324 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3325 return nfserr_openmode;
3326 else
3327 return nfs_ok;
3328}
3329
Daniel Mackc47d8322011-05-16 16:38:14 +02003330static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003331{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003332 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3333}
3334
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003335static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003336{
3337 struct nfs4_stid *ret;
3338
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003339 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003340 if (!ret)
3341 return NULL;
3342 return delegstateid(ret);
3343}
3344
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003345static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3346{
3347 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3348 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3349}
3350
Al Virob37ad282006-10-19 23:28:59 -07003351static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003352nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003353 struct nfs4_delegation **dp)
3354{
3355 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003356 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003357 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003358
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003359 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3360 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003361 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003362 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003363 status = nfs4_check_delegmode(deleg, flags);
3364 if (status) {
3365 nfs4_put_stid(&deleg->dl_stid);
3366 goto out;
3367 }
3368 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003369out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003370 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003371 return nfs_ok;
3372 if (status)
3373 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003374 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003375 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003376}
3377
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003378static struct nfs4_ol_stateid *
3379nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003381 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003382 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383
Trond Myklebust1d31a252014-07-10 14:07:25 -04003384 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003385 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 /* ignore lock owners */
3387 if (local->st_stateowner->so_is_open_owner == 0)
3388 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003389 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003390 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003391 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003392 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003395 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003396 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397}
3398
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003399static inline int nfs4_access_to_access(u32 nfs4_access)
3400{
3401 int flags = 0;
3402
3403 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3404 flags |= NFSD_MAY_READ;
3405 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3406 flags |= NFSD_MAY_WRITE;
3407 return flags;
3408}
3409
Al Virob37ad282006-10-19 23:28:59 -07003410static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3412 struct nfsd4_open *open)
3413{
3414 struct iattr iattr = {
3415 .ia_valid = ATTR_SIZE,
3416 .ia_size = 0,
3417 };
3418 if (!open->op_truncate)
3419 return 0;
3420 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003421 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3423}
3424
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003425static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003426 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3427 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003428{
Trond Myklebustde186432014-07-10 14:07:26 -04003429 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003430 __be32 status;
3431 int oflag = nfs4_access_to_omode(open->op_share_access);
3432 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003433 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003434
Trond Myklebustde186432014-07-10 14:07:26 -04003435 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003436
3437 /*
3438 * Are we trying to set a deny mode that would conflict with
3439 * current access?
3440 */
3441 status = nfs4_file_check_deny(fp, open->op_share_deny);
3442 if (status != nfs_ok) {
3443 spin_unlock(&fp->fi_lock);
3444 goto out;
3445 }
3446
3447 /* set access to the file */
3448 status = nfs4_file_get_access(fp, open->op_share_access);
3449 if (status != nfs_ok) {
3450 spin_unlock(&fp->fi_lock);
3451 goto out;
3452 }
3453
3454 /* Set access bits in stateid */
3455 old_access_bmap = stp->st_access_bmap;
3456 set_access(open->op_share_access, stp);
3457
3458 /* Set new deny mask */
3459 old_deny_bmap = stp->st_deny_bmap;
3460 set_deny(open->op_share_deny, stp);
3461 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3462
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003463 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003464 spin_unlock(&fp->fi_lock);
3465 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003466 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003467 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003468 spin_lock(&fp->fi_lock);
3469 if (!fp->fi_fds[oflag]) {
3470 fp->fi_fds[oflag] = filp;
3471 filp = NULL;
3472 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003473 }
Trond Myklebustde186432014-07-10 14:07:26 -04003474 spin_unlock(&fp->fi_lock);
3475 if (filp)
3476 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003477
3478 status = nfsd4_truncate(rqstp, cur_fh, open);
3479 if (status)
3480 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003481out:
3482 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003483out_put_access:
3484 stp->st_access_bmap = old_access_bmap;
3485 nfs4_file_put_access(fp, open->op_share_access);
3486 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3487 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003488}
3489
Al Virob37ad282006-10-19 23:28:59 -07003490static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003491nfs4_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 -07003492{
Al Virob37ad282006-10-19 23:28:59 -07003493 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003494 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003496 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003497 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003498
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003499 /* test and set deny mode */
3500 spin_lock(&fp->fi_lock);
3501 status = nfs4_file_check_deny(fp, open->op_share_deny);
3502 if (status == nfs_ok) {
3503 old_deny_bmap = stp->st_deny_bmap;
3504 set_deny(open->op_share_deny, stp);
3505 fp->fi_share_deny |=
3506 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3507 }
3508 spin_unlock(&fp->fi_lock);
3509
3510 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003513 status = nfsd4_truncate(rqstp, cur_fh, open);
3514 if (status != nfs_ok)
3515 reset_union_bmap_deny(old_deny_bmap, stp);
3516 return status;
3517}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003520nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003522 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523}
3524
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003525/* Should we give out recallable state?: */
3526static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3527{
3528 if (clp->cl_cb_state == NFSD4_CB_UP)
3529 return true;
3530 /*
3531 * In the sessions case, since we don't have to establish a
3532 * separate connection for callbacks, we assume it's OK
3533 * until we hear otherwise:
3534 */
3535 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3536}
3537
Jeff Laytond564fbe2014-07-16 10:31:58 -04003538static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003539{
3540 struct file_lock *fl;
3541
3542 fl = locks_alloc_lock();
3543 if (!fl)
3544 return NULL;
3545 locks_init_lock(fl);
3546 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003547 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003548 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3549 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003550 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003551 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003552 return fl;
3553}
3554
J. Bruce Fields99c41512013-05-21 16:21:25 -04003555static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003556{
Trond Myklebust11b91642014-07-29 21:34:08 -04003557 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003558 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003559 struct file *filp;
3560 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003561
Jeff Laytond564fbe2014-07-16 10:31:58 -04003562 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003563 if (!fl)
3564 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003565 filp = find_readable_file(fp);
3566 if (!filp) {
3567 /* We should always have a readable file here */
3568 WARN_ON_ONCE(1);
3569 return -EBADF;
3570 }
3571 fl->fl_file = filp;
3572 status = vfs_setlease(filp, fl->fl_type, &fl);
3573 if (status) {
3574 locks_free_lock(fl);
3575 goto out_fput;
3576 }
Benny Halevycdc97502014-05-30 09:09:30 -04003577 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003578 spin_lock(&fp->fi_lock);
3579 /* Did the lease get broken before we took the lock? */
3580 status = -EAGAIN;
3581 if (fp->fi_had_conflict)
3582 goto out_unlock;
3583 /* Race breaker */
3584 if (fp->fi_lease) {
3585 status = 0;
3586 atomic_inc(&fp->fi_delegees);
3587 hash_delegation_locked(dp, fp);
3588 goto out_unlock;
3589 }
3590 fp->fi_lease = fl;
3591 fp->fi_deleg_file = filp;
3592 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003593 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003594 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003595 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003596 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003597out_unlock:
3598 spin_unlock(&fp->fi_lock);
3599 spin_unlock(&state_lock);
3600out_fput:
3601 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003602 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003603}
3604
Jeff Layton0b266932014-07-25 07:34:25 -04003605static struct nfs4_delegation *
3606nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3607 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003608{
Jeff Layton0b266932014-07-25 07:34:25 -04003609 int status;
3610 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003611
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003612 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003613 return ERR_PTR(-EAGAIN);
3614
3615 dp = alloc_init_deleg(clp, fh);
3616 if (!dp)
3617 return ERR_PTR(-ENOMEM);
3618
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003619 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003620 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003621 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003622 dp->dl_stid.sc_file = fp;
Jeff Layton417c6622014-07-21 09:34:57 -04003623 if (!fp->fi_lease) {
3624 spin_unlock(&fp->fi_lock);
3625 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003626 status = nfs4_setlease(dp);
3627 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003628 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003629 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003630 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003631 status = -EAGAIN;
3632 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003633 }
Benny Halevy931ee562014-05-30 09:09:27 -04003634 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003635 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003636out_unlock:
3637 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003638 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003639out:
3640 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003641 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003642 return ERR_PTR(status);
3643 }
3644 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003645}
3646
Benny Halevy4aa89132012-02-21 14:16:44 -08003647static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3648{
3649 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3650 if (status == -EAGAIN)
3651 open->op_why_no_deleg = WND4_CONTENTION;
3652 else {
3653 open->op_why_no_deleg = WND4_RESOURCE;
3654 switch (open->op_deleg_want) {
3655 case NFS4_SHARE_WANT_READ_DELEG:
3656 case NFS4_SHARE_WANT_WRITE_DELEG:
3657 case NFS4_SHARE_WANT_ANY_DELEG:
3658 break;
3659 case NFS4_SHARE_WANT_CANCEL:
3660 open->op_why_no_deleg = WND4_CANCELLED;
3661 break;
3662 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003663 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003664 }
3665 }
3666}
3667
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668/*
3669 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003670 *
3671 * Note we don't support write delegations, and won't until the vfs has
3672 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 */
3674static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003675nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3676 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677{
3678 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003679 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3680 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003681 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003682 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003684 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003685 open->op_recall = 0;
3686 switch (open->op_claim_type) {
3687 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003688 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003689 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003690 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3691 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003692 break;
3693 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003694 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003695 /*
3696 * Let's not give out any delegations till everyone's
3697 * had the chance to reclaim theirs....
3698 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003699 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003700 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003701 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003702 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003703 /*
3704 * Also, if the file was opened for write or
3705 * create, there's a good chance the client's
3706 * about to write to it, resulting in an
3707 * immediate recall (since we don't support
3708 * write delegations):
3709 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003710 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003711 goto out_no_deleg;
3712 if (open->op_create == NFS4_OPEN_CREATE)
3713 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003714 break;
3715 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003716 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003717 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003718 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003719 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003720 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003722 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003724 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003725 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003726 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003727 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003728 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003729out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003730 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3731 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003732 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003733 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003734 open->op_recall = 1;
3735 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003736
3737 /* 4.1 client asking for a delegation? */
3738 if (open->op_deleg_want)
3739 nfsd4_open_deleg_none_ext(open, status);
3740 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741}
3742
Benny Halevye27f49c2012-02-21 14:16:54 -08003743static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3744 struct nfs4_delegation *dp)
3745{
3746 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3747 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3748 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3749 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3750 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3751 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3752 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3753 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3754 }
3755 /* Otherwise the client must be confused wanting a delegation
3756 * it already has, therefore we don't return
3757 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3758 */
3759}
3760
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761/*
3762 * called with nfs4_lock_state() held.
3763 */
Al Virob37ad282006-10-19 23:28:59 -07003764__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3766{
Andy Adamson66689582009-04-03 08:28:45 +03003767 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003768 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003770 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003771 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003772 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 /*
3775 * Lookup file; if found, lookup stateid and check open request,
3776 * and check for delegations in the process of being recalled.
3777 * If not found, create the nfs4_file struct
3778 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003779 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04003780 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04003781 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003782 if (status)
3783 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003784 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04003786 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003787 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003788 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003789 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003790 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 }
3792
3793 /*
3794 * OPEN the file, or upgrade an existing OPEN.
3795 * If truncate fails, the OPEN fails.
3796 */
3797 if (stp) {
3798 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003799 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 if (status)
3801 goto out;
3802 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003803 stp = open->op_stp;
3804 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003805 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003806 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3807 if (status) {
3808 release_open_stateid(stp);
3809 goto out;
3810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003812 update_stateid(&stp->st_stid.sc_stateid);
3813 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
Benny Halevyd24433c2012-02-16 20:57:17 +02003815 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02003816 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3817 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3818 open->op_why_no_deleg = WND4_NOT_WANTED;
3819 goto nodeleg;
3820 }
3821 }
3822
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 /*
3824 * Attempt to hand out a delegation. No error return, because the
3825 * OPEN succeeds even if we fail.
3826 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003827 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003828nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 status = nfs_ok;
3830
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003831 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003832 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003834 /* 4.1 client trying to upgrade/downgrade delegation? */
3835 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003836 open->op_deleg_want)
3837 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003838
NeilBrown13cd2182005-06-23 22:03:10 -07003839 if (fp)
3840 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003841 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003842 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 /*
3844 * To finish the open response, we just need to set the rflags.
3845 */
3846 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003847 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003848 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003850 if (dp)
3851 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003852 if (stp)
3853 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854
3855 return status;
3856}
3857
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003858void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3859{
3860 if (open->op_openowner) {
3861 struct nfs4_openowner *oo = open->op_openowner;
3862
3863 if (!list_empty(&oo->oo_owner.so_stateids))
3864 list_del_init(&oo->oo_close_lru);
3865 if (oo->oo_flags & NFS4_OO_NEW) {
3866 if (status) {
3867 release_openowner(oo);
3868 open->op_openowner = NULL;
3869 } else
3870 oo->oo_flags &= ~NFS4_OO_NEW;
3871 }
3872 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003873 if (open->op_file)
3874 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003875 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04003876 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003877}
3878
Al Virob37ad282006-10-19 23:28:59 -07003879__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003880nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3881 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882{
3883 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003884 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003885 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886
3887 nfs4_lock_state();
3888 dprintk("process_renew(%08x/%08x): starting\n",
3889 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003890 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003891 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003893 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003895 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003896 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 goto out;
3898 status = nfs_ok;
3899out:
3900 nfs4_unlock_state();
3901 return status;
3902}
3903
NeilBrowna76b4312005-06-23 22:04:01 -07003904static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003905nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07003906{
Jeff Layton33dcc482012-04-10 11:08:48 -04003907 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003908 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04003909 return;
3910
NeilBrowna76b4312005-06-23 22:04:01 -07003911 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003912 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003913 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003914 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003915 /*
3916 * Now that every NFSv4 client has had the chance to recover and
3917 * to see the (possibly new, possibly shorter) lease time, we
3918 * can safely set the next grace time to the current lease time:
3919 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03003920 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003921}
3922
NeilBrownfd39ca92005-06-23 22:04:03 -07003923static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003924nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925{
3926 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003927 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 struct nfs4_delegation *dp;
3929 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003930 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003931 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932
3933 nfs4_lock_state();
3934
3935 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003936 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03003937 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003938 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03003939 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 clp = list_entry(pos, struct nfs4_client, cl_lru);
3941 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3942 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003943 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 break;
3945 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003946 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03003947 dprintk("NFSD: client in use (clientid %08x)\n",
3948 clp->cl_clientid.cl_id);
3949 continue;
3950 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003951 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003952 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003953 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03003954 list_for_each_safe(pos, next, &reaplist) {
3955 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 dprintk("NFSD: purging unused client (clientid %08x)\n",
3957 clp->cl_clientid.cl_id);
3958 expire_client(clp);
3959 }
Benny Halevycdc97502014-05-30 09:09:30 -04003960 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003961 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03003963 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3964 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003966 t = dp->dl_time - cutoff;
3967 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 break;
3969 }
Jeff Layton42690672014-07-25 07:34:20 -04003970 unhash_delegation_locked(dp);
3971 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 }
Benny Halevycdc97502014-05-30 09:09:30 -04003973 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04003974 while (!list_empty(&reaplist)) {
3975 dp = list_first_entry(&reaplist, struct nfs4_delegation,
3976 dl_recall_lru);
3977 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003978 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003980 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003981 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3982 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003983 t = oo->oo_time - cutoff;
3984 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 break;
3986 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003987 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04003989 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04003991 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992}
3993
Harvey Harrisona254b242008-02-20 12:49:00 -08003994static struct workqueue_struct *laundry_wq;
3995static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08003996
3997static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003998laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999{
4000 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004001 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4002 work);
4003 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4004 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004006 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004008 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009}
4010
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004011static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004012{
Trond Myklebust11b91642014-07-29 21:34:08 -04004013 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004014 return nfserr_bad_stateid;
4015 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016}
4017
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004019access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004021 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4022 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4023 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024}
4025
4026static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004027access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004029 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4030 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031}
4032
4033static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004034__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035{
Al Virob37ad282006-10-19 23:28:59 -07004036 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037
J. Bruce Fields02921912010-07-29 15:16:59 -04004038 /* For lock stateid's, we test the parent open, not the lock: */
4039 if (stp->st_openstp)
4040 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004041 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004043 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 goto out;
4045 status = nfs_ok;
4046out:
4047 return status;
4048}
4049
Al Virob37ad282006-10-19 23:28:59 -07004050static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004051check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004053 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004055 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004056 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 * conflicting state; so we must wait out the grace period. */
4058 return nfserr_grace;
4059 } else if (flags & WR_STATE)
4060 return nfs4_share_conflict(current_fh,
4061 NFS4_SHARE_DENY_WRITE);
4062 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4063 return nfs4_share_conflict(current_fh,
4064 NFS4_SHARE_DENY_READ);
4065}
4066
4067/*
4068 * Allow READ/WRITE during grace period on recovered state only for files
4069 * that are not able to provide mandatory locking.
4070 */
4071static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004072grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004074 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075}
4076
J. Bruce Fields81b82962011-08-23 11:03:29 -04004077/* Returns true iff a is later than b: */
4078static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4079{
Jim Rees1a9357f2013-05-17 17:33:00 -04004080 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004081}
4082
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004083static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004084{
Andy Adamson66689582009-04-03 08:28:45 +03004085 /*
4086 * When sessions are used the stateid generation number is ignored
4087 * when it is zero.
4088 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004089 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004090 return nfs_ok;
4091
4092 if (in->si_generation == ref->si_generation)
4093 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004094
J. Bruce Fields0836f582008-01-26 19:08:12 -05004095 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004096 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004097 return nfserr_bad_stateid;
4098 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004099 * However, we could see a stateid from the past, even from a
4100 * non-buggy client. For example, if the client sends a lock
4101 * while some IO is outstanding, the lock may bump si_generation
4102 * while the IO is still in flight. The client could avoid that
4103 * situation by waiting for responses on all the IO requests,
4104 * but better performance may result in retrying IO that
4105 * receives an old_stateid error if requests are rarely
4106 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004107 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004108 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004109}
4110
Chuck Lever7df302f2012-05-29 13:56:37 -04004111static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004112{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004113 struct nfs4_stid *s;
4114 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004115 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004116
Chuck Lever7df302f2012-05-29 13:56:37 -04004117 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004118 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004119 /* Client debugging aid. */
4120 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4121 char addr_str[INET6_ADDRSTRLEN];
4122 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4123 sizeof(addr_str));
4124 pr_warn_ratelimited("NFSD: client %s testing state ID "
4125 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004126 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004127 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004128 spin_lock(&cl->cl_lock);
4129 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004130 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004131 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004132 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004133 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004134 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004135 switch (s->sc_type) {
4136 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004137 status = nfs_ok;
4138 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004139 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004140 status = nfserr_deleg_revoked;
4141 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004142 case NFS4_OPEN_STID:
4143 case NFS4_LOCK_STID:
4144 ols = openlockstateid(s);
4145 if (ols->st_stateowner->so_is_open_owner
4146 && !(openowner(ols->st_stateowner)->oo_flags
4147 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004148 status = nfserr_bad_stateid;
4149 else
4150 status = nfs_ok;
4151 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004152 default:
4153 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004154 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004155 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004156 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004157 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004158 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004159out_unlock:
4160 spin_unlock(&cl->cl_lock);
4161 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004162}
4163
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004164static __be32
4165nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4166 stateid_t *stateid, unsigned char typemask,
4167 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004168{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004169 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004170
4171 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4172 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004173 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004174 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004175 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004176 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004177 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004178 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004179 if (status)
4180 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004181 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004182 if (!*s)
4183 return nfserr_bad_stateid;
4184 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004185}
4186
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187/*
4188* Checks for stateid operations
4189*/
Al Virob37ad282006-10-19 23:28:59 -07004190__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004191nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004192 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004194 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004195 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004197 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004199 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004200 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004201 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 if (filpp)
4204 *filpp = NULL;
4205
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004206 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 return nfserr_grace;
4208
4209 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004210 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211
Trond Myklebust14bcab12014-04-18 14:44:07 -04004212 nfs4_lock_state();
4213
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004214 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004215 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004216 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004217 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004218 goto unlock_state;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004219 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4220 if (status)
4221 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004222 switch (s->sc_type) {
4223 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004224 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004225 status = nfs4_check_delegmode(dp, flags);
4226 if (status)
4227 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004228 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004229 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004230 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004231 WARN_ON_ONCE(1);
4232 status = nfserr_serverfault;
4233 goto out;
4234 }
Trond Myklebustde186432014-07-10 14:07:26 -04004235 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004236 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004237 break;
4238 case NFS4_OPEN_STID:
4239 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004240 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004241 status = nfs4_check_fh(current_fh, stp);
4242 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004244 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004245 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004247 status = nfs4_check_openmode(stp, flags);
4248 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004250 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004251 struct nfs4_file *fp = stp->st_stid.sc_file;
4252
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004253 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004254 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004255 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004256 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004257 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004258 break;
4259 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004260 status = nfserr_bad_stateid;
4261 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 }
4263 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004264 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004265 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004267 nfs4_put_stid(s);
4268unlock_state:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004269 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 return status;
4271}
4272
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004273static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004274nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004275{
J. Bruce Fieldsa1b8ff42014-05-20 15:55:21 -04004276 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4277
Trond Myklebust11b91642014-07-29 21:34:08 -04004278 if (check_for_locks(stp->st_stid.sc_file, lo))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004279 return nfserr_locks_held;
Jeff Laytonc53530d2014-06-30 11:48:39 -04004280 release_lockowner_if_empty(lo);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004281 return nfs_ok;
4282}
4283
4284/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004285 * Test if the stateid is valid
4286 */
4287__be32
4288nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4289 struct nfsd4_test_stateid *test_stateid)
4290{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004291 struct nfsd4_test_stateid_id *stateid;
4292 struct nfs4_client *cl = cstate->session->se_client;
4293
4294 nfs4_lock_state();
4295 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004296 stateid->ts_id_status =
4297 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004298 nfs4_unlock_state();
4299
Bryan Schumaker17456802011-07-13 10:50:48 -04004300 return nfs_ok;
4301}
4302
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004303__be32
4304nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4305 struct nfsd4_free_stateid *free_stateid)
4306{
4307 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004308 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004309 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004310 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004311 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004312
4313 nfs4_lock_state();
Jeff Layton1af71cc2014-07-29 21:34:14 -04004314 spin_lock(&cl->cl_lock);
4315 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004316 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004317 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004318 switch (s->sc_type) {
4319 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004320 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004321 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004322 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004323 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4324 if (ret)
4325 break;
4326 ret = nfserr_locks_held;
4327 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004328 case NFS4_LOCK_STID:
4329 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4330 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004331 break;
4332 spin_unlock(&cl->cl_lock);
4333 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4334 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004335 case NFS4_REVOKED_DELEG_STID:
4336 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004337 list_del_init(&dp->dl_recall_lru);
4338 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004339 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004340 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004341 goto out;
4342 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004343 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004344out_unlock:
4345 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004346out:
4347 nfs4_unlock_state();
4348 return ret;
4349}
4350
NeilBrown4c4cd222005-07-07 17:59:27 -07004351static inline int
4352setlkflg (int type)
4353{
4354 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4355 RD_STATE : WR_STATE;
4356}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004358static __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 -04004359{
4360 struct svc_fh *current_fh = &cstate->current_fh;
4361 struct nfs4_stateowner *sop = stp->st_stateowner;
4362 __be32 status;
4363
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004364 status = nfsd4_check_seqid(cstate, sop, seqid);
4365 if (status)
4366 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004367 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4368 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004369 /*
4370 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004371 * nfserr_replay_me from the previous step, and
4372 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004373 */
4374 return nfserr_bad_stateid;
4375 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4376 if (status)
4377 return status;
4378 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004379}
4380
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381/*
4382 * Checks for sequence id mutating operations.
4383 */
Al Virob37ad282006-10-19 23:28:59 -07004384static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004385nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004386 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004387 struct nfs4_ol_stateid **stpp,
4388 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004390 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004391 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004392 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004394 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4395 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004396
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004398 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004399 if (status)
4400 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004401 stp = openlockstateid(s);
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04004402 if (!nfsd4_has_session(cstate))
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004403 cstate->replay_owner = stp->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004405 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004406 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004407 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004408 else
4409 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004410 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004411}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004412
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004413static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4414 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004415{
4416 __be32 status;
4417 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004418 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004420 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004421 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004422 if (status)
4423 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004424 oo = openowner(stp->st_stateowner);
4425 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4426 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004427 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004428 }
4429 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004430 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431}
4432
Al Virob37ad282006-10-19 23:28:59 -07004433__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004434nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004435 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436{
Al Virob37ad282006-10-19 23:28:59 -07004437 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004438 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004439 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004440 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441
Al Viroa6a9f182013-09-16 10:57:01 -04004442 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4443 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004445 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004446 if (status)
4447 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448
4449 nfs4_lock_state();
4450
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004451 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004452 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004453 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004454 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004455 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004456 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004457 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004458 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004459 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004460 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004461 update_stateid(&stp->st_stid.sc_stateid);
4462 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004463 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004464 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004465
Jeff Layton2a4317c2012-03-21 16:42:43 -04004466 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004467 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004468put_stateid:
4469 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004471 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004472 if (!cstate->replay_owner)
4473 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 return status;
4475}
4476
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004477static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004479 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004480 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004481 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004482 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004483}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004484
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004485static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4486{
4487 switch (to_access) {
4488 case NFS4_SHARE_ACCESS_READ:
4489 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4490 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4491 break;
4492 case NFS4_SHARE_ACCESS_WRITE:
4493 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4494 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4495 break;
4496 case NFS4_SHARE_ACCESS_BOTH:
4497 break;
4498 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004499 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 }
4501}
4502
Al Virob37ad282006-10-19 23:28:59 -07004503__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004504nfsd4_open_downgrade(struct svc_rqst *rqstp,
4505 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004506 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507{
Al Virob37ad282006-10-19 23:28:59 -07004508 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004509 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004510 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511
Al Viroa6a9f182013-09-16 10:57:01 -04004512 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4513 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004515 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004516 if (od->od_deleg_want)
4517 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4518 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519
4520 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004521 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004522 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004523 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004526 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004527 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004529 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004531 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004532 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004534 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004536 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537
Jeff Laytonce0fc432012-05-11 09:45:14 -04004538 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004540 update_stateid(&stp->st_stid.sc_stateid);
4541 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004543put_stateid:
4544 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004546 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004547 if (!cstate->replay_owner)
4548 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 return status;
4550}
4551
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004552static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4553{
Trond Myklebustacf92952014-06-30 11:48:37 -04004554 struct nfs4_client *clp = s->st_stid.sc_client;
4555 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4556
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004557 s->st_stid.sc_type = NFS4_CLOSED_STID;
Trond Myklebustacf92952014-06-30 11:48:37 -04004558 unhash_open_stateid(s);
4559
4560 if (clp->cl_minorversion) {
Trond Myklebustacf92952014-06-30 11:48:37 -04004561 if (list_empty(&oo->oo_owner.so_stateids))
4562 release_openowner(oo);
Trond Myklebust60116952014-07-29 21:34:06 -04004563 nfs4_put_stid(&s->st_stid);
Trond Myklebustacf92952014-06-30 11:48:37 -04004564 } else {
Trond Myklebust60116952014-07-29 21:34:06 -04004565 /*
4566 * In the 4.0 case we need to keep the owners around a
4567 * little while to handle CLOSE replay. We still do need
4568 * to release any file access that is held by them
4569 * before returning however.
4570 */
4571 release_all_access(s);
Trond Myklebust11b91642014-07-29 21:34:08 -04004572 if (s->st_stid.sc_file) {
4573 put_nfs4_file(s->st_stid.sc_file);
4574 s->st_stid.sc_file = NULL;
Trond Myklebustf8338832014-07-25 07:34:19 -04004575 }
Trond Myklebustacf92952014-06-30 11:48:37 -04004576 oo->oo_last_closed_stid = s;
Trond Myklebustacf92952014-06-30 11:48:37 -04004577 if (list_empty(&oo->oo_owner.so_stateids))
4578 move_to_close_lru(oo, clp->net);
4579 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004580}
4581
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582/*
4583 * nfs4_unlock_state() called after encode
4584 */
Al Virob37ad282006-10-19 23:28:59 -07004585__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004586nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004587 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588{
Al Virob37ad282006-10-19 23:28:59 -07004589 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004590 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004591 struct net *net = SVC_NET(rqstp);
4592 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593
Al Viroa6a9f182013-09-16 10:57:01 -04004594 dprintk("NFSD: nfsd4_close on file %pd\n",
4595 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596
4597 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004598 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4599 &close->cl_stateid,
4600 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004601 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004602 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004603 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004605 update_stateid(&stp->st_stid.sc_stateid);
4606 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004608 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004609
4610 /* put reference from nfs4_preprocess_seqid_op */
4611 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004613 if (!cstate->replay_owner)
4614 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 return status;
4616}
4617
Al Virob37ad282006-10-19 23:28:59 -07004618__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004619nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4620 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004622 struct nfs4_delegation *dp;
4623 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004624 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004625 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004626 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004628 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004629 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630
4631 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004632 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004633 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004634 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004635 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004636 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004637 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004638 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004639
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004640 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004641put_stateid:
4642 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004644 nfs4_unlock_state();
4645
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 return status;
4647}
4648
4649
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651
Benny Halevy87df4de2008-12-15 19:42:03 +02004652static inline u64
4653end_offset(u64 start, u64 len)
4654{
4655 u64 end;
4656
4657 end = start + len;
4658 return end >= start ? end: NFS4_MAX_UINT64;
4659}
4660
4661/* last octet in a range */
4662static inline u64
4663last_byte_offset(u64 start, u64 len)
4664{
4665 u64 end;
4666
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004667 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004668 end = start + len;
4669 return end > start ? end - 1: NFS4_MAX_UINT64;
4670}
4671
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672/*
4673 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4674 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4675 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4676 * locking, this prevents us from being completely protocol-compliant. The
4677 * real solution to this problem is to start using unsigned file offsets in
4678 * the VFS, but this is a very deep change!
4679 */
4680static inline void
4681nfs4_transform_lock_offset(struct file_lock *lock)
4682{
4683 if (lock->fl_start < 0)
4684 lock->fl_start = OFFSET_MAX;
4685 if (lock->fl_end < 0)
4686 lock->fl_end = OFFSET_MAX;
4687}
4688
NeilBrownd5b90262006-04-10 22:55:22 -07004689/* Hack!: For now, we're defining this just so we can use a pointer to it
4690 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004691static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004692};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693
4694static inline void
4695nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4696{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004697 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698
NeilBrownd5b90262006-04-10 22:55:22 -07004699 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004700 lo = (struct nfs4_lockowner *) fl->fl_owner;
4701 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4702 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004703 if (!deny->ld_owner.data)
4704 /* We just don't care that much */
4705 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004706 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4707 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004708 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004709nevermind:
4710 deny->ld_owner.len = 0;
4711 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004712 deny->ld_clientid.cl_boot = 0;
4713 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 }
4715 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004716 deny->ld_length = NFS4_MAX_UINT64;
4717 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4719 deny->ld_type = NFS4_READ_LT;
4720 if (fl->fl_type != F_RDLCK)
4721 deny->ld_type = NFS4_WRITE_LT;
4722}
4723
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004724static struct nfs4_lockowner *
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004725find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4726 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727{
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004728 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4729 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004731 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4732 if (so->so_is_open_owner)
4733 continue;
4734 if (!same_owner_str(so, owner, clid))
4735 continue;
4736 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 }
4738 return NULL;
4739}
4740
Jeff Layton6b180f02014-07-29 21:34:26 -04004741static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4742{
4743 struct nfs4_lockowner *lo = lockowner(sop);
4744
4745 kmem_cache_free(lockowner_slab, lo);
4746}
4747
4748static const struct nfs4_stateowner_operations lockowner_ops = {
4749 .so_free = nfs4_free_lockowner,
4750};
4751
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752/*
4753 * Alloc a lock owner structure.
4754 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004755 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004757 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004759static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004760alloc_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 -04004761 struct nfs4_lockowner *lo;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004762 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004764 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4765 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004767 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4768 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07004769 /* It is the openowner seqid that will be incremented in encode in the
4770 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004771 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
Jeff Layton6b180f02014-07-29 21:34:26 -04004772 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004773 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004774 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775}
4776
Jeff Layton356a95e2014-07-29 21:34:13 -04004777static void
4778init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
4779 struct nfs4_file *fp, struct inode *inode,
4780 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004782 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783
Jeff Layton356a95e2014-07-29 21:34:13 -04004784 lockdep_assert_held(&clp->cl_lock);
4785
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004786 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004787 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004788 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07004789 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004790 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04004791 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004792 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004794 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004795 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04004796 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004797 spin_lock(&fp->fi_lock);
4798 list_add(&stp->st_perfile, &fp->fi_stateids);
4799 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800}
4801
Jeff Laytonc53530d2014-06-30 11:48:39 -04004802static struct nfs4_ol_stateid *
4803find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4804{
4805 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04004806 struct nfs4_client *clp = lo->lo_owner.so_client;
4807
4808 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004809
4810 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004811 if (lst->st_stid.sc_file == fp) {
4812 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004813 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004814 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004815 }
4816 return NULL;
4817}
4818
Jeff Layton356a95e2014-07-29 21:34:13 -04004819static struct nfs4_ol_stateid *
4820find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
4821 struct inode *inode, struct nfs4_ol_stateid *ost,
4822 bool *new)
4823{
4824 struct nfs4_stid *ns = NULL;
4825 struct nfs4_ol_stateid *lst;
4826 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4827 struct nfs4_client *clp = oo->oo_owner.so_client;
4828
4829 spin_lock(&clp->cl_lock);
4830 lst = find_lock_stateid(lo, fi);
4831 if (lst == NULL) {
4832 spin_unlock(&clp->cl_lock);
4833 ns = nfs4_alloc_stid(clp, stateid_slab);
4834 if (ns == NULL)
4835 return NULL;
4836
4837 spin_lock(&clp->cl_lock);
4838 lst = find_lock_stateid(lo, fi);
4839 if (likely(!lst)) {
4840 lst = openlockstateid(ns);
4841 init_lock_stateid(lst, lo, fi, inode, ost);
4842 ns = NULL;
4843 *new = true;
4844 }
4845 }
4846 spin_unlock(&clp->cl_lock);
4847 if (ns)
4848 nfs4_put_stid(ns);
4849 return lst;
4850}
Jeff Laytonc53530d2014-06-30 11:48:39 -04004851
NeilBrownfd39ca92005-06-23 22:04:03 -07004852static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853check_lock_length(u64 offset, u64 length)
4854{
Benny Halevy87df4de2008-12-15 19:42:03 +02004855 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 LOFF_OVERFLOW(offset, length)));
4857}
4858
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004859static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004860{
Trond Myklebust11b91642014-07-29 21:34:08 -04004861 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004862
Jeff Layton7214e862014-07-10 14:07:33 -04004863 lockdep_assert_held(&fp->fi_lock);
4864
Jeff Layton82c5ff12012-05-11 09:45:13 -04004865 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004866 return;
Jeff Layton12659652014-07-10 14:07:28 -04004867 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004868 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004869}
4870
Jeff Layton356a95e2014-07-29 21:34:13 -04004871static __be32
4872lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
4873 struct nfs4_ol_stateid *ost,
4874 struct nfsd4_lock *lock,
4875 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004876{
Trond Myklebust11b91642014-07-29 21:34:08 -04004877 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004878 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4879 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004880 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004881 struct nfs4_lockowner *lo;
4882 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004883 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004884
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004885 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004886 if (!lo) {
4887 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4888 &lock->v.new.owner);
4889 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4890 if (lo == NULL)
4891 return nfserr_jukebox;
4892 } else {
4893 /* with an existing lockowner, seqids must be the same */
4894 if (!cstate->minorversion &&
4895 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004896 return nfserr_bad_seqid;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004897 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004898
Jeff Layton356a95e2014-07-29 21:34:13 -04004899 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004900 if (*lst == NULL) {
Jeff Layton356a95e2014-07-29 21:34:13 -04004901 release_lockowner_if_empty(lo);
4902 return nfserr_jukebox;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004903 }
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004904 return nfs_ok;
4905}
4906
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907/*
4908 * LOCK operation
4909 */
Al Virob37ad282006-10-19 23:28:59 -07004910__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004911nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004912 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004914 struct nfs4_openowner *open_sop = NULL;
4915 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004916 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004917 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04004918 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004919 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004920 struct file_lock *file_lock = NULL;
4921 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004922 __be32 status = 0;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004923 bool new_state = false;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004924 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004925 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004926 struct net *net = SVC_NET(rqstp);
4927 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928
4929 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4930 (long long) lock->lk_offset,
4931 (long long) lock->lk_length);
4932
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 if (check_lock_length(lock->lk_offset, lock->lk_length))
4934 return nfserr_inval;
4935
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004936 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004937 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004938 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4939 return status;
4940 }
4941
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 nfs4_lock_state();
4943
4944 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04004945 if (nfsd4_has_session(cstate))
4946 /* See rfc 5661 18.10.3: given clientid is ignored: */
4947 memcpy(&lock->v.new.clientid,
4948 &cstate->session->se_client->cl_clientid,
4949 sizeof(clientid_t));
4950
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004952 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004956 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 lock->lk_new_open_seqid,
4958 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004959 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07004960 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004962 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004963 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004964 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004965 &lock->v.new.clientid))
4966 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004967 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4968 &lock_stp, &new_state);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004969 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03004970 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004971 lock->lk_old_lock_seqid,
4972 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004973 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004974 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004975 if (status)
4976 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004977 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004979 lkflg = setlkflg(lock->lk_type);
4980 status = nfs4_check_openmode(lock_stp, lkflg);
4981 if (status)
4982 goto out;
4983
NeilBrown0dd395d2005-07-07 17:59:15 -07004984 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004985 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004986 goto out;
4987 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004988 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004989 goto out;
4990
Jeff Layton21179d82012-08-21 08:03:32 -04004991 file_lock = locks_alloc_lock();
4992 if (!file_lock) {
4993 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4994 status = nfserr_jukebox;
4995 goto out;
4996 }
4997
Trond Myklebust11b91642014-07-29 21:34:08 -04004998 fp = lock_stp->st_stid.sc_file;
Jeff Layton21179d82012-08-21 08:03:32 -04004999 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 switch (lock->lk_type) {
5001 case NFS4_READ_LT:
5002 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005003 spin_lock(&fp->fi_lock);
5004 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005005 if (filp)
5006 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005007 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005008 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005009 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 case NFS4_WRITE_LT:
5011 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005012 spin_lock(&fp->fi_lock);
5013 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005014 if (filp)
5015 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005016 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005017 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005018 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 default:
5020 status = nfserr_inval;
5021 goto out;
5022 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005023 if (!filp) {
5024 status = nfserr_openmode;
5025 goto out;
5026 }
Jeff Layton21179d82012-08-21 08:03:32 -04005027 file_lock->fl_owner = (fl_owner_t)lock_sop;
5028 file_lock->fl_pid = current->tgid;
5029 file_lock->fl_file = filp;
5030 file_lock->fl_flags = FL_POSIX;
5031 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5032 file_lock->fl_start = lock->lk_offset;
5033 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5034 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035
Jeff Layton21179d82012-08-21 08:03:32 -04005036 conflock = locks_alloc_lock();
5037 if (!conflock) {
5038 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5039 status = nfserr_jukebox;
5040 goto out;
5041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042
Jeff Layton21179d82012-08-21 08:03:32 -04005043 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005044 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005046 update_stateid(&lock_stp->st_stid.sc_stateid);
5047 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005049 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005050 break;
5051 case (EAGAIN): /* conflock holds conflicting lock */
5052 status = nfserr_denied;
5053 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005054 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005055 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 case (EDEADLK):
5057 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005058 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005059 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005060 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005061 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005062 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064out:
Trond Myklebustde186432014-07-10 14:07:26 -04005065 if (filp)
5066 fput(filp);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005067 if (lock_stp)
5068 nfs4_put_stid(&lock_stp->st_stid);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005069 if (open_stp)
5070 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005071 if (status && new_state)
Jeff Laytonc53530d2014-06-30 11:48:39 -04005072 release_lock_stateid(lock_stp);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005073 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04005074 if (!cstate->replay_owner)
5075 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005076 if (file_lock)
5077 locks_free_lock(file_lock);
5078 if (conflock)
5079 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 return status;
5081}
5082
5083/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005084 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5085 * so we do a temporary open here just to get an open file to pass to
5086 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5087 * inode operation.)
5088 */
Al Viro04da6e92012-04-13 00:00:04 -04005089static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005090{
5091 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005092 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5093 if (!err) {
5094 err = nfserrno(vfs_test_lock(file, lock));
5095 nfsd_close(file);
5096 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005097 return err;
5098}
5099
5100/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 * LOCKT operation
5102 */
Al Virob37ad282006-10-19 23:28:59 -07005103__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005104nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5105 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106{
Jeff Layton21179d82012-08-21 08:03:32 -04005107 struct file_lock *file_lock = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005108 struct nfs4_lockowner *lo;
Al Virob37ad282006-10-19 23:28:59 -07005109 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005110 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005112 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 return nfserr_grace;
5114
5115 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5116 return nfserr_inval;
5117
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 nfs4_lock_state();
5119
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005120 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005121 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005122 if (status)
5123 goto out;
5124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005126 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128
Jeff Layton21179d82012-08-21 08:03:32 -04005129 file_lock = locks_alloc_lock();
5130 if (!file_lock) {
5131 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5132 status = nfserr_jukebox;
5133 goto out;
5134 }
5135 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 switch (lockt->lt_type) {
5137 case NFS4_READ_LT:
5138 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005139 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 break;
5141 case NFS4_WRITE_LT:
5142 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005143 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 break;
5145 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005146 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 status = nfserr_inval;
5148 goto out;
5149 }
5150
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005151 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005152 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005153 file_lock->fl_owner = (fl_owner_t)lo;
5154 file_lock->fl_pid = current->tgid;
5155 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156
Jeff Layton21179d82012-08-21 08:03:32 -04005157 file_lock->fl_start = lockt->lt_offset;
5158 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159
Jeff Layton21179d82012-08-21 08:03:32 -04005160 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161
Jeff Layton21179d82012-08-21 08:03:32 -04005162 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005163 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005164 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005165
Jeff Layton21179d82012-08-21 08:03:32 -04005166 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005168 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 }
5170out:
5171 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005172 if (file_lock)
5173 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 return status;
5175}
5176
Al Virob37ad282006-10-19 23:28:59 -07005177__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005178nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005179 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005181 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005183 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005184 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005185 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005186 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5187
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5189 (long long) locku->lu_offset,
5190 (long long) locku->lu_length);
5191
5192 if (check_lock_length(locku->lu_offset, locku->lu_length))
5193 return nfserr_inval;
5194
5195 nfs4_lock_state();
5196
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005197 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005198 &locku->lu_stateid, NFS4_LOCK_STID,
5199 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005200 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005202 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005203 if (!filp) {
5204 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005205 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005206 }
Jeff Layton21179d82012-08-21 08:03:32 -04005207 file_lock = locks_alloc_lock();
5208 if (!file_lock) {
5209 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5210 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005211 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005212 }
5213 locks_init_lock(file_lock);
5214 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005215 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005216 file_lock->fl_pid = current->tgid;
5217 file_lock->fl_file = filp;
5218 file_lock->fl_flags = FL_POSIX;
5219 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5220 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221
Jeff Layton21179d82012-08-21 08:03:32 -04005222 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5223 locku->lu_length);
5224 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225
Jeff Layton21179d82012-08-21 08:03:32 -04005226 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005227 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005228 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 goto out_nfserr;
5230 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005231 update_stateid(&stp->st_stid.sc_stateid);
5232 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005233fput:
5234 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005235put_stateid:
5236 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005238 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields71c3bcd2011-09-27 21:42:29 -04005239 if (!cstate->replay_owner)
5240 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005241 if (file_lock)
5242 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 return status;
5244
5245out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005246 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005247 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248}
5249
5250/*
5251 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005252 * true: locks held by lockowner
5253 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005255static bool
5256check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257{
5258 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005259 int status = false;
5260 struct file *filp = find_any_file(fp);
5261 struct inode *inode;
5262
5263 if (!filp) {
5264 /* Any valid lock stateid should have some sort of access */
5265 WARN_ON_ONCE(1);
5266 return status;
5267 }
5268
5269 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270
Jeff Layton1c8c6012013-06-21 08:58:15 -04005271 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005273 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005274 status = true;
5275 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005278 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005279 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 return status;
5281}
5282
Al Virob37ad282006-10-19 23:28:59 -07005283__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005284nfsd4_release_lockowner(struct svc_rqst *rqstp,
5285 struct nfsd4_compound_state *cstate,
5286 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287{
5288 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Laytonfd449072014-06-30 11:48:41 -04005289 struct nfs4_stateowner *sop = NULL, *tmp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005290 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005291 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 struct xdr_netobj *owner = &rlockowner->rl_owner;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005293 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07005294 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005295 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296
5297 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5298 clid->cl_boot, clid->cl_id);
5299
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 nfs4_lock_state();
5301
Jeff Layton4b24ca72014-06-30 11:48:44 -04005302 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005303 if (status)
5304 goto out;
5305
NeilBrown3e9e3db2005-06-23 22:04:20 -07005306 status = nfserr_locks_held;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05005307
Jeff Laytonfd449072014-06-30 11:48:41 -04005308 /* Find the matching lock stateowner */
5309 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
5310 if (tmp->so_is_open_owner)
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005311 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005312 if (same_owner_str(tmp, owner, clid)) {
5313 sop = tmp;
5314 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07005316 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005317
5318 /* No matching owner found, maybe a replay? Just declare victory... */
5319 if (!sop) {
5320 status = nfs_ok;
5321 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005323
5324 lo = lockowner(sop);
5325 /* see if there are still any locks associated with it */
5326 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
Trond Myklebust11b91642014-07-29 21:34:08 -04005327 if (check_for_locks(stp->st_stid.sc_file, lo))
Jeff Laytonfd449072014-06-30 11:48:41 -04005328 goto out;
5329 }
5330
5331 status = nfs_ok;
5332 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333out:
5334 nfs4_unlock_state();
5335 return status;
5336}
5337
5338static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005339alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340{
NeilBrowna55370a2005-06-23 22:03:52 -07005341 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342}
5343
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005344bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005345nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005346{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005347 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005348
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005349 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005350 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005351}
5352
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353/*
5354 * failure => all reset bets are off, nfserr_no_grace...
5355 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005356struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005357nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358{
5359 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005360 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361
NeilBrowna55370a2005-06-23 22:03:52 -07005362 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5363 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005364 if (crp) {
5365 strhashval = clientstr_hashval(name);
5366 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005367 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005368 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005369 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005370 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005371 }
5372 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373}
5374
Jeff Layton2a4317c2012-03-21 16:42:43 -04005375void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005376nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005377{
5378 list_del(&crp->cr_strhash);
5379 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005380 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005381}
5382
5383void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005384nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385{
5386 struct nfs4_client_reclaim *crp = NULL;
5387 int i;
5388
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005390 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5391 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005393 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 }
5395 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005396 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397}
5398
5399/*
5400 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005401struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005402nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403{
5404 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 struct nfs4_client_reclaim *crp = NULL;
5406
Jeff Layton278c9312012-11-12 15:00:52 -05005407 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408
Jeff Layton278c9312012-11-12 15:00:52 -05005409 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005410 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005411 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 return crp;
5413 }
5414 }
5415 return NULL;
5416}
5417
5418/*
5419* Called from OPEN. Look for clientid in reclaim list.
5420*/
Al Virob37ad282006-10-19 23:28:59 -07005421__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005422nfs4_check_open_reclaim(clientid_t *clid,
5423 struct nfsd4_compound_state *cstate,
5424 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005426 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005427
5428 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005429 status = lookup_clientid(clid, cstate, nn);
5430 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005431 return nfserr_reclaim_bad;
5432
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005433 if (nfsd4_client_record_check(cstate->clp))
5434 return nfserr_reclaim_bad;
5435
5436 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437}
5438
Bryan Schumaker65178db2011-11-01 13:35:21 -04005439#ifdef CONFIG_NFSD_FAULT_INJECTION
5440
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005441u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5442{
J. Bruce Fields221a6872013-04-01 22:23:49 -04005443 if (mark_client_expired(clp))
5444 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005445 expire_client(clp);
5446 return 1;
5447}
5448
Bryan Schumaker184c1842012-11-29 11:40:44 -05005449u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5450{
5451 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005452 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005453 printk(KERN_INFO "NFS Client: %s\n", buf);
5454 return 1;
5455}
5456
5457static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5458 const char *type)
5459{
5460 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005461 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005462 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5463}
5464
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005465static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5466 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005467{
5468 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005469 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005470 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005471 u64 count = 0;
5472
5473 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005474 list_for_each_entry_safe(stp, st_next,
5475 &oop->oo_owner.so_stateids, st_perstateowner) {
5476 list_for_each_entry_safe(lst, lst_next,
5477 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005478 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005479 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005480 if (++count == max)
5481 return count;
5482 }
5483 }
5484 }
5485
5486 return count;
5487}
5488
5489u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5490{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005491 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005492}
5493
Bryan Schumaker184c1842012-11-29 11:40:44 -05005494u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5495{
5496 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5497 nfsd_print_count(clp, count, "locked files");
5498 return count;
5499}
5500
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005501static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5502{
5503 struct nfs4_openowner *oop, *next;
5504 u64 count = 0;
5505
5506 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5507 if (func)
5508 func(oop);
5509 if (++count == max)
5510 break;
5511 }
5512
5513 return count;
5514}
5515
5516u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5517{
5518 return nfsd_foreach_client_open(clp, max, release_openowner);
5519}
5520
Bryan Schumaker184c1842012-11-29 11:40:44 -05005521u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5522{
5523 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5524 nfsd_print_count(clp, count, "open files");
5525 return count;
5526}
5527
Bryan Schumaker269de302012-11-29 11:40:42 -05005528static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5529 struct list_head *victims)
5530{
5531 struct nfs4_delegation *dp, *next;
5532 u64 count = 0;
5533
Benny Halevycdc97502014-05-30 09:09:30 -04005534 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005535 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005536 if (victims) {
5537 /*
5538 * It's not safe to mess with delegations that have a
5539 * non-zero dl_time. They might have already been broken
5540 * and could be processed by the laundromat outside of
5541 * the state_lock. Just leave them be.
5542 */
5543 if (dp->dl_time != 0)
5544 continue;
5545
Jeff Layton42690672014-07-25 07:34:20 -04005546 unhash_delegation_locked(dp);
5547 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005548 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005549 if (++count == max)
5550 break;
5551 }
5552 return count;
5553}
5554
5555u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5556{
5557 struct nfs4_delegation *dp, *next;
5558 LIST_HEAD(victims);
5559 u64 count;
5560
Benny Halevycdc97502014-05-30 09:09:30 -04005561 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005562 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005563 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005564
Jeff Layton2d4a5322014-07-25 07:34:21 -04005565 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5566 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005567 revoke_delegation(dp);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005568 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005569
5570 return count;
5571}
5572
5573u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5574{
Jeff Laytondff13992014-07-08 14:02:49 -04005575 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005576 LIST_HEAD(victims);
5577 u64 count;
5578
Benny Halevycdc97502014-05-30 09:09:30 -04005579 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005580 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005581 while (!list_empty(&victims)) {
5582 dp = list_first_entry(&victims, struct nfs4_delegation,
5583 dl_recall_lru);
5584 list_del_init(&dp->dl_recall_lru);
5585 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005586 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005587 }
Benny Halevycdc97502014-05-30 09:09:30 -04005588 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005589
5590 return count;
5591}
5592
Bryan Schumaker184c1842012-11-29 11:40:44 -05005593u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5594{
5595 u64 count = 0;
5596
Benny Halevycdc97502014-05-30 09:09:30 -04005597 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005598 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005599 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005600
5601 nfsd_print_count(clp, count, "delegations");
5602 return count;
5603}
5604
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005605u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005606{
5607 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005608 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005609 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005610
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005611 if (!nfsd_netns_ready(nn))
5612 return 0;
5613
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005614 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005615 count += func(clp, max - count);
5616 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005617 break;
5618 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005619
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005620 return count;
5621}
5622
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005623struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5624{
5625 struct nfs4_client *clp;
5626 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5627
5628 if (!nfsd_netns_ready(nn))
5629 return NULL;
5630
5631 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5632 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5633 return clp;
5634 }
5635 return NULL;
5636}
5637
Bryan Schumaker65178db2011-11-01 13:35:21 -04005638#endif /* CONFIG_NFSD_FAULT_INJECTION */
5639
Meelap Shahc2f1a552007-07-17 04:04:39 -07005640/*
5641 * Since the lifetime of a delegation isn't limited to that of an open, a
5642 * client may quite reasonably hang on to a delegation as long as it has
5643 * the inode cached. This becomes an obvious problem the first time a
5644 * client's inode cache approaches the size of the server's total memory.
5645 *
5646 * For now we avoid this problem by imposing a hard limit on the number
5647 * of delegations, which varies according to the server's memory size.
5648 */
5649static void
5650set_max_delegations(void)
5651{
5652 /*
5653 * Allow at most 4 delegations per megabyte of RAM. Quick
5654 * estimates suggest that in the worst case (where every delegation
5655 * is for a different inode), a delegation could take about 1.5K,
5656 * giving a worst case usage of about 6% of memory.
5657 */
5658 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5659}
5660
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005661static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005662{
5663 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5664 int i;
5665
5666 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5667 CLIENT_HASH_SIZE, GFP_KERNEL);
5668 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005669 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005670 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5671 CLIENT_HASH_SIZE, GFP_KERNEL);
5672 if (!nn->unconf_id_hashtbl)
5673 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005674 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5675 OWNER_HASH_SIZE, GFP_KERNEL);
5676 if (!nn->ownerstr_hashtbl)
5677 goto err_ownerstr;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005678 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5679 SESSION_HASH_SIZE, GFP_KERNEL);
5680 if (!nn->sessionid_hashtbl)
5681 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005682
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005683 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005684 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005685 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005686 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005687 for (i = 0; i < OWNER_HASH_SIZE; i++)
5688 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005689 for (i = 0; i < SESSION_HASH_SIZE; i++)
5690 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005691 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005692 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005693 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005694 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005695 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005696 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005697
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005698 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005699 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005700
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005701 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005702
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005703err_sessionid:
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03005704 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005705err_ownerstr:
5706 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005707err_unconf_id:
5708 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005709err:
5710 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005711}
5712
5713static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005714nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005715{
5716 int i;
5717 struct nfs4_client *clp = NULL;
5718 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5719
5720 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5721 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5722 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5723 destroy_client(clp);
5724 }
5725 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005726
Kinglong Mee2b905632014-03-26 22:09:30 +08005727 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5728 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5729 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5730 destroy_client(clp);
5731 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005732 }
5733
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005734 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005735 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005736 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005737 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005738 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005739}
5740
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005741int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005742nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005743{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005744 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005745 int ret;
5746
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005747 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005748 if (ret)
5749 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005750 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005751 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005752 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005753 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005754 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005755 nn->nfsd4_grace, net);
5756 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005757 return 0;
5758}
5759
5760/* initialization to perform when the nfsd service is started: */
5761
5762int
5763nfs4_state_start(void)
5764{
5765 int ret;
5766
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005767 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005768 if (ret)
5769 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005770 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005771 if (laundry_wq == NULL) {
5772 ret = -ENOMEM;
5773 goto out_recovery;
5774 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005775 ret = nfsd4_create_callback_queue();
5776 if (ret)
5777 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005778
Meelap Shahc2f1a552007-07-17 04:04:39 -07005779 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005780
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005781 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005782
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005783out_free_laundry:
5784 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005785out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005786 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787}
5788
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005789void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005790nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005794 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005796 cancel_delayed_work_sync(&nn->laundromat_work);
5797 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005798
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005799 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04005801 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005802 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005804 unhash_delegation_locked(dp);
5805 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 }
Benny Halevycdc97502014-05-30 09:09:30 -04005807 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 list_for_each_safe(pos, next, &reaplist) {
5809 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005810 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04005811 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 }
5813
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005814 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005815 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005816 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817}
5818
5819void
5820nfs4_state_shutdown(void)
5821{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005822 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005823 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005825
5826static void
5827get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5828{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005829 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5830 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005831}
5832
5833static void
5834put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5835{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005836 if (cstate->minorversion) {
5837 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5838 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5839 }
5840}
5841
5842void
5843clear_current_stateid(struct nfsd4_compound_state *cstate)
5844{
5845 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005846}
5847
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005848/*
5849 * functions to set current state id
5850 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005851void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005852nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5853{
5854 put_stateid(cstate, &odp->od_stateid);
5855}
5856
5857void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005858nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5859{
5860 put_stateid(cstate, &open->op_stateid);
5861}
5862
5863void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005864nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5865{
5866 put_stateid(cstate, &close->cl_stateid);
5867}
5868
5869void
5870nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5871{
5872 put_stateid(cstate, &lock->lk_resp_stateid);
5873}
5874
5875/*
5876 * functions to consume current state id
5877 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005878
5879void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005880nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5881{
5882 get_stateid(cstate, &odp->od_stateid);
5883}
5884
5885void
5886nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5887{
5888 get_stateid(cstate, &drp->dr_stateid);
5889}
5890
5891void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005892nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5893{
5894 get_stateid(cstate, &fsp->fr_stateid);
5895}
5896
5897void
5898nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5899{
5900 get_stateid(cstate, &setattr->sa_stateid);
5901}
5902
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005903void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005904nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5905{
5906 get_stateid(cstate, &close->cl_stateid);
5907}
5908
5909void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005910nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005911{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005912 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005913}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01005914
5915void
5916nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5917{
5918 get_stateid(cstate, &read->rd_stateid);
5919}
5920
5921void
5922nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5923{
5924 get_stateid(cstate, &write->wr_stateid);
5925}