blob: 653de6b14665a55d7208f5fefdb5431d56da5e82 [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
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400893static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500894{
Trond Myklebust11b91642014-07-29 21:34:08 -0400895 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -0400896
Jeff Layton1c755dc2014-07-29 21:34:12 -0400897 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
898
Trond Myklebust1d31a252014-07-10 14:07:25 -0400899 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500900 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400901 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500902 list_del(&stp->st_perstateowner);
903}
904
Trond Myklebust60116952014-07-29 21:34:06 -0400905static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500906{
Trond Myklebust60116952014-07-29 21:34:06 -0400907 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400908
Trond Myklebust60116952014-07-29 21:34:06 -0400909 release_all_access(stp);
Trond Myklebust60116952014-07-29 21:34:06 -0400910 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500911}
912
Jeff Laytonb49e0842014-07-29 21:34:11 -0400913static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500914{
Jeff Laytonb49e0842014-07-29 21:34:11 -0400915 struct nfs4_ol_stateid *stp = openlockstateid(stid);
916 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500917 struct file *file;
918
Jeff Laytonb49e0842014-07-29 21:34:11 -0400919 file = find_any_file(stp->st_stid.sc_file);
920 if (file)
921 filp_close(file, (fl_owner_t)lo);
922 nfs4_free_ol_stateid(stid);
923}
924
925static void __release_lock_stateid(struct nfs4_ol_stateid *stp)
926{
Jeff Layton1c755dc2014-07-29 21:34:12 -0400927 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
928
929 spin_lock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400930 list_del(&stp->st_locks);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500931 unhash_generic_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400932 unhash_stid(&stp->st_stid);
Jeff Layton1c755dc2014-07-29 21:34:12 -0400933 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400934 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500935}
936
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400937static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500938{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400939 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500940
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400941 list_del(&lo->lo_owner.so_strhash);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400942 while (!list_empty(&lo->lo_owner.so_stateids)) {
943 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400944 struct nfs4_ol_stateid, st_perstateowner);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400945 __release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500946 }
947}
948
Trond Myklebust50cc6232014-04-18 14:44:03 -0400949static void nfs4_free_lockowner(struct nfs4_lockowner *lo)
950{
951 kfree(lo->lo_owner.so_owner.data);
952 kmem_cache_free(lockowner_slab, lo);
953}
954
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400955static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500956{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400957 unhash_lockowner(lo);
958 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500959}
960
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400961static void release_lockowner_if_empty(struct nfs4_lockowner *lo)
962{
963 if (list_empty(&lo->lo_owner.so_stateids))
964 release_lockowner(lo);
965}
966
967static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500968{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400969 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500970
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400971 lo = lockowner(stp->st_stateowner);
972 __release_lock_stateid(stp);
973 release_lockowner_if_empty(lo);
974}
975
976static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
Jeff Layton1c755dc2014-07-29 21:34:12 -0400977 __releases(&open_stp->st_stateowner->so_client->cl_lock)
978 __acquires(&open_stp->st_stateowner->so_client->cl_lock)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400979{
980 struct nfs4_ol_stateid *stp;
981
982 while (!list_empty(&open_stp->st_locks)) {
983 stp = list_entry(open_stp->st_locks.next,
984 struct nfs4_ol_stateid, st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -0400985 spin_unlock(&open_stp->st_stateowner->so_client->cl_lock);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400986 release_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -0400987 spin_lock(&open_stp->st_stateowner->so_client->cl_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500988 }
989}
990
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400991static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500992{
Jeff Layton1c755dc2014-07-29 21:34:12 -0400993 spin_lock(&stp->st_stateowner->so_client->cl_lock);
J. Bruce Fields22839632009-01-11 14:27:17 -0500994 unhash_generic_stateid(stp);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400995 release_open_stateid_locks(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -0400996 spin_unlock(&stp->st_stateowner->so_client->cl_lock);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400997}
998
999static void release_open_stateid(struct nfs4_ol_stateid *stp)
1000{
1001 unhash_open_stateid(stp);
Trond Myklebust60116952014-07-29 21:34:06 -04001002 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields22839632009-01-11 14:27:17 -05001003}
1004
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001005static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001006{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001007 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001008
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001009 list_del(&oo->oo_owner.so_strhash);
1010 list_del(&oo->oo_perclient);
1011 while (!list_empty(&oo->oo_owner.so_stateids)) {
1012 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001013 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05001014 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001015 }
1016}
1017
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001018static void release_last_closed_stateid(struct nfs4_openowner *oo)
1019{
1020 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
1021
1022 if (s) {
Trond Myklebust60116952014-07-29 21:34:06 -04001023 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001024 oo->oo_last_closed_stid = NULL;
1025 }
1026}
1027
Trond Myklebust50cc6232014-04-18 14:44:03 -04001028static void nfs4_free_openowner(struct nfs4_openowner *oo)
1029{
1030 kfree(oo->oo_owner.so_owner.data);
1031 kmem_cache_free(openowner_slab, oo);
1032}
1033
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001034static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001035{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001036 unhash_openowner(oo);
1037 list_del(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001038 release_last_closed_stateid(oo);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001039 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001040}
1041
Marc Eshel5282fd72009-04-03 08:27:52 +03001042static inline int
1043hash_sessionid(struct nfs4_sessionid *sessionid)
1044{
1045 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1046
1047 return sid->sequence % SESSION_HASH_SIZE;
1048}
1049
Trond Myklebust8f199b82012-03-20 15:11:17 -04001050#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001051static inline void
1052dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1053{
1054 u32 *ptr = (u32 *)(&sessionid->data[0]);
1055 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1056}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001057#else
1058static inline void
1059dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1060{
1061}
1062#endif
1063
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001064/*
1065 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1066 * won't be used for replay.
1067 */
1068void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1069{
1070 struct nfs4_stateowner *so = cstate->replay_owner;
1071
1072 if (nfserr == nfserr_replay_me)
1073 return;
1074
1075 if (!seqid_mutating_err(ntohl(nfserr))) {
1076 cstate->replay_owner = NULL;
1077 return;
1078 }
1079 if (!so)
1080 return;
1081 if (so->so_is_open_owner)
1082 release_last_closed_stateid(openowner(so));
1083 so->so_seqid++;
1084 return;
1085}
Marc Eshel5282fd72009-04-03 08:27:52 +03001086
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001087static void
1088gen_sessionid(struct nfsd4_session *ses)
1089{
1090 struct nfs4_client *clp = ses->se_client;
1091 struct nfsd4_sessionid *sid;
1092
1093 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1094 sid->clientid = clp->cl_clientid;
1095 sid->sequence = current_sessionid++;
1096 sid->reserved = 0;
1097}
1098
1099/*
Andy Adamsona6496372009-08-28 08:45:01 -04001100 * The protocol defines ca_maxresponssize_cached to include the size of
1101 * the rpc header, but all we need to cache is the data starting after
1102 * the end of the initial SEQUENCE operation--the rest we regenerate
1103 * each time. Therefore we can advertise a ca_maxresponssize_cached
1104 * value that is the number of bytes in our cache plus a few additional
1105 * bytes. In order to stay on the safe side, and not promise more than
1106 * we can cache, those additional bytes must be the minimum possible: 24
1107 * bytes of rpc header (xid through accept state, with AUTH_NULL
1108 * verifier), 12 for the compound header (with zero-length tag), and 44
1109 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001110 */
Andy Adamsona6496372009-08-28 08:45:01 -04001111#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1112
Andy Adamson557ce262009-08-28 08:45:04 -04001113static void
1114free_session_slots(struct nfsd4_session *ses)
1115{
1116 int i;
1117
1118 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1119 kfree(ses->se_slots[i]);
1120}
1121
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001122/*
1123 * We don't actually need to cache the rpc and session headers, so we
1124 * can allocate a little less for each slot:
1125 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001126static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001127{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001128 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001129
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001130 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1131 size = 0;
1132 else
1133 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1134 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001135}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001136
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001137/*
1138 * XXX: If we run out of reserved DRC memory we could (up to a point)
1139 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001140 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001141 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001142static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001143{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001144 u32 slotsize = slot_bytes(ca);
1145 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001146 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001147
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001148 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001149 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1150 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001151 num = min_t(int, num, avail / slotsize);
1152 nfsd_drc_mem_used += num * slotsize;
1153 spin_unlock(&nfsd_drc_lock);
1154
1155 return num;
1156}
1157
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001158static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001159{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001160 int slotsize = slot_bytes(ca);
1161
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001162 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001163 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001164 spin_unlock(&nfsd_drc_lock);
1165}
1166
Kinglong Mee60810e52014-01-01 00:35:47 +08001167static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1168 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001169{
Kinglong Mee60810e52014-01-01 00:35:47 +08001170 int numslots = fattrs->maxreqs;
1171 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001172 struct nfsd4_session *new;
1173 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001174
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001175 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001176 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1177 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001178
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001179 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001180 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001181 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001182 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001183 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001184 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001185 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001186 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001187 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001188
1189 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1190 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1191
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001192 return new;
1193out_free:
1194 while (i--)
1195 kfree(new->se_slots[i]);
1196 kfree(new);
1197 return NULL;
1198}
1199
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001200static void free_conn(struct nfsd4_conn *c)
1201{
1202 svc_xprt_put(c->cn_xprt);
1203 kfree(c);
1204}
1205
1206static void nfsd4_conn_lost(struct svc_xpt_user *u)
1207{
1208 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1209 struct nfs4_client *clp = c->cn_session->se_client;
1210
1211 spin_lock(&clp->cl_lock);
1212 if (!list_empty(&c->cn_persession)) {
1213 list_del(&c->cn_persession);
1214 free_conn(c);
1215 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001216 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001217 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001218}
1219
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001220static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001221{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001222 struct nfsd4_conn *conn;
1223
1224 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1225 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001226 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001227 svc_xprt_get(rqstp->rq_xprt);
1228 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001229 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001230 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1231 return conn;
1232}
1233
J. Bruce Fields328ead22010-09-29 16:11:06 -04001234static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1235{
1236 conn->cn_session = ses;
1237 list_add(&conn->cn_persession, &ses->se_conns);
1238}
1239
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001240static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1241{
1242 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001243
1244 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001245 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001246 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001247}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001248
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001249static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001250{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001251 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001252 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001253}
1254
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001255static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001256{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001257 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001258
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001259 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001260 ret = nfsd4_register_conn(conn);
1261 if (ret)
1262 /* oops; xprt is already down: */
1263 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001264 /* We may have gained or lost a callback channel: */
1265 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001266}
1267
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001268static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001269{
1270 u32 dir = NFS4_CDFC4_FORE;
1271
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001272 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001273 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001274 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001275}
1276
1277/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001278static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001279{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001280 struct nfs4_client *clp = s->se_client;
1281 struct nfsd4_conn *c;
1282
1283 spin_lock(&clp->cl_lock);
1284 while (!list_empty(&s->se_conns)) {
1285 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1286 list_del_init(&c->cn_persession);
1287 spin_unlock(&clp->cl_lock);
1288
1289 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1290 free_conn(c);
1291
1292 spin_lock(&clp->cl_lock);
1293 }
1294 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001295}
1296
J. Bruce Fields1377b692012-09-11 21:42:40 -04001297static void __free_session(struct nfsd4_session *ses)
1298{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001299 free_session_slots(ses);
1300 kfree(ses);
1301}
1302
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001303static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001304{
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001305 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1306
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001307 lockdep_assert_held(&nn->client_lock);
1308 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001309 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001310 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001311}
Andy Adamson557ce262009-08-28 08:45:04 -04001312
Fengguang Wu135ae822012-11-10 07:20:25 -05001313static 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 -04001314{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001315 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001316 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001317
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001318 new->se_client = clp;
1319 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001320
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001321 INIT_LIST_HEAD(&new->se_conns);
1322
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001323 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001324 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001325 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001326 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001327 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001328 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001329 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001330 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001331 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001332 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001333 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001334 spin_unlock(&nn->client_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001335
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001336 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001337 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001338 /*
1339 * This is a little silly; with sessions there's no real
1340 * use for the callback address. Use the peer address
1341 * as a reasonable default for now, but consider fixing
1342 * the rpc client not to require an address in the
1343 * future:
1344 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001345 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1346 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001347 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001348}
1349
Benny Halevy9089f1b2010-05-12 00:12:26 +03001350/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001351static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001352__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001353{
1354 struct nfsd4_session *elem;
1355 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001356 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001357
1358 dump_sessionid(__func__, sessionid);
1359 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001360 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001361 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001362 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1363 NFS4_MAX_SESSIONID_LEN)) {
1364 return elem;
1365 }
1366 }
1367
1368 dprintk("%s: session not found\n", __func__);
1369 return NULL;
1370}
1371
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001372static struct nfsd4_session *
1373find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1374 __be32 *ret)
1375{
1376 struct nfsd4_session *session;
1377 __be32 status = nfserr_badsession;
1378
1379 session = __find_in_sessionid_hashtbl(sessionid, net);
1380 if (!session)
1381 goto out;
1382 status = nfsd4_get_session_locked(session);
1383 if (status)
1384 session = NULL;
1385out:
1386 *ret = status;
1387 return session;
1388}
1389
Benny Halevy9089f1b2010-05-12 00:12:26 +03001390/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001391static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001392unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001393{
1394 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001395 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001396 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001397 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001398}
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1401static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001402STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001404 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001406 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001407 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return 1;
1409}
1410
1411/*
1412 * XXX Should we use a slab cache ?
1413 * This type of memory management is somewhat inefficient, but we use it
1414 * anyway since SETCLIENTID is not a common operation.
1415 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001416static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
1418 struct nfs4_client *clp;
1419
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001420 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1421 if (clp == NULL)
1422 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001423 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001424 if (clp->cl_name.data == NULL) {
1425 kfree(clp);
1426 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001428 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001429 INIT_LIST_HEAD(&clp->cl_sessions);
1430 idr_init(&clp->cl_stateids);
1431 atomic_set(&clp->cl_refcount, 0);
1432 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1433 INIT_LIST_HEAD(&clp->cl_idhash);
1434 INIT_LIST_HEAD(&clp->cl_openowners);
1435 INIT_LIST_HEAD(&clp->cl_delegations);
1436 INIT_LIST_HEAD(&clp->cl_lru);
1437 INIT_LIST_HEAD(&clp->cl_callbacks);
1438 INIT_LIST_HEAD(&clp->cl_revoked);
1439 spin_lock_init(&clp->cl_lock);
1440 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return clp;
1442}
1443
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001444static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445free_client(struct nfs4_client *clp)
1446{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001447 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001448
1449 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001450 while (!list_empty(&clp->cl_sessions)) {
1451 struct nfsd4_session *ses;
1452 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1453 se_perclnt);
1454 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001455 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1456 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001457 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001458 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001459 free_svc_cred(&clp->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001461 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 kfree(clp);
1463}
1464
Benny Halevy84d38ac2010-05-12 00:13:16 +03001465/* must be called under the client_lock */
1466static inline void
1467unhash_client_locked(struct nfs4_client *clp)
1468{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001469 struct nfsd4_session *ses;
1470
Benny Halevy84d38ac2010-05-12 00:13:16 +03001471 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001472 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001473 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1474 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001475 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001476}
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001479destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001481 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001484 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001487 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001488 while (!list_empty(&clp->cl_delegations)) {
1489 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001490 unhash_delegation_locked(dp);
1491 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 }
Benny Halevycdc97502014-05-30 09:09:30 -04001493 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 while (!list_empty(&reaplist)) {
1495 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001496 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001497 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001499 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001500 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001501 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001502 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001503 }
NeilBrownea1da632005-06-23 22:04:17 -07001504 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001505 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1506 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001508 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001509 if (clp->cl_cb_conn.cb_xprt)
1510 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001511 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001512 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001513 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001514 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001515 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001516 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001517 unhash_client_locked(clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001518 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1519 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001520 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001523static void expire_client(struct nfs4_client *clp)
1524{
1525 nfsd4_client_record_remove(clp);
1526 destroy_client(clp);
1527}
1528
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001529static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1530{
1531 memcpy(target->cl_verifier.data, source->data,
1532 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001535static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1536{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1538 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1539}
1540
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001541static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001542{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001543 if (source->cr_principal) {
1544 target->cr_principal =
1545 kstrdup(source->cr_principal, GFP_KERNEL);
1546 if (target->cr_principal == NULL)
1547 return -ENOMEM;
1548 } else
1549 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001550 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 target->cr_uid = source->cr_uid;
1552 target->cr_gid = source->cr_gid;
1553 target->cr_group_info = source->cr_group_info;
1554 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001555 target->cr_gss_mech = source->cr_gss_mech;
1556 if (source->cr_gss_mech)
1557 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001558 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001561static long long
1562compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1563{
1564 long long res;
1565
1566 res = o1->len - o2->len;
1567 if (res)
1568 return res;
1569 return (long long)memcmp(o1->data, o2->data, o1->len);
1570}
1571
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001572static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001573{
NeilBrowna55370a2005-06-23 22:03:52 -07001574 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
1577static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001578same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1579{
1580 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582
1583static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001584same_clid(clientid_t *cl1, clientid_t *cl2)
1585{
1586 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
1588
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001589static bool groups_equal(struct group_info *g1, struct group_info *g2)
1590{
1591 int i;
1592
1593 if (g1->ngroups != g2->ngroups)
1594 return false;
1595 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001596 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001597 return false;
1598 return true;
1599}
1600
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001601/*
1602 * RFC 3530 language requires clid_inuse be returned when the
1603 * "principal" associated with a requests differs from that previously
1604 * used. We use uid, gid's, and gss principal string as our best
1605 * approximation. We also don't want to allow non-gss use of a client
1606 * established using gss: in theory cr_principal should catch that
1607 * change, but in practice cr_principal can be null even in the gss case
1608 * since gssd doesn't always pass down a principal string.
1609 */
1610static bool is_gss_cred(struct svc_cred *cr)
1611{
1612 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1613 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1614}
1615
1616
Vivek Trivedi5559b502012-07-24 21:18:20 +05301617static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001618same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1619{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001620 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001621 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1622 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001623 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1624 return false;
1625 if (cr1->cr_principal == cr2->cr_principal)
1626 return true;
1627 if (!cr1->cr_principal || !cr2->cr_principal)
1628 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301629 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630}
1631
J. Bruce Fields57266a62013-04-13 14:27:29 -04001632static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1633{
1634 struct svc_cred *cr = &rqstp->rq_cred;
1635 u32 service;
1636
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001637 if (!cr->cr_gss_mech)
1638 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001639 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1640 return service == RPC_GSS_SVC_INTEGRITY ||
1641 service == RPC_GSS_SVC_PRIVACY;
1642}
1643
1644static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1645{
1646 struct svc_cred *cr = &rqstp->rq_cred;
1647
1648 if (!cl->cl_mach_cred)
1649 return true;
1650 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1651 return false;
1652 if (!svc_rqst_integrity_protected(rqstp))
1653 return false;
1654 if (!cr->cr_principal)
1655 return false;
1656 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1657}
1658
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001659static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001660{
1661 static u32 current_clientid = 1;
1662
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001663 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 clp->cl_clientid.cl_id = current_clientid++;
1665}
1666
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001667static void gen_confirm(struct nfs4_client *clp)
1668{
Chuck Leverab4684d2012-03-02 17:13:50 -05001669 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001670 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Jeff Laytonf4199922014-06-17 07:44:11 -04001672 /*
1673 * This is opaque to client, so no need to byte-swap. Use
1674 * __force to keep sparse happy
1675 */
1676 verf[0] = (__force __be32)get_seconds();
1677 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001678 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
1680
Jeff Layton4770d722014-07-29 21:34:10 -04001681static struct nfs4_stid *
1682find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001683{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001684 struct nfs4_stid *ret;
1685
1686 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1687 if (!ret || !ret->sc_type)
1688 return NULL;
1689 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001690}
1691
Jeff Layton4770d722014-07-29 21:34:10 -04001692static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001693find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001694{
1695 struct nfs4_stid *s;
1696
Jeff Layton4770d722014-07-29 21:34:10 -04001697 spin_lock(&cl->cl_lock);
1698 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001699 if (s != NULL) {
1700 if (typemask & s->sc_type)
1701 atomic_inc(&s->sc_count);
1702 else
1703 s = NULL;
1704 }
Jeff Layton4770d722014-07-29 21:34:10 -04001705 spin_unlock(&cl->cl_lock);
1706 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001707}
1708
Jeff Layton2216d442012-11-12 15:00:57 -05001709static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001710 struct svc_rqst *rqstp, nfs4_verifier *verf)
1711{
1712 struct nfs4_client *clp;
1713 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001714 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001715 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001716 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001717
1718 clp = alloc_client(name);
1719 if (clp == NULL)
1720 return NULL;
1721
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001722 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1723 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001724 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001725 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001726 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001727 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001728 }
Jeff Layton02e12152014-07-16 10:31:57 -04001729 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001730 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001731 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001732 copy_verf(clp, verf);
1733 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001734 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001735 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001736 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001737 return clp;
1738}
1739
NeilBrownfd39ca92005-06-23 22:04:03 -07001740static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001741add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1742{
1743 struct rb_node **new = &(root->rb_node), *parent = NULL;
1744 struct nfs4_client *clp;
1745
1746 while (*new) {
1747 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1748 parent = *new;
1749
1750 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1751 new = &((*new)->rb_left);
1752 else
1753 new = &((*new)->rb_right);
1754 }
1755
1756 rb_link_node(&new_clp->cl_namenode, parent, new);
1757 rb_insert_color(&new_clp->cl_namenode, root);
1758}
1759
1760static struct nfs4_client *
1761find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1762{
1763 long long cmp;
1764 struct rb_node *node = root->rb_node;
1765 struct nfs4_client *clp;
1766
1767 while (node) {
1768 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1769 cmp = compare_blob(&clp->cl_name, name);
1770 if (cmp > 0)
1771 node = node->rb_left;
1772 else if (cmp < 0)
1773 node = node->rb_right;
1774 else
1775 return clp;
1776 }
1777 return NULL;
1778}
1779
1780static void
1781add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
1783 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001784 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001786 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001787 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001789 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001790 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791}
1792
NeilBrownfd39ca92005-06-23 22:04:03 -07001793static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794move_to_confirmed(struct nfs4_client *clp)
1795{
1796 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001797 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
1799 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001800 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001801 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001802 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001803 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 renew_client(clp);
1805}
1806
1807static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001808find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
1810 struct nfs4_client *clp;
1811 unsigned int idhashval = clientid_hashval(clid->cl_id);
1812
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001813 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001814 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001815 if ((bool)clp->cl_minorversion != sessions)
1816 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001817 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 }
1821 return NULL;
1822}
1823
1824static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001825find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1826{
1827 struct list_head *tbl = nn->conf_id_hashtbl;
1828
1829 return find_client_in_id_table(tbl, clid, sessions);
1830}
1831
1832static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001833find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001835 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001837 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001840static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001841{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001842 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001843}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001844
NeilBrown28ce6052005-06-23 22:03:56 -07001845static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001846find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001847{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001848 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001849}
1850
1851static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001852find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001853{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001854 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001855}
1856
NeilBrownfd39ca92005-06-23 22:04:03 -07001857static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001858gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001860 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001861 struct sockaddr *sa = svc_addr(rqstp);
1862 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001863 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Jeff Layton7077ecb2009-08-14 12:57:58 -04001865 /* Currently, we only support tcp and tcp6 for the callback channel */
1866 if (se->se_callback_netid_len == 3 &&
1867 !memcmp(se->se_callback_netid_val, "tcp", 3))
1868 expected_family = AF_INET;
1869 else if (se->se_callback_netid_len == 4 &&
1870 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1871 expected_family = AF_INET6;
1872 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 goto out_err;
1874
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001875 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001876 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001877 (struct sockaddr *)&conn->cb_addr,
1878 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001879
J. Bruce Fields07263f12010-05-31 19:09:40 -04001880 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001882
J. Bruce Fields07263f12010-05-31 19:09:40 -04001883 if (conn->cb_addr.ss_family == AF_INET6)
1884 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001885
J. Bruce Fields07263f12010-05-31 19:09:40 -04001886 conn->cb_prog = se->se_callback_prog;
1887 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001888 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 return;
1890out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001891 conn->cb_addr.ss_family = AF_UNSPEC;
1892 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001893 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 "will not receive delegations\n",
1895 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 return;
1898}
1899
Andy Adamson074fe892009-04-03 08:28:15 +03001900/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04001901 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001902 */
Trond Myklebustb6076642014-06-30 11:48:35 -04001903static void
Andy Adamson074fe892009-04-03 08:28:15 +03001904nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1905{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001906 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04001907 struct nfsd4_slot *slot = resp->cstate.slot;
1908 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001909
Andy Adamson557ce262009-08-28 08:45:04 -04001910 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001911
Andy Adamson557ce262009-08-28 08:45:04 -04001912 slot->sl_opcnt = resp->opcnt;
1913 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001914
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001915 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001916 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001917 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001918 return;
1919 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001920 base = resp->cstate.data_offset;
1921 slot->sl_datalen = buf->len - base;
1922 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04001923 WARN("%s: sessions DRC could not cache compound\n", __func__);
1924 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001925}
1926
1927/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001928 * Encode the replay sequence operation from the slot values.
1929 * If cachethis is FALSE encode the uncached rep error on the next
1930 * operation which sets resp->p and increments resp->opcnt for
1931 * nfs4svc_encode_compoundres.
1932 *
Andy Adamson074fe892009-04-03 08:28:15 +03001933 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001934static __be32
1935nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1936 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001937{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001938 struct nfsd4_op *op;
1939 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001940
Andy Adamsonabfabf82009-07-23 19:02:18 -04001941 /* Encode the replayed sequence operation */
1942 op = &args->ops[resp->opcnt - 1];
1943 nfsd4_encode_operation(resp, op);
1944
1945 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05001946 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001947 op = &args->ops[resp->opcnt++];
1948 op->status = nfserr_retry_uncached_rep;
1949 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001950 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001951 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001952}
1953
1954/*
Andy Adamson557ce262009-08-28 08:45:04 -04001955 * The sequence operation is not cached because we can use the slot and
1956 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001957 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04001958static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001959nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1960 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001961{
Andy Adamson557ce262009-08-28 08:45:04 -04001962 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001963 struct xdr_stream *xdr = &resp->xdr;
1964 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03001965 __be32 status;
1966
Andy Adamson557ce262009-08-28 08:45:04 -04001967 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001968
Andy Adamsonabfabf82009-07-23 19:02:18 -04001969 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04001970 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04001971 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001972
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001973 p = xdr_reserve_space(xdr, slot->sl_datalen);
1974 if (!p) {
1975 WARN_ON_ONCE(1);
1976 return nfserr_serverfault;
1977 }
1978 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
1979 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03001980
Andy Adamson557ce262009-08-28 08:45:04 -04001981 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001982 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001983}
1984
Andy Adamson0733d212009-04-03 08:28:01 +03001985/*
1986 * Set the exchange_id flags returned by the server.
1987 */
1988static void
1989nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1990{
1991 /* pNFS is not supported */
1992 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1993
1994 /* Referrals are supported, Migration is not. */
1995 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1996
1997 /* set the wire flags to return to client. */
1998 clid->flags = new->cl_exchange_flags;
1999}
2000
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002001static bool client_has_state(struct nfs4_client *clp)
2002{
2003 /*
2004 * Note clp->cl_openowners check isn't quite right: there's no
2005 * need to count owners without stateid's.
2006 *
2007 * Also note we should probably be using this in 4.0 case too.
2008 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002009 return !list_empty(&clp->cl_openowners)
2010 || !list_empty(&clp->cl_delegations)
2011 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002012}
2013
Al Virob37ad282006-10-19 23:28:59 -07002014__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002015nfsd4_exchange_id(struct svc_rqst *rqstp,
2016 struct nfsd4_compound_state *cstate,
2017 struct nfsd4_exchange_id *exid)
2018{
Andy Adamson0733d212009-04-03 08:28:01 +03002019 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002020 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002021 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002022 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002023 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002024 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002025 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002026
Jeff Layton363168b2009-08-14 12:57:56 -04002027 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002028 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002029 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002030 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002031 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002032
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002033 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002034 return nfserr_inval;
2035
Andy Adamson0733d212009-04-03 08:28:01 +03002036 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002037 case SP4_MACH_CRED:
2038 if (!svc_rqst_integrity_protected(rqstp))
2039 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002040 case SP4_NONE:
2041 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002042 default: /* checked by xdr code */
2043 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002044 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002045 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002046 }
2047
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002048 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03002049 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002050 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002051 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002052 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2053 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2054
J. Bruce Fields136e6582012-05-12 20:37:23 -04002055 if (update) {
2056 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002057 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002058 goto out;
2059 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002060 if (!mach_creds_match(conf, rqstp)) {
2061 status = nfserr_wrong_cred;
2062 goto out;
2063 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002064 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002065 status = nfserr_perm;
2066 goto out;
2067 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002068 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002069 status = nfserr_not_same;
2070 goto out;
2071 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002072 /* case 6 */
2073 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2074 new = conf;
2075 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002076 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002077 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002078 if (client_has_state(conf)) {
2079 status = nfserr_clid_inuse;
2080 goto out;
2081 }
Andy Adamson0733d212009-04-03 08:28:01 +03002082 expire_client(conf);
2083 goto out_new;
2084 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002085 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002086 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002087 new = conf;
2088 goto out_copy;
2089 }
2090 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04002091 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002092 }
2093
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002094 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002095 status = nfserr_noent;
2096 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002097 }
2098
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002099 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002100 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03002101 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002102
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002103 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002104out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05002105 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03002106 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04002107 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03002108 goto out;
2109 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002110 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002111 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002112
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002113 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002114 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03002115out_copy:
2116 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2117 exid->clientid.cl_id = new->cl_clientid.cl_id;
2118
J. Bruce Fields778df3f2012-05-25 21:40:23 -04002119 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03002120 nfsd4_set_ex_flags(new, exid);
2121
2122 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04002123 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002124 status = nfs_ok;
2125
2126out:
2127 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03002128 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002129}
2130
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002131static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002132check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002133{
Andy Adamson88e588d2009-07-23 19:02:15 -04002134 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2135 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002136
2137 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002138 if (slot_inuse) {
2139 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002140 return nfserr_jukebox;
2141 else
2142 return nfserr_seq_misordered;
2143 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002144 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002145 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002146 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002147 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002148 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002149 return nfserr_seq_misordered;
2150}
2151
Andy Adamson49557cc2009-07-23 19:02:16 -04002152/*
2153 * Cache the create session result into the create session single DRC
2154 * slot cache by saving the xdr structure. sl_seqid has been set.
2155 * Do this for solo or embedded create session operations.
2156 */
2157static void
2158nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002159 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002160{
2161 slot->sl_status = nfserr;
2162 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2163}
2164
2165static __be32
2166nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2167 struct nfsd4_clid_slot *slot)
2168{
2169 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2170 return slot->sl_status;
2171}
2172
Mi Jinlong1b74c252011-07-14 14:50:17 +08002173#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2174 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2175 1 + /* MIN tag is length with zero, only length */ \
2176 3 + /* version, opcount, opcode */ \
2177 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2178 /* seqid, slotID, slotID, cache */ \
2179 4 ) * sizeof(__be32))
2180
2181#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2182 2 + /* verifier: AUTH_NULL, length 0 */\
2183 1 + /* status */ \
2184 1 + /* MIN tag is length with zero, only length */ \
2185 3 + /* opcount, opcode, opstatus*/ \
2186 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2187 /* seqid, slotID, slotID, slotID, status */ \
2188 5 ) * sizeof(__be32))
2189
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002190static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002191{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002192 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2193
J. Bruce Fields373cd402013-04-08 15:42:12 -04002194 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2195 return nfserr_toosmall;
2196 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2197 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002198 ca->headerpadsz = 0;
2199 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2200 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2201 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2202 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2203 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2204 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2205 /*
2206 * Note decreasing slot size below client's request may make it
2207 * difficult for client to function correctly, whereas
2208 * decreasing the number of slots will (just?) affect
2209 * performance. When short on memory we therefore prefer to
2210 * decrease number of slots instead of their size. Clients that
2211 * request larger slots than they need will get poor results:
2212 */
2213 ca->maxreqs = nfsd4_get_drc_mem(ca);
2214 if (!ca->maxreqs)
2215 return nfserr_jukebox;
2216
J. Bruce Fields373cd402013-04-08 15:42:12 -04002217 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002218}
2219
Kinglong Mee8a891632013-12-23 18:11:02 +08002220#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2221 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2222#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2223 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2224
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002225static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2226{
2227 ca->headerpadsz = 0;
2228
2229 /*
2230 * These RPC_MAX_HEADER macros are overkill, especially since we
2231 * don't even do gss on the backchannel yet. But this is still
2232 * less than 1k. Tighten up this estimate in the unlikely event
2233 * it turns out to be a problem for some client:
2234 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002235 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002236 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002237 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002238 return nfserr_toosmall;
2239 ca->maxresp_cached = 0;
2240 if (ca->maxops < 2)
2241 return nfserr_toosmall;
2242
2243 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002244}
2245
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002246static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2247{
2248 switch (cbs->flavor) {
2249 case RPC_AUTH_NULL:
2250 case RPC_AUTH_UNIX:
2251 return nfs_ok;
2252 default:
2253 /*
2254 * GSS case: the spec doesn't allow us to return this
2255 * error. But it also doesn't allow us not to support
2256 * GSS.
2257 * I'd rather this fail hard than return some error the
2258 * client might think it can already handle:
2259 */
2260 return nfserr_encr_alg_unsupp;
2261 }
2262}
2263
Andy Adamson069b6ad2009-04-03 08:27:58 +03002264__be32
2265nfsd4_create_session(struct svc_rqst *rqstp,
2266 struct nfsd4_compound_state *cstate,
2267 struct nfsd4_create_session *cr_ses)
2268{
Jeff Layton363168b2009-08-14 12:57:56 -04002269 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002270 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002271 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002272 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002273 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002274 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002275 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002276
Mi Jinlonga62573d2011-03-23 17:57:07 +08002277 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2278 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002279 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2280 if (status)
2281 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002282 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002283 if (status)
2284 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002285 status = check_backchannel_attrs(&cr_ses->back_channel);
2286 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002287 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002288 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002289 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002290 if (!new)
2291 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002292 conn = alloc_conn_from_crses(rqstp, cr_ses);
2293 if (!conn)
2294 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002295
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002296 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002297 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002298 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002299 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002300
2301 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002302 status = nfserr_wrong_cred;
2303 if (!mach_creds_match(conf, rqstp))
2304 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002305 cs_slot = &conf->cl_cs_slot;
2306 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002307 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002308 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002309 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002310 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002311 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002312 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002313 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002314 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002315 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002316 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002317 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002318 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002319 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002320 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002321 status = nfserr_wrong_cred;
2322 if (!mach_creds_match(unconf, rqstp))
2323 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002324 cs_slot = &unconf->cl_cs_slot;
2325 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002326 if (status) {
2327 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002328 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002329 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002330 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002331 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002332 if (old) {
2333 status = mark_client_expired(old);
2334 if (status)
2335 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002336 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002337 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002338 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002339 conf = unconf;
2340 } else {
2341 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002342 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002343 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002344 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002345 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002346 * We do not support RDMA or persistent sessions
2347 */
2348 cr_ses->flags &= ~SESSION4_PERSIST;
2349 cr_ses->flags &= ~SESSION4_RDMA;
2350
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002351 init_session(rqstp, new, conf, cr_ses);
2352 nfsd4_init_conn(rqstp, conn, new);
2353
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002354 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002355 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002356 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002357 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002358
Andy Adamson49557cc2009-07-23 19:02:16 -04002359 /* cache solo and embedded create sessions under the state lock */
2360 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002361 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002362 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002363out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08002364 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002365 free_conn(conn);
2366out_free_session:
2367 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002368out_release_drc_mem:
2369 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002370 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002371}
2372
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002373static __be32 nfsd4_map_bcts_dir(u32 *dir)
2374{
2375 switch (*dir) {
2376 case NFS4_CDFC4_FORE:
2377 case NFS4_CDFC4_BACK:
2378 return nfs_ok;
2379 case NFS4_CDFC4_FORE_OR_BOTH:
2380 case NFS4_CDFC4_BACK_OR_BOTH:
2381 *dir = NFS4_CDFC4_BOTH;
2382 return nfs_ok;
2383 };
2384 return nfserr_inval;
2385}
2386
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002387__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2388{
2389 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002390 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002391 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002392
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002393 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2394 if (status)
2395 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002396 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002397 session->se_cb_prog = bc->bc_cb_program;
2398 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002399 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002400
2401 nfsd4_probe_callback(session->se_client);
2402
2403 return nfs_ok;
2404}
2405
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002406__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2407 struct nfsd4_compound_state *cstate,
2408 struct nfsd4_bind_conn_to_session *bcts)
2409{
2410 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002411 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002412 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002413 struct net *net = SVC_NET(rqstp);
2414 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002415
2416 if (!nfsd4_last_compound_op(rqstp))
2417 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002418 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002419 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002420 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002421 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002422 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002423 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002424 status = nfserr_wrong_cred;
2425 if (!mach_creds_match(session->se_client, rqstp))
2426 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002427 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002428 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002429 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002430 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002431 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002432 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002433 goto out;
2434 nfsd4_init_conn(rqstp, conn, session);
2435 status = nfs_ok;
2436out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002437 nfsd4_put_session(session);
2438out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002439 nfs4_unlock_state();
2440 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002441}
2442
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002443static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2444{
2445 if (!session)
2446 return 0;
2447 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2448}
2449
Andy Adamson069b6ad2009-04-03 08:27:58 +03002450__be32
2451nfsd4_destroy_session(struct svc_rqst *r,
2452 struct nfsd4_compound_state *cstate,
2453 struct nfsd4_destroy_session *sessionid)
2454{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002455 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002456 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002457 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002458 struct net *net = SVC_NET(r);
2459 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002460
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002461 nfs4_lock_state();
2462 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002463 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002464 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002465 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002466 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002467 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002468 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002469 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002470 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002471 if (!ses)
2472 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002473 status = nfserr_wrong_cred;
2474 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002475 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002476 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002477 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002478 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002479 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002480 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002481
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002482 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002483
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002484 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002485 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002486out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002487 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002488out_client_lock:
2489 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002490out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002491 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002492 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002493}
2494
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002495static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002496{
2497 struct nfsd4_conn *c;
2498
2499 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002500 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002501 return c;
2502 }
2503 }
2504 return NULL;
2505}
2506
J. Bruce Fields57266a62013-04-13 14:27:29 -04002507static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002508{
2509 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002510 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002511 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002512 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002513
2514 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002515 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002516 if (c)
2517 goto out_free;
2518 status = nfserr_conn_not_bound_to_session;
2519 if (clp->cl_mach_cred)
2520 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002521 __nfsd4_hash_conn(new, ses);
2522 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002523 ret = nfsd4_register_conn(new);
2524 if (ret)
2525 /* oops; xprt is already down: */
2526 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002527 return nfs_ok;
2528out_free:
2529 spin_unlock(&clp->cl_lock);
2530 free_conn(new);
2531 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002532}
2533
Mi Jinlong868b89c2011-04-27 09:09:58 +08002534static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2535{
2536 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2537
2538 return args->opcnt > session->se_fchannel.maxops;
2539}
2540
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002541static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2542 struct nfsd4_session *session)
2543{
2544 struct xdr_buf *xb = &rqstp->rq_arg;
2545
2546 return xb->len > session->se_fchannel.maxreq_sz;
2547}
2548
Andy Adamson069b6ad2009-04-03 08:27:58 +03002549__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002550nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002551 struct nfsd4_compound_state *cstate,
2552 struct nfsd4_sequence *seq)
2553{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002554 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002555 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002556 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002557 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002558 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002559 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002560 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002561 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002562 struct net *net = SVC_NET(rqstp);
2563 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002564
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002565 if (resp->opcnt != 1)
2566 return nfserr_sequence_pos;
2567
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002568 /*
2569 * Will be either used or freed by nfsd4_sequence_check_conn
2570 * below.
2571 */
2572 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2573 if (!conn)
2574 return nfserr_jukebox;
2575
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002576 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002577 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002578 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002579 goto out_no_session;
2580 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002581
Mi Jinlong868b89c2011-04-27 09:09:58 +08002582 status = nfserr_too_many_ops;
2583 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002584 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002585
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002586 status = nfserr_req_too_big;
2587 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002588 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002589
Benny Halevyb85d4c02009-04-03 08:28:08 +03002590 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002591 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002592 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002593
Andy Adamson557ce262009-08-28 08:45:04 -04002594 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002595 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2596
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002597 /* We do not negotiate the number of slots yet, so set the
2598 * maxslots to the session maxreqs which is used to encode
2599 * sr_highest_slotid and the sr_target_slot id to maxslots */
2600 seq->maxslots = session->se_fchannel.maxreqs;
2601
J. Bruce Fields73e79482012-02-13 16:39:00 -05002602 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2603 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002604 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002605 status = nfserr_seq_misordered;
2606 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002607 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002608 cstate->slot = slot;
2609 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002610 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002611 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002612 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002613 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002614 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002615 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002616 }
2617 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002618 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002619
J. Bruce Fields57266a62013-04-13 14:27:29 -04002620 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002621 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002622 if (status)
2623 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002624
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002625 buflen = (seq->cachethis) ?
2626 session->se_fchannel.maxresp_cached :
2627 session->se_fchannel.maxresp_sz;
2628 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2629 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002630 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002631 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002632 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002633
2634 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002635 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002636 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002637 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002638 if (seq->cachethis)
2639 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002640 else
2641 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002642
2643 cstate->slot = slot;
2644 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002645 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002646
Benny Halevyb85d4c02009-04-03 08:28:08 +03002647out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002648 switch (clp->cl_cb_state) {
2649 case NFSD4_CB_DOWN:
2650 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2651 break;
2652 case NFSD4_CB_FAULT:
2653 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2654 break;
2655 default:
2656 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002657 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002658 if (!list_empty(&clp->cl_revoked))
2659 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002660out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002661 if (conn)
2662 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002663 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002664 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002665out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002666 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002667 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002668}
2669
Trond Myklebustb6076642014-06-30 11:48:35 -04002670void
2671nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2672{
2673 struct nfsd4_compound_state *cs = &resp->cstate;
2674
2675 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002676 if (cs->status != nfserr_replay_cache) {
2677 nfsd4_store_cache_entry(resp);
2678 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2679 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002680 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002681 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002682 } else if (cs->clp)
2683 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002684}
2685
Mi Jinlong345c2842011-10-20 17:51:39 +08002686__be32
2687nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2688{
2689 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002690 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002691 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002692
2693 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002694 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002695 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002696 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002697
2698 if (conf) {
2699 clp = conf;
2700
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002701 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002702 status = nfserr_clientid_busy;
2703 goto out;
2704 }
2705 } else if (unconf)
2706 clp = unconf;
2707 else {
2708 status = nfserr_stale_clientid;
2709 goto out;
2710 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002711 if (!mach_creds_match(clp, rqstp)) {
2712 status = nfserr_wrong_cred;
2713 goto out;
2714 }
Mi Jinlong345c2842011-10-20 17:51:39 +08002715 expire_client(clp);
2716out:
2717 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002718 return status;
2719}
2720
Andy Adamson069b6ad2009-04-03 08:27:58 +03002721__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002722nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2723{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002724 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002725
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002726 if (rc->rca_one_fs) {
2727 if (!cstate->current_fh.fh_dentry)
2728 return nfserr_nofilehandle;
2729 /*
2730 * We don't take advantage of the rca_one_fs case.
2731 * That's OK, it's optional, we can safely ignore it.
2732 */
2733 return nfs_ok;
2734 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002735
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002736 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002737 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002738 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2739 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002740 goto out;
2741
2742 status = nfserr_stale_clientid;
2743 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002744 /*
2745 * The following error isn't really legal.
2746 * But we only get here if the client just explicitly
2747 * destroyed the client. Surely it no longer cares what
2748 * error it gets back on an operation for the dead
2749 * client.
2750 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002751 goto out;
2752
2753 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002754 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002755out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002756 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002757 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002758}
2759
2760__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002761nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2762 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002764 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002766 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002767 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002768 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2769
J. Bruce Fields63db4632012-05-18 21:54:19 -04002770 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002772 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002773 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002774 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002776 if (clp_used_exchangeid(conf))
2777 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002778 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002779 char addr_str[INET6_ADDRSTRLEN];
2780 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2781 sizeof(addr_str));
2782 dprintk("NFSD: setclientid: string in use by client "
2783 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 goto out;
2785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002787 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002788 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002791 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002792 if (new == NULL)
2793 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002794 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002795 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002797 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002798 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002799 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002800 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002801 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2803 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2804 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2805 status = nfs_ok;
2806out:
2807 nfs4_unlock_state();
2808 return status;
2809}
2810
2811
Al Virob37ad282006-10-19 23:28:59 -07002812__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002813nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2814 struct nfsd4_compound_state *cstate,
2815 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816{
NeilBrown21ab45a2005-06-23 22:04:14 -07002817 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2819 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002820 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002821 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002823 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002826
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002827 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002828 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002829 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002830 * We try hard to give out unique clientid's, so if we get an
2831 * attempt to confirm the same clientid with a different cred,
2832 * there's a bug somewhere. Let's charitably assume it's our
2833 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002834 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002835 status = nfserr_serverfault;
2836 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2837 goto out;
2838 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2839 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002840 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002841 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2842 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002844 else /* case 4: client hasn't noticed we rebooted yet? */
2845 status = nfserr_stale_clientid;
2846 goto out;
2847 }
2848 status = nfs_ok;
2849 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002850 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2851 nfsd4_probe_callback(conf);
2852 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002853 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002854 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002855 if (conf) {
2856 status = mark_client_expired(conf);
2857 if (status)
2858 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002859 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002860 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002861 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002862 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 nfs4_unlock_state();
2866 return status;
2867}
2868
J. Bruce Fields32513b42011-10-13 16:00:16 -04002869static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002871 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2872}
2873
2874/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04002875static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04002876{
Trond Myklebustca943212014-07-23 16:17:39 -04002877 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
Trond Myklebust950e0112014-06-30 11:48:31 -04002879 lockdep_assert_held(&state_lock);
2880
J. Bruce Fields32513b42011-10-13 16:00:16 -04002881 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04002882 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002883 INIT_LIST_HEAD(&fp->fi_stateids);
2884 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04002885 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002886 fp->fi_had_conflict = false;
2887 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04002888 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002889 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2890 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04002891 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892}
2893
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002894void
NeilBrowne60d4392005-06-23 22:03:01 -07002895nfsd4_free_slabs(void)
2896{
Christoph Hellwigabf11352014-05-21 07:43:03 -07002897 kmem_cache_destroy(openowner_slab);
2898 kmem_cache_destroy(lockowner_slab);
2899 kmem_cache_destroy(file_slab);
2900 kmem_cache_destroy(stateid_slab);
2901 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002902}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
Bryan Schumaker72083392011-11-01 15:24:59 -04002904int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905nfsd4_init_slabs(void)
2906{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002907 openowner_slab = kmem_cache_create("nfsd4_openowners",
2908 sizeof(struct nfs4_openowner), 0, 0, NULL);
2909 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002910 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002911 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002912 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002913 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002914 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07002915 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002916 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002917 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002918 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07002919 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002920 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002921 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002922 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002923 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002924 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002925 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002926 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07002928
2929out_free_stateid_slab:
2930 kmem_cache_destroy(stateid_slab);
2931out_free_file_slab:
2932 kmem_cache_destroy(file_slab);
2933out_free_lockowner_slab:
2934 kmem_cache_destroy(lockowner_slab);
2935out_free_openowner_slab:
2936 kmem_cache_destroy(openowner_slab);
2937out:
NeilBrowne60d4392005-06-23 22:03:01 -07002938 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2939 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940}
2941
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002942static void init_nfs4_replay(struct nfs4_replay *rp)
2943{
2944 rp->rp_status = nfserr_serverfault;
2945 rp->rp_buflen = 0;
2946 rp->rp_buf = rp->rp_ibuf;
2947}
2948
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002949static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950{
2951 struct nfs4_stateowner *sop;
2952
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002953 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002954 if (!sop)
2955 return NULL;
2956
2957 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2958 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002959 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002960 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002962 sop->so_owner.len = owner->len;
2963
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002964 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002965 sop->so_client = clp;
2966 init_nfs4_replay(&sop->so_replay);
2967 return sop;
2968}
2969
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002970static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002971{
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002972 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2973
2974 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002975 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976}
2977
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002978static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04002979alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002980 struct nfsd4_compound_state *cstate)
2981{
Trond Myklebust13d6f662014-06-30 11:48:45 -04002982 struct nfs4_client *clp = cstate->clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002983 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002985 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2986 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002988 oo->oo_owner.so_is_open_owner = 1;
2989 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04002990 oo->oo_flags = NFS4_OO_NEW;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002991 if (nfsd4_has_session(cstate))
2992 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002993 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002994 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002995 INIT_LIST_HEAD(&oo->oo_close_lru);
2996 hash_openowner(oo, clp, strhashval);
2997 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998}
2999
J. Bruce Fields996e0932011-10-17 11:14:48 -04003000static 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 -04003001 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003003 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003004 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003005 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003006 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07003007 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003008 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 stp->st_access_bmap = 0;
3010 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003011 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003012 spin_lock(&oo->oo_owner.so_client->cl_lock);
3013 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003014 spin_lock(&fp->fi_lock);
3015 list_add(&stp->st_perfile, &fp->fi_stateids);
3016 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003017 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018}
3019
3020static void
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003021move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003023 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3024
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003025 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003027 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003028 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029}
3030
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04003032same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
3033 clientid_t *clid)
3034{
3035 return (sop->so_owner.len == owner->len) &&
3036 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
3037 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038}
3039
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003040static struct nfs4_openowner *
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003041find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
3042 bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043{
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003044 struct nfs4_stateowner *so;
3045 struct nfs4_openowner *oo;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003046 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003048 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003049 if (!so->so_is_open_owner)
3050 continue;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003051 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
3052 oo = openowner(so);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003053 clp = oo->oo_owner.so_client;
3054 if ((bool)clp->cl_minorversion != sessions)
3055 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003056 renew_client(oo->oo_owner.so_client);
3057 return oo;
3058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 }
3060 return NULL;
3061}
3062
3063/* search file_hashtbl[] for file */
3064static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003065find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066{
Trond Myklebustca943212014-07-23 16:17:39 -04003067 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 struct nfs4_file *fp;
3069
Trond Myklebust950e0112014-06-30 11:48:31 -04003070 lockdep_assert_held(&state_lock);
3071
Jeff Layton89876f82013-04-02 09:01:59 -04003072 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003073 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003074 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 }
3078 return NULL;
3079}
3080
Trond Myklebust950e0112014-06-30 11:48:31 -04003081static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003082find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003083{
3084 struct nfs4_file *fp;
3085
3086 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003087 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003088 spin_unlock(&state_lock);
3089 return fp;
3090}
3091
3092static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003093find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003094{
3095 struct nfs4_file *fp;
3096
3097 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003098 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003099 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003100 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003101 fp = new;
3102 }
3103 spin_unlock(&state_lock);
3104
3105 return fp;
3106}
3107
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003108/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 * Called to check deny when READ with all zero stateid or
3110 * WRITE with all zero or all one stateid
3111 */
Al Virob37ad282006-10-19 23:28:59 -07003112static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3114{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003116 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Trond Myklebustca943212014-07-23 16:17:39 -04003118 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003119 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003120 return ret;
3121 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003122 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003123 if (fp->fi_share_deny & deny_type)
3124 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003125 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003126 put_nfs4_file(fp);
3127 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128}
3129
Jeff Layton02e12152014-07-16 10:31:57 -04003130void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131{
Trond Myklebust11b91642014-07-29 21:34:08 -04003132 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3133 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003134
Trond Myklebust11b91642014-07-29 21:34:08 -04003135 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003136
Jeff Layton02e12152014-07-16 10:31:57 -04003137 /*
3138 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003139 * already holding inode->i_lock.
3140 *
Jeff Laytondff13992014-07-08 14:02:49 -04003141 * If the dl_time != 0, then we know that it has already been
3142 * queued for a lease break. Don't queue it again.
3143 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003144 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003145 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003146 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003147 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003148 }
Jeff Layton02e12152014-07-16 10:31:57 -04003149 spin_unlock(&state_lock);
3150}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
Jeff Layton02e12152014-07-16 10:31:57 -04003152static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3153{
3154 /*
3155 * We're assuming the state code never drops its reference
3156 * without first removing the lease. Since we're in this lease
3157 * callback (and since the lease code is serialized by the kernel
3158 * lock) we know the server hasn't removed the lease yet, we know
3159 * it's safe to take a reference.
3160 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003161 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003162 nfsd4_cb_recall(dp);
3163}
3164
Jeff Layton1c8c6012013-06-21 08:58:15 -04003165/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003166static void nfsd_break_deleg_cb(struct file_lock *fl)
3167{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003168 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3169 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003170
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003171 if (!fp) {
3172 WARN(1, "(%p)->fl_owner NULL\n", fl);
3173 return;
3174 }
3175 if (fp->fi_had_conflict) {
3176 WARN(1, "duplicate break on %p\n", fp);
3177 return;
3178 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003179 /*
3180 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003181 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003182 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003183 */
3184 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Jeff Layton02e12152014-07-16 10:31:57 -04003186 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003187 fp->fi_had_conflict = true;
3188 /*
3189 * If there are no delegations on the list, then we can't count on this
3190 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3191 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3192 * true should keep any new delegations from being hashed.
3193 */
3194 if (list_empty(&fp->fi_delegations))
3195 fl->fl_break_time = jiffies;
3196 else
3197 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3198 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003199 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200}
3201
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202static
3203int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3204{
3205 if (arg & F_UNLCK)
3206 return lease_modify(onlist, arg);
3207 else
3208 return -EAGAIN;
3209}
3210
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003211static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003212 .lm_break = nfsd_break_deleg_cb,
3213 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214};
3215
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003216static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3217{
3218 if (nfsd4_has_session(cstate))
3219 return nfs_ok;
3220 if (seqid == so->so_seqid - 1)
3221 return nfserr_replay_me;
3222 if (seqid == so->so_seqid)
3223 return nfs_ok;
3224 return nfserr_bad_seqid;
3225}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
Jeff Layton4b24ca72014-06-30 11:48:44 -04003227static __be32 lookup_clientid(clientid_t *clid,
3228 struct nfsd4_compound_state *cstate,
3229 struct nfsd_net *nn)
3230{
3231 struct nfs4_client *found;
3232
3233 if (cstate->clp) {
3234 found = cstate->clp;
3235 if (!same_clid(&found->cl_clientid, clid))
3236 return nfserr_stale_clientid;
3237 return nfs_ok;
3238 }
3239
3240 if (STALE_CLIENTID(clid, nn))
3241 return nfserr_stale_clientid;
3242
3243 /*
3244 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3245 * cached already then we know this is for is for v4.0 and "sessions"
3246 * will be false.
3247 */
3248 WARN_ON_ONCE(cstate->session);
3249 found = find_confirmed_client(clid, false, nn);
3250 if (!found)
3251 return nfserr_expired;
3252
3253 /* Cache the nfs4_client in cstate! */
3254 cstate->clp = found;
3255 atomic_inc(&found->cl_refcount);
3256 return nfs_ok;
3257}
3258
Al Virob37ad282006-10-19 23:28:59 -07003259__be32
Andy Adamson66689582009-04-03 08:28:45 +03003260nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003261 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 clientid_t *clientid = &open->op_clientid;
3264 struct nfs4_client *clp = NULL;
3265 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003266 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003267 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003269 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003271 /*
3272 * In case we need it later, after we've already created the
3273 * file and don't want to risk a further failure:
3274 */
3275 open->op_file = nfsd4_alloc_file();
3276 if (open->op_file == NULL)
3277 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
Trond Myklebust2d91e892014-06-30 11:48:46 -04003279 status = lookup_clientid(clientid, cstate, nn);
3280 if (status)
3281 return status;
3282 clp = cstate->clp;
3283
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003284 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003285 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003286 open->op_openowner = oo;
3287 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003288 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003290 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003291 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003292 release_openowner(oo);
3293 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003294 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003295 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003296 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3297 if (status)
3298 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003299 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003300new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003301 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003302 if (oo == NULL)
3303 return nfserr_jukebox;
3304 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003305alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003306 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003307 if (!open->op_stp)
3308 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003309 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310}
3311
Al Virob37ad282006-10-19 23:28:59 -07003312static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003313nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3314{
3315 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3316 return nfserr_openmode;
3317 else
3318 return nfs_ok;
3319}
3320
Daniel Mackc47d8322011-05-16 16:38:14 +02003321static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003322{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003323 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3324}
3325
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003326static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003327{
3328 struct nfs4_stid *ret;
3329
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003330 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003331 if (!ret)
3332 return NULL;
3333 return delegstateid(ret);
3334}
3335
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003336static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3337{
3338 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3339 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3340}
3341
Al Virob37ad282006-10-19 23:28:59 -07003342static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003343nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003344 struct nfs4_delegation **dp)
3345{
3346 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003347 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003348 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003349
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003350 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3351 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003352 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003353 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003354 status = nfs4_check_delegmode(deleg, flags);
3355 if (status) {
3356 nfs4_put_stid(&deleg->dl_stid);
3357 goto out;
3358 }
3359 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003360out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003361 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003362 return nfs_ok;
3363 if (status)
3364 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003365 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003366 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003367}
3368
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003369static struct nfs4_ol_stateid *
3370nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003372 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003373 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
Trond Myklebust1d31a252014-07-10 14:07:25 -04003375 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003376 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 /* ignore lock owners */
3378 if (local->st_stateowner->so_is_open_owner == 0)
3379 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003380 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003381 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003382 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003383 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003386 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003387 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388}
3389
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003390static inline int nfs4_access_to_access(u32 nfs4_access)
3391{
3392 int flags = 0;
3393
3394 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3395 flags |= NFSD_MAY_READ;
3396 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3397 flags |= NFSD_MAY_WRITE;
3398 return flags;
3399}
3400
Al Virob37ad282006-10-19 23:28:59 -07003401static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3403 struct nfsd4_open *open)
3404{
3405 struct iattr iattr = {
3406 .ia_valid = ATTR_SIZE,
3407 .ia_size = 0,
3408 };
3409 if (!open->op_truncate)
3410 return 0;
3411 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003412 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3414}
3415
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003416static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003417 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3418 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003419{
Trond Myklebustde186432014-07-10 14:07:26 -04003420 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003421 __be32 status;
3422 int oflag = nfs4_access_to_omode(open->op_share_access);
3423 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003424 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003425
Trond Myklebustde186432014-07-10 14:07:26 -04003426 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003427
3428 /*
3429 * Are we trying to set a deny mode that would conflict with
3430 * current access?
3431 */
3432 status = nfs4_file_check_deny(fp, open->op_share_deny);
3433 if (status != nfs_ok) {
3434 spin_unlock(&fp->fi_lock);
3435 goto out;
3436 }
3437
3438 /* set access to the file */
3439 status = nfs4_file_get_access(fp, open->op_share_access);
3440 if (status != nfs_ok) {
3441 spin_unlock(&fp->fi_lock);
3442 goto out;
3443 }
3444
3445 /* Set access bits in stateid */
3446 old_access_bmap = stp->st_access_bmap;
3447 set_access(open->op_share_access, stp);
3448
3449 /* Set new deny mask */
3450 old_deny_bmap = stp->st_deny_bmap;
3451 set_deny(open->op_share_deny, stp);
3452 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3453
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003454 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003455 spin_unlock(&fp->fi_lock);
3456 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003457 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003458 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003459 spin_lock(&fp->fi_lock);
3460 if (!fp->fi_fds[oflag]) {
3461 fp->fi_fds[oflag] = filp;
3462 filp = NULL;
3463 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003464 }
Trond Myklebustde186432014-07-10 14:07:26 -04003465 spin_unlock(&fp->fi_lock);
3466 if (filp)
3467 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003468
3469 status = nfsd4_truncate(rqstp, cur_fh, open);
3470 if (status)
3471 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003472out:
3473 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003474out_put_access:
3475 stp->st_access_bmap = old_access_bmap;
3476 nfs4_file_put_access(fp, open->op_share_access);
3477 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3478 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003479}
3480
Al Virob37ad282006-10-19 23:28:59 -07003481static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003482nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483{
Al Virob37ad282006-10-19 23:28:59 -07003484 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003485 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003487 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003488 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003489
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003490 /* test and set deny mode */
3491 spin_lock(&fp->fi_lock);
3492 status = nfs4_file_check_deny(fp, open->op_share_deny);
3493 if (status == nfs_ok) {
3494 old_deny_bmap = stp->st_deny_bmap;
3495 set_deny(open->op_share_deny, stp);
3496 fp->fi_share_deny |=
3497 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3498 }
3499 spin_unlock(&fp->fi_lock);
3500
3501 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003504 status = nfsd4_truncate(rqstp, cur_fh, open);
3505 if (status != nfs_ok)
3506 reset_union_bmap_deny(old_deny_bmap, stp);
3507 return status;
3508}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003511nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003513 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514}
3515
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003516/* Should we give out recallable state?: */
3517static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3518{
3519 if (clp->cl_cb_state == NFSD4_CB_UP)
3520 return true;
3521 /*
3522 * In the sessions case, since we don't have to establish a
3523 * separate connection for callbacks, we assume it's OK
3524 * until we hear otherwise:
3525 */
3526 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3527}
3528
Jeff Laytond564fbe2014-07-16 10:31:58 -04003529static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003530{
3531 struct file_lock *fl;
3532
3533 fl = locks_alloc_lock();
3534 if (!fl)
3535 return NULL;
3536 locks_init_lock(fl);
3537 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003538 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003539 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3540 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003541 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003542 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003543 return fl;
3544}
3545
J. Bruce Fields99c41512013-05-21 16:21:25 -04003546static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003547{
Trond Myklebust11b91642014-07-29 21:34:08 -04003548 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003549 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003550 struct file *filp;
3551 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003552
Jeff Laytond564fbe2014-07-16 10:31:58 -04003553 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003554 if (!fl)
3555 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003556 filp = find_readable_file(fp);
3557 if (!filp) {
3558 /* We should always have a readable file here */
3559 WARN_ON_ONCE(1);
3560 return -EBADF;
3561 }
3562 fl->fl_file = filp;
3563 status = vfs_setlease(filp, fl->fl_type, &fl);
3564 if (status) {
3565 locks_free_lock(fl);
3566 goto out_fput;
3567 }
Benny Halevycdc97502014-05-30 09:09:30 -04003568 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003569 spin_lock(&fp->fi_lock);
3570 /* Did the lease get broken before we took the lock? */
3571 status = -EAGAIN;
3572 if (fp->fi_had_conflict)
3573 goto out_unlock;
3574 /* Race breaker */
3575 if (fp->fi_lease) {
3576 status = 0;
3577 atomic_inc(&fp->fi_delegees);
3578 hash_delegation_locked(dp, fp);
3579 goto out_unlock;
3580 }
3581 fp->fi_lease = fl;
3582 fp->fi_deleg_file = filp;
3583 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003584 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003585 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003586 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003587 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003588out_unlock:
3589 spin_unlock(&fp->fi_lock);
3590 spin_unlock(&state_lock);
3591out_fput:
3592 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003593 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003594}
3595
Jeff Layton0b266932014-07-25 07:34:25 -04003596static struct nfs4_delegation *
3597nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3598 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003599{
Jeff Layton0b266932014-07-25 07:34:25 -04003600 int status;
3601 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003602
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003603 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003604 return ERR_PTR(-EAGAIN);
3605
3606 dp = alloc_init_deleg(clp, fh);
3607 if (!dp)
3608 return ERR_PTR(-ENOMEM);
3609
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003610 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003611 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003612 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003613 dp->dl_stid.sc_file = fp;
Jeff Layton417c6622014-07-21 09:34:57 -04003614 if (!fp->fi_lease) {
3615 spin_unlock(&fp->fi_lock);
3616 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003617 status = nfs4_setlease(dp);
3618 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003619 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003620 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003621 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003622 status = -EAGAIN;
3623 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003624 }
Benny Halevy931ee562014-05-30 09:09:27 -04003625 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003626 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003627out_unlock:
3628 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003629 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003630out:
3631 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003632 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003633 return ERR_PTR(status);
3634 }
3635 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003636}
3637
Benny Halevy4aa89132012-02-21 14:16:44 -08003638static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3639{
3640 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3641 if (status == -EAGAIN)
3642 open->op_why_no_deleg = WND4_CONTENTION;
3643 else {
3644 open->op_why_no_deleg = WND4_RESOURCE;
3645 switch (open->op_deleg_want) {
3646 case NFS4_SHARE_WANT_READ_DELEG:
3647 case NFS4_SHARE_WANT_WRITE_DELEG:
3648 case NFS4_SHARE_WANT_ANY_DELEG:
3649 break;
3650 case NFS4_SHARE_WANT_CANCEL:
3651 open->op_why_no_deleg = WND4_CANCELLED;
3652 break;
3653 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003654 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003655 }
3656 }
3657}
3658
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659/*
3660 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003661 *
3662 * Note we don't support write delegations, and won't until the vfs has
3663 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 */
3665static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003666nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3667 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668{
3669 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003670 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3671 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003672 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003673 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003675 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003676 open->op_recall = 0;
3677 switch (open->op_claim_type) {
3678 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003679 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003680 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003681 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3682 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003683 break;
3684 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003685 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003686 /*
3687 * Let's not give out any delegations till everyone's
3688 * had the chance to reclaim theirs....
3689 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003690 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003691 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003692 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003693 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003694 /*
3695 * Also, if the file was opened for write or
3696 * create, there's a good chance the client's
3697 * about to write to it, resulting in an
3698 * immediate recall (since we don't support
3699 * write delegations):
3700 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003701 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003702 goto out_no_deleg;
3703 if (open->op_create == NFS4_OPEN_CREATE)
3704 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003705 break;
3706 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003707 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003708 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003709 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003710 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003711 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003713 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003715 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003716 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003717 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003718 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003719 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003720out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003721 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3722 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003723 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003724 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003725 open->op_recall = 1;
3726 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003727
3728 /* 4.1 client asking for a delegation? */
3729 if (open->op_deleg_want)
3730 nfsd4_open_deleg_none_ext(open, status);
3731 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732}
3733
Benny Halevye27f49c2012-02-21 14:16:54 -08003734static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3735 struct nfs4_delegation *dp)
3736{
3737 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3738 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3739 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3740 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3741 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3742 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3743 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3744 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3745 }
3746 /* Otherwise the client must be confused wanting a delegation
3747 * it already has, therefore we don't return
3748 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3749 */
3750}
3751
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752/*
3753 * called with nfs4_lock_state() held.
3754 */
Al Virob37ad282006-10-19 23:28:59 -07003755__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3757{
Andy Adamson66689582009-04-03 08:28:45 +03003758 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003759 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003761 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003762 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003763 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 /*
3766 * Lookup file; if found, lookup stateid and check open request,
3767 * and check for delegations in the process of being recalled.
3768 * If not found, create the nfs4_file struct
3769 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003770 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04003771 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04003772 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003773 if (status)
3774 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003775 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04003777 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003778 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003779 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003780 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003781 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 }
3783
3784 /*
3785 * OPEN the file, or upgrade an existing OPEN.
3786 * If truncate fails, the OPEN fails.
3787 */
3788 if (stp) {
3789 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003790 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 if (status)
3792 goto out;
3793 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003794 stp = open->op_stp;
3795 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003796 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003797 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3798 if (status) {
3799 release_open_stateid(stp);
3800 goto out;
3801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003803 update_stateid(&stp->st_stid.sc_stateid);
3804 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
Benny Halevyd24433c2012-02-16 20:57:17 +02003806 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02003807 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3808 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3809 open->op_why_no_deleg = WND4_NOT_WANTED;
3810 goto nodeleg;
3811 }
3812 }
3813
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 /*
3815 * Attempt to hand out a delegation. No error return, because the
3816 * OPEN succeeds even if we fail.
3817 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003818 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003819nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 status = nfs_ok;
3821
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003822 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003823 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003825 /* 4.1 client trying to upgrade/downgrade delegation? */
3826 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003827 open->op_deleg_want)
3828 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003829
NeilBrown13cd2182005-06-23 22:03:10 -07003830 if (fp)
3831 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003832 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003833 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 /*
3835 * To finish the open response, we just need to set the rflags.
3836 */
3837 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003838 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003839 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003841 if (dp)
3842 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003843 if (stp)
3844 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845
3846 return status;
3847}
3848
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003849void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3850{
3851 if (open->op_openowner) {
3852 struct nfs4_openowner *oo = open->op_openowner;
3853
3854 if (!list_empty(&oo->oo_owner.so_stateids))
3855 list_del_init(&oo->oo_close_lru);
3856 if (oo->oo_flags & NFS4_OO_NEW) {
3857 if (status) {
3858 release_openowner(oo);
3859 open->op_openowner = NULL;
3860 } else
3861 oo->oo_flags &= ~NFS4_OO_NEW;
3862 }
3863 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003864 if (open->op_file)
3865 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003866 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04003867 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003868}
3869
Al Virob37ad282006-10-19 23:28:59 -07003870__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003871nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3872 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873{
3874 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003875 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003876 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877
3878 nfs4_lock_state();
3879 dprintk("process_renew(%08x/%08x): starting\n",
3880 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003881 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003882 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003884 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003886 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003887 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 goto out;
3889 status = nfs_ok;
3890out:
3891 nfs4_unlock_state();
3892 return status;
3893}
3894
NeilBrowna76b4312005-06-23 22:04:01 -07003895static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003896nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07003897{
Jeff Layton33dcc482012-04-10 11:08:48 -04003898 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003899 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04003900 return;
3901
NeilBrowna76b4312005-06-23 22:04:01 -07003902 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003903 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003904 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003905 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003906 /*
3907 * Now that every NFSv4 client has had the chance to recover and
3908 * to see the (possibly new, possibly shorter) lease time, we
3909 * can safely set the next grace time to the current lease time:
3910 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03003911 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003912}
3913
NeilBrownfd39ca92005-06-23 22:04:03 -07003914static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003915nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916{
3917 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003918 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 struct nfs4_delegation *dp;
3920 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003921 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003922 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923
3924 nfs4_lock_state();
3925
3926 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003927 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03003928 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003929 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03003930 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 clp = list_entry(pos, struct nfs4_client, cl_lru);
3932 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3933 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003934 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 break;
3936 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003937 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03003938 dprintk("NFSD: client in use (clientid %08x)\n",
3939 clp->cl_clientid.cl_id);
3940 continue;
3941 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003942 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003943 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003944 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03003945 list_for_each_safe(pos, next, &reaplist) {
3946 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 dprintk("NFSD: purging unused client (clientid %08x)\n",
3948 clp->cl_clientid.cl_id);
3949 expire_client(clp);
3950 }
Benny Halevycdc97502014-05-30 09:09:30 -04003951 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003952 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03003954 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3955 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003957 t = dp->dl_time - cutoff;
3958 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 break;
3960 }
Jeff Layton42690672014-07-25 07:34:20 -04003961 unhash_delegation_locked(dp);
3962 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 }
Benny Halevycdc97502014-05-30 09:09:30 -04003964 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04003965 while (!list_empty(&reaplist)) {
3966 dp = list_first_entry(&reaplist, struct nfs4_delegation,
3967 dl_recall_lru);
3968 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003969 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003971 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003972 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3973 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003974 t = oo->oo_time - cutoff;
3975 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 break;
3977 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003978 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04003980 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04003982 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983}
3984
Harvey Harrisona254b242008-02-20 12:49:00 -08003985static struct workqueue_struct *laundry_wq;
3986static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08003987
3988static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003989laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990{
3991 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003992 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3993 work);
3994 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3995 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003997 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003999 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000}
4001
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004002static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004003{
Trond Myklebust11b91642014-07-29 21:34:08 -04004004 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004005 return nfserr_bad_stateid;
4006 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007}
4008
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004010access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004012 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4013 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4014 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015}
4016
4017static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004018access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004020 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4021 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022}
4023
4024static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004025__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026{
Al Virob37ad282006-10-19 23:28:59 -07004027 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028
J. Bruce Fields02921912010-07-29 15:16:59 -04004029 /* For lock stateid's, we test the parent open, not the lock: */
4030 if (stp->st_openstp)
4031 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004032 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004034 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 goto out;
4036 status = nfs_ok;
4037out:
4038 return status;
4039}
4040
Al Virob37ad282006-10-19 23:28:59 -07004041static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004042check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004044 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004046 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004047 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 * conflicting state; so we must wait out the grace period. */
4049 return nfserr_grace;
4050 } else if (flags & WR_STATE)
4051 return nfs4_share_conflict(current_fh,
4052 NFS4_SHARE_DENY_WRITE);
4053 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4054 return nfs4_share_conflict(current_fh,
4055 NFS4_SHARE_DENY_READ);
4056}
4057
4058/*
4059 * Allow READ/WRITE during grace period on recovered state only for files
4060 * that are not able to provide mandatory locking.
4061 */
4062static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004063grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004065 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066}
4067
J. Bruce Fields81b82962011-08-23 11:03:29 -04004068/* Returns true iff a is later than b: */
4069static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4070{
Jim Rees1a9357f2013-05-17 17:33:00 -04004071 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004072}
4073
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004074static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004075{
Andy Adamson66689582009-04-03 08:28:45 +03004076 /*
4077 * When sessions are used the stateid generation number is ignored
4078 * when it is zero.
4079 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004080 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004081 return nfs_ok;
4082
4083 if (in->si_generation == ref->si_generation)
4084 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004085
J. Bruce Fields0836f582008-01-26 19:08:12 -05004086 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004087 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004088 return nfserr_bad_stateid;
4089 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004090 * However, we could see a stateid from the past, even from a
4091 * non-buggy client. For example, if the client sends a lock
4092 * while some IO is outstanding, the lock may bump si_generation
4093 * while the IO is still in flight. The client could avoid that
4094 * situation by waiting for responses on all the IO requests,
4095 * but better performance may result in retrying IO that
4096 * receives an old_stateid error if requests are rarely
4097 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004098 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004099 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004100}
4101
Chuck Lever7df302f2012-05-29 13:56:37 -04004102static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004103{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004104 struct nfs4_stid *s;
4105 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004106 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004107
Chuck Lever7df302f2012-05-29 13:56:37 -04004108 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004109 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004110 /* Client debugging aid. */
4111 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4112 char addr_str[INET6_ADDRSTRLEN];
4113 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4114 sizeof(addr_str));
4115 pr_warn_ratelimited("NFSD: client %s testing state ID "
4116 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004117 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004118 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004119 spin_lock(&cl->cl_lock);
4120 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004121 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004122 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004123 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004124 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004125 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004126 switch (s->sc_type) {
4127 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004128 status = nfs_ok;
4129 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004130 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004131 status = nfserr_deleg_revoked;
4132 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004133 case NFS4_OPEN_STID:
4134 case NFS4_LOCK_STID:
4135 ols = openlockstateid(s);
4136 if (ols->st_stateowner->so_is_open_owner
4137 && !(openowner(ols->st_stateowner)->oo_flags
4138 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004139 status = nfserr_bad_stateid;
4140 else
4141 status = nfs_ok;
4142 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004143 default:
4144 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004145 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004146 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004147 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004148 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004149 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004150out_unlock:
4151 spin_unlock(&cl->cl_lock);
4152 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004153}
4154
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004155static __be32
4156nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4157 stateid_t *stateid, unsigned char typemask,
4158 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004159{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004160 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004161
4162 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4163 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004164 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004165 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004166 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004167 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004168 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004169 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004170 if (status)
4171 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004172 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004173 if (!*s)
4174 return nfserr_bad_stateid;
4175 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004176}
4177
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178/*
4179* Checks for stateid operations
4180*/
Al Virob37ad282006-10-19 23:28:59 -07004181__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004182nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004183 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004185 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004186 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004188 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004190 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004191 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004192 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 if (filpp)
4195 *filpp = NULL;
4196
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004197 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 return nfserr_grace;
4199
4200 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004201 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
Trond Myklebust14bcab12014-04-18 14:44:07 -04004203 nfs4_lock_state();
4204
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004205 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004206 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004207 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004208 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004209 goto unlock_state;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004210 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4211 if (status)
4212 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004213 switch (s->sc_type) {
4214 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004215 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004216 status = nfs4_check_delegmode(dp, flags);
4217 if (status)
4218 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004219 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004220 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004221 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004222 WARN_ON_ONCE(1);
4223 status = nfserr_serverfault;
4224 goto out;
4225 }
Trond Myklebustde186432014-07-10 14:07:26 -04004226 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004227 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004228 break;
4229 case NFS4_OPEN_STID:
4230 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004231 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004232 status = nfs4_check_fh(current_fh, stp);
4233 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004235 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004236 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004238 status = nfs4_check_openmode(stp, flags);
4239 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004241 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004242 struct nfs4_file *fp = stp->st_stid.sc_file;
4243
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004244 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004245 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004246 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004247 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004248 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004249 break;
4250 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004251 status = nfserr_bad_stateid;
4252 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 }
4254 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004255 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004256 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004258 nfs4_put_stid(s);
4259unlock_state:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004260 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 return status;
4262}
4263
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004264static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004265nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004266{
J. Bruce Fieldsa1b8ff42014-05-20 15:55:21 -04004267 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4268
Trond Myklebust11b91642014-07-29 21:34:08 -04004269 if (check_for_locks(stp->st_stid.sc_file, lo))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004270 return nfserr_locks_held;
Jeff Laytonc53530d2014-06-30 11:48:39 -04004271 release_lockowner_if_empty(lo);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004272 return nfs_ok;
4273}
4274
4275/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004276 * Test if the stateid is valid
4277 */
4278__be32
4279nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4280 struct nfsd4_test_stateid *test_stateid)
4281{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004282 struct nfsd4_test_stateid_id *stateid;
4283 struct nfs4_client *cl = cstate->session->se_client;
4284
4285 nfs4_lock_state();
4286 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004287 stateid->ts_id_status =
4288 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004289 nfs4_unlock_state();
4290
Bryan Schumaker17456802011-07-13 10:50:48 -04004291 return nfs_ok;
4292}
4293
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004294__be32
4295nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4296 struct nfsd4_free_stateid *free_stateid)
4297{
4298 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004299 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004300 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004301 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004302 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004303
4304 nfs4_lock_state();
Jeff Layton1af71cc2014-07-29 21:34:14 -04004305 spin_lock(&cl->cl_lock);
4306 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004307 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004308 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004309 switch (s->sc_type) {
4310 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004311 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004312 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004313 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004314 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4315 if (ret)
4316 break;
4317 ret = nfserr_locks_held;
4318 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004319 case NFS4_LOCK_STID:
4320 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4321 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004322 break;
4323 spin_unlock(&cl->cl_lock);
4324 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4325 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004326 case NFS4_REVOKED_DELEG_STID:
4327 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004328 list_del_init(&dp->dl_recall_lru);
4329 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004330 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004331 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004332 goto out;
4333 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004334 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004335out_unlock:
4336 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004337out:
4338 nfs4_unlock_state();
4339 return ret;
4340}
4341
NeilBrown4c4cd222005-07-07 17:59:27 -07004342static inline int
4343setlkflg (int type)
4344{
4345 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4346 RD_STATE : WR_STATE;
4347}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004349static __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 -04004350{
4351 struct svc_fh *current_fh = &cstate->current_fh;
4352 struct nfs4_stateowner *sop = stp->st_stateowner;
4353 __be32 status;
4354
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004355 status = nfsd4_check_seqid(cstate, sop, seqid);
4356 if (status)
4357 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004358 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4359 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004360 /*
4361 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004362 * nfserr_replay_me from the previous step, and
4363 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004364 */
4365 return nfserr_bad_stateid;
4366 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4367 if (status)
4368 return status;
4369 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004370}
4371
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372/*
4373 * Checks for sequence id mutating operations.
4374 */
Al Virob37ad282006-10-19 23:28:59 -07004375static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004376nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004377 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004378 struct nfs4_ol_stateid **stpp,
4379 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004381 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004382 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004383 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004385 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4386 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004387
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004389 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004390 if (status)
4391 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004392 stp = openlockstateid(s);
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04004393 if (!nfsd4_has_session(cstate))
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004394 cstate->replay_owner = stp->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004396 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004397 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004398 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004399 else
4400 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004401 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004402}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004403
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004404static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4405 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004406{
4407 __be32 status;
4408 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004409 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004411 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004412 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004413 if (status)
4414 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004415 oo = openowner(stp->st_stateowner);
4416 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4417 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004418 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004419 }
4420 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004421 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422}
4423
Al Virob37ad282006-10-19 23:28:59 -07004424__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004425nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004426 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427{
Al Virob37ad282006-10-19 23:28:59 -07004428 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004429 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004430 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004431 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
Al Viroa6a9f182013-09-16 10:57:01 -04004433 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4434 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004436 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004437 if (status)
4438 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439
4440 nfs4_lock_state();
4441
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004442 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004443 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004444 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004445 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004446 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004447 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004448 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004449 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004450 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004451 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004452 update_stateid(&stp->st_stid.sc_stateid);
4453 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004454 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004455 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004456
Jeff Layton2a4317c2012-03-21 16:42:43 -04004457 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004458 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004459put_stateid:
4460 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004462 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004463 if (!cstate->replay_owner)
4464 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 return status;
4466}
4467
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004468static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004470 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004471 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004472 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004473 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004474}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004475
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004476static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4477{
4478 switch (to_access) {
4479 case NFS4_SHARE_ACCESS_READ:
4480 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4481 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4482 break;
4483 case NFS4_SHARE_ACCESS_WRITE:
4484 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4485 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4486 break;
4487 case NFS4_SHARE_ACCESS_BOTH:
4488 break;
4489 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004490 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 }
4492}
4493
Al Virob37ad282006-10-19 23:28:59 -07004494__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004495nfsd4_open_downgrade(struct svc_rqst *rqstp,
4496 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004497 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498{
Al Virob37ad282006-10-19 23:28:59 -07004499 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004500 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004501 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
Al Viroa6a9f182013-09-16 10:57:01 -04004503 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4504 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004506 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004507 if (od->od_deleg_want)
4508 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4509 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510
4511 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004512 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004513 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004514 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004517 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004518 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004520 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004522 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004523 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004525 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004527 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528
Jeff Laytonce0fc432012-05-11 09:45:14 -04004529 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004531 update_stateid(&stp->st_stid.sc_stateid);
4532 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004534put_stateid:
4535 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004537 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004538 if (!cstate->replay_owner)
4539 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 return status;
4541}
4542
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004543static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4544{
Trond Myklebustacf92952014-06-30 11:48:37 -04004545 struct nfs4_client *clp = s->st_stid.sc_client;
4546 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4547
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004548 s->st_stid.sc_type = NFS4_CLOSED_STID;
Trond Myklebustacf92952014-06-30 11:48:37 -04004549 unhash_open_stateid(s);
4550
4551 if (clp->cl_minorversion) {
Trond Myklebustacf92952014-06-30 11:48:37 -04004552 if (list_empty(&oo->oo_owner.so_stateids))
4553 release_openowner(oo);
Trond Myklebust60116952014-07-29 21:34:06 -04004554 nfs4_put_stid(&s->st_stid);
Trond Myklebustacf92952014-06-30 11:48:37 -04004555 } else {
Trond Myklebust60116952014-07-29 21:34:06 -04004556 /*
4557 * In the 4.0 case we need to keep the owners around a
4558 * little while to handle CLOSE replay. We still do need
4559 * to release any file access that is held by them
4560 * before returning however.
4561 */
4562 release_all_access(s);
Trond Myklebust11b91642014-07-29 21:34:08 -04004563 if (s->st_stid.sc_file) {
4564 put_nfs4_file(s->st_stid.sc_file);
4565 s->st_stid.sc_file = NULL;
Trond Myklebustf8338832014-07-25 07:34:19 -04004566 }
Trond Myklebustacf92952014-06-30 11:48:37 -04004567 oo->oo_last_closed_stid = s;
Trond Myklebustacf92952014-06-30 11:48:37 -04004568 if (list_empty(&oo->oo_owner.so_stateids))
4569 move_to_close_lru(oo, clp->net);
4570 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004571}
4572
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573/*
4574 * nfs4_unlock_state() called after encode
4575 */
Al Virob37ad282006-10-19 23:28:59 -07004576__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004577nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004578 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579{
Al Virob37ad282006-10-19 23:28:59 -07004580 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004581 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004582 struct net *net = SVC_NET(rqstp);
4583 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584
Al Viroa6a9f182013-09-16 10:57:01 -04004585 dprintk("NFSD: nfsd4_close on file %pd\n",
4586 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
4588 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004589 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4590 &close->cl_stateid,
4591 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004592 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004593 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004594 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004596 update_stateid(&stp->st_stid.sc_stateid);
4597 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004599 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004600
4601 /* put reference from nfs4_preprocess_seqid_op */
4602 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004604 if (!cstate->replay_owner)
4605 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 return status;
4607}
4608
Al Virob37ad282006-10-19 23:28:59 -07004609__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004610nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4611 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004613 struct nfs4_delegation *dp;
4614 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004615 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004616 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004617 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004619 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004620 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621
4622 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004623 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004624 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004625 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004626 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004627 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004628 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004629 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004630
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004631 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004632put_stateid:
4633 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004635 nfs4_unlock_state();
4636
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 return status;
4638}
4639
4640
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
Benny Halevy87df4de2008-12-15 19:42:03 +02004643static inline u64
4644end_offset(u64 start, u64 len)
4645{
4646 u64 end;
4647
4648 end = start + len;
4649 return end >= start ? end: NFS4_MAX_UINT64;
4650}
4651
4652/* last octet in a range */
4653static inline u64
4654last_byte_offset(u64 start, u64 len)
4655{
4656 u64 end;
4657
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004658 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004659 end = start + len;
4660 return end > start ? end - 1: NFS4_MAX_UINT64;
4661}
4662
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663/*
4664 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4665 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4666 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4667 * locking, this prevents us from being completely protocol-compliant. The
4668 * real solution to this problem is to start using unsigned file offsets in
4669 * the VFS, but this is a very deep change!
4670 */
4671static inline void
4672nfs4_transform_lock_offset(struct file_lock *lock)
4673{
4674 if (lock->fl_start < 0)
4675 lock->fl_start = OFFSET_MAX;
4676 if (lock->fl_end < 0)
4677 lock->fl_end = OFFSET_MAX;
4678}
4679
NeilBrownd5b90262006-04-10 22:55:22 -07004680/* Hack!: For now, we're defining this just so we can use a pointer to it
4681 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004682static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004683};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684
4685static inline void
4686nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4687{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004688 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689
NeilBrownd5b90262006-04-10 22:55:22 -07004690 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004691 lo = (struct nfs4_lockowner *) fl->fl_owner;
4692 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4693 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004694 if (!deny->ld_owner.data)
4695 /* We just don't care that much */
4696 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004697 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4698 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004699 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004700nevermind:
4701 deny->ld_owner.len = 0;
4702 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004703 deny->ld_clientid.cl_boot = 0;
4704 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 }
4706 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004707 deny->ld_length = NFS4_MAX_UINT64;
4708 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4710 deny->ld_type = NFS4_READ_LT;
4711 if (fl->fl_type != F_RDLCK)
4712 deny->ld_type = NFS4_WRITE_LT;
4713}
4714
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004715static struct nfs4_lockowner *
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004716find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4717 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718{
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004719 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4720 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004722 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4723 if (so->so_is_open_owner)
4724 continue;
4725 if (!same_owner_str(so, owner, clid))
4726 continue;
4727 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 }
4729 return NULL;
4730}
4731
4732/*
4733 * Alloc a lock owner structure.
4734 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004735 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004737 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004739static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004740alloc_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 -04004741 struct nfs4_lockowner *lo;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004742 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004744 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4745 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004747 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4748 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07004749 /* It is the openowner seqid that will be incremented in encode in the
4750 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004751 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004752 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004753 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754}
4755
Jeff Layton356a95e2014-07-29 21:34:13 -04004756static void
4757init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
4758 struct nfs4_file *fp, struct inode *inode,
4759 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004761 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762
Jeff Layton356a95e2014-07-29 21:34:13 -04004763 lockdep_assert_held(&clp->cl_lock);
4764
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004765 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004766 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004767 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07004768 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004769 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04004770 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004771 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004773 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004774 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04004775 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004776 spin_lock(&fp->fi_lock);
4777 list_add(&stp->st_perfile, &fp->fi_stateids);
4778 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779}
4780
Jeff Laytonc53530d2014-06-30 11:48:39 -04004781static struct nfs4_ol_stateid *
4782find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4783{
4784 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04004785 struct nfs4_client *clp = lo->lo_owner.so_client;
4786
4787 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004788
4789 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004790 if (lst->st_stid.sc_file == fp) {
4791 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004792 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004793 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004794 }
4795 return NULL;
4796}
4797
Jeff Layton356a95e2014-07-29 21:34:13 -04004798static struct nfs4_ol_stateid *
4799find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
4800 struct inode *inode, struct nfs4_ol_stateid *ost,
4801 bool *new)
4802{
4803 struct nfs4_stid *ns = NULL;
4804 struct nfs4_ol_stateid *lst;
4805 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4806 struct nfs4_client *clp = oo->oo_owner.so_client;
4807
4808 spin_lock(&clp->cl_lock);
4809 lst = find_lock_stateid(lo, fi);
4810 if (lst == NULL) {
4811 spin_unlock(&clp->cl_lock);
4812 ns = nfs4_alloc_stid(clp, stateid_slab);
4813 if (ns == NULL)
4814 return NULL;
4815
4816 spin_lock(&clp->cl_lock);
4817 lst = find_lock_stateid(lo, fi);
4818 if (likely(!lst)) {
4819 lst = openlockstateid(ns);
4820 init_lock_stateid(lst, lo, fi, inode, ost);
4821 ns = NULL;
4822 *new = true;
4823 }
4824 }
4825 spin_unlock(&clp->cl_lock);
4826 if (ns)
4827 nfs4_put_stid(ns);
4828 return lst;
4829}
Jeff Laytonc53530d2014-06-30 11:48:39 -04004830
NeilBrownfd39ca92005-06-23 22:04:03 -07004831static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832check_lock_length(u64 offset, u64 length)
4833{
Benny Halevy87df4de2008-12-15 19:42:03 +02004834 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 LOFF_OVERFLOW(offset, length)));
4836}
4837
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004838static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004839{
Trond Myklebust11b91642014-07-29 21:34:08 -04004840 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004841
Jeff Layton7214e862014-07-10 14:07:33 -04004842 lockdep_assert_held(&fp->fi_lock);
4843
Jeff Layton82c5ff12012-05-11 09:45:13 -04004844 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004845 return;
Jeff Layton12659652014-07-10 14:07:28 -04004846 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004847 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004848}
4849
Jeff Layton356a95e2014-07-29 21:34:13 -04004850static __be32
4851lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
4852 struct nfs4_ol_stateid *ost,
4853 struct nfsd4_lock *lock,
4854 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004855{
Trond Myklebust11b91642014-07-29 21:34:08 -04004856 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004857 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4858 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004859 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004860 struct nfs4_lockowner *lo;
4861 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004862 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004863
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004864 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004865 if (!lo) {
4866 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4867 &lock->v.new.owner);
4868 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4869 if (lo == NULL)
4870 return nfserr_jukebox;
4871 } else {
4872 /* with an existing lockowner, seqids must be the same */
4873 if (!cstate->minorversion &&
4874 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004875 return nfserr_bad_seqid;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004876 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004877
Jeff Layton356a95e2014-07-29 21:34:13 -04004878 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004879 if (*lst == NULL) {
Jeff Layton356a95e2014-07-29 21:34:13 -04004880 release_lockowner_if_empty(lo);
4881 return nfserr_jukebox;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004882 }
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004883 return nfs_ok;
4884}
4885
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886/*
4887 * LOCK operation
4888 */
Al Virob37ad282006-10-19 23:28:59 -07004889__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004890nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004891 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004893 struct nfs4_openowner *open_sop = NULL;
4894 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004895 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004896 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04004897 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004898 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004899 struct file_lock *file_lock = NULL;
4900 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004901 __be32 status = 0;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004902 bool new_state = false;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004903 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004904 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004905 struct net *net = SVC_NET(rqstp);
4906 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907
4908 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4909 (long long) lock->lk_offset,
4910 (long long) lock->lk_length);
4911
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 if (check_lock_length(lock->lk_offset, lock->lk_length))
4913 return nfserr_inval;
4914
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004915 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004916 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004917 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4918 return status;
4919 }
4920
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 nfs4_lock_state();
4922
4923 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04004924 if (nfsd4_has_session(cstate))
4925 /* See rfc 5661 18.10.3: given clientid is ignored: */
4926 memcpy(&lock->v.new.clientid,
4927 &cstate->session->se_client->cl_clientid,
4928 sizeof(clientid_t));
4929
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004931 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004935 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 lock->lk_new_open_seqid,
4937 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004938 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07004939 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004941 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004942 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004943 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004944 &lock->v.new.clientid))
4945 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004946 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4947 &lock_stp, &new_state);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004948 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03004949 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004950 lock->lk_old_lock_seqid,
4951 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004952 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004953 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004954 if (status)
4955 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004956 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004958 lkflg = setlkflg(lock->lk_type);
4959 status = nfs4_check_openmode(lock_stp, lkflg);
4960 if (status)
4961 goto out;
4962
NeilBrown0dd395d2005-07-07 17:59:15 -07004963 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004964 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004965 goto out;
4966 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004967 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004968 goto out;
4969
Jeff Layton21179d82012-08-21 08:03:32 -04004970 file_lock = locks_alloc_lock();
4971 if (!file_lock) {
4972 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4973 status = nfserr_jukebox;
4974 goto out;
4975 }
4976
Trond Myklebust11b91642014-07-29 21:34:08 -04004977 fp = lock_stp->st_stid.sc_file;
Jeff Layton21179d82012-08-21 08:03:32 -04004978 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 switch (lock->lk_type) {
4980 case NFS4_READ_LT:
4981 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04004982 spin_lock(&fp->fi_lock);
4983 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004984 if (filp)
4985 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04004986 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04004987 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004988 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 case NFS4_WRITE_LT:
4990 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04004991 spin_lock(&fp->fi_lock);
4992 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004993 if (filp)
4994 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04004995 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04004996 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004997 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 default:
4999 status = nfserr_inval;
5000 goto out;
5001 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005002 if (!filp) {
5003 status = nfserr_openmode;
5004 goto out;
5005 }
Jeff Layton21179d82012-08-21 08:03:32 -04005006 file_lock->fl_owner = (fl_owner_t)lock_sop;
5007 file_lock->fl_pid = current->tgid;
5008 file_lock->fl_file = filp;
5009 file_lock->fl_flags = FL_POSIX;
5010 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5011 file_lock->fl_start = lock->lk_offset;
5012 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5013 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014
Jeff Layton21179d82012-08-21 08:03:32 -04005015 conflock = locks_alloc_lock();
5016 if (!conflock) {
5017 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5018 status = nfserr_jukebox;
5019 goto out;
5020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021
Jeff Layton21179d82012-08-21 08:03:32 -04005022 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005023 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005025 update_stateid(&lock_stp->st_stid.sc_stateid);
5026 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005028 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005029 break;
5030 case (EAGAIN): /* conflock holds conflicting lock */
5031 status = nfserr_denied;
5032 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005033 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005034 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 case (EDEADLK):
5036 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005037 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005038 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005039 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005040 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005041 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043out:
Trond Myklebustde186432014-07-10 14:07:26 -04005044 if (filp)
5045 fput(filp);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005046 if (lock_stp)
5047 nfs4_put_stid(&lock_stp->st_stid);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005048 if (open_stp)
5049 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005050 if (status && new_state)
Jeff Laytonc53530d2014-06-30 11:48:39 -04005051 release_lock_stateid(lock_stp);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005052 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04005053 if (!cstate->replay_owner)
5054 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005055 if (file_lock)
5056 locks_free_lock(file_lock);
5057 if (conflock)
5058 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 return status;
5060}
5061
5062/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005063 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5064 * so we do a temporary open here just to get an open file to pass to
5065 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5066 * inode operation.)
5067 */
Al Viro04da6e92012-04-13 00:00:04 -04005068static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005069{
5070 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005071 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5072 if (!err) {
5073 err = nfserrno(vfs_test_lock(file, lock));
5074 nfsd_close(file);
5075 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005076 return err;
5077}
5078
5079/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 * LOCKT operation
5081 */
Al Virob37ad282006-10-19 23:28:59 -07005082__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005083nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5084 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085{
Jeff Layton21179d82012-08-21 08:03:32 -04005086 struct file_lock *file_lock = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005087 struct nfs4_lockowner *lo;
Al Virob37ad282006-10-19 23:28:59 -07005088 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005089 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005091 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 return nfserr_grace;
5093
5094 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5095 return nfserr_inval;
5096
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 nfs4_lock_state();
5098
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005099 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005100 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005101 if (status)
5102 goto out;
5103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005105 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
Jeff Layton21179d82012-08-21 08:03:32 -04005108 file_lock = locks_alloc_lock();
5109 if (!file_lock) {
5110 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5111 status = nfserr_jukebox;
5112 goto out;
5113 }
5114 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 switch (lockt->lt_type) {
5116 case NFS4_READ_LT:
5117 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005118 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 break;
5120 case NFS4_WRITE_LT:
5121 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005122 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 break;
5124 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005125 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 status = nfserr_inval;
5127 goto out;
5128 }
5129
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005130 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005131 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005132 file_lock->fl_owner = (fl_owner_t)lo;
5133 file_lock->fl_pid = current->tgid;
5134 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135
Jeff Layton21179d82012-08-21 08:03:32 -04005136 file_lock->fl_start = lockt->lt_offset;
5137 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138
Jeff Layton21179d82012-08-21 08:03:32 -04005139 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140
Jeff Layton21179d82012-08-21 08:03:32 -04005141 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005142 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005143 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005144
Jeff Layton21179d82012-08-21 08:03:32 -04005145 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005147 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 }
5149out:
5150 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005151 if (file_lock)
5152 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 return status;
5154}
5155
Al Virob37ad282006-10-19 23:28:59 -07005156__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005157nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005158 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005160 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005162 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005163 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005164 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005165 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5166
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5168 (long long) locku->lu_offset,
5169 (long long) locku->lu_length);
5170
5171 if (check_lock_length(locku->lu_offset, locku->lu_length))
5172 return nfserr_inval;
5173
5174 nfs4_lock_state();
5175
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005176 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005177 &locku->lu_stateid, NFS4_LOCK_STID,
5178 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005179 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005181 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005182 if (!filp) {
5183 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005184 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005185 }
Jeff Layton21179d82012-08-21 08:03:32 -04005186 file_lock = locks_alloc_lock();
5187 if (!file_lock) {
5188 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5189 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005190 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005191 }
5192 locks_init_lock(file_lock);
5193 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005194 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005195 file_lock->fl_pid = current->tgid;
5196 file_lock->fl_file = filp;
5197 file_lock->fl_flags = FL_POSIX;
5198 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5199 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200
Jeff Layton21179d82012-08-21 08:03:32 -04005201 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5202 locku->lu_length);
5203 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204
Jeff Layton21179d82012-08-21 08:03:32 -04005205 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005206 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005207 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 goto out_nfserr;
5209 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005210 update_stateid(&stp->st_stid.sc_stateid);
5211 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005212fput:
5213 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005214put_stateid:
5215 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005217 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields71c3bcd2011-09-27 21:42:29 -04005218 if (!cstate->replay_owner)
5219 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005220 if (file_lock)
5221 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 return status;
5223
5224out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005225 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005226 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227}
5228
5229/*
5230 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005231 * true: locks held by lockowner
5232 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005234static bool
5235check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236{
5237 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005238 int status = false;
5239 struct file *filp = find_any_file(fp);
5240 struct inode *inode;
5241
5242 if (!filp) {
5243 /* Any valid lock stateid should have some sort of access */
5244 WARN_ON_ONCE(1);
5245 return status;
5246 }
5247
5248 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249
Jeff Layton1c8c6012013-06-21 08:58:15 -04005250 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005252 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005253 status = true;
5254 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005257 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005258 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 return status;
5260}
5261
Al Virob37ad282006-10-19 23:28:59 -07005262__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005263nfsd4_release_lockowner(struct svc_rqst *rqstp,
5264 struct nfsd4_compound_state *cstate,
5265 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266{
5267 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Laytonfd449072014-06-30 11:48:41 -04005268 struct nfs4_stateowner *sop = NULL, *tmp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005269 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005270 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 struct xdr_netobj *owner = &rlockowner->rl_owner;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005272 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07005273 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005274 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275
5276 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5277 clid->cl_boot, clid->cl_id);
5278
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 nfs4_lock_state();
5280
Jeff Layton4b24ca72014-06-30 11:48:44 -04005281 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005282 if (status)
5283 goto out;
5284
NeilBrown3e9e3db2005-06-23 22:04:20 -07005285 status = nfserr_locks_held;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05005286
Jeff Laytonfd449072014-06-30 11:48:41 -04005287 /* Find the matching lock stateowner */
5288 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
5289 if (tmp->so_is_open_owner)
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005290 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005291 if (same_owner_str(tmp, owner, clid)) {
5292 sop = tmp;
5293 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07005295 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005296
5297 /* No matching owner found, maybe a replay? Just declare victory... */
5298 if (!sop) {
5299 status = nfs_ok;
5300 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005302
5303 lo = lockowner(sop);
5304 /* see if there are still any locks associated with it */
5305 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
Trond Myklebust11b91642014-07-29 21:34:08 -04005306 if (check_for_locks(stp->st_stid.sc_file, lo))
Jeff Laytonfd449072014-06-30 11:48:41 -04005307 goto out;
5308 }
5309
5310 status = nfs_ok;
5311 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312out:
5313 nfs4_unlock_state();
5314 return status;
5315}
5316
5317static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005318alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319{
NeilBrowna55370a2005-06-23 22:03:52 -07005320 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321}
5322
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005323bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005324nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005325{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005326 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005327
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005328 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005329 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005330}
5331
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332/*
5333 * failure => all reset bets are off, nfserr_no_grace...
5334 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005335struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005336nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337{
5338 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005339 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340
NeilBrowna55370a2005-06-23 22:03:52 -07005341 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5342 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005343 if (crp) {
5344 strhashval = clientstr_hashval(name);
5345 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005346 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005347 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005348 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005349 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005350 }
5351 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352}
5353
Jeff Layton2a4317c2012-03-21 16:42:43 -04005354void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005355nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005356{
5357 list_del(&crp->cr_strhash);
5358 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005359 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005360}
5361
5362void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005363nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364{
5365 struct nfs4_client_reclaim *crp = NULL;
5366 int i;
5367
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005369 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5370 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005372 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 }
5374 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005375 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376}
5377
5378/*
5379 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005380struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005381nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382{
5383 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 struct nfs4_client_reclaim *crp = NULL;
5385
Jeff Layton278c9312012-11-12 15:00:52 -05005386 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387
Jeff Layton278c9312012-11-12 15:00:52 -05005388 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005389 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005390 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 return crp;
5392 }
5393 }
5394 return NULL;
5395}
5396
5397/*
5398* Called from OPEN. Look for clientid in reclaim list.
5399*/
Al Virob37ad282006-10-19 23:28:59 -07005400__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005401nfs4_check_open_reclaim(clientid_t *clid,
5402 struct nfsd4_compound_state *cstate,
5403 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005405 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005406
5407 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005408 status = lookup_clientid(clid, cstate, nn);
5409 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005410 return nfserr_reclaim_bad;
5411
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005412 if (nfsd4_client_record_check(cstate->clp))
5413 return nfserr_reclaim_bad;
5414
5415 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416}
5417
Bryan Schumaker65178db2011-11-01 13:35:21 -04005418#ifdef CONFIG_NFSD_FAULT_INJECTION
5419
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005420u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5421{
J. Bruce Fields221a6872013-04-01 22:23:49 -04005422 if (mark_client_expired(clp))
5423 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005424 expire_client(clp);
5425 return 1;
5426}
5427
Bryan Schumaker184c1842012-11-29 11:40:44 -05005428u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5429{
5430 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005431 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005432 printk(KERN_INFO "NFS Client: %s\n", buf);
5433 return 1;
5434}
5435
5436static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5437 const char *type)
5438{
5439 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005440 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005441 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5442}
5443
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005444static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5445 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005446{
5447 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005448 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005449 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005450 u64 count = 0;
5451
5452 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005453 list_for_each_entry_safe(stp, st_next,
5454 &oop->oo_owner.so_stateids, st_perstateowner) {
5455 list_for_each_entry_safe(lst, lst_next,
5456 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005457 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005458 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005459 if (++count == max)
5460 return count;
5461 }
5462 }
5463 }
5464
5465 return count;
5466}
5467
5468u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5469{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005470 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005471}
5472
Bryan Schumaker184c1842012-11-29 11:40:44 -05005473u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5474{
5475 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5476 nfsd_print_count(clp, count, "locked files");
5477 return count;
5478}
5479
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005480static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5481{
5482 struct nfs4_openowner *oop, *next;
5483 u64 count = 0;
5484
5485 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5486 if (func)
5487 func(oop);
5488 if (++count == max)
5489 break;
5490 }
5491
5492 return count;
5493}
5494
5495u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5496{
5497 return nfsd_foreach_client_open(clp, max, release_openowner);
5498}
5499
Bryan Schumaker184c1842012-11-29 11:40:44 -05005500u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5501{
5502 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5503 nfsd_print_count(clp, count, "open files");
5504 return count;
5505}
5506
Bryan Schumaker269de302012-11-29 11:40:42 -05005507static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5508 struct list_head *victims)
5509{
5510 struct nfs4_delegation *dp, *next;
5511 u64 count = 0;
5512
Benny Halevycdc97502014-05-30 09:09:30 -04005513 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005514 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005515 if (victims) {
5516 /*
5517 * It's not safe to mess with delegations that have a
5518 * non-zero dl_time. They might have already been broken
5519 * and could be processed by the laundromat outside of
5520 * the state_lock. Just leave them be.
5521 */
5522 if (dp->dl_time != 0)
5523 continue;
5524
Jeff Layton42690672014-07-25 07:34:20 -04005525 unhash_delegation_locked(dp);
5526 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005527 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005528 if (++count == max)
5529 break;
5530 }
5531 return count;
5532}
5533
5534u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5535{
5536 struct nfs4_delegation *dp, *next;
5537 LIST_HEAD(victims);
5538 u64 count;
5539
Benny Halevycdc97502014-05-30 09:09:30 -04005540 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005541 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005542 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005543
Jeff Layton2d4a5322014-07-25 07:34:21 -04005544 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5545 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005546 revoke_delegation(dp);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005547 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005548
5549 return count;
5550}
5551
5552u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5553{
Jeff Laytondff13992014-07-08 14:02:49 -04005554 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005555 LIST_HEAD(victims);
5556 u64 count;
5557
Benny Halevycdc97502014-05-30 09:09:30 -04005558 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005559 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005560 while (!list_empty(&victims)) {
5561 dp = list_first_entry(&victims, struct nfs4_delegation,
5562 dl_recall_lru);
5563 list_del_init(&dp->dl_recall_lru);
5564 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005565 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005566 }
Benny Halevycdc97502014-05-30 09:09:30 -04005567 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005568
5569 return count;
5570}
5571
Bryan Schumaker184c1842012-11-29 11:40:44 -05005572u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5573{
5574 u64 count = 0;
5575
Benny Halevycdc97502014-05-30 09:09:30 -04005576 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005577 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005578 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005579
5580 nfsd_print_count(clp, count, "delegations");
5581 return count;
5582}
5583
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005584u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005585{
5586 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005587 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005588 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005589
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005590 if (!nfsd_netns_ready(nn))
5591 return 0;
5592
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005593 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005594 count += func(clp, max - count);
5595 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005596 break;
5597 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005598
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005599 return count;
5600}
5601
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005602struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5603{
5604 struct nfs4_client *clp;
5605 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5606
5607 if (!nfsd_netns_ready(nn))
5608 return NULL;
5609
5610 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5611 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5612 return clp;
5613 }
5614 return NULL;
5615}
5616
Bryan Schumaker65178db2011-11-01 13:35:21 -04005617#endif /* CONFIG_NFSD_FAULT_INJECTION */
5618
Meelap Shahc2f1a552007-07-17 04:04:39 -07005619/*
5620 * Since the lifetime of a delegation isn't limited to that of an open, a
5621 * client may quite reasonably hang on to a delegation as long as it has
5622 * the inode cached. This becomes an obvious problem the first time a
5623 * client's inode cache approaches the size of the server's total memory.
5624 *
5625 * For now we avoid this problem by imposing a hard limit on the number
5626 * of delegations, which varies according to the server's memory size.
5627 */
5628static void
5629set_max_delegations(void)
5630{
5631 /*
5632 * Allow at most 4 delegations per megabyte of RAM. Quick
5633 * estimates suggest that in the worst case (where every delegation
5634 * is for a different inode), a delegation could take about 1.5K,
5635 * giving a worst case usage of about 6% of memory.
5636 */
5637 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5638}
5639
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005640static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005641{
5642 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5643 int i;
5644
5645 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5646 CLIENT_HASH_SIZE, GFP_KERNEL);
5647 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005648 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005649 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5650 CLIENT_HASH_SIZE, GFP_KERNEL);
5651 if (!nn->unconf_id_hashtbl)
5652 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005653 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5654 OWNER_HASH_SIZE, GFP_KERNEL);
5655 if (!nn->ownerstr_hashtbl)
5656 goto err_ownerstr;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005657 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5658 SESSION_HASH_SIZE, GFP_KERNEL);
5659 if (!nn->sessionid_hashtbl)
5660 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005661
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005662 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005663 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005664 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005665 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005666 for (i = 0; i < OWNER_HASH_SIZE; i++)
5667 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005668 for (i = 0; i < SESSION_HASH_SIZE; i++)
5669 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005670 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005671 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005672 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005673 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005674 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005675 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005676
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005677 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005678 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005679
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005680 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005681
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005682err_sessionid:
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03005683 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005684err_ownerstr:
5685 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005686err_unconf_id:
5687 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005688err:
5689 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005690}
5691
5692static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005693nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005694{
5695 int i;
5696 struct nfs4_client *clp = NULL;
5697 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5698
5699 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5700 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5701 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5702 destroy_client(clp);
5703 }
5704 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005705
Kinglong Mee2b905632014-03-26 22:09:30 +08005706 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5707 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5708 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5709 destroy_client(clp);
5710 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005711 }
5712
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005713 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005714 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005715 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005716 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005717 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005718}
5719
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005720int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005721nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005722{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005723 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005724 int ret;
5725
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005726 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005727 if (ret)
5728 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005729 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005730 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005731 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005732 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005733 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005734 nn->nfsd4_grace, net);
5735 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005736 return 0;
5737}
5738
5739/* initialization to perform when the nfsd service is started: */
5740
5741int
5742nfs4_state_start(void)
5743{
5744 int ret;
5745
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005746 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005747 if (ret)
5748 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005749 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005750 if (laundry_wq == NULL) {
5751 ret = -ENOMEM;
5752 goto out_recovery;
5753 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005754 ret = nfsd4_create_callback_queue();
5755 if (ret)
5756 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005757
Meelap Shahc2f1a552007-07-17 04:04:39 -07005758 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005759
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005760 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005761
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005762out_free_laundry:
5763 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005764out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005765 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766}
5767
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005768void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005769nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005773 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005775 cancel_delayed_work_sync(&nn->laundromat_work);
5776 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005777
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005778 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04005780 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005781 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005783 unhash_delegation_locked(dp);
5784 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 }
Benny Halevycdc97502014-05-30 09:09:30 -04005786 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 list_for_each_safe(pos, next, &reaplist) {
5788 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005789 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04005790 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 }
5792
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005793 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005794 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005795 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796}
5797
5798void
5799nfs4_state_shutdown(void)
5800{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005801 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005802 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005804
5805static void
5806get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5807{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005808 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5809 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005810}
5811
5812static void
5813put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5814{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005815 if (cstate->minorversion) {
5816 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5817 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5818 }
5819}
5820
5821void
5822clear_current_stateid(struct nfsd4_compound_state *cstate)
5823{
5824 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005825}
5826
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005827/*
5828 * functions to set current state id
5829 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005830void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005831nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5832{
5833 put_stateid(cstate, &odp->od_stateid);
5834}
5835
5836void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005837nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5838{
5839 put_stateid(cstate, &open->op_stateid);
5840}
5841
5842void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005843nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5844{
5845 put_stateid(cstate, &close->cl_stateid);
5846}
5847
5848void
5849nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5850{
5851 put_stateid(cstate, &lock->lk_resp_stateid);
5852}
5853
5854/*
5855 * functions to consume current state id
5856 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005857
5858void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005859nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5860{
5861 get_stateid(cstate, &odp->od_stateid);
5862}
5863
5864void
5865nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5866{
5867 get_stateid(cstate, &drp->dr_stateid);
5868}
5869
5870void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005871nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5872{
5873 get_stateid(cstate, &fsp->fr_stateid);
5874}
5875
5876void
5877nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5878{
5879 get_stateid(cstate, &setattr->sa_stateid);
5880}
5881
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005882void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005883nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5884{
5885 get_stateid(cstate, &close->cl_stateid);
5886}
5887
5888void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005889nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005890{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005891 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005892}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01005893
5894void
5895nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5896{
5897 get_stateid(cstate, &read->rd_stateid);
5898}
5899
5900void
5901nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5902{
5903 get_stateid(cstate, &write->wr_stateid);
5904}