blob: 906c8604de3091a3314dbc60f5aa318afdcb53ab [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
Dave Hansenaceaf782008-02-15 14:37:31 -080035#include <linux/file.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020036#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070038#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070039#include <linux/swap.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
Chuck Lever7df302f2012-05-29 13:56:37 -040041#include <linux/ratelimit.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050042#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton59766872013-02-04 12:50:00 -050043#include <linux/sunrpc/addr.h>
NeilBrown6282cd52014-06-04 17:39:26 +100044#include <linux/hash.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020045#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040046#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050047#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040048#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040050#include "netns.h"
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define NFSDDBG_FACILITY NFSDDBG_PROC
53
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050054#define all_ones {{~0,~0},~0}
55static const stateid_t one_stateid = {
56 .si_generation = ~0,
57 .si_opaque = all_ones,
58};
59static const stateid_t zero_stateid = {
60 /* all fields zero */
61};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010062static const stateid_t currentstateid = {
63 .si_generation = 1,
64};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050065
Andy Adamsonec6b5d72009-04-03 08:28:28 +030066static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070067
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050068#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
69#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010070#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040073static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040074static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
J. Bruce Fields8b671b82009-02-22 14:51:34 -080076/* Locking: */
77
78/* Currently used for almost all code touching nfsv4 state: */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080079static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
J. Bruce Fields8b671b82009-02-22 14:51:34 -080081/*
82 * Currently used for the del_recall_lru and file hash table. In an
83 * effort to decrease the scope of the client_mutex, this spinlock may
84 * eventually cover more:
85 */
Benny Halevycdc97502014-05-30 09:09:30 -040086static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080087
Christoph Hellwigabf11352014-05-21 07:43:03 -070088static struct kmem_cache *openowner_slab;
89static struct kmem_cache *lockowner_slab;
90static struct kmem_cache *file_slab;
91static struct kmem_cache *stateid_slab;
92static struct kmem_cache *deleg_slab;
NeilBrowne60d4392005-06-23 22:03:01 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094void
95nfs4_lock_state(void)
96{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080097 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400100static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800101
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400102static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800103{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400104 return ses->se_flags & NFS4_SESSION_DEAD;
105}
106
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400107static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
108{
109 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400110 return nfserr_jukebox;
111 ses->se_flags |= NFS4_SESSION_DEAD;
112 return nfs_ok;
113}
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115void
116nfs4_unlock_state(void)
117{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800118 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
J. Bruce Fields221a6872013-04-01 22:23:49 -0400121static bool is_client_expired(struct nfs4_client *clp)
122{
123 return clp->cl_time == 0;
124}
125
126static __be32 mark_client_expired_locked(struct nfs4_client *clp)
127{
128 if (atomic_read(&clp->cl_refcount))
129 return nfserr_jukebox;
130 clp->cl_time = 0;
131 return nfs_ok;
132}
133
134static __be32 mark_client_expired(struct nfs4_client *clp)
135{
136 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
137 __be32 ret;
138
139 spin_lock(&nn->client_lock);
140 ret = mark_client_expired_locked(clp);
141 spin_unlock(&nn->client_lock);
142 return ret;
143}
144
145static __be32 get_client_locked(struct nfs4_client *clp)
146{
147 if (is_client_expired(clp))
148 return nfserr_expired;
149 atomic_inc(&clp->cl_refcount);
150 return nfs_ok;
151}
152
153/* must be called under the client_lock */
154static inline void
155renew_client_locked(struct nfs4_client *clp)
156{
157 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
158
159 if (is_client_expired(clp)) {
160 WARN_ON(1);
161 printk("%s: client (clientid %08x/%08x) already expired\n",
162 __func__,
163 clp->cl_clientid.cl_boot,
164 clp->cl_clientid.cl_id);
165 return;
166 }
167
168 dprintk("renewing client (clientid %08x/%08x)\n",
169 clp->cl_clientid.cl_boot,
170 clp->cl_clientid.cl_id);
171 list_move_tail(&clp->cl_lru, &nn->client_lru);
172 clp->cl_time = get_seconds();
173}
174
175static inline void
176renew_client(struct nfs4_client *clp)
177{
178 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
179
180 spin_lock(&nn->client_lock);
181 renew_client_locked(clp);
182 spin_unlock(&nn->client_lock);
183}
184
Fengguang Wuba138432013-04-16 22:14:15 -0400185static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400186{
187 if (!atomic_dec_and_test(&clp->cl_refcount))
188 return;
189 if (!is_client_expired(clp))
190 renew_client_locked(clp);
191}
192
Jeff Layton4b24ca72014-06-30 11:48:44 -0400193static void put_client_renew(struct nfs4_client *clp)
194{
195 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
196
Jeff Laytond6c249b2014-07-08 14:02:50 -0400197 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
198 return;
199 if (!is_client_expired(clp))
200 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400201 spin_unlock(&nn->client_lock);
202}
203
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400204static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
205{
206 __be32 status;
207
208 if (is_session_dead(ses))
209 return nfserr_badsession;
210 status = get_client_locked(ses->se_client);
211 if (status)
212 return status;
213 atomic_inc(&ses->se_ref);
214 return nfs_ok;
215}
216
217static void nfsd4_put_session_locked(struct nfsd4_session *ses)
218{
219 struct nfs4_client *clp = ses->se_client;
220
221 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
222 free_session(ses);
223 put_client_renew_locked(clp);
224}
225
226static void nfsd4_put_session(struct nfsd4_session *ses)
227{
228 struct nfs4_client *clp = ses->se_client;
229 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
230
231 spin_lock(&nn->client_lock);
232 nfsd4_put_session_locked(ses);
233 spin_unlock(&nn->client_lock);
234}
235
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237static inline u32
238opaque_hashval(const void *ptr, int nbytes)
239{
240 unsigned char *cptr = (unsigned char *) ptr;
241
242 u32 x = 0;
243 while (nbytes--) {
244 x *= 37;
245 x += *cptr++;
246 }
247 return x;
248}
249
J. Bruce Fields32513b42011-10-13 16:00:16 -0400250static void nfsd4_free_file(struct nfs4_file *f)
251{
252 kmem_cache_free(file_slab, f);
253}
254
NeilBrown13cd2182005-06-23 22:03:10 -0700255static inline void
256put_nfs4_file(struct nfs4_file *fi)
257{
Jeff Layton02e12152014-07-16 10:31:57 -0400258 might_lock(&state_lock);
259
Benny Halevycdc97502014-05-30 09:09:30 -0400260 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton89876f82013-04-02 09:01:59 -0400261 hlist_del(&fi->fi_hash);
Benny Halevycdc97502014-05-30 09:09:30 -0400262 spin_unlock(&state_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400263 nfsd4_free_file(fi);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800264 }
NeilBrown13cd2182005-06-23 22:03:10 -0700265}
266
267static inline void
268get_nfs4_file(struct nfs4_file *fi)
269{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800270 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700271}
272
Trond Myklebustde186432014-07-10 14:07:26 -0400273static struct file *
274__nfs4_get_fd(struct nfs4_file *f, int oflag)
275{
276 if (f->fi_fds[oflag])
277 return get_file(f->fi_fds[oflag]);
278 return NULL;
279}
280
281static struct file *
282find_writeable_file_locked(struct nfs4_file *f)
283{
284 struct file *ret;
285
286 lockdep_assert_held(&f->fi_lock);
287
288 ret = __nfs4_get_fd(f, O_WRONLY);
289 if (!ret)
290 ret = __nfs4_get_fd(f, O_RDWR);
291 return ret;
292}
293
294static struct file *
295find_writeable_file(struct nfs4_file *f)
296{
297 struct file *ret;
298
299 spin_lock(&f->fi_lock);
300 ret = find_writeable_file_locked(f);
301 spin_unlock(&f->fi_lock);
302
303 return ret;
304}
305
306static struct file *find_readable_file_locked(struct nfs4_file *f)
307{
308 struct file *ret;
309
310 lockdep_assert_held(&f->fi_lock);
311
312 ret = __nfs4_get_fd(f, O_RDONLY);
313 if (!ret)
314 ret = __nfs4_get_fd(f, O_RDWR);
315 return ret;
316}
317
318static struct file *
319find_readable_file(struct nfs4_file *f)
320{
321 struct file *ret;
322
323 spin_lock(&f->fi_lock);
324 ret = find_readable_file_locked(f);
325 spin_unlock(&f->fi_lock);
326
327 return ret;
328}
329
330static struct file *
331find_any_file(struct nfs4_file *f)
332{
333 struct file *ret;
334
335 spin_lock(&f->fi_lock);
336 ret = __nfs4_get_fd(f, O_RDWR);
337 if (!ret) {
338 ret = __nfs4_get_fd(f, O_WRONLY);
339 if (!ret)
340 ret = __nfs4_get_fd(f, O_RDONLY);
341 }
342 spin_unlock(&f->fi_lock);
343 return ret;
344}
345
Trond Myklebust02a35082014-07-25 07:34:22 -0400346static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800347unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700348
349/*
350 * Open owner state (share locks)
351 */
352
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500353/* hash tables for lock and open owners */
354#define OWNER_HASH_BITS 8
355#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
356#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700357
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500358static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400359{
360 unsigned int ret;
361
362 ret = opaque_hashval(ownername->data, ownername->len);
363 ret += clientid;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500364 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400365}
NeilBrownef0f3392006-04-10 22:55:41 -0700366
NeilBrownef0f3392006-04-10 22:55:41 -0700367/* hash table for nfs4_file */
368#define FILE_HASH_BITS 8
369#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800370
Trond Myklebustca943212014-07-23 16:17:39 -0400371static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400372{
Trond Myklebustca943212014-07-23 16:17:39 -0400373 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
374}
375
376static unsigned int file_hashval(struct knfsd_fh *fh)
377{
378 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
379}
380
381static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
382{
383 return fh1->fh_size == fh2->fh_size &&
384 !memcmp(fh1->fh_base.fh_pad,
385 fh2->fh_base.fh_pad,
386 fh1->fh_size);
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400387}
388
Jeff Layton89876f82013-04-02 09:01:59 -0400389static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700390
Jeff Layton12659652014-07-10 14:07:28 -0400391static void
392__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400393{
Jeff Layton7214e862014-07-10 14:07:33 -0400394 lockdep_assert_held(&fp->fi_lock);
395
Jeff Layton12659652014-07-10 14:07:28 -0400396 if (access & NFS4_SHARE_ACCESS_WRITE)
397 atomic_inc(&fp->fi_access[O_WRONLY]);
398 if (access & NFS4_SHARE_ACCESS_READ)
399 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400400}
401
Jeff Layton12659652014-07-10 14:07:28 -0400402static __be32
403nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400404{
Jeff Layton7214e862014-07-10 14:07:33 -0400405 lockdep_assert_held(&fp->fi_lock);
406
Jeff Layton12659652014-07-10 14:07:28 -0400407 /* Does this access mode make sense? */
408 if (access & ~NFS4_SHARE_ACCESS_BOTH)
409 return nfserr_inval;
410
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400411 /* Does it conflict with a deny mode already set? */
412 if ((access & fp->fi_share_deny) != 0)
413 return nfserr_share_denied;
414
Jeff Layton12659652014-07-10 14:07:28 -0400415 __nfs4_file_get_access(fp, access);
416 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400417}
418
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400419static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
420{
421 /* Common case is that there is no deny mode. */
422 if (deny) {
423 /* Does this deny mode make sense? */
424 if (deny & ~NFS4_SHARE_DENY_BOTH)
425 return nfserr_inval;
426
427 if ((deny & NFS4_SHARE_DENY_READ) &&
428 atomic_read(&fp->fi_access[O_RDONLY]))
429 return nfserr_share_denied;
430
431 if ((deny & NFS4_SHARE_DENY_WRITE) &&
432 atomic_read(&fp->fi_access[O_WRONLY]))
433 return nfserr_share_denied;
434 }
435 return nfs_ok;
436}
437
J. Bruce Fields998db522010-08-07 09:21:41 -0400438static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400439{
Trond Myklebustde186432014-07-10 14:07:26 -0400440 might_lock(&fp->fi_lock);
441
442 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
443 struct file *f1 = NULL;
444 struct file *f2 = NULL;
445
Jeff Layton6d338b52014-07-10 14:07:29 -0400446 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400447 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400448 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400449 spin_unlock(&fp->fi_lock);
450 if (f1)
451 fput(f1);
452 if (f2)
453 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400454 }
455}
456
Jeff Layton12659652014-07-10 14:07:28 -0400457static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400458{
Jeff Layton12659652014-07-10 14:07:28 -0400459 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
460
461 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400462 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400463 if (access & NFS4_SHARE_ACCESS_READ)
464 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400465}
466
Trond Myklebust60116952014-07-29 21:34:06 -0400467static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
468 struct kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400469{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500470 struct nfs4_stid *stid;
471 int new_id;
472
Trond Myklebustf8338832014-07-25 07:34:19 -0400473 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500474 if (!stid)
475 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400476
Jeff Layton4770d722014-07-29 21:34:10 -0400477 idr_preload(GFP_KERNEL);
478 spin_lock(&cl->cl_lock);
479 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
480 spin_unlock(&cl->cl_lock);
481 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700482 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500483 goto out_free;
484 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500485 stid->sc_stateid.si_opaque.so_id = new_id;
486 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
487 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400488 atomic_set(&stid->sc_count, 1);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500489
J. Bruce Fields996e0932011-10-17 11:14:48 -0400490 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500491 * It shouldn't be a problem to reuse an opaque stateid value.
492 * I don't think it is for 4.1. But with 4.0 I worry that, for
493 * example, a stray write retransmission could be accepted by
494 * the server when it should have been rejected. Therefore,
495 * adopt a trick from the sctp code to attempt to maximize the
496 * amount of time until an id is reused, by ensuring they always
497 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400498 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500499 return stid;
500out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800501 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500502 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400503}
504
Jeff Laytonb49e0842014-07-29 21:34:11 -0400505static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400506{
Trond Myklebust60116952014-07-29 21:34:06 -0400507 struct nfs4_stid *stid;
508 struct nfs4_ol_stateid *stp;
509
510 stid = nfs4_alloc_stid(clp, stateid_slab);
511 if (!stid)
512 return NULL;
513
514 stp = openlockstateid(stid);
515 stp->st_stid.sc_free = nfs4_free_ol_stateid;
516 return stp;
517}
518
519static void nfs4_free_deleg(struct nfs4_stid *stid)
520{
Trond Myklebust60116952014-07-29 21:34:06 -0400521 kmem_cache_free(deleg_slab, stid);
522 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400523}
524
NeilBrown6282cd52014-06-04 17:39:26 +1000525/*
526 * When we recall a delegation, we should be careful not to hand it
527 * out again straight away.
528 * To ensure this we keep a pair of bloom filters ('new' and 'old')
529 * in which the filehandles of recalled delegations are "stored".
530 * If a filehandle appear in either filter, a delegation is blocked.
531 * When a delegation is recalled, the filehandle is stored in the "new"
532 * filter.
533 * Every 30 seconds we swap the filters and clear the "new" one,
534 * unless both are empty of course.
535 *
536 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
537 * low 3 bytes as hash-table indices.
538 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400539 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000540 * is used to manage concurrent access. Testing does not need the lock
541 * except when swapping the two filters.
542 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400543static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000544static struct bloom_pair {
545 int entries, old_entries;
546 time_t swap_time;
547 int new; /* index into 'set' */
548 DECLARE_BITMAP(set[2], 256);
549} blocked_delegations;
550
551static int delegation_blocked(struct knfsd_fh *fh)
552{
553 u32 hash;
554 struct bloom_pair *bd = &blocked_delegations;
555
556 if (bd->entries == 0)
557 return 0;
558 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400559 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000560 if (seconds_since_boot() - bd->swap_time > 30) {
561 bd->entries -= bd->old_entries;
562 bd->old_entries = bd->entries;
563 memset(bd->set[bd->new], 0,
564 sizeof(bd->set[0]));
565 bd->new = 1-bd->new;
566 bd->swap_time = seconds_since_boot();
567 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400568 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000569 }
570 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
571 if (test_bit(hash&255, bd->set[0]) &&
572 test_bit((hash>>8)&255, bd->set[0]) &&
573 test_bit((hash>>16)&255, bd->set[0]))
574 return 1;
575
576 if (test_bit(hash&255, bd->set[1]) &&
577 test_bit((hash>>8)&255, bd->set[1]) &&
578 test_bit((hash>>16)&255, bd->set[1]))
579 return 1;
580
581 return 0;
582}
583
584static void block_delegations(struct knfsd_fh *fh)
585{
586 u32 hash;
587 struct bloom_pair *bd = &blocked_delegations;
588
589 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
590
Jeff Laytonf54fe962014-07-25 07:34:26 -0400591 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000592 __set_bit(hash&255, bd->set[bd->new]);
593 __set_bit((hash>>8)&255, bd->set[bd->new]);
594 __set_bit((hash>>16)&255, bd->set[bd->new]);
595 if (bd->entries == 0)
596 bd->swap_time = seconds_since_boot();
597 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400598 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601static struct nfs4_delegation *
Jeff Laytonf9416e22014-07-25 07:34:23 -0400602alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400605 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400608 n = atomic_long_inc_return(&num_delegations);
609 if (n < 0 || n > max_delegations)
610 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000611 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400612 goto out_dec;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400613 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700614 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400615 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400616
617 dp->dl_stid.sc_free = nfs4_free_deleg;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400618 /*
619 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400620 * meaning of seqid 0 isn't meaningful, really, but let's avoid
621 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400622 */
623 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700624 INIT_LIST_HEAD(&dp->dl_perfile);
625 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 INIT_LIST_HEAD(&dp->dl_recall_lru);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400627 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Jeff Layton02e12152014-07-16 10:31:57 -0400628 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400630out_dec:
631 atomic_long_dec(&num_delegations);
632 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
635void
Trond Myklebust60116952014-07-29 21:34:06 -0400636nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Trond Myklebust11b91642014-07-29 21:34:08 -0400638 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400639 struct nfs4_client *clp = s->sc_client;
640
Jeff Layton4770d722014-07-29 21:34:10 -0400641 might_lock(&clp->cl_lock);
642
643 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock))
Trond Myklebust60116952014-07-29 21:34:06 -0400644 return;
645 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400646 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400647 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400648 if (fp)
649 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500652static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Jeff Layton417c6622014-07-21 09:34:57 -0400654 lockdep_assert_held(&state_lock);
655
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -0500656 if (!fp->fi_lease)
657 return;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500658 if (atomic_dec_and_test(&fp->fi_delegees)) {
659 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
660 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400661 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500662 fp->fi_deleg_file = NULL;
663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400666static void unhash_stid(struct nfs4_stid *s)
667{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500668 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400669}
670
Benny Halevy931ee562014-05-30 09:09:27 -0400671static void
672hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
673{
Benny Halevycdc97502014-05-30 09:09:30 -0400674 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400675 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400676
Trond Myklebust67cb1272014-07-29 21:34:17 -0400677 atomic_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400678 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400679 list_add(&dp->dl_perfile, &fp->fi_delegations);
680 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
681}
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683static void
Jeff Layton42690672014-07-25 07:34:20 -0400684unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Trond Myklebust11b91642014-07-29 21:34:08 -0400686 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400687
Jeff Layton42690672014-07-25 07:34:20 -0400688 lockdep_assert_held(&state_lock);
689
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400690 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400691 /* Ensure that deleg break won't try to requeue it */
692 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400693 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400694 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400696 list_del_init(&dp->dl_perfile);
697 spin_unlock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400698 if (fp)
Trond Myklebustf8338832014-07-25 07:34:19 -0400699 nfs4_put_deleg_lease(fp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400700}
701
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400702static void destroy_delegation(struct nfs4_delegation *dp)
703{
Jeff Layton42690672014-07-25 07:34:20 -0400704 spin_lock(&state_lock);
705 unhash_delegation_locked(dp);
706 spin_unlock(&state_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400707 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400708}
709
710static void revoke_delegation(struct nfs4_delegation *dp)
711{
712 struct nfs4_client *clp = dp->dl_stid.sc_client;
713
Jeff Layton2d4a5322014-07-25 07:34:21 -0400714 WARN_ON(!list_empty(&dp->dl_recall_lru));
715
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400716 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400717 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400718 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400719 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400720 spin_lock(&clp->cl_lock);
721 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
722 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400723 }
724}
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726/*
727 * SETCLIENTID state
728 */
729
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400730static unsigned int clientid_hashval(u32 id)
731{
732 return id & CLIENT_HASH_MASK;
733}
734
735static unsigned int clientstr_hashval(const char *name)
736{
737 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
738}
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400741 * We store the NONE, READ, WRITE, and BOTH bits separately in the
742 * st_{access,deny}_bmap field of the stateid, in order to track not
743 * only what share bits are currently in force, but also what
744 * combinations of share bits previous opens have used. This allows us
745 * to enforce the recommendation of rfc 3530 14.2.19 that the server
746 * return an error if the client attempt to downgrade to a combination
747 * of share bits not explicable by closing some of its previous opens.
748 *
749 * XXX: This enforcement is actually incomplete, since we don't keep
750 * track of access/deny bit combinations; so, e.g., we allow:
751 *
752 * OPEN allow read, deny write
753 * OPEN allow both, deny none
754 * DOWNGRADE allow read, deny none
755 *
756 * which we should reject.
757 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400758static unsigned int
759bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400760 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400761 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400762
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400763 for (i = 1; i < 4; i++) {
764 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400765 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400766 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400767 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400768}
769
Jeff Layton82c5ff12012-05-11 09:45:13 -0400770/* set share access for a given stateid */
771static inline void
772set_access(u32 access, struct nfs4_ol_stateid *stp)
773{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400774 unsigned char mask = 1 << access;
775
776 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
777 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400778}
779
780/* clear share access for a given stateid */
781static inline void
782clear_access(u32 access, struct nfs4_ol_stateid *stp)
783{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400784 unsigned char mask = 1 << access;
785
786 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
787 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400788}
789
790/* test whether a given stateid has access */
791static inline bool
792test_access(u32 access, struct nfs4_ol_stateid *stp)
793{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400794 unsigned char mask = 1 << access;
795
796 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400797}
798
Jeff Laytonce0fc432012-05-11 09:45:14 -0400799/* set share deny for a given stateid */
800static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400801set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400802{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400803 unsigned char mask = 1 << deny;
804
805 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
806 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400807}
808
809/* clear share deny for a given stateid */
810static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400811clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400812{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400813 unsigned char mask = 1 << deny;
814
815 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
816 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400817}
818
819/* test whether a given stateid is denying specific access */
820static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400821test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400822{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400823 unsigned char mask = 1 << deny;
824
825 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400826}
827
828static int nfs4_access_to_omode(u32 access)
829{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400830 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400831 case NFS4_SHARE_ACCESS_READ:
832 return O_RDONLY;
833 case NFS4_SHARE_ACCESS_WRITE:
834 return O_WRONLY;
835 case NFS4_SHARE_ACCESS_BOTH:
836 return O_RDWR;
837 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500838 WARN_ON_ONCE(1);
839 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400840}
841
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400842/*
843 * A stateid that had a deny mode associated with it is being released
844 * or downgraded. Recalculate the deny mode on the file.
845 */
846static void
847recalculate_deny_mode(struct nfs4_file *fp)
848{
849 struct nfs4_ol_stateid *stp;
850
851 spin_lock(&fp->fi_lock);
852 fp->fi_share_deny = 0;
853 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
854 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
855 spin_unlock(&fp->fi_lock);
856}
857
858static void
859reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
860{
861 int i;
862 bool change = false;
863
864 for (i = 1; i < 4; i++) {
865 if ((i & deny) != i) {
866 change = true;
867 clear_deny(i, stp);
868 }
869 }
870
871 /* Recalculate per-file deny mode if there was a change */
872 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -0400873 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400874}
875
Jeff Layton82c5ff12012-05-11 09:45:13 -0400876/* release all access and file references for a given stateid */
877static void
878release_all_access(struct nfs4_ol_stateid *stp)
879{
880 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -0400881 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400882
883 if (fp && stp->st_deny_bmap != 0)
884 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400885
886 for (i = 1; i < 4; i++) {
887 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -0400888 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400889 clear_access(i, stp);
890 }
891}
892
Jeff Layton6b180f02014-07-29 21:34:26 -0400893static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
894{
895 if (!atomic_dec_and_test(&sop->so_count))
896 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -0400897 sop->so_ops->so_unhash(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -0400898 kfree(sop->so_owner.data);
899 sop->so_ops->so_free(sop);
900}
901
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400902static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500903{
Trond Myklebust11b91642014-07-29 21:34:08 -0400904 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -0400905
Jeff Layton1c755dc2014-07-29 21:34:12 -0400906 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
907
Trond Myklebust1d31a252014-07-10 14:07:25 -0400908 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500909 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400910 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500911 list_del(&stp->st_perstateowner);
912}
913
Trond Myklebust60116952014-07-29 21:34:06 -0400914static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500915{
Trond Myklebust60116952014-07-29 21:34:06 -0400916 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400917
Trond Myklebust60116952014-07-29 21:34:06 -0400918 release_all_access(stp);
Trond Myklebust60116952014-07-29 21:34:06 -0400919 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500920}
921
Jeff Laytonb49e0842014-07-29 21:34:11 -0400922static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500923{
Jeff Laytonb49e0842014-07-29 21:34:11 -0400924 struct nfs4_ol_stateid *stp = openlockstateid(stid);
925 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500926 struct file *file;
927
Jeff Laytonb49e0842014-07-29 21:34:11 -0400928 file = find_any_file(stp->st_stid.sc_file);
929 if (file)
930 filp_close(file, (fl_owner_t)lo);
931 nfs4_free_ol_stateid(stid);
932}
933
934static void __release_lock_stateid(struct nfs4_ol_stateid *stp)
935{
Jeff Layton1c755dc2014-07-29 21:34:12 -0400936 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
937
938 spin_lock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400939 list_del(&stp->st_locks);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500940 unhash_generic_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400941 unhash_stid(&stp->st_stid);
Jeff Layton1c755dc2014-07-29 21:34:12 -0400942 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400943 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500944}
945
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400946static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500947{
Jeff Layton8f4b54c2014-07-29 21:34:29 -0400948 list_del_init(&lo->lo_owner.so_strhash);
949}
950
951static void release_lockowner_stateids(struct nfs4_lockowner *lo)
952{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400953 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500954
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400955 while (!list_empty(&lo->lo_owner.so_stateids)) {
956 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400957 struct nfs4_ol_stateid, st_perstateowner);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400958 __release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500959 }
960}
961
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400962static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500963{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400964 unhash_lockowner(lo);
Jeff Layton8f4b54c2014-07-29 21:34:29 -0400965 release_lockowner_stateids(lo);
Jeff Layton6b180f02014-07-29 21:34:26 -0400966 nfs4_put_stateowner(&lo->lo_owner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500967}
968
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400969static void release_lockowner_if_empty(struct nfs4_lockowner *lo)
970{
971 if (list_empty(&lo->lo_owner.so_stateids))
972 release_lockowner(lo);
973}
974
975static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500976{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400977 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500978
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400979 lo = lockowner(stp->st_stateowner);
980 __release_lock_stateid(stp);
981 release_lockowner_if_empty(lo);
982}
983
984static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
Jeff Layton1c755dc2014-07-29 21:34:12 -0400985 __releases(&open_stp->st_stateowner->so_client->cl_lock)
986 __acquires(&open_stp->st_stateowner->so_client->cl_lock)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400987{
988 struct nfs4_ol_stateid *stp;
989
990 while (!list_empty(&open_stp->st_locks)) {
991 stp = list_entry(open_stp->st_locks.next,
992 struct nfs4_ol_stateid, st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -0400993 spin_unlock(&open_stp->st_stateowner->so_client->cl_lock);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400994 release_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -0400995 spin_lock(&open_stp->st_stateowner->so_client->cl_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500996 }
997}
998
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400999static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -05001000{
Jeff Layton1c755dc2014-07-29 21:34:12 -04001001 spin_lock(&stp->st_stateowner->so_client->cl_lock);
J. Bruce Fields22839632009-01-11 14:27:17 -05001002 unhash_generic_stateid(stp);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001003 release_open_stateid_locks(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001004 spin_unlock(&stp->st_stateowner->so_client->cl_lock);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001005}
1006
1007static void release_open_stateid(struct nfs4_ol_stateid *stp)
1008{
1009 unhash_open_stateid(stp);
Trond Myklebust60116952014-07-29 21:34:06 -04001010 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields22839632009-01-11 14:27:17 -05001011}
1012
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001013static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001014{
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001015 list_del_init(&oo->oo_owner.so_strhash);
1016 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001017}
1018
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001019static void release_last_closed_stateid(struct nfs4_openowner *oo)
1020{
1021 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
1022
1023 if (s) {
Trond Myklebust60116952014-07-29 21:34:06 -04001024 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001025 oo->oo_last_closed_stid = NULL;
1026 }
1027}
1028
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001029static void release_openowner_stateids(struct nfs4_openowner *oo)
1030{
1031 struct nfs4_ol_stateid *stp;
1032
1033 while (!list_empty(&oo->oo_owner.so_stateids)) {
1034 stp = list_first_entry(&oo->oo_owner.so_stateids,
1035 struct nfs4_ol_stateid, st_perstateowner);
1036 release_open_stateid(stp);
1037 }
1038}
1039
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001040static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001041{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001042 unhash_openowner(oo);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001043 release_openowner_stateids(oo);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001044 list_del(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001045 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001046 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001047}
1048
Marc Eshel5282fd72009-04-03 08:27:52 +03001049static inline int
1050hash_sessionid(struct nfs4_sessionid *sessionid)
1051{
1052 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1053
1054 return sid->sequence % SESSION_HASH_SIZE;
1055}
1056
Trond Myklebust8f199b82012-03-20 15:11:17 -04001057#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001058static inline void
1059dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1060{
1061 u32 *ptr = (u32 *)(&sessionid->data[0]);
1062 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1063}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001064#else
1065static inline void
1066dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1067{
1068}
1069#endif
1070
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001071/*
1072 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1073 * won't be used for replay.
1074 */
1075void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1076{
1077 struct nfs4_stateowner *so = cstate->replay_owner;
1078
1079 if (nfserr == nfserr_replay_me)
1080 return;
1081
1082 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001083 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001084 return;
1085 }
1086 if (!so)
1087 return;
1088 if (so->so_is_open_owner)
1089 release_last_closed_stateid(openowner(so));
1090 so->so_seqid++;
1091 return;
1092}
Marc Eshel5282fd72009-04-03 08:27:52 +03001093
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001094static void
1095gen_sessionid(struct nfsd4_session *ses)
1096{
1097 struct nfs4_client *clp = ses->se_client;
1098 struct nfsd4_sessionid *sid;
1099
1100 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1101 sid->clientid = clp->cl_clientid;
1102 sid->sequence = current_sessionid++;
1103 sid->reserved = 0;
1104}
1105
1106/*
Andy Adamsona6496372009-08-28 08:45:01 -04001107 * The protocol defines ca_maxresponssize_cached to include the size of
1108 * the rpc header, but all we need to cache is the data starting after
1109 * the end of the initial SEQUENCE operation--the rest we regenerate
1110 * each time. Therefore we can advertise a ca_maxresponssize_cached
1111 * value that is the number of bytes in our cache plus a few additional
1112 * bytes. In order to stay on the safe side, and not promise more than
1113 * we can cache, those additional bytes must be the minimum possible: 24
1114 * bytes of rpc header (xid through accept state, with AUTH_NULL
1115 * verifier), 12 for the compound header (with zero-length tag), and 44
1116 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001117 */
Andy Adamsona6496372009-08-28 08:45:01 -04001118#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1119
Andy Adamson557ce262009-08-28 08:45:04 -04001120static void
1121free_session_slots(struct nfsd4_session *ses)
1122{
1123 int i;
1124
1125 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1126 kfree(ses->se_slots[i]);
1127}
1128
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001129/*
1130 * We don't actually need to cache the rpc and session headers, so we
1131 * can allocate a little less for each slot:
1132 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001133static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001134{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001135 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001136
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001137 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1138 size = 0;
1139 else
1140 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1141 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001142}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001143
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001144/*
1145 * XXX: If we run out of reserved DRC memory we could (up to a point)
1146 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001147 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001148 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001149static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001150{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001151 u32 slotsize = slot_bytes(ca);
1152 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001153 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001154
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001155 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001156 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1157 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001158 num = min_t(int, num, avail / slotsize);
1159 nfsd_drc_mem_used += num * slotsize;
1160 spin_unlock(&nfsd_drc_lock);
1161
1162 return num;
1163}
1164
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001165static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001166{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001167 int slotsize = slot_bytes(ca);
1168
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001169 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001170 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001171 spin_unlock(&nfsd_drc_lock);
1172}
1173
Kinglong Mee60810e52014-01-01 00:35:47 +08001174static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1175 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001176{
Kinglong Mee60810e52014-01-01 00:35:47 +08001177 int numslots = fattrs->maxreqs;
1178 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001179 struct nfsd4_session *new;
1180 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001181
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001182 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001183 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1184 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001185
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001186 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001187 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001188 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001189 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001190 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001191 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001192 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001193 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001194 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001195
1196 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1197 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1198
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001199 return new;
1200out_free:
1201 while (i--)
1202 kfree(new->se_slots[i]);
1203 kfree(new);
1204 return NULL;
1205}
1206
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001207static void free_conn(struct nfsd4_conn *c)
1208{
1209 svc_xprt_put(c->cn_xprt);
1210 kfree(c);
1211}
1212
1213static void nfsd4_conn_lost(struct svc_xpt_user *u)
1214{
1215 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1216 struct nfs4_client *clp = c->cn_session->se_client;
1217
1218 spin_lock(&clp->cl_lock);
1219 if (!list_empty(&c->cn_persession)) {
1220 list_del(&c->cn_persession);
1221 free_conn(c);
1222 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001223 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001224 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001225}
1226
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001227static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001228{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001229 struct nfsd4_conn *conn;
1230
1231 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1232 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001233 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001234 svc_xprt_get(rqstp->rq_xprt);
1235 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001236 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001237 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1238 return conn;
1239}
1240
J. Bruce Fields328ead22010-09-29 16:11:06 -04001241static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1242{
1243 conn->cn_session = ses;
1244 list_add(&conn->cn_persession, &ses->se_conns);
1245}
1246
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001247static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1248{
1249 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001250
1251 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001252 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001253 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001254}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001255
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001256static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001257{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001258 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001259 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001260}
1261
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001262static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001263{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001264 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001265
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001266 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001267 ret = nfsd4_register_conn(conn);
1268 if (ret)
1269 /* oops; xprt is already down: */
1270 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001271 /* We may have gained or lost a callback channel: */
1272 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001273}
1274
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001275static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001276{
1277 u32 dir = NFS4_CDFC4_FORE;
1278
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001279 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001280 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001281 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001282}
1283
1284/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001285static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001286{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001287 struct nfs4_client *clp = s->se_client;
1288 struct nfsd4_conn *c;
1289
1290 spin_lock(&clp->cl_lock);
1291 while (!list_empty(&s->se_conns)) {
1292 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1293 list_del_init(&c->cn_persession);
1294 spin_unlock(&clp->cl_lock);
1295
1296 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1297 free_conn(c);
1298
1299 spin_lock(&clp->cl_lock);
1300 }
1301 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001302}
1303
J. Bruce Fields1377b692012-09-11 21:42:40 -04001304static void __free_session(struct nfsd4_session *ses)
1305{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001306 free_session_slots(ses);
1307 kfree(ses);
1308}
1309
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001310static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001311{
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001312 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1313
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001314 lockdep_assert_held(&nn->client_lock);
1315 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001316 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001317 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001318}
Andy Adamson557ce262009-08-28 08:45:04 -04001319
Fengguang Wu135ae822012-11-10 07:20:25 -05001320static 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 -04001321{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001322 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001323 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001324
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001325 new->se_client = clp;
1326 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001327
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001328 INIT_LIST_HEAD(&new->se_conns);
1329
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001330 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001331 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001332 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001333 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001334 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001335 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001336 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001337 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001338 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001339 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001340 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001341 spin_unlock(&nn->client_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001342
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001343 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001344 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001345 /*
1346 * This is a little silly; with sessions there's no real
1347 * use for the callback address. Use the peer address
1348 * as a reasonable default for now, but consider fixing
1349 * the rpc client not to require an address in the
1350 * future:
1351 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001352 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1353 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001354 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001355}
1356
Benny Halevy9089f1b2010-05-12 00:12:26 +03001357/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001358static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001359__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001360{
1361 struct nfsd4_session *elem;
1362 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001363 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001364
1365 dump_sessionid(__func__, sessionid);
1366 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001367 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001368 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001369 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1370 NFS4_MAX_SESSIONID_LEN)) {
1371 return elem;
1372 }
1373 }
1374
1375 dprintk("%s: session not found\n", __func__);
1376 return NULL;
1377}
1378
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001379static struct nfsd4_session *
1380find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1381 __be32 *ret)
1382{
1383 struct nfsd4_session *session;
1384 __be32 status = nfserr_badsession;
1385
1386 session = __find_in_sessionid_hashtbl(sessionid, net);
1387 if (!session)
1388 goto out;
1389 status = nfsd4_get_session_locked(session);
1390 if (status)
1391 session = NULL;
1392out:
1393 *ret = status;
1394 return session;
1395}
1396
Benny Halevy9089f1b2010-05-12 00:12:26 +03001397/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001398static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001399unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001400{
1401 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001402 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001403 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001404 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001405}
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1408static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001409STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001411 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001413 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001414 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return 1;
1416}
1417
1418/*
1419 * XXX Should we use a slab cache ?
1420 * This type of memory management is somewhat inefficient, but we use it
1421 * anyway since SETCLIENTID is not a common operation.
1422 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001423static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
1425 struct nfs4_client *clp;
1426
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001427 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1428 if (clp == NULL)
1429 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001430 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001431 if (clp->cl_name.data == NULL) {
1432 kfree(clp);
1433 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001435 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001436 INIT_LIST_HEAD(&clp->cl_sessions);
1437 idr_init(&clp->cl_stateids);
1438 atomic_set(&clp->cl_refcount, 0);
1439 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1440 INIT_LIST_HEAD(&clp->cl_idhash);
1441 INIT_LIST_HEAD(&clp->cl_openowners);
1442 INIT_LIST_HEAD(&clp->cl_delegations);
1443 INIT_LIST_HEAD(&clp->cl_lru);
1444 INIT_LIST_HEAD(&clp->cl_callbacks);
1445 INIT_LIST_HEAD(&clp->cl_revoked);
1446 spin_lock_init(&clp->cl_lock);
1447 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return clp;
1449}
1450
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001451static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452free_client(struct nfs4_client *clp)
1453{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001454 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001455
1456 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001457 while (!list_empty(&clp->cl_sessions)) {
1458 struct nfsd4_session *ses;
1459 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1460 se_perclnt);
1461 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001462 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1463 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001464 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001465 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001466 free_svc_cred(&clp->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001468 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 kfree(clp);
1470}
1471
Benny Halevy84d38ac2010-05-12 00:13:16 +03001472/* must be called under the client_lock */
1473static inline void
1474unhash_client_locked(struct nfs4_client *clp)
1475{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001476 struct nfsd4_session *ses;
1477
Benny Halevy84d38ac2010-05-12 00:13:16 +03001478 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001479 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001480 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1481 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001482 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001483}
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001486destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001488 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001491 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001494 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001495 while (!list_empty(&clp->cl_delegations)) {
1496 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001497 unhash_delegation_locked(dp);
1498 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
Benny Halevycdc97502014-05-30 09:09:30 -04001500 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 while (!list_empty(&reaplist)) {
1502 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001503 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001504 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001506 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001507 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001508 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001509 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001510 }
NeilBrownea1da632005-06-23 22:04:17 -07001511 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001512 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1513 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001515 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001516 if (clp->cl_cb_conn.cb_xprt)
1517 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001518 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001519 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001520 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001521 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001522 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001523 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001524 unhash_client_locked(clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001525 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1526 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001527 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
1529
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001530static void expire_client(struct nfs4_client *clp)
1531{
1532 nfsd4_client_record_remove(clp);
1533 destroy_client(clp);
1534}
1535
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001536static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1537{
1538 memcpy(target->cl_verifier.data, source->data,
1539 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001542static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1543{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1545 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1546}
1547
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001548static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001549{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001550 if (source->cr_principal) {
1551 target->cr_principal =
1552 kstrdup(source->cr_principal, GFP_KERNEL);
1553 if (target->cr_principal == NULL)
1554 return -ENOMEM;
1555 } else
1556 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001557 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 target->cr_uid = source->cr_uid;
1559 target->cr_gid = source->cr_gid;
1560 target->cr_group_info = source->cr_group_info;
1561 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001562 target->cr_gss_mech = source->cr_gss_mech;
1563 if (source->cr_gss_mech)
1564 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001565 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001568static long long
1569compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1570{
1571 long long res;
1572
1573 res = o1->len - o2->len;
1574 if (res)
1575 return res;
1576 return (long long)memcmp(o1->data, o2->data, o1->len);
1577}
1578
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001579static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001580{
NeilBrowna55370a2005-06-23 22:03:52 -07001581 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
1584static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001585same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1586{
1587 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588}
1589
1590static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001591same_clid(clientid_t *cl1, clientid_t *cl2)
1592{
1593 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
1595
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001596static bool groups_equal(struct group_info *g1, struct group_info *g2)
1597{
1598 int i;
1599
1600 if (g1->ngroups != g2->ngroups)
1601 return false;
1602 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001603 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001604 return false;
1605 return true;
1606}
1607
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001608/*
1609 * RFC 3530 language requires clid_inuse be returned when the
1610 * "principal" associated with a requests differs from that previously
1611 * used. We use uid, gid's, and gss principal string as our best
1612 * approximation. We also don't want to allow non-gss use of a client
1613 * established using gss: in theory cr_principal should catch that
1614 * change, but in practice cr_principal can be null even in the gss case
1615 * since gssd doesn't always pass down a principal string.
1616 */
1617static bool is_gss_cred(struct svc_cred *cr)
1618{
1619 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1620 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1621}
1622
1623
Vivek Trivedi5559b502012-07-24 21:18:20 +05301624static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001625same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1626{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001627 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001628 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1629 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001630 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1631 return false;
1632 if (cr1->cr_principal == cr2->cr_principal)
1633 return true;
1634 if (!cr1->cr_principal || !cr2->cr_principal)
1635 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301636 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
1638
J. Bruce Fields57266a62013-04-13 14:27:29 -04001639static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1640{
1641 struct svc_cred *cr = &rqstp->rq_cred;
1642 u32 service;
1643
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001644 if (!cr->cr_gss_mech)
1645 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001646 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1647 return service == RPC_GSS_SVC_INTEGRITY ||
1648 service == RPC_GSS_SVC_PRIVACY;
1649}
1650
1651static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1652{
1653 struct svc_cred *cr = &rqstp->rq_cred;
1654
1655 if (!cl->cl_mach_cred)
1656 return true;
1657 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1658 return false;
1659 if (!svc_rqst_integrity_protected(rqstp))
1660 return false;
1661 if (!cr->cr_principal)
1662 return false;
1663 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1664}
1665
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001666static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001667{
1668 static u32 current_clientid = 1;
1669
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001670 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 clp->cl_clientid.cl_id = current_clientid++;
1672}
1673
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001674static void gen_confirm(struct nfs4_client *clp)
1675{
Chuck Leverab4684d2012-03-02 17:13:50 -05001676 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001677 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Jeff Laytonf4199922014-06-17 07:44:11 -04001679 /*
1680 * This is opaque to client, so no need to byte-swap. Use
1681 * __force to keep sparse happy
1682 */
1683 verf[0] = (__force __be32)get_seconds();
1684 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001685 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
Jeff Layton4770d722014-07-29 21:34:10 -04001688static struct nfs4_stid *
1689find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001690{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001691 struct nfs4_stid *ret;
1692
1693 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1694 if (!ret || !ret->sc_type)
1695 return NULL;
1696 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001697}
1698
Jeff Layton4770d722014-07-29 21:34:10 -04001699static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001700find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001701{
1702 struct nfs4_stid *s;
1703
Jeff Layton4770d722014-07-29 21:34:10 -04001704 spin_lock(&cl->cl_lock);
1705 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001706 if (s != NULL) {
1707 if (typemask & s->sc_type)
1708 atomic_inc(&s->sc_count);
1709 else
1710 s = NULL;
1711 }
Jeff Layton4770d722014-07-29 21:34:10 -04001712 spin_unlock(&cl->cl_lock);
1713 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001714}
1715
Jeff Layton2216d442012-11-12 15:00:57 -05001716static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001717 struct svc_rqst *rqstp, nfs4_verifier *verf)
1718{
1719 struct nfs4_client *clp;
1720 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001721 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001722 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001723 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001724
1725 clp = alloc_client(name);
1726 if (clp == NULL)
1727 return NULL;
1728
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001729 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1730 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001731 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001732 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001733 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001734 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001735 }
Jeff Layton02e12152014-07-16 10:31:57 -04001736 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001737 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001738 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001739 copy_verf(clp, verf);
1740 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001741 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001742 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001743 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001744 return clp;
1745}
1746
NeilBrownfd39ca92005-06-23 22:04:03 -07001747static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001748add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1749{
1750 struct rb_node **new = &(root->rb_node), *parent = NULL;
1751 struct nfs4_client *clp;
1752
1753 while (*new) {
1754 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1755 parent = *new;
1756
1757 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1758 new = &((*new)->rb_left);
1759 else
1760 new = &((*new)->rb_right);
1761 }
1762
1763 rb_link_node(&new_clp->cl_namenode, parent, new);
1764 rb_insert_color(&new_clp->cl_namenode, root);
1765}
1766
1767static struct nfs4_client *
1768find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1769{
1770 long long cmp;
1771 struct rb_node *node = root->rb_node;
1772 struct nfs4_client *clp;
1773
1774 while (node) {
1775 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1776 cmp = compare_blob(&clp->cl_name, name);
1777 if (cmp > 0)
1778 node = node->rb_left;
1779 else if (cmp < 0)
1780 node = node->rb_right;
1781 else
1782 return clp;
1783 }
1784 return NULL;
1785}
1786
1787static void
1788add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
1790 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001791 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001793 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001794 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001796 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001797 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798}
1799
NeilBrownfd39ca92005-06-23 22:04:03 -07001800static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801move_to_confirmed(struct nfs4_client *clp)
1802{
1803 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001804 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001807 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001808 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001809 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001810 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 renew_client(clp);
1812}
1813
1814static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001815find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
1817 struct nfs4_client *clp;
1818 unsigned int idhashval = clientid_hashval(clid->cl_id);
1819
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001820 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001821 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001822 if ((bool)clp->cl_minorversion != sessions)
1823 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001824 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 }
1828 return NULL;
1829}
1830
1831static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001832find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1833{
1834 struct list_head *tbl = nn->conf_id_hashtbl;
1835
1836 return find_client_in_id_table(tbl, clid, sessions);
1837}
1838
1839static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001840find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001842 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001844 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845}
1846
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001847static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001848{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001849 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001850}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001851
NeilBrown28ce6052005-06-23 22:03:56 -07001852static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001853find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001854{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001855 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001856}
1857
1858static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001859find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001860{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001861 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001862}
1863
NeilBrownfd39ca92005-06-23 22:04:03 -07001864static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001865gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001867 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001868 struct sockaddr *sa = svc_addr(rqstp);
1869 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001870 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Jeff Layton7077ecb2009-08-14 12:57:58 -04001872 /* Currently, we only support tcp and tcp6 for the callback channel */
1873 if (se->se_callback_netid_len == 3 &&
1874 !memcmp(se->se_callback_netid_val, "tcp", 3))
1875 expected_family = AF_INET;
1876 else if (se->se_callback_netid_len == 4 &&
1877 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1878 expected_family = AF_INET6;
1879 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 goto out_err;
1881
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001882 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001883 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001884 (struct sockaddr *)&conn->cb_addr,
1885 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001886
J. Bruce Fields07263f12010-05-31 19:09:40 -04001887 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001889
J. Bruce Fields07263f12010-05-31 19:09:40 -04001890 if (conn->cb_addr.ss_family == AF_INET6)
1891 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001892
J. Bruce Fields07263f12010-05-31 19:09:40 -04001893 conn->cb_prog = se->se_callback_prog;
1894 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001895 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 return;
1897out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001898 conn->cb_addr.ss_family = AF_UNSPEC;
1899 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001900 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 "will not receive delegations\n",
1902 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 return;
1905}
1906
Andy Adamson074fe892009-04-03 08:28:15 +03001907/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04001908 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001909 */
Trond Myklebustb6076642014-06-30 11:48:35 -04001910static void
Andy Adamson074fe892009-04-03 08:28:15 +03001911nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1912{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001913 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04001914 struct nfsd4_slot *slot = resp->cstate.slot;
1915 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001916
Andy Adamson557ce262009-08-28 08:45:04 -04001917 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001918
Andy Adamson557ce262009-08-28 08:45:04 -04001919 slot->sl_opcnt = resp->opcnt;
1920 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001921
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001922 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001923 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001924 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001925 return;
1926 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001927 base = resp->cstate.data_offset;
1928 slot->sl_datalen = buf->len - base;
1929 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04001930 WARN("%s: sessions DRC could not cache compound\n", __func__);
1931 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001932}
1933
1934/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001935 * Encode the replay sequence operation from the slot values.
1936 * If cachethis is FALSE encode the uncached rep error on the next
1937 * operation which sets resp->p and increments resp->opcnt for
1938 * nfs4svc_encode_compoundres.
1939 *
Andy Adamson074fe892009-04-03 08:28:15 +03001940 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001941static __be32
1942nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1943 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001944{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001945 struct nfsd4_op *op;
1946 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001947
Andy Adamsonabfabf82009-07-23 19:02:18 -04001948 /* Encode the replayed sequence operation */
1949 op = &args->ops[resp->opcnt - 1];
1950 nfsd4_encode_operation(resp, op);
1951
1952 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05001953 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001954 op = &args->ops[resp->opcnt++];
1955 op->status = nfserr_retry_uncached_rep;
1956 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001957 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001958 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001959}
1960
1961/*
Andy Adamson557ce262009-08-28 08:45:04 -04001962 * The sequence operation is not cached because we can use the slot and
1963 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001964 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04001965static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001966nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1967 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001968{
Andy Adamson557ce262009-08-28 08:45:04 -04001969 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001970 struct xdr_stream *xdr = &resp->xdr;
1971 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03001972 __be32 status;
1973
Andy Adamson557ce262009-08-28 08:45:04 -04001974 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001975
Andy Adamsonabfabf82009-07-23 19:02:18 -04001976 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04001977 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04001978 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001979
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001980 p = xdr_reserve_space(xdr, slot->sl_datalen);
1981 if (!p) {
1982 WARN_ON_ONCE(1);
1983 return nfserr_serverfault;
1984 }
1985 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
1986 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03001987
Andy Adamson557ce262009-08-28 08:45:04 -04001988 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001989 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001990}
1991
Andy Adamson0733d212009-04-03 08:28:01 +03001992/*
1993 * Set the exchange_id flags returned by the server.
1994 */
1995static void
1996nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1997{
1998 /* pNFS is not supported */
1999 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2000
2001 /* Referrals are supported, Migration is not. */
2002 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2003
2004 /* set the wire flags to return to client. */
2005 clid->flags = new->cl_exchange_flags;
2006}
2007
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002008static bool client_has_state(struct nfs4_client *clp)
2009{
2010 /*
2011 * Note clp->cl_openowners check isn't quite right: there's no
2012 * need to count owners without stateid's.
2013 *
2014 * Also note we should probably be using this in 4.0 case too.
2015 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002016 return !list_empty(&clp->cl_openowners)
2017 || !list_empty(&clp->cl_delegations)
2018 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002019}
2020
Al Virob37ad282006-10-19 23:28:59 -07002021__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002022nfsd4_exchange_id(struct svc_rqst *rqstp,
2023 struct nfsd4_compound_state *cstate,
2024 struct nfsd4_exchange_id *exid)
2025{
Andy Adamson0733d212009-04-03 08:28:01 +03002026 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002027 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002028 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002029 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002030 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002031 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002032 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002033
Jeff Layton363168b2009-08-14 12:57:56 -04002034 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002035 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002036 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002037 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002038 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002039
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002040 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002041 return nfserr_inval;
2042
Andy Adamson0733d212009-04-03 08:28:01 +03002043 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002044 case SP4_MACH_CRED:
2045 if (!svc_rqst_integrity_protected(rqstp))
2046 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002047 case SP4_NONE:
2048 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002049 default: /* checked by xdr code */
2050 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002051 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002052 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002053 }
2054
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002055 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03002056 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002057 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002058 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002059 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2060 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2061
J. Bruce Fields136e6582012-05-12 20:37:23 -04002062 if (update) {
2063 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002064 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002065 goto out;
2066 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002067 if (!mach_creds_match(conf, rqstp)) {
2068 status = nfserr_wrong_cred;
2069 goto out;
2070 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002071 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002072 status = nfserr_perm;
2073 goto out;
2074 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002075 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002076 status = nfserr_not_same;
2077 goto out;
2078 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002079 /* case 6 */
2080 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2081 new = conf;
2082 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002083 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002084 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002085 if (client_has_state(conf)) {
2086 status = nfserr_clid_inuse;
2087 goto out;
2088 }
Andy Adamson0733d212009-04-03 08:28:01 +03002089 expire_client(conf);
2090 goto out_new;
2091 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002092 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002093 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002094 new = conf;
2095 goto out_copy;
2096 }
2097 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04002098 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002099 }
2100
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002101 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002102 status = nfserr_noent;
2103 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002104 }
2105
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002106 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002107 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03002108 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002109
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002110 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002111out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05002112 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03002113 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04002114 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03002115 goto out;
2116 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002117 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002118 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002119
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002120 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002121 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03002122out_copy:
2123 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2124 exid->clientid.cl_id = new->cl_clientid.cl_id;
2125
J. Bruce Fields778df3f2012-05-25 21:40:23 -04002126 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03002127 nfsd4_set_ex_flags(new, exid);
2128
2129 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04002130 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002131 status = nfs_ok;
2132
2133out:
2134 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03002135 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002136}
2137
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002138static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002139check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002140{
Andy Adamson88e588d2009-07-23 19:02:15 -04002141 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2142 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002143
2144 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002145 if (slot_inuse) {
2146 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002147 return nfserr_jukebox;
2148 else
2149 return nfserr_seq_misordered;
2150 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002151 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002152 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002153 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002154 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002155 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002156 return nfserr_seq_misordered;
2157}
2158
Andy Adamson49557cc2009-07-23 19:02:16 -04002159/*
2160 * Cache the create session result into the create session single DRC
2161 * slot cache by saving the xdr structure. sl_seqid has been set.
2162 * Do this for solo or embedded create session operations.
2163 */
2164static void
2165nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002166 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002167{
2168 slot->sl_status = nfserr;
2169 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2170}
2171
2172static __be32
2173nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2174 struct nfsd4_clid_slot *slot)
2175{
2176 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2177 return slot->sl_status;
2178}
2179
Mi Jinlong1b74c252011-07-14 14:50:17 +08002180#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2181 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2182 1 + /* MIN tag is length with zero, only length */ \
2183 3 + /* version, opcount, opcode */ \
2184 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2185 /* seqid, slotID, slotID, cache */ \
2186 4 ) * sizeof(__be32))
2187
2188#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2189 2 + /* verifier: AUTH_NULL, length 0 */\
2190 1 + /* status */ \
2191 1 + /* MIN tag is length with zero, only length */ \
2192 3 + /* opcount, opcode, opstatus*/ \
2193 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2194 /* seqid, slotID, slotID, slotID, status */ \
2195 5 ) * sizeof(__be32))
2196
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002197static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002198{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002199 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2200
J. Bruce Fields373cd402013-04-08 15:42:12 -04002201 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2202 return nfserr_toosmall;
2203 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2204 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002205 ca->headerpadsz = 0;
2206 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2207 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2208 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2209 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2210 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2211 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2212 /*
2213 * Note decreasing slot size below client's request may make it
2214 * difficult for client to function correctly, whereas
2215 * decreasing the number of slots will (just?) affect
2216 * performance. When short on memory we therefore prefer to
2217 * decrease number of slots instead of their size. Clients that
2218 * request larger slots than they need will get poor results:
2219 */
2220 ca->maxreqs = nfsd4_get_drc_mem(ca);
2221 if (!ca->maxreqs)
2222 return nfserr_jukebox;
2223
J. Bruce Fields373cd402013-04-08 15:42:12 -04002224 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002225}
2226
Kinglong Mee8a891632013-12-23 18:11:02 +08002227#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2228 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2229#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2230 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2231
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002232static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2233{
2234 ca->headerpadsz = 0;
2235
2236 /*
2237 * These RPC_MAX_HEADER macros are overkill, especially since we
2238 * don't even do gss on the backchannel yet. But this is still
2239 * less than 1k. Tighten up this estimate in the unlikely event
2240 * it turns out to be a problem for some client:
2241 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002242 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002243 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002244 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002245 return nfserr_toosmall;
2246 ca->maxresp_cached = 0;
2247 if (ca->maxops < 2)
2248 return nfserr_toosmall;
2249
2250 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002251}
2252
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002253static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2254{
2255 switch (cbs->flavor) {
2256 case RPC_AUTH_NULL:
2257 case RPC_AUTH_UNIX:
2258 return nfs_ok;
2259 default:
2260 /*
2261 * GSS case: the spec doesn't allow us to return this
2262 * error. But it also doesn't allow us not to support
2263 * GSS.
2264 * I'd rather this fail hard than return some error the
2265 * client might think it can already handle:
2266 */
2267 return nfserr_encr_alg_unsupp;
2268 }
2269}
2270
Andy Adamson069b6ad2009-04-03 08:27:58 +03002271__be32
2272nfsd4_create_session(struct svc_rqst *rqstp,
2273 struct nfsd4_compound_state *cstate,
2274 struct nfsd4_create_session *cr_ses)
2275{
Jeff Layton363168b2009-08-14 12:57:56 -04002276 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002277 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002278 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002279 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002280 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002281 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002282 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002283
Mi Jinlonga62573d2011-03-23 17:57:07 +08002284 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2285 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002286 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2287 if (status)
2288 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002289 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002290 if (status)
2291 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002292 status = check_backchannel_attrs(&cr_ses->back_channel);
2293 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002294 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002295 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002296 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002297 if (!new)
2298 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002299 conn = alloc_conn_from_crses(rqstp, cr_ses);
2300 if (!conn)
2301 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002302
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002303 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002304 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002305 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002306 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002307
2308 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002309 status = nfserr_wrong_cred;
2310 if (!mach_creds_match(conf, rqstp))
2311 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002312 cs_slot = &conf->cl_cs_slot;
2313 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002314 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002315 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002316 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002317 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002318 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002319 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002320 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002321 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002322 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002323 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002324 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002325 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002326 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002327 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002328 status = nfserr_wrong_cred;
2329 if (!mach_creds_match(unconf, rqstp))
2330 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002331 cs_slot = &unconf->cl_cs_slot;
2332 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002333 if (status) {
2334 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002335 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002336 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002337 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002338 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002339 if (old) {
2340 status = mark_client_expired(old);
2341 if (status)
2342 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002343 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002344 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002345 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002346 conf = unconf;
2347 } else {
2348 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002349 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002350 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002351 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002352 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002353 * We do not support RDMA or persistent sessions
2354 */
2355 cr_ses->flags &= ~SESSION4_PERSIST;
2356 cr_ses->flags &= ~SESSION4_RDMA;
2357
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002358 init_session(rqstp, new, conf, cr_ses);
2359 nfsd4_init_conn(rqstp, conn, new);
2360
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002361 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002362 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002363 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002364 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002365
Andy Adamson49557cc2009-07-23 19:02:16 -04002366 /* cache solo and embedded create sessions under the state lock */
2367 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002368 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002369 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002370out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08002371 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002372 free_conn(conn);
2373out_free_session:
2374 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002375out_release_drc_mem:
2376 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002377 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002378}
2379
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002380static __be32 nfsd4_map_bcts_dir(u32 *dir)
2381{
2382 switch (*dir) {
2383 case NFS4_CDFC4_FORE:
2384 case NFS4_CDFC4_BACK:
2385 return nfs_ok;
2386 case NFS4_CDFC4_FORE_OR_BOTH:
2387 case NFS4_CDFC4_BACK_OR_BOTH:
2388 *dir = NFS4_CDFC4_BOTH;
2389 return nfs_ok;
2390 };
2391 return nfserr_inval;
2392}
2393
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002394__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2395{
2396 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002397 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002398 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002399
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002400 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2401 if (status)
2402 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002403 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002404 session->se_cb_prog = bc->bc_cb_program;
2405 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002406 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002407
2408 nfsd4_probe_callback(session->se_client);
2409
2410 return nfs_ok;
2411}
2412
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002413__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2414 struct nfsd4_compound_state *cstate,
2415 struct nfsd4_bind_conn_to_session *bcts)
2416{
2417 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002418 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002419 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002420 struct net *net = SVC_NET(rqstp);
2421 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002422
2423 if (!nfsd4_last_compound_op(rqstp))
2424 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002425 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002426 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002427 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002428 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002429 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002430 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002431 status = nfserr_wrong_cred;
2432 if (!mach_creds_match(session->se_client, rqstp))
2433 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002434 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002435 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002436 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002437 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002438 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002439 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002440 goto out;
2441 nfsd4_init_conn(rqstp, conn, session);
2442 status = nfs_ok;
2443out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002444 nfsd4_put_session(session);
2445out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002446 nfs4_unlock_state();
2447 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002448}
2449
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002450static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2451{
2452 if (!session)
2453 return 0;
2454 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2455}
2456
Andy Adamson069b6ad2009-04-03 08:27:58 +03002457__be32
2458nfsd4_destroy_session(struct svc_rqst *r,
2459 struct nfsd4_compound_state *cstate,
2460 struct nfsd4_destroy_session *sessionid)
2461{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002462 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002463 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002464 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002465 struct net *net = SVC_NET(r);
2466 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002467
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002468 nfs4_lock_state();
2469 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002470 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002471 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002472 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002473 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002474 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002475 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002476 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002477 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002478 if (!ses)
2479 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002480 status = nfserr_wrong_cred;
2481 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002482 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002483 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002484 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002485 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002486 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002487 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002488
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002489 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002490
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002491 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002492 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002493out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002494 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002495out_client_lock:
2496 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002497out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002498 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002499 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002500}
2501
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002502static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002503{
2504 struct nfsd4_conn *c;
2505
2506 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002507 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002508 return c;
2509 }
2510 }
2511 return NULL;
2512}
2513
J. Bruce Fields57266a62013-04-13 14:27:29 -04002514static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002515{
2516 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002517 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002518 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002519 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002520
2521 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002522 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002523 if (c)
2524 goto out_free;
2525 status = nfserr_conn_not_bound_to_session;
2526 if (clp->cl_mach_cred)
2527 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002528 __nfsd4_hash_conn(new, ses);
2529 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002530 ret = nfsd4_register_conn(new);
2531 if (ret)
2532 /* oops; xprt is already down: */
2533 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002534 return nfs_ok;
2535out_free:
2536 spin_unlock(&clp->cl_lock);
2537 free_conn(new);
2538 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002539}
2540
Mi Jinlong868b89c2011-04-27 09:09:58 +08002541static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2542{
2543 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2544
2545 return args->opcnt > session->se_fchannel.maxops;
2546}
2547
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002548static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2549 struct nfsd4_session *session)
2550{
2551 struct xdr_buf *xb = &rqstp->rq_arg;
2552
2553 return xb->len > session->se_fchannel.maxreq_sz;
2554}
2555
Andy Adamson069b6ad2009-04-03 08:27:58 +03002556__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002557nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002558 struct nfsd4_compound_state *cstate,
2559 struct nfsd4_sequence *seq)
2560{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002561 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002562 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002563 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002564 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002565 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002566 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002567 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002568 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002569 struct net *net = SVC_NET(rqstp);
2570 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002571
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002572 if (resp->opcnt != 1)
2573 return nfserr_sequence_pos;
2574
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002575 /*
2576 * Will be either used or freed by nfsd4_sequence_check_conn
2577 * below.
2578 */
2579 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2580 if (!conn)
2581 return nfserr_jukebox;
2582
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002583 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002584 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002585 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002586 goto out_no_session;
2587 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002588
Mi Jinlong868b89c2011-04-27 09:09:58 +08002589 status = nfserr_too_many_ops;
2590 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002591 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002592
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002593 status = nfserr_req_too_big;
2594 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002595 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002596
Benny Halevyb85d4c02009-04-03 08:28:08 +03002597 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002598 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002599 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002600
Andy Adamson557ce262009-08-28 08:45:04 -04002601 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002602 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2603
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002604 /* We do not negotiate the number of slots yet, so set the
2605 * maxslots to the session maxreqs which is used to encode
2606 * sr_highest_slotid and the sr_target_slot id to maxslots */
2607 seq->maxslots = session->se_fchannel.maxreqs;
2608
J. Bruce Fields73e79482012-02-13 16:39:00 -05002609 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2610 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002611 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002612 status = nfserr_seq_misordered;
2613 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002614 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002615 cstate->slot = slot;
2616 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002617 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002618 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002619 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002620 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002621 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002622 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002623 }
2624 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002625 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002626
J. Bruce Fields57266a62013-04-13 14:27:29 -04002627 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002628 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002629 if (status)
2630 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002631
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002632 buflen = (seq->cachethis) ?
2633 session->se_fchannel.maxresp_cached :
2634 session->se_fchannel.maxresp_sz;
2635 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2636 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002637 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002638 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002639 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002640
2641 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002642 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002643 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002644 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002645 if (seq->cachethis)
2646 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002647 else
2648 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002649
2650 cstate->slot = slot;
2651 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002652 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002653
Benny Halevyb85d4c02009-04-03 08:28:08 +03002654out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002655 switch (clp->cl_cb_state) {
2656 case NFSD4_CB_DOWN:
2657 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2658 break;
2659 case NFSD4_CB_FAULT:
2660 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2661 break;
2662 default:
2663 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002664 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002665 if (!list_empty(&clp->cl_revoked))
2666 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002667out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002668 if (conn)
2669 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002670 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002671 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002672out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002673 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002674 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002675}
2676
Trond Myklebustb6076642014-06-30 11:48:35 -04002677void
2678nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2679{
2680 struct nfsd4_compound_state *cs = &resp->cstate;
2681
2682 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002683 if (cs->status != nfserr_replay_cache) {
2684 nfsd4_store_cache_entry(resp);
2685 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2686 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002687 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002688 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002689 } else if (cs->clp)
2690 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002691}
2692
Mi Jinlong345c2842011-10-20 17:51:39 +08002693__be32
2694nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2695{
2696 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002697 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002698 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002699
2700 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002701 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002702 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002703 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002704
2705 if (conf) {
2706 clp = conf;
2707
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002708 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002709 status = nfserr_clientid_busy;
2710 goto out;
2711 }
2712 } else if (unconf)
2713 clp = unconf;
2714 else {
2715 status = nfserr_stale_clientid;
2716 goto out;
2717 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002718 if (!mach_creds_match(clp, rqstp)) {
2719 status = nfserr_wrong_cred;
2720 goto out;
2721 }
Mi Jinlong345c2842011-10-20 17:51:39 +08002722 expire_client(clp);
2723out:
2724 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002725 return status;
2726}
2727
Andy Adamson069b6ad2009-04-03 08:27:58 +03002728__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002729nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2730{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002731 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002732
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002733 if (rc->rca_one_fs) {
2734 if (!cstate->current_fh.fh_dentry)
2735 return nfserr_nofilehandle;
2736 /*
2737 * We don't take advantage of the rca_one_fs case.
2738 * That's OK, it's optional, we can safely ignore it.
2739 */
2740 return nfs_ok;
2741 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002742
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002743 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002744 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002745 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2746 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002747 goto out;
2748
2749 status = nfserr_stale_clientid;
2750 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002751 /*
2752 * The following error isn't really legal.
2753 * But we only get here if the client just explicitly
2754 * destroyed the client. Surely it no longer cares what
2755 * error it gets back on an operation for the dead
2756 * client.
2757 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002758 goto out;
2759
2760 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002761 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002762out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002763 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002764 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002765}
2766
2767__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002768nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2769 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002771 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002773 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002774 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002775 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2776
J. Bruce Fields63db4632012-05-18 21:54:19 -04002777 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002779 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002780 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002781 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002783 if (clp_used_exchangeid(conf))
2784 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002785 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002786 char addr_str[INET6_ADDRSTRLEN];
2787 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2788 sizeof(addr_str));
2789 dprintk("NFSD: setclientid: string in use by client "
2790 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 goto out;
2792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002794 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002795 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002798 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002799 if (new == NULL)
2800 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002801 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002802 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002804 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002805 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002806 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002807 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002808 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2810 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2811 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2812 status = nfs_ok;
2813out:
2814 nfs4_unlock_state();
2815 return status;
2816}
2817
2818
Al Virob37ad282006-10-19 23:28:59 -07002819__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002820nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2821 struct nfsd4_compound_state *cstate,
2822 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823{
NeilBrown21ab45a2005-06-23 22:04:14 -07002824 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2826 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002827 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002828 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002830 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002833
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002834 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002835 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002836 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002837 * We try hard to give out unique clientid's, so if we get an
2838 * attempt to confirm the same clientid with a different cred,
2839 * there's a bug somewhere. Let's charitably assume it's our
2840 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002841 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002842 status = nfserr_serverfault;
2843 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2844 goto out;
2845 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2846 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002847 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002848 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2849 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002851 else /* case 4: client hasn't noticed we rebooted yet? */
2852 status = nfserr_stale_clientid;
2853 goto out;
2854 }
2855 status = nfs_ok;
2856 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002857 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2858 nfsd4_probe_callback(conf);
2859 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002860 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002861 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002862 if (conf) {
2863 status = mark_client_expired(conf);
2864 if (status)
2865 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002866 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002867 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002868 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002869 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 nfs4_unlock_state();
2873 return status;
2874}
2875
J. Bruce Fields32513b42011-10-13 16:00:16 -04002876static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002878 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2879}
2880
2881/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04002882static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04002883{
Trond Myklebustca943212014-07-23 16:17:39 -04002884 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
Trond Myklebust950e0112014-06-30 11:48:31 -04002886 lockdep_assert_held(&state_lock);
2887
J. Bruce Fields32513b42011-10-13 16:00:16 -04002888 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04002889 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002890 INIT_LIST_HEAD(&fp->fi_stateids);
2891 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04002892 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002893 fp->fi_had_conflict = false;
2894 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04002895 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002896 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2897 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04002898 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899}
2900
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002901void
NeilBrowne60d4392005-06-23 22:03:01 -07002902nfsd4_free_slabs(void)
2903{
Christoph Hellwigabf11352014-05-21 07:43:03 -07002904 kmem_cache_destroy(openowner_slab);
2905 kmem_cache_destroy(lockowner_slab);
2906 kmem_cache_destroy(file_slab);
2907 kmem_cache_destroy(stateid_slab);
2908 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002909}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Bryan Schumaker72083392011-11-01 15:24:59 -04002911int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912nfsd4_init_slabs(void)
2913{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002914 openowner_slab = kmem_cache_create("nfsd4_openowners",
2915 sizeof(struct nfs4_openowner), 0, 0, NULL);
2916 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002917 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002918 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002919 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002920 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002921 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07002922 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002923 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002924 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002925 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07002926 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002927 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002928 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002929 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002930 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002931 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002932 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002933 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07002935
2936out_free_stateid_slab:
2937 kmem_cache_destroy(stateid_slab);
2938out_free_file_slab:
2939 kmem_cache_destroy(file_slab);
2940out_free_lockowner_slab:
2941 kmem_cache_destroy(lockowner_slab);
2942out_free_openowner_slab:
2943 kmem_cache_destroy(openowner_slab);
2944out:
NeilBrowne60d4392005-06-23 22:03:01 -07002945 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2946 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947}
2948
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002949static void init_nfs4_replay(struct nfs4_replay *rp)
2950{
2951 rp->rp_status = nfserr_serverfault;
2952 rp->rp_buflen = 0;
2953 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04002954 mutex_init(&rp->rp_mutex);
2955}
2956
2957static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
2958 struct nfs4_stateowner *so)
2959{
2960 if (!nfsd4_has_session(cstate)) {
2961 mutex_lock(&so->so_replay.rp_mutex);
2962 cstate->replay_owner = so;
2963 atomic_inc(&so->so_count);
2964 }
2965}
2966
2967void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
2968{
2969 struct nfs4_stateowner *so = cstate->replay_owner;
2970
2971 if (so != NULL) {
2972 cstate->replay_owner = NULL;
2973 mutex_unlock(&so->so_replay.rp_mutex);
2974 nfs4_put_stateowner(so);
2975 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002976}
2977
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002978static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979{
2980 struct nfs4_stateowner *sop;
2981
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002982 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002983 if (!sop)
2984 return NULL;
2985
2986 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2987 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002988 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002989 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002991 sop->so_owner.len = owner->len;
2992
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002993 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002994 sop->so_client = clp;
2995 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04002996 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002997 return sop;
2998}
2999
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003000static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003001{
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003002 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3003
3004 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003005 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006}
3007
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003008static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3009{
3010 struct nfs4_openowner *oo = openowner(so);
3011
3012 unhash_openowner(oo);
3013}
3014
Jeff Layton6b180f02014-07-29 21:34:26 -04003015static void nfs4_free_openowner(struct nfs4_stateowner *so)
3016{
3017 struct nfs4_openowner *oo = openowner(so);
3018
3019 kmem_cache_free(openowner_slab, oo);
3020}
3021
3022static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003023 .so_unhash = nfs4_unhash_openowner,
3024 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003025};
3026
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003027static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003028alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003029 struct nfsd4_compound_state *cstate)
3030{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003031 struct nfs4_client *clp = cstate->clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003032 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003034 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3035 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003037 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003038 oo->oo_owner.so_is_open_owner = 1;
3039 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003040 oo->oo_flags = NFS4_OO_NEW;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003041 if (nfsd4_has_session(cstate))
3042 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003043 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003044 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003045 INIT_LIST_HEAD(&oo->oo_close_lru);
3046 hash_openowner(oo, clp, strhashval);
3047 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048}
3049
J. Bruce Fields996e0932011-10-17 11:14:48 -04003050static 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 -04003051 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003053 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003054 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003055 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003056 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07003057 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003058 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 stp->st_access_bmap = 0;
3060 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003061 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003062 spin_lock(&oo->oo_owner.so_client->cl_lock);
3063 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003064 spin_lock(&fp->fi_lock);
3065 list_add(&stp->st_perfile, &fp->fi_stateids);
3066 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003067 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068}
3069
3070static void
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003071move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072{
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003073 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3074
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003075 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003077 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003078 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079}
3080
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04003082same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
3083 clientid_t *clid)
3084{
3085 return (sop->so_owner.len == owner->len) &&
3086 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
3087 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088}
3089
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003090static struct nfs4_openowner *
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003091find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
3092 bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093{
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003094 struct nfs4_stateowner *so;
3095 struct nfs4_openowner *oo;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003096 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003098 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003099 if (!so->so_is_open_owner)
3100 continue;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003101 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
3102 oo = openowner(so);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003103 clp = oo->oo_owner.so_client;
3104 if ((bool)clp->cl_minorversion != sessions)
3105 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003106 renew_client(oo->oo_owner.so_client);
3107 return oo;
3108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 }
3110 return NULL;
3111}
3112
3113/* search file_hashtbl[] for file */
3114static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003115find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116{
Trond Myklebustca943212014-07-23 16:17:39 -04003117 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 struct nfs4_file *fp;
3119
Trond Myklebust950e0112014-06-30 11:48:31 -04003120 lockdep_assert_held(&state_lock);
3121
Jeff Layton89876f82013-04-02 09:01:59 -04003122 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003123 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003124 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 }
3128 return NULL;
3129}
3130
Trond Myklebust950e0112014-06-30 11:48:31 -04003131static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003132find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003133{
3134 struct nfs4_file *fp;
3135
3136 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003137 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003138 spin_unlock(&state_lock);
3139 return fp;
3140}
3141
3142static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003143find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003144{
3145 struct nfs4_file *fp;
3146
3147 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003148 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003149 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003150 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003151 fp = new;
3152 }
3153 spin_unlock(&state_lock);
3154
3155 return fp;
3156}
3157
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003158/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 * Called to check deny when READ with all zero stateid or
3160 * WRITE with all zero or all one stateid
3161 */
Al Virob37ad282006-10-19 23:28:59 -07003162static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3164{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003166 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Trond Myklebustca943212014-07-23 16:17:39 -04003168 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003169 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003170 return ret;
3171 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003172 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003173 if (fp->fi_share_deny & deny_type)
3174 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003175 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003176 put_nfs4_file(fp);
3177 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178}
3179
Jeff Layton02e12152014-07-16 10:31:57 -04003180void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181{
Trond Myklebust11b91642014-07-29 21:34:08 -04003182 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3183 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003184
Trond Myklebust11b91642014-07-29 21:34:08 -04003185 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003186
Jeff Layton02e12152014-07-16 10:31:57 -04003187 /*
3188 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003189 * already holding inode->i_lock.
3190 *
Jeff Laytondff13992014-07-08 14:02:49 -04003191 * If the dl_time != 0, then we know that it has already been
3192 * queued for a lease break. Don't queue it again.
3193 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003194 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003195 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003196 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003197 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003198 }
Jeff Layton02e12152014-07-16 10:31:57 -04003199 spin_unlock(&state_lock);
3200}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
Jeff Layton02e12152014-07-16 10:31:57 -04003202static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3203{
3204 /*
3205 * We're assuming the state code never drops its reference
3206 * without first removing the lease. Since we're in this lease
3207 * callback (and since the lease code is serialized by the kernel
3208 * lock) we know the server hasn't removed the lease yet, we know
3209 * it's safe to take a reference.
3210 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003211 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003212 nfsd4_cb_recall(dp);
3213}
3214
Jeff Layton1c8c6012013-06-21 08:58:15 -04003215/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003216static void nfsd_break_deleg_cb(struct file_lock *fl)
3217{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003218 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3219 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003220
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003221 if (!fp) {
3222 WARN(1, "(%p)->fl_owner NULL\n", fl);
3223 return;
3224 }
3225 if (fp->fi_had_conflict) {
3226 WARN(1, "duplicate break on %p\n", fp);
3227 return;
3228 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003229 /*
3230 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003231 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003232 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003233 */
3234 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Jeff Layton02e12152014-07-16 10:31:57 -04003236 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003237 fp->fi_had_conflict = true;
3238 /*
3239 * If there are no delegations on the list, then we can't count on this
3240 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3241 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3242 * true should keep any new delegations from being hashed.
3243 */
3244 if (list_empty(&fp->fi_delegations))
3245 fl->fl_break_time = jiffies;
3246 else
3247 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3248 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003249 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250}
3251
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252static
3253int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3254{
3255 if (arg & F_UNLCK)
3256 return lease_modify(onlist, arg);
3257 else
3258 return -EAGAIN;
3259}
3260
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003261static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003262 .lm_break = nfsd_break_deleg_cb,
3263 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264};
3265
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003266static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3267{
3268 if (nfsd4_has_session(cstate))
3269 return nfs_ok;
3270 if (seqid == so->so_seqid - 1)
3271 return nfserr_replay_me;
3272 if (seqid == so->so_seqid)
3273 return nfs_ok;
3274 return nfserr_bad_seqid;
3275}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
Jeff Layton4b24ca72014-06-30 11:48:44 -04003277static __be32 lookup_clientid(clientid_t *clid,
3278 struct nfsd4_compound_state *cstate,
3279 struct nfsd_net *nn)
3280{
3281 struct nfs4_client *found;
3282
3283 if (cstate->clp) {
3284 found = cstate->clp;
3285 if (!same_clid(&found->cl_clientid, clid))
3286 return nfserr_stale_clientid;
3287 return nfs_ok;
3288 }
3289
3290 if (STALE_CLIENTID(clid, nn))
3291 return nfserr_stale_clientid;
3292
3293 /*
3294 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3295 * cached already then we know this is for is for v4.0 and "sessions"
3296 * will be false.
3297 */
3298 WARN_ON_ONCE(cstate->session);
3299 found = find_confirmed_client(clid, false, nn);
3300 if (!found)
3301 return nfserr_expired;
3302
3303 /* Cache the nfs4_client in cstate! */
3304 cstate->clp = found;
3305 atomic_inc(&found->cl_refcount);
3306 return nfs_ok;
3307}
3308
Al Virob37ad282006-10-19 23:28:59 -07003309__be32
Andy Adamson66689582009-04-03 08:28:45 +03003310nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003311 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 clientid_t *clientid = &open->op_clientid;
3314 struct nfs4_client *clp = NULL;
3315 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003316 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003317 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003319 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003321 /*
3322 * In case we need it later, after we've already created the
3323 * file and don't want to risk a further failure:
3324 */
3325 open->op_file = nfsd4_alloc_file();
3326 if (open->op_file == NULL)
3327 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
Trond Myklebust2d91e892014-06-30 11:48:46 -04003329 status = lookup_clientid(clientid, cstate, nn);
3330 if (status)
3331 return status;
3332 clp = cstate->clp;
3333
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003334 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003335 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003336 open->op_openowner = oo;
3337 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003338 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003340 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003341 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003342 release_openowner(oo);
3343 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003344 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003345 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003346 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3347 if (status)
3348 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003349 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003350new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003351 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003352 if (oo == NULL)
3353 return nfserr_jukebox;
3354 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003355alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003356 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003357 if (!open->op_stp)
3358 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003359 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360}
3361
Al Virob37ad282006-10-19 23:28:59 -07003362static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003363nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3364{
3365 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3366 return nfserr_openmode;
3367 else
3368 return nfs_ok;
3369}
3370
Daniel Mackc47d8322011-05-16 16:38:14 +02003371static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003372{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003373 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3374}
3375
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003376static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003377{
3378 struct nfs4_stid *ret;
3379
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003380 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003381 if (!ret)
3382 return NULL;
3383 return delegstateid(ret);
3384}
3385
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003386static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3387{
3388 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3389 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3390}
3391
Al Virob37ad282006-10-19 23:28:59 -07003392static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003393nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003394 struct nfs4_delegation **dp)
3395{
3396 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003397 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003398 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003399
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003400 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3401 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003402 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003403 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003404 status = nfs4_check_delegmode(deleg, flags);
3405 if (status) {
3406 nfs4_put_stid(&deleg->dl_stid);
3407 goto out;
3408 }
3409 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003410out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003411 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003412 return nfs_ok;
3413 if (status)
3414 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003415 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003416 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003417}
3418
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003419static struct nfs4_ol_stateid *
3420nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003422 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003423 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
Trond Myklebust1d31a252014-07-10 14:07:25 -04003425 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003426 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 /* ignore lock owners */
3428 if (local->st_stateowner->so_is_open_owner == 0)
3429 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003430 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003431 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003432 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003433 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003436 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003437 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438}
3439
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003440static inline int nfs4_access_to_access(u32 nfs4_access)
3441{
3442 int flags = 0;
3443
3444 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3445 flags |= NFSD_MAY_READ;
3446 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3447 flags |= NFSD_MAY_WRITE;
3448 return flags;
3449}
3450
Al Virob37ad282006-10-19 23:28:59 -07003451static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3453 struct nfsd4_open *open)
3454{
3455 struct iattr iattr = {
3456 .ia_valid = ATTR_SIZE,
3457 .ia_size = 0,
3458 };
3459 if (!open->op_truncate)
3460 return 0;
3461 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003462 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3464}
3465
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003466static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003467 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3468 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003469{
Trond Myklebustde186432014-07-10 14:07:26 -04003470 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003471 __be32 status;
3472 int oflag = nfs4_access_to_omode(open->op_share_access);
3473 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003474 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003475
Trond Myklebustde186432014-07-10 14:07:26 -04003476 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003477
3478 /*
3479 * Are we trying to set a deny mode that would conflict with
3480 * current access?
3481 */
3482 status = nfs4_file_check_deny(fp, open->op_share_deny);
3483 if (status != nfs_ok) {
3484 spin_unlock(&fp->fi_lock);
3485 goto out;
3486 }
3487
3488 /* set access to the file */
3489 status = nfs4_file_get_access(fp, open->op_share_access);
3490 if (status != nfs_ok) {
3491 spin_unlock(&fp->fi_lock);
3492 goto out;
3493 }
3494
3495 /* Set access bits in stateid */
3496 old_access_bmap = stp->st_access_bmap;
3497 set_access(open->op_share_access, stp);
3498
3499 /* Set new deny mask */
3500 old_deny_bmap = stp->st_deny_bmap;
3501 set_deny(open->op_share_deny, stp);
3502 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3503
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003504 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003505 spin_unlock(&fp->fi_lock);
3506 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003507 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003508 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003509 spin_lock(&fp->fi_lock);
3510 if (!fp->fi_fds[oflag]) {
3511 fp->fi_fds[oflag] = filp;
3512 filp = NULL;
3513 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003514 }
Trond Myklebustde186432014-07-10 14:07:26 -04003515 spin_unlock(&fp->fi_lock);
3516 if (filp)
3517 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003518
3519 status = nfsd4_truncate(rqstp, cur_fh, open);
3520 if (status)
3521 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003522out:
3523 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003524out_put_access:
3525 stp->st_access_bmap = old_access_bmap;
3526 nfs4_file_put_access(fp, open->op_share_access);
3527 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3528 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003529}
3530
Al Virob37ad282006-10-19 23:28:59 -07003531static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003532nfs4_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 -07003533{
Al Virob37ad282006-10-19 23:28:59 -07003534 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003535 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003537 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003538 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003539
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003540 /* test and set deny mode */
3541 spin_lock(&fp->fi_lock);
3542 status = nfs4_file_check_deny(fp, open->op_share_deny);
3543 if (status == nfs_ok) {
3544 old_deny_bmap = stp->st_deny_bmap;
3545 set_deny(open->op_share_deny, stp);
3546 fp->fi_share_deny |=
3547 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3548 }
3549 spin_unlock(&fp->fi_lock);
3550
3551 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003554 status = nfsd4_truncate(rqstp, cur_fh, open);
3555 if (status != nfs_ok)
3556 reset_union_bmap_deny(old_deny_bmap, stp);
3557 return status;
3558}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003561nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003563 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564}
3565
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003566/* Should we give out recallable state?: */
3567static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3568{
3569 if (clp->cl_cb_state == NFSD4_CB_UP)
3570 return true;
3571 /*
3572 * In the sessions case, since we don't have to establish a
3573 * separate connection for callbacks, we assume it's OK
3574 * until we hear otherwise:
3575 */
3576 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3577}
3578
Jeff Laytond564fbe2014-07-16 10:31:58 -04003579static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003580{
3581 struct file_lock *fl;
3582
3583 fl = locks_alloc_lock();
3584 if (!fl)
3585 return NULL;
3586 locks_init_lock(fl);
3587 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003588 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003589 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3590 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003591 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003592 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003593 return fl;
3594}
3595
J. Bruce Fields99c41512013-05-21 16:21:25 -04003596static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003597{
Trond Myklebust11b91642014-07-29 21:34:08 -04003598 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003599 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003600 struct file *filp;
3601 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003602
Jeff Laytond564fbe2014-07-16 10:31:58 -04003603 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003604 if (!fl)
3605 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003606 filp = find_readable_file(fp);
3607 if (!filp) {
3608 /* We should always have a readable file here */
3609 WARN_ON_ONCE(1);
3610 return -EBADF;
3611 }
3612 fl->fl_file = filp;
3613 status = vfs_setlease(filp, fl->fl_type, &fl);
3614 if (status) {
3615 locks_free_lock(fl);
3616 goto out_fput;
3617 }
Benny Halevycdc97502014-05-30 09:09:30 -04003618 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003619 spin_lock(&fp->fi_lock);
3620 /* Did the lease get broken before we took the lock? */
3621 status = -EAGAIN;
3622 if (fp->fi_had_conflict)
3623 goto out_unlock;
3624 /* Race breaker */
3625 if (fp->fi_lease) {
3626 status = 0;
3627 atomic_inc(&fp->fi_delegees);
3628 hash_delegation_locked(dp, fp);
3629 goto out_unlock;
3630 }
3631 fp->fi_lease = fl;
3632 fp->fi_deleg_file = filp;
3633 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003634 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003635 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003636 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003637 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003638out_unlock:
3639 spin_unlock(&fp->fi_lock);
3640 spin_unlock(&state_lock);
3641out_fput:
3642 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003643 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003644}
3645
Jeff Layton0b266932014-07-25 07:34:25 -04003646static struct nfs4_delegation *
3647nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3648 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003649{
Jeff Layton0b266932014-07-25 07:34:25 -04003650 int status;
3651 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003652
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003653 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003654 return ERR_PTR(-EAGAIN);
3655
3656 dp = alloc_init_deleg(clp, fh);
3657 if (!dp)
3658 return ERR_PTR(-ENOMEM);
3659
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003660 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003661 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003662 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003663 dp->dl_stid.sc_file = fp;
Jeff Layton417c6622014-07-21 09:34:57 -04003664 if (!fp->fi_lease) {
3665 spin_unlock(&fp->fi_lock);
3666 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003667 status = nfs4_setlease(dp);
3668 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003669 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003670 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003671 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003672 status = -EAGAIN;
3673 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003674 }
Benny Halevy931ee562014-05-30 09:09:27 -04003675 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003676 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003677out_unlock:
3678 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003679 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003680out:
3681 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003682 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003683 return ERR_PTR(status);
3684 }
3685 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003686}
3687
Benny Halevy4aa89132012-02-21 14:16:44 -08003688static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3689{
3690 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3691 if (status == -EAGAIN)
3692 open->op_why_no_deleg = WND4_CONTENTION;
3693 else {
3694 open->op_why_no_deleg = WND4_RESOURCE;
3695 switch (open->op_deleg_want) {
3696 case NFS4_SHARE_WANT_READ_DELEG:
3697 case NFS4_SHARE_WANT_WRITE_DELEG:
3698 case NFS4_SHARE_WANT_ANY_DELEG:
3699 break;
3700 case NFS4_SHARE_WANT_CANCEL:
3701 open->op_why_no_deleg = WND4_CANCELLED;
3702 break;
3703 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003704 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003705 }
3706 }
3707}
3708
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709/*
3710 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003711 *
3712 * Note we don't support write delegations, and won't until the vfs has
3713 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 */
3715static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003716nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3717 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718{
3719 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003720 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3721 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003722 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003723 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003725 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003726 open->op_recall = 0;
3727 switch (open->op_claim_type) {
3728 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003729 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003730 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003731 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3732 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003733 break;
3734 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003735 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003736 /*
3737 * Let's not give out any delegations till everyone's
3738 * had the chance to reclaim theirs....
3739 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003740 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003741 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003742 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003743 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003744 /*
3745 * Also, if the file was opened for write or
3746 * create, there's a good chance the client's
3747 * about to write to it, resulting in an
3748 * immediate recall (since we don't support
3749 * write delegations):
3750 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003751 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003752 goto out_no_deleg;
3753 if (open->op_create == NFS4_OPEN_CREATE)
3754 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003755 break;
3756 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003757 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003758 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003759 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003760 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003761 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003763 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003765 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003766 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003767 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003768 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003769 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003770out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003771 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3772 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003773 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003774 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003775 open->op_recall = 1;
3776 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003777
3778 /* 4.1 client asking for a delegation? */
3779 if (open->op_deleg_want)
3780 nfsd4_open_deleg_none_ext(open, status);
3781 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782}
3783
Benny Halevye27f49c2012-02-21 14:16:54 -08003784static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3785 struct nfs4_delegation *dp)
3786{
3787 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3788 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3789 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3790 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3791 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3792 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3793 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3794 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3795 }
3796 /* Otherwise the client must be confused wanting a delegation
3797 * it already has, therefore we don't return
3798 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3799 */
3800}
3801
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802/*
3803 * called with nfs4_lock_state() held.
3804 */
Al Virob37ad282006-10-19 23:28:59 -07003805__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3807{
Andy Adamson66689582009-04-03 08:28:45 +03003808 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003809 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003811 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003812 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003813 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 /*
3816 * Lookup file; if found, lookup stateid and check open request,
3817 * and check for delegations in the process of being recalled.
3818 * If not found, create the nfs4_file struct
3819 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003820 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04003821 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04003822 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003823 if (status)
3824 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003825 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04003827 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003828 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003829 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003830 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003831 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 }
3833
3834 /*
3835 * OPEN the file, or upgrade an existing OPEN.
3836 * If truncate fails, the OPEN fails.
3837 */
3838 if (stp) {
3839 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003840 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 if (status)
3842 goto out;
3843 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003844 stp = open->op_stp;
3845 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003846 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003847 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3848 if (status) {
3849 release_open_stateid(stp);
3850 goto out;
3851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003853 update_stateid(&stp->st_stid.sc_stateid);
3854 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855
Benny Halevyd24433c2012-02-16 20:57:17 +02003856 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02003857 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3858 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3859 open->op_why_no_deleg = WND4_NOT_WANTED;
3860 goto nodeleg;
3861 }
3862 }
3863
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 /*
3865 * Attempt to hand out a delegation. No error return, because the
3866 * OPEN succeeds even if we fail.
3867 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003868 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003869nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 status = nfs_ok;
3871
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003872 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003873 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003875 /* 4.1 client trying to upgrade/downgrade delegation? */
3876 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003877 open->op_deleg_want)
3878 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003879
NeilBrown13cd2182005-06-23 22:03:10 -07003880 if (fp)
3881 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003882 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003883 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 /*
3885 * To finish the open response, we just need to set the rflags.
3886 */
3887 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003888 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003889 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003891 if (dp)
3892 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003893 if (stp)
3894 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895
3896 return status;
3897}
3898
Jeff Layton58fb12e2014-07-29 21:34:27 -04003899void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
3900 struct nfsd4_open *open, __be32 status)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003901{
3902 if (open->op_openowner) {
3903 struct nfs4_openowner *oo = open->op_openowner;
3904
3905 if (!list_empty(&oo->oo_owner.so_stateids))
3906 list_del_init(&oo->oo_close_lru);
3907 if (oo->oo_flags & NFS4_OO_NEW) {
3908 if (status) {
3909 release_openowner(oo);
3910 open->op_openowner = NULL;
3911 } else
3912 oo->oo_flags &= ~NFS4_OO_NEW;
3913 }
Jeff Layton58fb12e2014-07-29 21:34:27 -04003914 if (open->op_openowner)
3915 nfsd4_cstate_assign_replay(cstate, &oo->oo_owner);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003916 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003917 if (open->op_file)
3918 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003919 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04003920 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003921}
3922
Al Virob37ad282006-10-19 23:28:59 -07003923__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003924nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3925 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926{
3927 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003928 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003929 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
3931 nfs4_lock_state();
3932 dprintk("process_renew(%08x/%08x): starting\n",
3933 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003934 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003935 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003937 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003939 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003940 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 goto out;
3942 status = nfs_ok;
3943out:
3944 nfs4_unlock_state();
3945 return status;
3946}
3947
NeilBrowna76b4312005-06-23 22:04:01 -07003948static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003949nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07003950{
Jeff Layton33dcc482012-04-10 11:08:48 -04003951 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003952 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04003953 return;
3954
NeilBrowna76b4312005-06-23 22:04:01 -07003955 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003956 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003957 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003958 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003959 /*
3960 * Now that every NFSv4 client has had the chance to recover and
3961 * to see the (possibly new, possibly shorter) lease time, we
3962 * can safely set the next grace time to the current lease time:
3963 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03003964 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003965}
3966
NeilBrownfd39ca92005-06-23 22:04:03 -07003967static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003968nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969{
3970 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003971 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 struct nfs4_delegation *dp;
3973 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003974 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003975 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976
3977 nfs4_lock_state();
3978
3979 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003980 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03003981 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003982 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03003983 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 clp = list_entry(pos, struct nfs4_client, cl_lru);
3985 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3986 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003987 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 break;
3989 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003990 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03003991 dprintk("NFSD: client in use (clientid %08x)\n",
3992 clp->cl_clientid.cl_id);
3993 continue;
3994 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003995 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003996 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003997 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03003998 list_for_each_safe(pos, next, &reaplist) {
3999 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 dprintk("NFSD: purging unused client (clientid %08x)\n",
4001 clp->cl_clientid.cl_id);
4002 expire_client(clp);
4003 }
Benny Halevycdc97502014-05-30 09:09:30 -04004004 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004005 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004007 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4008 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004010 t = dp->dl_time - cutoff;
4011 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 break;
4013 }
Jeff Layton42690672014-07-25 07:34:20 -04004014 unhash_delegation_locked(dp);
4015 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 }
Benny Halevycdc97502014-05-30 09:09:30 -04004017 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004018 while (!list_empty(&reaplist)) {
4019 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4020 dl_recall_lru);
4021 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004022 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004024 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004025 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
4026 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004027 t = oo->oo_time - cutoff;
4028 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 break;
4030 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004031 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04004033 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04004035 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036}
4037
Harvey Harrisona254b242008-02-20 12:49:00 -08004038static struct workqueue_struct *laundry_wq;
4039static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004040
4041static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004042laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043{
4044 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004045 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4046 work);
4047 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4048 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004050 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004052 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053}
4054
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004055static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004056{
Trond Myklebust11b91642014-07-29 21:34:08 -04004057 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004058 return nfserr_bad_stateid;
4059 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060}
4061
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004063access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004065 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4066 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4067 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068}
4069
4070static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004071access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004073 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4074 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075}
4076
4077static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004078__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079{
Al Virob37ad282006-10-19 23:28:59 -07004080 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081
J. Bruce Fields02921912010-07-29 15:16:59 -04004082 /* For lock stateid's, we test the parent open, not the lock: */
4083 if (stp->st_openstp)
4084 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004085 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004087 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 goto out;
4089 status = nfs_ok;
4090out:
4091 return status;
4092}
4093
Al Virob37ad282006-10-19 23:28:59 -07004094static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004095check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004097 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004099 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004100 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 * conflicting state; so we must wait out the grace period. */
4102 return nfserr_grace;
4103 } else if (flags & WR_STATE)
4104 return nfs4_share_conflict(current_fh,
4105 NFS4_SHARE_DENY_WRITE);
4106 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4107 return nfs4_share_conflict(current_fh,
4108 NFS4_SHARE_DENY_READ);
4109}
4110
4111/*
4112 * Allow READ/WRITE during grace period on recovered state only for files
4113 * that are not able to provide mandatory locking.
4114 */
4115static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004116grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004118 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119}
4120
J. Bruce Fields81b82962011-08-23 11:03:29 -04004121/* Returns true iff a is later than b: */
4122static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4123{
Jim Rees1a9357f2013-05-17 17:33:00 -04004124 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004125}
4126
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004127static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004128{
Andy Adamson66689582009-04-03 08:28:45 +03004129 /*
4130 * When sessions are used the stateid generation number is ignored
4131 * when it is zero.
4132 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004133 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004134 return nfs_ok;
4135
4136 if (in->si_generation == ref->si_generation)
4137 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004138
J. Bruce Fields0836f582008-01-26 19:08:12 -05004139 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004140 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004141 return nfserr_bad_stateid;
4142 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004143 * However, we could see a stateid from the past, even from a
4144 * non-buggy client. For example, if the client sends a lock
4145 * while some IO is outstanding, the lock may bump si_generation
4146 * while the IO is still in flight. The client could avoid that
4147 * situation by waiting for responses on all the IO requests,
4148 * but better performance may result in retrying IO that
4149 * receives an old_stateid error if requests are rarely
4150 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004151 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004152 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004153}
4154
Chuck Lever7df302f2012-05-29 13:56:37 -04004155static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004156{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004157 struct nfs4_stid *s;
4158 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004159 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004160
Chuck Lever7df302f2012-05-29 13:56:37 -04004161 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004162 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004163 /* Client debugging aid. */
4164 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4165 char addr_str[INET6_ADDRSTRLEN];
4166 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4167 sizeof(addr_str));
4168 pr_warn_ratelimited("NFSD: client %s testing state ID "
4169 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004170 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004171 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004172 spin_lock(&cl->cl_lock);
4173 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004174 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004175 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004176 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004177 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004178 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004179 switch (s->sc_type) {
4180 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004181 status = nfs_ok;
4182 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004183 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004184 status = nfserr_deleg_revoked;
4185 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004186 case NFS4_OPEN_STID:
4187 case NFS4_LOCK_STID:
4188 ols = openlockstateid(s);
4189 if (ols->st_stateowner->so_is_open_owner
4190 && !(openowner(ols->st_stateowner)->oo_flags
4191 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004192 status = nfserr_bad_stateid;
4193 else
4194 status = nfs_ok;
4195 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004196 default:
4197 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004198 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004199 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004200 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004201 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004202 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004203out_unlock:
4204 spin_unlock(&cl->cl_lock);
4205 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004206}
4207
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004208static __be32
4209nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4210 stateid_t *stateid, unsigned char typemask,
4211 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004212{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004213 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004214
4215 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4216 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004217 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004218 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004219 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004220 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004221 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004222 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004223 if (status)
4224 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004225 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004226 if (!*s)
4227 return nfserr_bad_stateid;
4228 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004229}
4230
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231/*
4232* Checks for stateid operations
4233*/
Al Virob37ad282006-10-19 23:28:59 -07004234__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004235nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004236 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004238 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004239 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004241 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004243 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004244 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004245 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 if (filpp)
4248 *filpp = NULL;
4249
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004250 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 return nfserr_grace;
4252
4253 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004254 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255
Trond Myklebust14bcab12014-04-18 14:44:07 -04004256 nfs4_lock_state();
4257
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004258 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004259 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004260 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004261 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004262 goto unlock_state;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004263 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4264 if (status)
4265 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004266 switch (s->sc_type) {
4267 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004268 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004269 status = nfs4_check_delegmode(dp, flags);
4270 if (status)
4271 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004272 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004273 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004274 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004275 WARN_ON_ONCE(1);
4276 status = nfserr_serverfault;
4277 goto out;
4278 }
Trond Myklebustde186432014-07-10 14:07:26 -04004279 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004280 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004281 break;
4282 case NFS4_OPEN_STID:
4283 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004284 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004285 status = nfs4_check_fh(current_fh, stp);
4286 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004288 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004289 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004291 status = nfs4_check_openmode(stp, flags);
4292 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004294 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004295 struct nfs4_file *fp = stp->st_stid.sc_file;
4296
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004297 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004298 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004299 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004300 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004301 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004302 break;
4303 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004304 status = nfserr_bad_stateid;
4305 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 }
4307 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004308 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004309 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004311 nfs4_put_stid(s);
4312unlock_state:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004313 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 return status;
4315}
4316
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004317static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004318nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004319{
J. Bruce Fieldsa1b8ff42014-05-20 15:55:21 -04004320 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4321
Trond Myklebust11b91642014-07-29 21:34:08 -04004322 if (check_for_locks(stp->st_stid.sc_file, lo))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004323 return nfserr_locks_held;
Jeff Laytonc53530d2014-06-30 11:48:39 -04004324 release_lockowner_if_empty(lo);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004325 return nfs_ok;
4326}
4327
4328/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004329 * Test if the stateid is valid
4330 */
4331__be32
4332nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4333 struct nfsd4_test_stateid *test_stateid)
4334{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004335 struct nfsd4_test_stateid_id *stateid;
4336 struct nfs4_client *cl = cstate->session->se_client;
4337
4338 nfs4_lock_state();
4339 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004340 stateid->ts_id_status =
4341 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004342 nfs4_unlock_state();
4343
Bryan Schumaker17456802011-07-13 10:50:48 -04004344 return nfs_ok;
4345}
4346
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004347__be32
4348nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4349 struct nfsd4_free_stateid *free_stateid)
4350{
4351 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004352 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004353 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004354 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004355 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004356
4357 nfs4_lock_state();
Jeff Layton1af71cc2014-07-29 21:34:14 -04004358 spin_lock(&cl->cl_lock);
4359 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004360 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004361 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004362 switch (s->sc_type) {
4363 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004364 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004365 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004366 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004367 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4368 if (ret)
4369 break;
4370 ret = nfserr_locks_held;
4371 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004372 case NFS4_LOCK_STID:
4373 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4374 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004375 break;
4376 spin_unlock(&cl->cl_lock);
4377 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4378 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004379 case NFS4_REVOKED_DELEG_STID:
4380 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004381 list_del_init(&dp->dl_recall_lru);
4382 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004383 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004384 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004385 goto out;
4386 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004387 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004388out_unlock:
4389 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004390out:
4391 nfs4_unlock_state();
4392 return ret;
4393}
4394
NeilBrown4c4cd222005-07-07 17:59:27 -07004395static inline int
4396setlkflg (int type)
4397{
4398 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4399 RD_STATE : WR_STATE;
4400}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004402static __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 -04004403{
4404 struct svc_fh *current_fh = &cstate->current_fh;
4405 struct nfs4_stateowner *sop = stp->st_stateowner;
4406 __be32 status;
4407
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004408 status = nfsd4_check_seqid(cstate, sop, seqid);
4409 if (status)
4410 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004411 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4412 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004413 /*
4414 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004415 * nfserr_replay_me from the previous step, and
4416 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004417 */
4418 return nfserr_bad_stateid;
4419 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4420 if (status)
4421 return status;
4422 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004423}
4424
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425/*
4426 * Checks for sequence id mutating operations.
4427 */
Al Virob37ad282006-10-19 23:28:59 -07004428static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004429nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004430 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004431 struct nfs4_ol_stateid **stpp,
4432 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004434 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004435 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004436 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004438 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4439 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004440
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004442 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004443 if (status)
4444 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004445 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004446 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004448 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004449 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004450 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004451 else
4452 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004453 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004454}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004455
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004456static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4457 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004458{
4459 __be32 status;
4460 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004461 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004463 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004464 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004465 if (status)
4466 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004467 oo = openowner(stp->st_stateowner);
4468 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4469 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004470 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004471 }
4472 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004473 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474}
4475
Al Virob37ad282006-10-19 23:28:59 -07004476__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004477nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004478 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479{
Al Virob37ad282006-10-19 23:28:59 -07004480 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004481 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004482 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004483 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484
Al Viroa6a9f182013-09-16 10:57:01 -04004485 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4486 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004488 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004489 if (status)
4490 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491
4492 nfs4_lock_state();
4493
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004494 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004495 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004496 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004497 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004498 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004499 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004500 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004501 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004502 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004503 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004504 update_stateid(&stp->st_stid.sc_stateid);
4505 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004506 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004507 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004508
Jeff Layton2a4317c2012-03-21 16:42:43 -04004509 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004510 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004511put_stateid:
4512 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004514 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004515 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 return status;
4517}
4518
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004519static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004521 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004522 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004523 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004524 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004525}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004526
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004527static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4528{
4529 switch (to_access) {
4530 case NFS4_SHARE_ACCESS_READ:
4531 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4532 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4533 break;
4534 case NFS4_SHARE_ACCESS_WRITE:
4535 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4536 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4537 break;
4538 case NFS4_SHARE_ACCESS_BOTH:
4539 break;
4540 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004541 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 }
4543}
4544
Al Virob37ad282006-10-19 23:28:59 -07004545__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004546nfsd4_open_downgrade(struct svc_rqst *rqstp,
4547 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004548 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549{
Al Virob37ad282006-10-19 23:28:59 -07004550 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004551 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004552 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553
Al Viroa6a9f182013-09-16 10:57:01 -04004554 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4555 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004557 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004558 if (od->od_deleg_want)
4559 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4560 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561
4562 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004563 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004564 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004565 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004568 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004569 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004571 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004573 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004574 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004576 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004578 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579
Jeff Laytonce0fc432012-05-11 09:45:14 -04004580 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004582 update_stateid(&stp->st_stid.sc_stateid);
4583 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004585put_stateid:
4586 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004588 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004589 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 return status;
4591}
4592
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004593static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4594{
Trond Myklebustacf92952014-06-30 11:48:37 -04004595 struct nfs4_client *clp = s->st_stid.sc_client;
4596 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4597
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004598 s->st_stid.sc_type = NFS4_CLOSED_STID;
Trond Myklebustacf92952014-06-30 11:48:37 -04004599 unhash_open_stateid(s);
4600
4601 if (clp->cl_minorversion) {
Trond Myklebustacf92952014-06-30 11:48:37 -04004602 if (list_empty(&oo->oo_owner.so_stateids))
4603 release_openowner(oo);
Trond Myklebust60116952014-07-29 21:34:06 -04004604 nfs4_put_stid(&s->st_stid);
Trond Myklebustacf92952014-06-30 11:48:37 -04004605 } else {
Trond Myklebust60116952014-07-29 21:34:06 -04004606 /*
4607 * In the 4.0 case we need to keep the owners around a
4608 * little while to handle CLOSE replay. We still do need
4609 * to release any file access that is held by them
4610 * before returning however.
4611 */
4612 release_all_access(s);
Trond Myklebust11b91642014-07-29 21:34:08 -04004613 if (s->st_stid.sc_file) {
4614 put_nfs4_file(s->st_stid.sc_file);
4615 s->st_stid.sc_file = NULL;
Trond Myklebustf8338832014-07-25 07:34:19 -04004616 }
Trond Myklebustacf92952014-06-30 11:48:37 -04004617 oo->oo_last_closed_stid = s;
Trond Myklebustacf92952014-06-30 11:48:37 -04004618 if (list_empty(&oo->oo_owner.so_stateids))
4619 move_to_close_lru(oo, clp->net);
4620 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004621}
4622
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623/*
4624 * nfs4_unlock_state() called after encode
4625 */
Al Virob37ad282006-10-19 23:28:59 -07004626__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004627nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004628 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629{
Al Virob37ad282006-10-19 23:28:59 -07004630 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004631 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004632 struct net *net = SVC_NET(rqstp);
4633 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634
Al Viroa6a9f182013-09-16 10:57:01 -04004635 dprintk("NFSD: nfsd4_close on file %pd\n",
4636 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637
4638 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004639 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4640 &close->cl_stateid,
4641 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004642 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004643 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004644 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004646 update_stateid(&stp->st_stid.sc_stateid);
4647 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004649 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004650
4651 /* put reference from nfs4_preprocess_seqid_op */
4652 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653out:
Jeff Layton58fb12e2014-07-29 21:34:27 -04004654 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 return status;
4656}
4657
Al Virob37ad282006-10-19 23:28:59 -07004658__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004659nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4660 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004662 struct nfs4_delegation *dp;
4663 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004664 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004665 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004666 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004668 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004669 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670
4671 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004672 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004673 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004674 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004675 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004676 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004677 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004678 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004679
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004680 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004681put_stateid:
4682 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004684 nfs4_unlock_state();
4685
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 return status;
4687}
4688
4689
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691
Benny Halevy87df4de2008-12-15 19:42:03 +02004692static inline u64
4693end_offset(u64 start, u64 len)
4694{
4695 u64 end;
4696
4697 end = start + len;
4698 return end >= start ? end: NFS4_MAX_UINT64;
4699}
4700
4701/* last octet in a range */
4702static inline u64
4703last_byte_offset(u64 start, u64 len)
4704{
4705 u64 end;
4706
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004707 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004708 end = start + len;
4709 return end > start ? end - 1: NFS4_MAX_UINT64;
4710}
4711
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712/*
4713 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4714 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4715 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4716 * locking, this prevents us from being completely protocol-compliant. The
4717 * real solution to this problem is to start using unsigned file offsets in
4718 * the VFS, but this is a very deep change!
4719 */
4720static inline void
4721nfs4_transform_lock_offset(struct file_lock *lock)
4722{
4723 if (lock->fl_start < 0)
4724 lock->fl_start = OFFSET_MAX;
4725 if (lock->fl_end < 0)
4726 lock->fl_end = OFFSET_MAX;
4727}
4728
NeilBrownd5b90262006-04-10 22:55:22 -07004729/* Hack!: For now, we're defining this just so we can use a pointer to it
4730 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004731static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004732};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733
4734static inline void
4735nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4736{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004737 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738
NeilBrownd5b90262006-04-10 22:55:22 -07004739 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004740 lo = (struct nfs4_lockowner *) fl->fl_owner;
4741 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4742 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004743 if (!deny->ld_owner.data)
4744 /* We just don't care that much */
4745 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004746 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4747 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004748 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004749nevermind:
4750 deny->ld_owner.len = 0;
4751 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004752 deny->ld_clientid.cl_boot = 0;
4753 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 }
4755 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004756 deny->ld_length = NFS4_MAX_UINT64;
4757 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4759 deny->ld_type = NFS4_READ_LT;
4760 if (fl->fl_type != F_RDLCK)
4761 deny->ld_type = NFS4_WRITE_LT;
4762}
4763
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004764static struct nfs4_lockowner *
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004765find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4766 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767{
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004768 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4769 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004771 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4772 if (so->so_is_open_owner)
4773 continue;
4774 if (!same_owner_str(so, owner, clid))
4775 continue;
Jeff Layton5db1c032014-07-29 21:34:28 -04004776 atomic_inc(&so->so_count);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004777 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 }
4779 return NULL;
4780}
4781
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004782static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4783{
4784 unhash_lockowner(lockowner(sop));
4785}
4786
Jeff Layton6b180f02014-07-29 21:34:26 -04004787static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4788{
4789 struct nfs4_lockowner *lo = lockowner(sop);
4790
4791 kmem_cache_free(lockowner_slab, lo);
4792}
4793
4794static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004795 .so_unhash = nfs4_unhash_lockowner,
4796 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004797};
4798
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799/*
4800 * Alloc a lock owner structure.
4801 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004802 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004804 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004806static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004807alloc_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 -04004808 struct nfs4_lockowner *lo;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004809 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004811 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4812 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004814 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4815 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04004816 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04004817 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004818 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004819 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820}
4821
Jeff Layton356a95e2014-07-29 21:34:13 -04004822static void
4823init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
4824 struct nfs4_file *fp, struct inode *inode,
4825 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004827 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828
Jeff Layton356a95e2014-07-29 21:34:13 -04004829 lockdep_assert_held(&clp->cl_lock);
4830
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004831 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004832 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004833 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07004834 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004835 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04004836 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004837 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004839 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004840 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04004841 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004842 spin_lock(&fp->fi_lock);
4843 list_add(&stp->st_perfile, &fp->fi_stateids);
4844 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845}
4846
Jeff Laytonc53530d2014-06-30 11:48:39 -04004847static struct nfs4_ol_stateid *
4848find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4849{
4850 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04004851 struct nfs4_client *clp = lo->lo_owner.so_client;
4852
4853 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004854
4855 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004856 if (lst->st_stid.sc_file == fp) {
4857 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004858 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004859 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004860 }
4861 return NULL;
4862}
4863
Jeff Layton356a95e2014-07-29 21:34:13 -04004864static struct nfs4_ol_stateid *
4865find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
4866 struct inode *inode, struct nfs4_ol_stateid *ost,
4867 bool *new)
4868{
4869 struct nfs4_stid *ns = NULL;
4870 struct nfs4_ol_stateid *lst;
4871 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4872 struct nfs4_client *clp = oo->oo_owner.so_client;
4873
4874 spin_lock(&clp->cl_lock);
4875 lst = find_lock_stateid(lo, fi);
4876 if (lst == NULL) {
4877 spin_unlock(&clp->cl_lock);
4878 ns = nfs4_alloc_stid(clp, stateid_slab);
4879 if (ns == NULL)
4880 return NULL;
4881
4882 spin_lock(&clp->cl_lock);
4883 lst = find_lock_stateid(lo, fi);
4884 if (likely(!lst)) {
4885 lst = openlockstateid(ns);
4886 init_lock_stateid(lst, lo, fi, inode, ost);
4887 ns = NULL;
4888 *new = true;
4889 }
4890 }
4891 spin_unlock(&clp->cl_lock);
4892 if (ns)
4893 nfs4_put_stid(ns);
4894 return lst;
4895}
Jeff Laytonc53530d2014-06-30 11:48:39 -04004896
NeilBrownfd39ca92005-06-23 22:04:03 -07004897static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898check_lock_length(u64 offset, u64 length)
4899{
Benny Halevy87df4de2008-12-15 19:42:03 +02004900 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 LOFF_OVERFLOW(offset, length)));
4902}
4903
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004904static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004905{
Trond Myklebust11b91642014-07-29 21:34:08 -04004906 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004907
Jeff Layton7214e862014-07-10 14:07:33 -04004908 lockdep_assert_held(&fp->fi_lock);
4909
Jeff Layton82c5ff12012-05-11 09:45:13 -04004910 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004911 return;
Jeff Layton12659652014-07-10 14:07:28 -04004912 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004913 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004914}
4915
Jeff Layton356a95e2014-07-29 21:34:13 -04004916static __be32
4917lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
4918 struct nfs4_ol_stateid *ost,
4919 struct nfsd4_lock *lock,
4920 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004921{
Jeff Layton5db1c032014-07-29 21:34:28 -04004922 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04004923 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004924 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4925 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004926 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004927 struct nfs4_lockowner *lo;
4928 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004929 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004930
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004931 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004932 if (!lo) {
4933 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4934 &lock->v.new.owner);
4935 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4936 if (lo == NULL)
4937 return nfserr_jukebox;
Jeff Layton5db1c032014-07-29 21:34:28 -04004938 /* FIXME: extra reference for new lockowners for the client */
4939 atomic_inc(&lo->lo_owner.so_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004940 } else {
4941 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04004942 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04004943 if (!cstate->minorversion &&
4944 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04004945 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004946 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004947
Jeff Layton356a95e2014-07-29 21:34:13 -04004948 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004949 if (*lst == NULL) {
Jeff Layton356a95e2014-07-29 21:34:13 -04004950 release_lockowner_if_empty(lo);
Jeff Layton5db1c032014-07-29 21:34:28 -04004951 status = nfserr_jukebox;
4952 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004953 }
Jeff Layton5db1c032014-07-29 21:34:28 -04004954 status = nfs_ok;
4955out:
4956 nfs4_put_stateowner(&lo->lo_owner);
4957 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004958}
4959
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960/*
4961 * LOCK operation
4962 */
Al Virob37ad282006-10-19 23:28:59 -07004963__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004964nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004965 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004967 struct nfs4_openowner *open_sop = NULL;
4968 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004969 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004970 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04004971 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004972 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004973 struct file_lock *file_lock = NULL;
4974 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004975 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004976 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004977 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04004978 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004979 struct net *net = SVC_NET(rqstp);
4980 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981
4982 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4983 (long long) lock->lk_offset,
4984 (long long) lock->lk_length);
4985
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 if (check_lock_length(lock->lk_offset, lock->lk_length))
4987 return nfserr_inval;
4988
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004989 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004990 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004991 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4992 return status;
4993 }
4994
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 nfs4_lock_state();
4996
4997 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04004998 if (nfsd4_has_session(cstate))
4999 /* See rfc 5661 18.10.3: given clientid is ignored: */
5000 memcpy(&lock->v.new.clientid,
5001 &cstate->session->se_client->cl_clientid,
5002 sizeof(clientid_t));
5003
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005005 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005009 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 lock->lk_new_open_seqid,
5011 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005012 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005013 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005015 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005016 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005017 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005018 &lock->v.new.clientid))
5019 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005020 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005021 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005022 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005023 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005024 lock->lk_old_lock_seqid,
5025 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005026 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005027 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005028 if (status)
5029 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005030 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005032 lkflg = setlkflg(lock->lk_type);
5033 status = nfs4_check_openmode(lock_stp, lkflg);
5034 if (status)
5035 goto out;
5036
NeilBrown0dd395d2005-07-07 17:59:15 -07005037 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005038 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005039 goto out;
5040 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005041 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005042 goto out;
5043
Jeff Layton21179d82012-08-21 08:03:32 -04005044 file_lock = locks_alloc_lock();
5045 if (!file_lock) {
5046 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5047 status = nfserr_jukebox;
5048 goto out;
5049 }
5050
Trond Myklebust11b91642014-07-29 21:34:08 -04005051 fp = lock_stp->st_stid.sc_file;
Jeff Layton21179d82012-08-21 08:03:32 -04005052 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 switch (lock->lk_type) {
5054 case NFS4_READ_LT:
5055 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005056 spin_lock(&fp->fi_lock);
5057 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005058 if (filp)
5059 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005060 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005061 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005062 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 case NFS4_WRITE_LT:
5064 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005065 spin_lock(&fp->fi_lock);
5066 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005067 if (filp)
5068 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005069 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005070 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005071 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 default:
5073 status = nfserr_inval;
5074 goto out;
5075 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005076 if (!filp) {
5077 status = nfserr_openmode;
5078 goto out;
5079 }
Jeff Layton21179d82012-08-21 08:03:32 -04005080 file_lock->fl_owner = (fl_owner_t)lock_sop;
5081 file_lock->fl_pid = current->tgid;
5082 file_lock->fl_file = filp;
5083 file_lock->fl_flags = FL_POSIX;
5084 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5085 file_lock->fl_start = lock->lk_offset;
5086 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5087 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088
Jeff Layton21179d82012-08-21 08:03:32 -04005089 conflock = locks_alloc_lock();
5090 if (!conflock) {
5091 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5092 status = nfserr_jukebox;
5093 goto out;
5094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
Jeff Layton21179d82012-08-21 08:03:32 -04005096 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005097 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005099 update_stateid(&lock_stp->st_stid.sc_stateid);
5100 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005102 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005103 break;
5104 case (EAGAIN): /* conflock holds conflicting lock */
5105 status = nfserr_denied;
5106 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005107 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005108 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 case (EDEADLK):
5110 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005111 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005112 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005113 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005114 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005115 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117out:
Trond Myklebustde186432014-07-10 14:07:26 -04005118 if (filp)
5119 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005120 if (lock_stp) {
5121 /* Bump seqid manually if the 4.0 replay owner is openowner */
5122 if (cstate->replay_owner &&
5123 cstate->replay_owner != &lock_sop->lo_owner &&
5124 seqid_mutating_err(ntohl(status)))
5125 lock_sop->lo_owner.so_seqid++;
5126
5127 /*
5128 * If this is a new, never-before-used stateid, and we are
5129 * returning an error, then just go ahead and release it.
5130 */
5131 if (status && new)
5132 release_lock_stateid(lock_stp);
5133
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005134 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005135 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005136 if (open_stp)
5137 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005138 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005139 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005140 if (file_lock)
5141 locks_free_lock(file_lock);
5142 if (conflock)
5143 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 return status;
5145}
5146
5147/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005148 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5149 * so we do a temporary open here just to get an open file to pass to
5150 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5151 * inode operation.)
5152 */
Al Viro04da6e92012-04-13 00:00:04 -04005153static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005154{
5155 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005156 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5157 if (!err) {
5158 err = nfserrno(vfs_test_lock(file, lock));
5159 nfsd_close(file);
5160 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005161 return err;
5162}
5163
5164/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 * LOCKT operation
5166 */
Al Virob37ad282006-10-19 23:28:59 -07005167__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005168nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5169 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170{
Jeff Layton21179d82012-08-21 08:03:32 -04005171 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005172 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005173 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005174 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005176 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 return nfserr_grace;
5178
5179 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5180 return nfserr_inval;
5181
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 nfs4_lock_state();
5183
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005184 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005185 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005186 if (status)
5187 goto out;
5188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005190 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192
Jeff Layton21179d82012-08-21 08:03:32 -04005193 file_lock = locks_alloc_lock();
5194 if (!file_lock) {
5195 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5196 status = nfserr_jukebox;
5197 goto out;
5198 }
5199 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 switch (lockt->lt_type) {
5201 case NFS4_READ_LT:
5202 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005203 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 break;
5205 case NFS4_WRITE_LT:
5206 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005207 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 break;
5209 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005210 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 status = nfserr_inval;
5212 goto out;
5213 }
5214
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005215 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005216 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005217 file_lock->fl_owner = (fl_owner_t)lo;
5218 file_lock->fl_pid = current->tgid;
5219 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220
Jeff Layton21179d82012-08-21 08:03:32 -04005221 file_lock->fl_start = lockt->lt_offset;
5222 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223
Jeff Layton21179d82012-08-21 08:03:32 -04005224 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225
Jeff Layton21179d82012-08-21 08:03:32 -04005226 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005227 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005228 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005229
Jeff Layton21179d82012-08-21 08:03:32 -04005230 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005232 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 }
5234out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005235 if (lo)
5236 nfs4_put_stateowner(&lo->lo_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005238 if (file_lock)
5239 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 return status;
5241}
5242
Al Virob37ad282006-10-19 23:28:59 -07005243__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005244nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005245 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005247 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005249 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005250 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005251 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005252 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5253
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5255 (long long) locku->lu_offset,
5256 (long long) locku->lu_length);
5257
5258 if (check_lock_length(locku->lu_offset, locku->lu_length))
5259 return nfserr_inval;
5260
5261 nfs4_lock_state();
5262
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005263 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005264 &locku->lu_stateid, NFS4_LOCK_STID,
5265 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005266 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005268 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005269 if (!filp) {
5270 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005271 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005272 }
Jeff Layton21179d82012-08-21 08:03:32 -04005273 file_lock = locks_alloc_lock();
5274 if (!file_lock) {
5275 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5276 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005277 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005278 }
5279 locks_init_lock(file_lock);
5280 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005281 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005282 file_lock->fl_pid = current->tgid;
5283 file_lock->fl_file = filp;
5284 file_lock->fl_flags = FL_POSIX;
5285 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5286 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287
Jeff Layton21179d82012-08-21 08:03:32 -04005288 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5289 locku->lu_length);
5290 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291
Jeff Layton21179d82012-08-21 08:03:32 -04005292 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005293 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005294 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 goto out_nfserr;
5296 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005297 update_stateid(&stp->st_stid.sc_stateid);
5298 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005299fput:
5300 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005301put_stateid:
5302 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005304 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005305 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005306 if (file_lock)
5307 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 return status;
5309
5310out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005311 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005312 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313}
5314
5315/*
5316 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005317 * true: locks held by lockowner
5318 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005320static bool
5321check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322{
5323 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005324 int status = false;
5325 struct file *filp = find_any_file(fp);
5326 struct inode *inode;
5327
5328 if (!filp) {
5329 /* Any valid lock stateid should have some sort of access */
5330 WARN_ON_ONCE(1);
5331 return status;
5332 }
5333
5334 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335
Jeff Layton1c8c6012013-06-21 08:58:15 -04005336 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005338 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005339 status = true;
5340 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005343 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005344 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 return status;
5346}
5347
Al Virob37ad282006-10-19 23:28:59 -07005348__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005349nfsd4_release_lockowner(struct svc_rqst *rqstp,
5350 struct nfsd4_compound_state *cstate,
5351 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352{
5353 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Laytonfd449072014-06-30 11:48:41 -04005354 struct nfs4_stateowner *sop = NULL, *tmp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005355 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005356 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 struct xdr_netobj *owner = &rlockowner->rl_owner;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005358 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07005359 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005360 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361
5362 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5363 clid->cl_boot, clid->cl_id);
5364
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 nfs4_lock_state();
5366
Jeff Layton4b24ca72014-06-30 11:48:44 -04005367 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005368 if (status)
5369 goto out;
5370
NeilBrown3e9e3db2005-06-23 22:04:20 -07005371 status = nfserr_locks_held;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05005372
Jeff Laytonfd449072014-06-30 11:48:41 -04005373 /* Find the matching lock stateowner */
5374 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
5375 if (tmp->so_is_open_owner)
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005376 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005377 if (same_owner_str(tmp, owner, clid)) {
5378 sop = tmp;
5379 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07005381 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005382
5383 /* No matching owner found, maybe a replay? Just declare victory... */
5384 if (!sop) {
5385 status = nfs_ok;
5386 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005388
5389 lo = lockowner(sop);
5390 /* see if there are still any locks associated with it */
5391 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
Trond Myklebust11b91642014-07-29 21:34:08 -04005392 if (check_for_locks(stp->st_stid.sc_file, lo))
Jeff Laytonfd449072014-06-30 11:48:41 -04005393 goto out;
5394 }
5395
5396 status = nfs_ok;
5397 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398out:
5399 nfs4_unlock_state();
5400 return status;
5401}
5402
5403static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005404alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405{
NeilBrowna55370a2005-06-23 22:03:52 -07005406 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407}
5408
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005409bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005410nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005411{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005412 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005413
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005414 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005415 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005416}
5417
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418/*
5419 * failure => all reset bets are off, nfserr_no_grace...
5420 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005421struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005422nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423{
5424 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005425 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426
NeilBrowna55370a2005-06-23 22:03:52 -07005427 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5428 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005429 if (crp) {
5430 strhashval = clientstr_hashval(name);
5431 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005432 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005433 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005434 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005435 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005436 }
5437 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438}
5439
Jeff Layton2a4317c2012-03-21 16:42:43 -04005440void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005441nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005442{
5443 list_del(&crp->cr_strhash);
5444 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005445 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005446}
5447
5448void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005449nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450{
5451 struct nfs4_client_reclaim *crp = NULL;
5452 int i;
5453
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005455 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5456 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005458 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 }
5460 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005461 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462}
5463
5464/*
5465 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005466struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005467nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468{
5469 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 struct nfs4_client_reclaim *crp = NULL;
5471
Jeff Layton278c9312012-11-12 15:00:52 -05005472 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473
Jeff Layton278c9312012-11-12 15:00:52 -05005474 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005475 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005476 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 return crp;
5478 }
5479 }
5480 return NULL;
5481}
5482
5483/*
5484* Called from OPEN. Look for clientid in reclaim list.
5485*/
Al Virob37ad282006-10-19 23:28:59 -07005486__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005487nfs4_check_open_reclaim(clientid_t *clid,
5488 struct nfsd4_compound_state *cstate,
5489 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005491 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005492
5493 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005494 status = lookup_clientid(clid, cstate, nn);
5495 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005496 return nfserr_reclaim_bad;
5497
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005498 if (nfsd4_client_record_check(cstate->clp))
5499 return nfserr_reclaim_bad;
5500
5501 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502}
5503
Bryan Schumaker65178db2011-11-01 13:35:21 -04005504#ifdef CONFIG_NFSD_FAULT_INJECTION
5505
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005506u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5507{
J. Bruce Fields221a6872013-04-01 22:23:49 -04005508 if (mark_client_expired(clp))
5509 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005510 expire_client(clp);
5511 return 1;
5512}
5513
Bryan Schumaker184c1842012-11-29 11:40:44 -05005514u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5515{
5516 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005517 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005518 printk(KERN_INFO "NFS Client: %s\n", buf);
5519 return 1;
5520}
5521
5522static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5523 const char *type)
5524{
5525 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005526 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005527 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5528}
5529
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005530static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5531 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005532{
5533 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005534 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005535 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005536 u64 count = 0;
5537
5538 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005539 list_for_each_entry_safe(stp, st_next,
5540 &oop->oo_owner.so_stateids, st_perstateowner) {
5541 list_for_each_entry_safe(lst, lst_next,
5542 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005543 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005544 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005545 if (++count == max)
5546 return count;
5547 }
5548 }
5549 }
5550
5551 return count;
5552}
5553
5554u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5555{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005556 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005557}
5558
Bryan Schumaker184c1842012-11-29 11:40:44 -05005559u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5560{
5561 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5562 nfsd_print_count(clp, count, "locked files");
5563 return count;
5564}
5565
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005566static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5567{
5568 struct nfs4_openowner *oop, *next;
5569 u64 count = 0;
5570
5571 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5572 if (func)
5573 func(oop);
5574 if (++count == max)
5575 break;
5576 }
5577
5578 return count;
5579}
5580
5581u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5582{
5583 return nfsd_foreach_client_open(clp, max, release_openowner);
5584}
5585
Bryan Schumaker184c1842012-11-29 11:40:44 -05005586u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5587{
5588 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5589 nfsd_print_count(clp, count, "open files");
5590 return count;
5591}
5592
Bryan Schumaker269de302012-11-29 11:40:42 -05005593static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5594 struct list_head *victims)
5595{
5596 struct nfs4_delegation *dp, *next;
5597 u64 count = 0;
5598
Benny Halevycdc97502014-05-30 09:09:30 -04005599 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005600 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005601 if (victims) {
5602 /*
5603 * It's not safe to mess with delegations that have a
5604 * non-zero dl_time. They might have already been broken
5605 * and could be processed by the laundromat outside of
5606 * the state_lock. Just leave them be.
5607 */
5608 if (dp->dl_time != 0)
5609 continue;
5610
Jeff Layton42690672014-07-25 07:34:20 -04005611 unhash_delegation_locked(dp);
5612 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005613 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005614 if (++count == max)
5615 break;
5616 }
5617 return count;
5618}
5619
5620u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5621{
5622 struct nfs4_delegation *dp, *next;
5623 LIST_HEAD(victims);
5624 u64 count;
5625
Benny Halevycdc97502014-05-30 09:09:30 -04005626 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005627 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005628 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005629
Jeff Layton2d4a5322014-07-25 07:34:21 -04005630 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5631 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005632 revoke_delegation(dp);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005633 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005634
5635 return count;
5636}
5637
5638u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5639{
Jeff Laytondff13992014-07-08 14:02:49 -04005640 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005641 LIST_HEAD(victims);
5642 u64 count;
5643
Benny Halevycdc97502014-05-30 09:09:30 -04005644 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005645 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005646 while (!list_empty(&victims)) {
5647 dp = list_first_entry(&victims, struct nfs4_delegation,
5648 dl_recall_lru);
5649 list_del_init(&dp->dl_recall_lru);
5650 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005651 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005652 }
Benny Halevycdc97502014-05-30 09:09:30 -04005653 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005654
5655 return count;
5656}
5657
Bryan Schumaker184c1842012-11-29 11:40:44 -05005658u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5659{
5660 u64 count = 0;
5661
Benny Halevycdc97502014-05-30 09:09:30 -04005662 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005663 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005664 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005665
5666 nfsd_print_count(clp, count, "delegations");
5667 return count;
5668}
5669
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005670u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005671{
5672 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005673 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005674 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005675
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005676 if (!nfsd_netns_ready(nn))
5677 return 0;
5678
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005679 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005680 count += func(clp, max - count);
5681 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005682 break;
5683 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005684
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005685 return count;
5686}
5687
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005688struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5689{
5690 struct nfs4_client *clp;
5691 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5692
5693 if (!nfsd_netns_ready(nn))
5694 return NULL;
5695
5696 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5697 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5698 return clp;
5699 }
5700 return NULL;
5701}
5702
Bryan Schumaker65178db2011-11-01 13:35:21 -04005703#endif /* CONFIG_NFSD_FAULT_INJECTION */
5704
Meelap Shahc2f1a552007-07-17 04:04:39 -07005705/*
5706 * Since the lifetime of a delegation isn't limited to that of an open, a
5707 * client may quite reasonably hang on to a delegation as long as it has
5708 * the inode cached. This becomes an obvious problem the first time a
5709 * client's inode cache approaches the size of the server's total memory.
5710 *
5711 * For now we avoid this problem by imposing a hard limit on the number
5712 * of delegations, which varies according to the server's memory size.
5713 */
5714static void
5715set_max_delegations(void)
5716{
5717 /*
5718 * Allow at most 4 delegations per megabyte of RAM. Quick
5719 * estimates suggest that in the worst case (where every delegation
5720 * is for a different inode), a delegation could take about 1.5K,
5721 * giving a worst case usage of about 6% of memory.
5722 */
5723 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5724}
5725
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005726static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005727{
5728 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5729 int i;
5730
5731 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5732 CLIENT_HASH_SIZE, GFP_KERNEL);
5733 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005734 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005735 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5736 CLIENT_HASH_SIZE, GFP_KERNEL);
5737 if (!nn->unconf_id_hashtbl)
5738 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005739 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5740 OWNER_HASH_SIZE, GFP_KERNEL);
5741 if (!nn->ownerstr_hashtbl)
5742 goto err_ownerstr;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005743 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5744 SESSION_HASH_SIZE, GFP_KERNEL);
5745 if (!nn->sessionid_hashtbl)
5746 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005747
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005748 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005749 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005750 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005751 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005752 for (i = 0; i < OWNER_HASH_SIZE; i++)
5753 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005754 for (i = 0; i < SESSION_HASH_SIZE; i++)
5755 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005756 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005757 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005758 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005759 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005760 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005761 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005762
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005763 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005764 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005765
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005766 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005767
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005768err_sessionid:
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03005769 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005770err_ownerstr:
5771 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005772err_unconf_id:
5773 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005774err:
5775 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005776}
5777
5778static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005779nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005780{
5781 int i;
5782 struct nfs4_client *clp = NULL;
5783 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5784
5785 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5786 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5787 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5788 destroy_client(clp);
5789 }
5790 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005791
Kinglong Mee2b905632014-03-26 22:09:30 +08005792 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5793 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5794 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5795 destroy_client(clp);
5796 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005797 }
5798
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005799 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005800 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005801 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005802 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005803 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005804}
5805
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005806int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005807nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005808{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005809 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005810 int ret;
5811
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005812 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005813 if (ret)
5814 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005815 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005816 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005817 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005818 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005819 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005820 nn->nfsd4_grace, net);
5821 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005822 return 0;
5823}
5824
5825/* initialization to perform when the nfsd service is started: */
5826
5827int
5828nfs4_state_start(void)
5829{
5830 int ret;
5831
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005832 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005833 if (ret)
5834 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005835 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005836 if (laundry_wq == NULL) {
5837 ret = -ENOMEM;
5838 goto out_recovery;
5839 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005840 ret = nfsd4_create_callback_queue();
5841 if (ret)
5842 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005843
Meelap Shahc2f1a552007-07-17 04:04:39 -07005844 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005845
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005846 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005847
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005848out_free_laundry:
5849 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005850out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005851 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852}
5853
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005854void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005855nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005859 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005861 cancel_delayed_work_sync(&nn->laundromat_work);
5862 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005863
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005864 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04005866 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005867 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005869 unhash_delegation_locked(dp);
5870 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871 }
Benny Halevycdc97502014-05-30 09:09:30 -04005872 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 list_for_each_safe(pos, next, &reaplist) {
5874 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005875 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04005876 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 }
5878
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005879 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005880 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005881 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882}
5883
5884void
5885nfs4_state_shutdown(void)
5886{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005887 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005888 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005890
5891static void
5892get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5893{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005894 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5895 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005896}
5897
5898static void
5899put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5900{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005901 if (cstate->minorversion) {
5902 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5903 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5904 }
5905}
5906
5907void
5908clear_current_stateid(struct nfsd4_compound_state *cstate)
5909{
5910 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005911}
5912
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005913/*
5914 * functions to set current state id
5915 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005916void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005917nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5918{
5919 put_stateid(cstate, &odp->od_stateid);
5920}
5921
5922void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005923nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5924{
5925 put_stateid(cstate, &open->op_stateid);
5926}
5927
5928void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005929nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5930{
5931 put_stateid(cstate, &close->cl_stateid);
5932}
5933
5934void
5935nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5936{
5937 put_stateid(cstate, &lock->lk_resp_stateid);
5938}
5939
5940/*
5941 * functions to consume current state id
5942 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005943
5944void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005945nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5946{
5947 get_stateid(cstate, &odp->od_stateid);
5948}
5949
5950void
5951nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5952{
5953 get_stateid(cstate, &drp->dr_stateid);
5954}
5955
5956void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005957nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5958{
5959 get_stateid(cstate, &fsp->fr_stateid);
5960}
5961
5962void
5963nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5964{
5965 get_stateid(cstate, &setattr->sa_stateid);
5966}
5967
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005968void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005969nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5970{
5971 get_stateid(cstate, &close->cl_stateid);
5972}
5973
5974void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005975nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005976{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005977 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005978}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01005979
5980void
5981nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5982{
5983 get_stateid(cstate, &read->rd_stateid);
5984}
5985
5986void
5987nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5988{
5989 get_stateid(cstate, &write->wr_stateid);
5990}