blob: 60ab22b6e09938b2206e7a240943f9ba62c7c77b [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{
470 struct idr *stateids = &cl->cl_stateids;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500471 struct nfs4_stid *stid;
472 int new_id;
473
Trond Myklebustf8338832014-07-25 07:34:19 -0400474 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500475 if (!stid)
476 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400477
Jeff Layton398c33a2013-04-29 16:21:20 -0700478 new_id = idr_alloc_cyclic(stateids, stid, 0, 0, GFP_KERNEL);
Tejun Heoebd6c702013-03-13 14:59:37 -0700479 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500480 goto out_free;
481 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500482 stid->sc_stateid.si_opaque.so_id = new_id;
483 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
484 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400485 atomic_set(&stid->sc_count, 1);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500486
J. Bruce Fields996e0932011-10-17 11:14:48 -0400487 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500488 * It shouldn't be a problem to reuse an opaque stateid value.
489 * I don't think it is for 4.1. But with 4.0 I worry that, for
490 * example, a stray write retransmission could be accepted by
491 * the server when it should have been rejected. Therefore,
492 * adopt a trick from the sctp code to attempt to maximize the
493 * amount of time until an id is reused, by ensuring they always
494 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400495 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500496 return stid;
497out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800498 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500499 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400500}
501
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400502static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
503{
Trond Myklebust60116952014-07-29 21:34:06 -0400504 struct nfs4_stid *stid;
505 struct nfs4_ol_stateid *stp;
506
507 stid = nfs4_alloc_stid(clp, stateid_slab);
508 if (!stid)
509 return NULL;
510
511 stp = openlockstateid(stid);
512 stp->st_stid.sc_free = nfs4_free_ol_stateid;
513 return stp;
514}
515
516static void nfs4_free_deleg(struct nfs4_stid *stid)
517{
518 struct nfs4_delegation *dp = delegstateid(stid);
519
520 if (dp->dl_file)
521 put_nfs4_file(dp->dl_file);
522 kmem_cache_free(deleg_slab, stid);
523 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400524}
525
NeilBrown6282cd52014-06-04 17:39:26 +1000526/*
527 * When we recall a delegation, we should be careful not to hand it
528 * out again straight away.
529 * To ensure this we keep a pair of bloom filters ('new' and 'old')
530 * in which the filehandles of recalled delegations are "stored".
531 * If a filehandle appear in either filter, a delegation is blocked.
532 * When a delegation is recalled, the filehandle is stored in the "new"
533 * filter.
534 * Every 30 seconds we swap the filters and clear the "new" one,
535 * unless both are empty of course.
536 *
537 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
538 * low 3 bytes as hash-table indices.
539 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400540 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000541 * is used to manage concurrent access. Testing does not need the lock
542 * except when swapping the two filters.
543 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400544static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000545static struct bloom_pair {
546 int entries, old_entries;
547 time_t swap_time;
548 int new; /* index into 'set' */
549 DECLARE_BITMAP(set[2], 256);
550} blocked_delegations;
551
552static int delegation_blocked(struct knfsd_fh *fh)
553{
554 u32 hash;
555 struct bloom_pair *bd = &blocked_delegations;
556
557 if (bd->entries == 0)
558 return 0;
559 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400560 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000561 if (seconds_since_boot() - bd->swap_time > 30) {
562 bd->entries -= bd->old_entries;
563 bd->old_entries = bd->entries;
564 memset(bd->set[bd->new], 0,
565 sizeof(bd->set[0]));
566 bd->new = 1-bd->new;
567 bd->swap_time = seconds_since_boot();
568 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400569 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000570 }
571 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
572 if (test_bit(hash&255, bd->set[0]) &&
573 test_bit((hash>>8)&255, bd->set[0]) &&
574 test_bit((hash>>16)&255, bd->set[0]))
575 return 1;
576
577 if (test_bit(hash&255, bd->set[1]) &&
578 test_bit((hash>>8)&255, bd->set[1]) &&
579 test_bit((hash>>16)&255, bd->set[1]))
580 return 1;
581
582 return 0;
583}
584
585static void block_delegations(struct knfsd_fh *fh)
586{
587 u32 hash;
588 struct bloom_pair *bd = &blocked_delegations;
589
590 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
591
Jeff Laytonf54fe962014-07-25 07:34:26 -0400592 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000593 __set_bit(hash&255, bd->set[bd->new]);
594 __set_bit((hash>>8)&255, bd->set[bd->new]);
595 __set_bit((hash>>16)&255, bd->set[bd->new]);
596 if (bd->entries == 0)
597 bd->swap_time = seconds_since_boot();
598 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400599 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000600}
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602static struct nfs4_delegation *
Jeff Laytonf9416e22014-07-25 07:34:23 -0400603alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400606 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400609 n = atomic_long_inc_return(&num_delegations);
610 if (n < 0 || n > max_delegations)
611 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000612 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400613 goto out_dec;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400614 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700615 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400616 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400617
618 dp->dl_stid.sc_free = nfs4_free_deleg;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400619 /*
620 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400621 * meaning of seqid 0 isn't meaningful, really, but let's avoid
622 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400623 */
624 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700625 INIT_LIST_HEAD(&dp->dl_perfile);
626 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 INIT_LIST_HEAD(&dp->dl_recall_lru);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400628 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Jeff Layton02e12152014-07-16 10:31:57 -0400629 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400631out_dec:
632 atomic_long_dec(&num_delegations);
633 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
636void
Trond Myklebust60116952014-07-29 21:34:06 -0400637nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Trond Myklebust60116952014-07-29 21:34:06 -0400639 struct nfs4_client *clp = s->sc_client;
640
641 if (!atomic_dec_and_test(&s->sc_count))
642 return;
643 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
644 s->sc_free(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500647static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Jeff Layton417c6622014-07-21 09:34:57 -0400649 lockdep_assert_held(&state_lock);
650
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -0500651 if (!fp->fi_lease)
652 return;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500653 if (atomic_dec_and_test(&fp->fi_delegees)) {
654 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
655 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400656 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500657 fp->fi_deleg_file = NULL;
658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400661static void unhash_stid(struct nfs4_stid *s)
662{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500663 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400664}
665
Benny Halevy931ee562014-05-30 09:09:27 -0400666static void
667hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
668{
Benny Halevycdc97502014-05-30 09:09:30 -0400669 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400670 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400671
Benny Halevy3fb87d12014-05-30 09:09:31 -0400672 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400673 list_add(&dp->dl_perfile, &fp->fi_delegations);
674 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
675}
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677static void
Jeff Layton42690672014-07-25 07:34:20 -0400678unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Jeff Layton02e12152014-07-16 10:31:57 -0400680 struct nfs4_file *fp = dp->dl_file;
681
Jeff Layton42690672014-07-25 07:34:20 -0400682 lockdep_assert_held(&state_lock);
683
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400684 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400685 /* Ensure that deleg break won't try to requeue it */
686 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400687 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400688 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400690 list_del_init(&dp->dl_perfile);
691 spin_unlock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400692 if (fp)
Trond Myklebustf8338832014-07-25 07:34:19 -0400693 nfs4_put_deleg_lease(fp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400694}
695
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400696static void destroy_delegation(struct nfs4_delegation *dp)
697{
Jeff Layton42690672014-07-25 07:34:20 -0400698 spin_lock(&state_lock);
699 unhash_delegation_locked(dp);
700 spin_unlock(&state_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400701 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400702}
703
704static void revoke_delegation(struct nfs4_delegation *dp)
705{
706 struct nfs4_client *clp = dp->dl_stid.sc_client;
707
Jeff Layton2d4a5322014-07-25 07:34:21 -0400708 WARN_ON(!list_empty(&dp->dl_recall_lru));
709
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400710 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400711 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400712 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400713 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400714 spin_lock(&clp->cl_lock);
715 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
716 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400717 }
718}
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720/*
721 * SETCLIENTID state
722 */
723
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400724static unsigned int clientid_hashval(u32 id)
725{
726 return id & CLIENT_HASH_MASK;
727}
728
729static unsigned int clientstr_hashval(const char *name)
730{
731 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
732}
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400735 * We store the NONE, READ, WRITE, and BOTH bits separately in the
736 * st_{access,deny}_bmap field of the stateid, in order to track not
737 * only what share bits are currently in force, but also what
738 * combinations of share bits previous opens have used. This allows us
739 * to enforce the recommendation of rfc 3530 14.2.19 that the server
740 * return an error if the client attempt to downgrade to a combination
741 * of share bits not explicable by closing some of its previous opens.
742 *
743 * XXX: This enforcement is actually incomplete, since we don't keep
744 * track of access/deny bit combinations; so, e.g., we allow:
745 *
746 * OPEN allow read, deny write
747 * OPEN allow both, deny none
748 * DOWNGRADE allow read, deny none
749 *
750 * which we should reject.
751 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400752static unsigned int
753bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400754 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400755 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400756
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400757 for (i = 1; i < 4; i++) {
758 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400759 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400760 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400761 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400762}
763
Jeff Layton82c5ff12012-05-11 09:45:13 -0400764/* set share access for a given stateid */
765static inline void
766set_access(u32 access, struct nfs4_ol_stateid *stp)
767{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400768 unsigned char mask = 1 << access;
769
770 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
771 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400772}
773
774/* clear share access for a given stateid */
775static inline void
776clear_access(u32 access, struct nfs4_ol_stateid *stp)
777{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400778 unsigned char mask = 1 << access;
779
780 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
781 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400782}
783
784/* test whether a given stateid has access */
785static inline bool
786test_access(u32 access, struct nfs4_ol_stateid *stp)
787{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400788 unsigned char mask = 1 << access;
789
790 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400791}
792
Jeff Laytonce0fc432012-05-11 09:45:14 -0400793/* set share deny for a given stateid */
794static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400795set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400796{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400797 unsigned char mask = 1 << deny;
798
799 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
800 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400801}
802
803/* clear share deny for a given stateid */
804static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400805clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400806{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400807 unsigned char mask = 1 << deny;
808
809 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
810 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400811}
812
813/* test whether a given stateid is denying specific access */
814static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400815test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400816{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400817 unsigned char mask = 1 << deny;
818
819 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400820}
821
822static int nfs4_access_to_omode(u32 access)
823{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400824 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400825 case NFS4_SHARE_ACCESS_READ:
826 return O_RDONLY;
827 case NFS4_SHARE_ACCESS_WRITE:
828 return O_WRONLY;
829 case NFS4_SHARE_ACCESS_BOTH:
830 return O_RDWR;
831 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500832 WARN_ON_ONCE(1);
833 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400834}
835
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400836/*
837 * A stateid that had a deny mode associated with it is being released
838 * or downgraded. Recalculate the deny mode on the file.
839 */
840static void
841recalculate_deny_mode(struct nfs4_file *fp)
842{
843 struct nfs4_ol_stateid *stp;
844
845 spin_lock(&fp->fi_lock);
846 fp->fi_share_deny = 0;
847 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
848 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
849 spin_unlock(&fp->fi_lock);
850}
851
852static void
853reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
854{
855 int i;
856 bool change = false;
857
858 for (i = 1; i < 4; i++) {
859 if ((i & deny) != i) {
860 change = true;
861 clear_deny(i, stp);
862 }
863 }
864
865 /* Recalculate per-file deny mode if there was a change */
866 if (change)
867 recalculate_deny_mode(stp->st_file);
868}
869
Jeff Layton82c5ff12012-05-11 09:45:13 -0400870/* release all access and file references for a given stateid */
871static void
872release_all_access(struct nfs4_ol_stateid *stp)
873{
874 int i;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400875 struct nfs4_file *fp = stp->st_file;
876
877 if (fp && stp->st_deny_bmap != 0)
878 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400879
880 for (i = 1; i < 4; i++) {
881 if (test_access(i, stp))
Jeff Layton12659652014-07-10 14:07:28 -0400882 nfs4_file_put_access(stp->st_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400883 clear_access(i, stp);
884 }
885}
886
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400887static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500888{
Trond Myklebust1d31a252014-07-10 14:07:25 -0400889 struct nfs4_file *fp = stp->st_file;
890
891 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500892 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400893 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500894 list_del(&stp->st_perstateowner);
895}
896
Trond Myklebust60116952014-07-29 21:34:06 -0400897static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500898{
Trond Myklebust60116952014-07-29 21:34:06 -0400899 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400900
Trond Myklebust60116952014-07-29 21:34:06 -0400901 release_all_access(stp);
Trond Myklebustf8338832014-07-25 07:34:19 -0400902 if (stp->st_file)
903 put_nfs4_file(stp->st_file);
Trond Myklebust60116952014-07-29 21:34:06 -0400904 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500905}
906
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400907static void __release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500908{
909 struct file *file;
910
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400911 list_del(&stp->st_locks);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500912 unhash_generic_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400913 unhash_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500914 file = find_any_file(stp->st_file);
Trond Myklebuste20fcf12014-07-10 14:07:27 -0400915 if (file)
916 filp_close(file, (fl_owner_t)lockowner(stp->st_stateowner));
Trond Myklebust60116952014-07-29 21:34:06 -0400917 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500918}
919
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400920static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500921{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400922 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500923
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400924 list_del(&lo->lo_owner.so_strhash);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400925 while (!list_empty(&lo->lo_owner.so_stateids)) {
926 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400927 struct nfs4_ol_stateid, st_perstateowner);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400928 __release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500929 }
930}
931
Trond Myklebust50cc6232014-04-18 14:44:03 -0400932static void nfs4_free_lockowner(struct nfs4_lockowner *lo)
933{
934 kfree(lo->lo_owner.so_owner.data);
935 kmem_cache_free(lockowner_slab, lo);
936}
937
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400938static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500939{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400940 unhash_lockowner(lo);
941 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500942}
943
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400944static void release_lockowner_if_empty(struct nfs4_lockowner *lo)
945{
946 if (list_empty(&lo->lo_owner.so_stateids))
947 release_lockowner(lo);
948}
949
950static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500951{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400952 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500953
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400954 lo = lockowner(stp->st_stateowner);
955 __release_lock_stateid(stp);
956 release_lockowner_if_empty(lo);
957}
958
959static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
960{
961 struct nfs4_ol_stateid *stp;
962
963 while (!list_empty(&open_stp->st_locks)) {
964 stp = list_entry(open_stp->st_locks.next,
965 struct nfs4_ol_stateid, st_locks);
966 release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500967 }
968}
969
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400970static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500971{
972 unhash_generic_stateid(stp);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400973 release_open_stateid_locks(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400974}
975
976static void release_open_stateid(struct nfs4_ol_stateid *stp)
977{
978 unhash_open_stateid(stp);
Trond Myklebust60116952014-07-29 21:34:06 -0400979 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields22839632009-01-11 14:27:17 -0500980}
981
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400982static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500983{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400984 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500985
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400986 list_del(&oo->oo_owner.so_strhash);
987 list_del(&oo->oo_perclient);
988 while (!list_empty(&oo->oo_owner.so_stateids)) {
989 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400990 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500991 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500992 }
993}
994
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400995static void release_last_closed_stateid(struct nfs4_openowner *oo)
996{
997 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
998
999 if (s) {
Trond Myklebust60116952014-07-29 21:34:06 -04001000 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001001 oo->oo_last_closed_stid = NULL;
1002 }
1003}
1004
Trond Myklebust50cc6232014-04-18 14:44:03 -04001005static void nfs4_free_openowner(struct nfs4_openowner *oo)
1006{
1007 kfree(oo->oo_owner.so_owner.data);
1008 kmem_cache_free(openowner_slab, oo);
1009}
1010
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001011static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001012{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001013 unhash_openowner(oo);
1014 list_del(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001015 release_last_closed_stateid(oo);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001016 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001017}
1018
Marc Eshel5282fd72009-04-03 08:27:52 +03001019static inline int
1020hash_sessionid(struct nfs4_sessionid *sessionid)
1021{
1022 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1023
1024 return sid->sequence % SESSION_HASH_SIZE;
1025}
1026
Trond Myklebust8f199b82012-03-20 15:11:17 -04001027#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001028static inline void
1029dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1030{
1031 u32 *ptr = (u32 *)(&sessionid->data[0]);
1032 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1033}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001034#else
1035static inline void
1036dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1037{
1038}
1039#endif
1040
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001041/*
1042 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1043 * won't be used for replay.
1044 */
1045void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1046{
1047 struct nfs4_stateowner *so = cstate->replay_owner;
1048
1049 if (nfserr == nfserr_replay_me)
1050 return;
1051
1052 if (!seqid_mutating_err(ntohl(nfserr))) {
1053 cstate->replay_owner = NULL;
1054 return;
1055 }
1056 if (!so)
1057 return;
1058 if (so->so_is_open_owner)
1059 release_last_closed_stateid(openowner(so));
1060 so->so_seqid++;
1061 return;
1062}
Marc Eshel5282fd72009-04-03 08:27:52 +03001063
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001064static void
1065gen_sessionid(struct nfsd4_session *ses)
1066{
1067 struct nfs4_client *clp = ses->se_client;
1068 struct nfsd4_sessionid *sid;
1069
1070 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1071 sid->clientid = clp->cl_clientid;
1072 sid->sequence = current_sessionid++;
1073 sid->reserved = 0;
1074}
1075
1076/*
Andy Adamsona6496372009-08-28 08:45:01 -04001077 * The protocol defines ca_maxresponssize_cached to include the size of
1078 * the rpc header, but all we need to cache is the data starting after
1079 * the end of the initial SEQUENCE operation--the rest we regenerate
1080 * each time. Therefore we can advertise a ca_maxresponssize_cached
1081 * value that is the number of bytes in our cache plus a few additional
1082 * bytes. In order to stay on the safe side, and not promise more than
1083 * we can cache, those additional bytes must be the minimum possible: 24
1084 * bytes of rpc header (xid through accept state, with AUTH_NULL
1085 * verifier), 12 for the compound header (with zero-length tag), and 44
1086 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001087 */
Andy Adamsona6496372009-08-28 08:45:01 -04001088#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1089
Andy Adamson557ce262009-08-28 08:45:04 -04001090static void
1091free_session_slots(struct nfsd4_session *ses)
1092{
1093 int i;
1094
1095 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1096 kfree(ses->se_slots[i]);
1097}
1098
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001099/*
1100 * We don't actually need to cache the rpc and session headers, so we
1101 * can allocate a little less for each slot:
1102 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001103static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001104{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001105 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001106
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001107 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1108 size = 0;
1109 else
1110 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1111 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001112}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001113
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001114/*
1115 * XXX: If we run out of reserved DRC memory we could (up to a point)
1116 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001117 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001118 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001119static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001120{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001121 u32 slotsize = slot_bytes(ca);
1122 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001123 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001124
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001125 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001126 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1127 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001128 num = min_t(int, num, avail / slotsize);
1129 nfsd_drc_mem_used += num * slotsize;
1130 spin_unlock(&nfsd_drc_lock);
1131
1132 return num;
1133}
1134
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001135static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001136{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001137 int slotsize = slot_bytes(ca);
1138
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001139 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001140 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001141 spin_unlock(&nfsd_drc_lock);
1142}
1143
Kinglong Mee60810e52014-01-01 00:35:47 +08001144static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1145 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001146{
Kinglong Mee60810e52014-01-01 00:35:47 +08001147 int numslots = fattrs->maxreqs;
1148 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001149 struct nfsd4_session *new;
1150 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001151
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001152 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001153 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1154 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001155
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001156 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001157 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001158 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001159 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001160 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001161 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001162 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001163 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001164 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001165
1166 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1167 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1168
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001169 return new;
1170out_free:
1171 while (i--)
1172 kfree(new->se_slots[i]);
1173 kfree(new);
1174 return NULL;
1175}
1176
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001177static void free_conn(struct nfsd4_conn *c)
1178{
1179 svc_xprt_put(c->cn_xprt);
1180 kfree(c);
1181}
1182
1183static void nfsd4_conn_lost(struct svc_xpt_user *u)
1184{
1185 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1186 struct nfs4_client *clp = c->cn_session->se_client;
1187
1188 spin_lock(&clp->cl_lock);
1189 if (!list_empty(&c->cn_persession)) {
1190 list_del(&c->cn_persession);
1191 free_conn(c);
1192 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001193 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001194 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001195}
1196
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001197static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001198{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001199 struct nfsd4_conn *conn;
1200
1201 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1202 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001203 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001204 svc_xprt_get(rqstp->rq_xprt);
1205 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001206 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001207 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1208 return conn;
1209}
1210
J. Bruce Fields328ead22010-09-29 16:11:06 -04001211static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1212{
1213 conn->cn_session = ses;
1214 list_add(&conn->cn_persession, &ses->se_conns);
1215}
1216
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001217static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1218{
1219 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001220
1221 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001222 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001223 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001224}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001225
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001226static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001227{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001228 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001229 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001230}
1231
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001232static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001233{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001234 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001235
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001236 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001237 ret = nfsd4_register_conn(conn);
1238 if (ret)
1239 /* oops; xprt is already down: */
1240 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001241 /* We may have gained or lost a callback channel: */
1242 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001243}
1244
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001245static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001246{
1247 u32 dir = NFS4_CDFC4_FORE;
1248
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001249 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001250 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001251 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001252}
1253
1254/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001255static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001256{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001257 struct nfs4_client *clp = s->se_client;
1258 struct nfsd4_conn *c;
1259
1260 spin_lock(&clp->cl_lock);
1261 while (!list_empty(&s->se_conns)) {
1262 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1263 list_del_init(&c->cn_persession);
1264 spin_unlock(&clp->cl_lock);
1265
1266 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1267 free_conn(c);
1268
1269 spin_lock(&clp->cl_lock);
1270 }
1271 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001272}
1273
J. Bruce Fields1377b692012-09-11 21:42:40 -04001274static void __free_session(struct nfsd4_session *ses)
1275{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001276 free_session_slots(ses);
1277 kfree(ses);
1278}
1279
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001280static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001281{
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001282 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1283
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001284 lockdep_assert_held(&nn->client_lock);
1285 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001286 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001287 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001288}
Andy Adamson557ce262009-08-28 08:45:04 -04001289
Fengguang Wu135ae822012-11-10 07:20:25 -05001290static 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 -04001291{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001292 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001293 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001294
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001295 new->se_client = clp;
1296 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001297
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001298 INIT_LIST_HEAD(&new->se_conns);
1299
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001300 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001301 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001302 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001303 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001304 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001305 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001306 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001307 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001308 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001309 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001310 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001311 spin_unlock(&nn->client_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001312
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001313 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001314 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001315 /*
1316 * This is a little silly; with sessions there's no real
1317 * use for the callback address. Use the peer address
1318 * as a reasonable default for now, but consider fixing
1319 * the rpc client not to require an address in the
1320 * future:
1321 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001322 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1323 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001324 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001325}
1326
Benny Halevy9089f1b2010-05-12 00:12:26 +03001327/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001328static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001329__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001330{
1331 struct nfsd4_session *elem;
1332 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001333 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001334
1335 dump_sessionid(__func__, sessionid);
1336 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001337 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001338 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001339 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1340 NFS4_MAX_SESSIONID_LEN)) {
1341 return elem;
1342 }
1343 }
1344
1345 dprintk("%s: session not found\n", __func__);
1346 return NULL;
1347}
1348
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001349static struct nfsd4_session *
1350find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1351 __be32 *ret)
1352{
1353 struct nfsd4_session *session;
1354 __be32 status = nfserr_badsession;
1355
1356 session = __find_in_sessionid_hashtbl(sessionid, net);
1357 if (!session)
1358 goto out;
1359 status = nfsd4_get_session_locked(session);
1360 if (status)
1361 session = NULL;
1362out:
1363 *ret = status;
1364 return session;
1365}
1366
Benny Halevy9089f1b2010-05-12 00:12:26 +03001367/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001368static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001369unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001370{
1371 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001372 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001373 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001374 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001375}
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1378static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001379STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001381 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001383 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001384 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return 1;
1386}
1387
1388/*
1389 * XXX Should we use a slab cache ?
1390 * This type of memory management is somewhat inefficient, but we use it
1391 * anyway since SETCLIENTID is not a common operation.
1392 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001393static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
1395 struct nfs4_client *clp;
1396
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001397 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1398 if (clp == NULL)
1399 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001400 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001401 if (clp->cl_name.data == NULL) {
1402 kfree(clp);
1403 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001405 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001406 INIT_LIST_HEAD(&clp->cl_sessions);
1407 idr_init(&clp->cl_stateids);
1408 atomic_set(&clp->cl_refcount, 0);
1409 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1410 INIT_LIST_HEAD(&clp->cl_idhash);
1411 INIT_LIST_HEAD(&clp->cl_openowners);
1412 INIT_LIST_HEAD(&clp->cl_delegations);
1413 INIT_LIST_HEAD(&clp->cl_lru);
1414 INIT_LIST_HEAD(&clp->cl_callbacks);
1415 INIT_LIST_HEAD(&clp->cl_revoked);
1416 spin_lock_init(&clp->cl_lock);
1417 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 return clp;
1419}
1420
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001421static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422free_client(struct nfs4_client *clp)
1423{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001424 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001425
1426 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001427 while (!list_empty(&clp->cl_sessions)) {
1428 struct nfsd4_session *ses;
1429 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1430 se_perclnt);
1431 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001432 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1433 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001434 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001435 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001436 free_svc_cred(&clp->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001438 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 kfree(clp);
1440}
1441
Benny Halevy84d38ac2010-05-12 00:13:16 +03001442/* must be called under the client_lock */
1443static inline void
1444unhash_client_locked(struct nfs4_client *clp)
1445{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001446 struct nfsd4_session *ses;
1447
Benny Halevy84d38ac2010-05-12 00:13:16 +03001448 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001449 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001450 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1451 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001452 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001453}
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001456destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001458 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001461 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001464 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001465 while (!list_empty(&clp->cl_delegations)) {
1466 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001467 unhash_delegation_locked(dp);
1468 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
Benny Halevycdc97502014-05-30 09:09:30 -04001470 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 while (!list_empty(&reaplist)) {
1472 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001473 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001474 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001476 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001477 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001478 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001479 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001480 }
NeilBrownea1da632005-06-23 22:04:17 -07001481 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001482 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1483 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001485 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001486 if (clp->cl_cb_conn.cb_xprt)
1487 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001488 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001489 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001490 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001491 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001492 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001493 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001494 unhash_client_locked(clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001495 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1496 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001497 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001500static void expire_client(struct nfs4_client *clp)
1501{
1502 nfsd4_client_record_remove(clp);
1503 destroy_client(clp);
1504}
1505
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001506static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1507{
1508 memcpy(target->cl_verifier.data, source->data,
1509 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001512static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1513{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1515 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1516}
1517
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001518static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001519{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001520 if (source->cr_principal) {
1521 target->cr_principal =
1522 kstrdup(source->cr_principal, GFP_KERNEL);
1523 if (target->cr_principal == NULL)
1524 return -ENOMEM;
1525 } else
1526 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001527 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 target->cr_uid = source->cr_uid;
1529 target->cr_gid = source->cr_gid;
1530 target->cr_group_info = source->cr_group_info;
1531 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001532 target->cr_gss_mech = source->cr_gss_mech;
1533 if (source->cr_gss_mech)
1534 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001535 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001538static long long
1539compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1540{
1541 long long res;
1542
1543 res = o1->len - o2->len;
1544 if (res)
1545 return res;
1546 return (long long)memcmp(o1->data, o2->data, o1->len);
1547}
1548
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001549static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001550{
NeilBrowna55370a2005-06-23 22:03:52 -07001551 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552}
1553
1554static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001555same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1556{
1557 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558}
1559
1560static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001561same_clid(clientid_t *cl1, clientid_t *cl2)
1562{
1563 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001566static bool groups_equal(struct group_info *g1, struct group_info *g2)
1567{
1568 int i;
1569
1570 if (g1->ngroups != g2->ngroups)
1571 return false;
1572 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001573 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001574 return false;
1575 return true;
1576}
1577
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001578/*
1579 * RFC 3530 language requires clid_inuse be returned when the
1580 * "principal" associated with a requests differs from that previously
1581 * used. We use uid, gid's, and gss principal string as our best
1582 * approximation. We also don't want to allow non-gss use of a client
1583 * established using gss: in theory cr_principal should catch that
1584 * change, but in practice cr_principal can be null even in the gss case
1585 * since gssd doesn't always pass down a principal string.
1586 */
1587static bool is_gss_cred(struct svc_cred *cr)
1588{
1589 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1590 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1591}
1592
1593
Vivek Trivedi5559b502012-07-24 21:18:20 +05301594static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001595same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1596{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001597 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001598 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1599 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001600 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1601 return false;
1602 if (cr1->cr_principal == cr2->cr_principal)
1603 return true;
1604 if (!cr1->cr_principal || !cr2->cr_principal)
1605 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301606 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
1608
J. Bruce Fields57266a62013-04-13 14:27:29 -04001609static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1610{
1611 struct svc_cred *cr = &rqstp->rq_cred;
1612 u32 service;
1613
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001614 if (!cr->cr_gss_mech)
1615 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001616 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1617 return service == RPC_GSS_SVC_INTEGRITY ||
1618 service == RPC_GSS_SVC_PRIVACY;
1619}
1620
1621static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1622{
1623 struct svc_cred *cr = &rqstp->rq_cred;
1624
1625 if (!cl->cl_mach_cred)
1626 return true;
1627 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1628 return false;
1629 if (!svc_rqst_integrity_protected(rqstp))
1630 return false;
1631 if (!cr->cr_principal)
1632 return false;
1633 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1634}
1635
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001636static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001637{
1638 static u32 current_clientid = 1;
1639
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001640 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 clp->cl_clientid.cl_id = current_clientid++;
1642}
1643
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001644static void gen_confirm(struct nfs4_client *clp)
1645{
Chuck Leverab4684d2012-03-02 17:13:50 -05001646 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001647 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Jeff Laytonf4199922014-06-17 07:44:11 -04001649 /*
1650 * This is opaque to client, so no need to byte-swap. Use
1651 * __force to keep sparse happy
1652 */
1653 verf[0] = (__force __be32)get_seconds();
1654 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001655 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001658static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001659{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001660 struct nfs4_stid *ret;
1661
1662 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1663 if (!ret || !ret->sc_type)
1664 return NULL;
1665 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001666}
1667
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001668static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001669{
1670 struct nfs4_stid *s;
1671
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001672 s = find_stateid(cl, t);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001673 if (!s)
1674 return NULL;
1675 if (typemask & s->sc_type)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001676 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001677 return NULL;
1678}
1679
Jeff Layton2216d442012-11-12 15:00:57 -05001680static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001681 struct svc_rqst *rqstp, nfs4_verifier *verf)
1682{
1683 struct nfs4_client *clp;
1684 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001685 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001686 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001687 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001688
1689 clp = alloc_client(name);
1690 if (clp == NULL)
1691 return NULL;
1692
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001693 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1694 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001695 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001696 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001697 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001698 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001699 }
Jeff Layton02e12152014-07-16 10:31:57 -04001700 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001701 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001702 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001703 copy_verf(clp, verf);
1704 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001705 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001706 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001707 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001708 return clp;
1709}
1710
NeilBrownfd39ca92005-06-23 22:04:03 -07001711static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001712add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1713{
1714 struct rb_node **new = &(root->rb_node), *parent = NULL;
1715 struct nfs4_client *clp;
1716
1717 while (*new) {
1718 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1719 parent = *new;
1720
1721 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1722 new = &((*new)->rb_left);
1723 else
1724 new = &((*new)->rb_right);
1725 }
1726
1727 rb_link_node(&new_clp->cl_namenode, parent, new);
1728 rb_insert_color(&new_clp->cl_namenode, root);
1729}
1730
1731static struct nfs4_client *
1732find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1733{
1734 long long cmp;
1735 struct rb_node *node = root->rb_node;
1736 struct nfs4_client *clp;
1737
1738 while (node) {
1739 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1740 cmp = compare_blob(&clp->cl_name, name);
1741 if (cmp > 0)
1742 node = node->rb_left;
1743 else if (cmp < 0)
1744 node = node->rb_right;
1745 else
1746 return clp;
1747 }
1748 return NULL;
1749}
1750
1751static void
1752add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753{
1754 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001755 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001757 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001758 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001760 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001761 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762}
1763
NeilBrownfd39ca92005-06-23 22:04:03 -07001764static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765move_to_confirmed(struct nfs4_client *clp)
1766{
1767 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001768 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001771 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001772 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001773 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001774 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 renew_client(clp);
1776}
1777
1778static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001779find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
1781 struct nfs4_client *clp;
1782 unsigned int idhashval = clientid_hashval(clid->cl_id);
1783
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001784 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001785 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001786 if ((bool)clp->cl_minorversion != sessions)
1787 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001788 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 }
1792 return NULL;
1793}
1794
1795static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001796find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1797{
1798 struct list_head *tbl = nn->conf_id_hashtbl;
1799
1800 return find_client_in_id_table(tbl, clid, sessions);
1801}
1802
1803static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001804find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001806 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001808 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809}
1810
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001811static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001812{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001813 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001814}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001815
NeilBrown28ce6052005-06-23 22:03:56 -07001816static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001817find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001818{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001819 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001820}
1821
1822static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001823find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001824{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001825 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001826}
1827
NeilBrownfd39ca92005-06-23 22:04:03 -07001828static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001829gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001831 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001832 struct sockaddr *sa = svc_addr(rqstp);
1833 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001834 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Jeff Layton7077ecb2009-08-14 12:57:58 -04001836 /* Currently, we only support tcp and tcp6 for the callback channel */
1837 if (se->se_callback_netid_len == 3 &&
1838 !memcmp(se->se_callback_netid_val, "tcp", 3))
1839 expected_family = AF_INET;
1840 else if (se->se_callback_netid_len == 4 &&
1841 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1842 expected_family = AF_INET6;
1843 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 goto out_err;
1845
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001846 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001847 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001848 (struct sockaddr *)&conn->cb_addr,
1849 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001850
J. Bruce Fields07263f12010-05-31 19:09:40 -04001851 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001853
J. Bruce Fields07263f12010-05-31 19:09:40 -04001854 if (conn->cb_addr.ss_family == AF_INET6)
1855 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001856
J. Bruce Fields07263f12010-05-31 19:09:40 -04001857 conn->cb_prog = se->se_callback_prog;
1858 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001859 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return;
1861out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001862 conn->cb_addr.ss_family = AF_UNSPEC;
1863 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001864 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 "will not receive delegations\n",
1866 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 return;
1869}
1870
Andy Adamson074fe892009-04-03 08:28:15 +03001871/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04001872 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001873 */
Trond Myklebustb6076642014-06-30 11:48:35 -04001874static void
Andy Adamson074fe892009-04-03 08:28:15 +03001875nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1876{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001877 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04001878 struct nfsd4_slot *slot = resp->cstate.slot;
1879 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001880
Andy Adamson557ce262009-08-28 08:45:04 -04001881 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001882
Andy Adamson557ce262009-08-28 08:45:04 -04001883 slot->sl_opcnt = resp->opcnt;
1884 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001885
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001886 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001887 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001888 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001889 return;
1890 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001891 base = resp->cstate.data_offset;
1892 slot->sl_datalen = buf->len - base;
1893 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04001894 WARN("%s: sessions DRC could not cache compound\n", __func__);
1895 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001896}
1897
1898/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001899 * Encode the replay sequence operation from the slot values.
1900 * If cachethis is FALSE encode the uncached rep error on the next
1901 * operation which sets resp->p and increments resp->opcnt for
1902 * nfs4svc_encode_compoundres.
1903 *
Andy Adamson074fe892009-04-03 08:28:15 +03001904 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001905static __be32
1906nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1907 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001908{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001909 struct nfsd4_op *op;
1910 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001911
Andy Adamsonabfabf82009-07-23 19:02:18 -04001912 /* Encode the replayed sequence operation */
1913 op = &args->ops[resp->opcnt - 1];
1914 nfsd4_encode_operation(resp, op);
1915
1916 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05001917 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001918 op = &args->ops[resp->opcnt++];
1919 op->status = nfserr_retry_uncached_rep;
1920 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001921 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001922 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001923}
1924
1925/*
Andy Adamson557ce262009-08-28 08:45:04 -04001926 * The sequence operation is not cached because we can use the slot and
1927 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001928 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04001929static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001930nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1931 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001932{
Andy Adamson557ce262009-08-28 08:45:04 -04001933 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001934 struct xdr_stream *xdr = &resp->xdr;
1935 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03001936 __be32 status;
1937
Andy Adamson557ce262009-08-28 08:45:04 -04001938 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001939
Andy Adamsonabfabf82009-07-23 19:02:18 -04001940 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04001941 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04001942 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001943
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001944 p = xdr_reserve_space(xdr, slot->sl_datalen);
1945 if (!p) {
1946 WARN_ON_ONCE(1);
1947 return nfserr_serverfault;
1948 }
1949 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
1950 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03001951
Andy Adamson557ce262009-08-28 08:45:04 -04001952 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001953 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001954}
1955
Andy Adamson0733d212009-04-03 08:28:01 +03001956/*
1957 * Set the exchange_id flags returned by the server.
1958 */
1959static void
1960nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1961{
1962 /* pNFS is not supported */
1963 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1964
1965 /* Referrals are supported, Migration is not. */
1966 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1967
1968 /* set the wire flags to return to client. */
1969 clid->flags = new->cl_exchange_flags;
1970}
1971
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001972static bool client_has_state(struct nfs4_client *clp)
1973{
1974 /*
1975 * Note clp->cl_openowners check isn't quite right: there's no
1976 * need to count owners without stateid's.
1977 *
1978 * Also note we should probably be using this in 4.0 case too.
1979 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04001980 return !list_empty(&clp->cl_openowners)
1981 || !list_empty(&clp->cl_delegations)
1982 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001983}
1984
Al Virob37ad282006-10-19 23:28:59 -07001985__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001986nfsd4_exchange_id(struct svc_rqst *rqstp,
1987 struct nfsd4_compound_state *cstate,
1988 struct nfsd4_exchange_id *exid)
1989{
Andy Adamson0733d212009-04-03 08:28:01 +03001990 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001991 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04001992 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001993 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001994 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001995 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001996 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03001997
Jeff Layton363168b2009-08-14 12:57:56 -04001998 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001999 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002000 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002001 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002002 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002003
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002004 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002005 return nfserr_inval;
2006
Andy Adamson0733d212009-04-03 08:28:01 +03002007 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002008 case SP4_MACH_CRED:
2009 if (!svc_rqst_integrity_protected(rqstp))
2010 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002011 case SP4_NONE:
2012 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002013 default: /* checked by xdr code */
2014 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002015 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002016 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002017 }
2018
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002019 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03002020 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002021 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002022 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002023 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2024 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2025
J. Bruce Fields136e6582012-05-12 20:37:23 -04002026 if (update) {
2027 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002028 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002029 goto out;
2030 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002031 if (!mach_creds_match(conf, rqstp)) {
2032 status = nfserr_wrong_cred;
2033 goto out;
2034 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002035 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002036 status = nfserr_perm;
2037 goto out;
2038 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002039 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002040 status = nfserr_not_same;
2041 goto out;
2042 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002043 /* case 6 */
2044 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2045 new = conf;
2046 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002047 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002048 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002049 if (client_has_state(conf)) {
2050 status = nfserr_clid_inuse;
2051 goto out;
2052 }
Andy Adamson0733d212009-04-03 08:28:01 +03002053 expire_client(conf);
2054 goto out_new;
2055 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002056 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002057 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002058 new = conf;
2059 goto out_copy;
2060 }
2061 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04002062 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002063 }
2064
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002065 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002066 status = nfserr_noent;
2067 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002068 }
2069
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002070 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002071 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03002072 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002073
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002074 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002075out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05002076 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03002077 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04002078 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03002079 goto out;
2080 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002081 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002082 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002083
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002084 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002085 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03002086out_copy:
2087 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2088 exid->clientid.cl_id = new->cl_clientid.cl_id;
2089
J. Bruce Fields778df3f2012-05-25 21:40:23 -04002090 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03002091 nfsd4_set_ex_flags(new, exid);
2092
2093 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04002094 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002095 status = nfs_ok;
2096
2097out:
2098 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03002099 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002100}
2101
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002102static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002103check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002104{
Andy Adamson88e588d2009-07-23 19:02:15 -04002105 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2106 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002107
2108 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002109 if (slot_inuse) {
2110 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002111 return nfserr_jukebox;
2112 else
2113 return nfserr_seq_misordered;
2114 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002115 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002116 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002117 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002118 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002119 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002120 return nfserr_seq_misordered;
2121}
2122
Andy Adamson49557cc2009-07-23 19:02:16 -04002123/*
2124 * Cache the create session result into the create session single DRC
2125 * slot cache by saving the xdr structure. sl_seqid has been set.
2126 * Do this for solo or embedded create session operations.
2127 */
2128static void
2129nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002130 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002131{
2132 slot->sl_status = nfserr;
2133 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2134}
2135
2136static __be32
2137nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2138 struct nfsd4_clid_slot *slot)
2139{
2140 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2141 return slot->sl_status;
2142}
2143
Mi Jinlong1b74c252011-07-14 14:50:17 +08002144#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2145 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2146 1 + /* MIN tag is length with zero, only length */ \
2147 3 + /* version, opcount, opcode */ \
2148 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2149 /* seqid, slotID, slotID, cache */ \
2150 4 ) * sizeof(__be32))
2151
2152#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2153 2 + /* verifier: AUTH_NULL, length 0 */\
2154 1 + /* status */ \
2155 1 + /* MIN tag is length with zero, only length */ \
2156 3 + /* opcount, opcode, opstatus*/ \
2157 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2158 /* seqid, slotID, slotID, slotID, status */ \
2159 5 ) * sizeof(__be32))
2160
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002161static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002162{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002163 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2164
J. Bruce Fields373cd402013-04-08 15:42:12 -04002165 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2166 return nfserr_toosmall;
2167 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2168 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002169 ca->headerpadsz = 0;
2170 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2171 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2172 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2173 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2174 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2175 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2176 /*
2177 * Note decreasing slot size below client's request may make it
2178 * difficult for client to function correctly, whereas
2179 * decreasing the number of slots will (just?) affect
2180 * performance. When short on memory we therefore prefer to
2181 * decrease number of slots instead of their size. Clients that
2182 * request larger slots than they need will get poor results:
2183 */
2184 ca->maxreqs = nfsd4_get_drc_mem(ca);
2185 if (!ca->maxreqs)
2186 return nfserr_jukebox;
2187
J. Bruce Fields373cd402013-04-08 15:42:12 -04002188 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002189}
2190
Kinglong Mee8a891632013-12-23 18:11:02 +08002191#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2192 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2193#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2194 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2195
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002196static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2197{
2198 ca->headerpadsz = 0;
2199
2200 /*
2201 * These RPC_MAX_HEADER macros are overkill, especially since we
2202 * don't even do gss on the backchannel yet. But this is still
2203 * less than 1k. Tighten up this estimate in the unlikely event
2204 * it turns out to be a problem for some client:
2205 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002206 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002207 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002208 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002209 return nfserr_toosmall;
2210 ca->maxresp_cached = 0;
2211 if (ca->maxops < 2)
2212 return nfserr_toosmall;
2213
2214 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002215}
2216
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002217static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2218{
2219 switch (cbs->flavor) {
2220 case RPC_AUTH_NULL:
2221 case RPC_AUTH_UNIX:
2222 return nfs_ok;
2223 default:
2224 /*
2225 * GSS case: the spec doesn't allow us to return this
2226 * error. But it also doesn't allow us not to support
2227 * GSS.
2228 * I'd rather this fail hard than return some error the
2229 * client might think it can already handle:
2230 */
2231 return nfserr_encr_alg_unsupp;
2232 }
2233}
2234
Andy Adamson069b6ad2009-04-03 08:27:58 +03002235__be32
2236nfsd4_create_session(struct svc_rqst *rqstp,
2237 struct nfsd4_compound_state *cstate,
2238 struct nfsd4_create_session *cr_ses)
2239{
Jeff Layton363168b2009-08-14 12:57:56 -04002240 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002241 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002242 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002243 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002244 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002245 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002246 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002247
Mi Jinlonga62573d2011-03-23 17:57:07 +08002248 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2249 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002250 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2251 if (status)
2252 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002253 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002254 if (status)
2255 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002256 status = check_backchannel_attrs(&cr_ses->back_channel);
2257 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002258 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002259 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002260 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002261 if (!new)
2262 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002263 conn = alloc_conn_from_crses(rqstp, cr_ses);
2264 if (!conn)
2265 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002266
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002267 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002268 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002269 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002270 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002271
2272 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002273 status = nfserr_wrong_cred;
2274 if (!mach_creds_match(conf, rqstp))
2275 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002276 cs_slot = &conf->cl_cs_slot;
2277 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002278 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002279 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002280 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002281 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002282 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002283 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002284 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002285 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002286 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002287 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002288 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002289 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002290 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002291 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002292 status = nfserr_wrong_cred;
2293 if (!mach_creds_match(unconf, rqstp))
2294 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002295 cs_slot = &unconf->cl_cs_slot;
2296 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002297 if (status) {
2298 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002299 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002300 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002301 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002302 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002303 if (old) {
2304 status = mark_client_expired(old);
2305 if (status)
2306 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002307 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002308 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002309 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002310 conf = unconf;
2311 } else {
2312 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002313 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002314 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002315 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002316 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002317 * We do not support RDMA or persistent sessions
2318 */
2319 cr_ses->flags &= ~SESSION4_PERSIST;
2320 cr_ses->flags &= ~SESSION4_RDMA;
2321
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002322 init_session(rqstp, new, conf, cr_ses);
2323 nfsd4_init_conn(rqstp, conn, new);
2324
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002325 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002326 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002327 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002328 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002329
Andy Adamson49557cc2009-07-23 19:02:16 -04002330 /* cache solo and embedded create sessions under the state lock */
2331 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002332 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002333 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002334out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08002335 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002336 free_conn(conn);
2337out_free_session:
2338 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002339out_release_drc_mem:
2340 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002341 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002342}
2343
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002344static __be32 nfsd4_map_bcts_dir(u32 *dir)
2345{
2346 switch (*dir) {
2347 case NFS4_CDFC4_FORE:
2348 case NFS4_CDFC4_BACK:
2349 return nfs_ok;
2350 case NFS4_CDFC4_FORE_OR_BOTH:
2351 case NFS4_CDFC4_BACK_OR_BOTH:
2352 *dir = NFS4_CDFC4_BOTH;
2353 return nfs_ok;
2354 };
2355 return nfserr_inval;
2356}
2357
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002358__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2359{
2360 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002361 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002362 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002363
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002364 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2365 if (status)
2366 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002367 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002368 session->se_cb_prog = bc->bc_cb_program;
2369 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002370 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002371
2372 nfsd4_probe_callback(session->se_client);
2373
2374 return nfs_ok;
2375}
2376
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002377__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2378 struct nfsd4_compound_state *cstate,
2379 struct nfsd4_bind_conn_to_session *bcts)
2380{
2381 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002382 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002383 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002384 struct net *net = SVC_NET(rqstp);
2385 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002386
2387 if (!nfsd4_last_compound_op(rqstp))
2388 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002389 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002390 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002391 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002392 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002393 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002394 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002395 status = nfserr_wrong_cred;
2396 if (!mach_creds_match(session->se_client, rqstp))
2397 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002398 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002399 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002400 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002401 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002402 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002403 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002404 goto out;
2405 nfsd4_init_conn(rqstp, conn, session);
2406 status = nfs_ok;
2407out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002408 nfsd4_put_session(session);
2409out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002410 nfs4_unlock_state();
2411 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002412}
2413
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002414static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2415{
2416 if (!session)
2417 return 0;
2418 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2419}
2420
Andy Adamson069b6ad2009-04-03 08:27:58 +03002421__be32
2422nfsd4_destroy_session(struct svc_rqst *r,
2423 struct nfsd4_compound_state *cstate,
2424 struct nfsd4_destroy_session *sessionid)
2425{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002426 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002427 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002428 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002429 struct net *net = SVC_NET(r);
2430 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002431
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002432 nfs4_lock_state();
2433 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002434 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002435 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002436 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002437 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002438 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002439 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002440 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002441 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002442 if (!ses)
2443 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002444 status = nfserr_wrong_cred;
2445 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002446 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002447 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002448 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002449 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002450 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002451 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002452
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002453 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002454
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002455 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002456 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002457out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002458 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002459out_client_lock:
2460 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002461out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002462 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002463 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002464}
2465
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002466static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002467{
2468 struct nfsd4_conn *c;
2469
2470 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002471 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002472 return c;
2473 }
2474 }
2475 return NULL;
2476}
2477
J. Bruce Fields57266a62013-04-13 14:27:29 -04002478static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002479{
2480 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002481 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002482 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002483 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002484
2485 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002486 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002487 if (c)
2488 goto out_free;
2489 status = nfserr_conn_not_bound_to_session;
2490 if (clp->cl_mach_cred)
2491 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002492 __nfsd4_hash_conn(new, ses);
2493 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002494 ret = nfsd4_register_conn(new);
2495 if (ret)
2496 /* oops; xprt is already down: */
2497 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002498 return nfs_ok;
2499out_free:
2500 spin_unlock(&clp->cl_lock);
2501 free_conn(new);
2502 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002503}
2504
Mi Jinlong868b89c2011-04-27 09:09:58 +08002505static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2506{
2507 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2508
2509 return args->opcnt > session->se_fchannel.maxops;
2510}
2511
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002512static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2513 struct nfsd4_session *session)
2514{
2515 struct xdr_buf *xb = &rqstp->rq_arg;
2516
2517 return xb->len > session->se_fchannel.maxreq_sz;
2518}
2519
Andy Adamson069b6ad2009-04-03 08:27:58 +03002520__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002521nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002522 struct nfsd4_compound_state *cstate,
2523 struct nfsd4_sequence *seq)
2524{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002525 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002526 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002527 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002528 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002529 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002530 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002531 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002532 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002533 struct net *net = SVC_NET(rqstp);
2534 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002535
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002536 if (resp->opcnt != 1)
2537 return nfserr_sequence_pos;
2538
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002539 /*
2540 * Will be either used or freed by nfsd4_sequence_check_conn
2541 * below.
2542 */
2543 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2544 if (!conn)
2545 return nfserr_jukebox;
2546
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002547 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002548 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002549 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002550 goto out_no_session;
2551 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002552
Mi Jinlong868b89c2011-04-27 09:09:58 +08002553 status = nfserr_too_many_ops;
2554 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002555 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002556
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002557 status = nfserr_req_too_big;
2558 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002559 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002560
Benny Halevyb85d4c02009-04-03 08:28:08 +03002561 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002562 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002563 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002564
Andy Adamson557ce262009-08-28 08:45:04 -04002565 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002566 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2567
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002568 /* We do not negotiate the number of slots yet, so set the
2569 * maxslots to the session maxreqs which is used to encode
2570 * sr_highest_slotid and the sr_target_slot id to maxslots */
2571 seq->maxslots = session->se_fchannel.maxreqs;
2572
J. Bruce Fields73e79482012-02-13 16:39:00 -05002573 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2574 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002575 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002576 status = nfserr_seq_misordered;
2577 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002578 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002579 cstate->slot = slot;
2580 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002581 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002582 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002583 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002584 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002585 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002586 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002587 }
2588 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002589 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002590
J. Bruce Fields57266a62013-04-13 14:27:29 -04002591 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002592 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002593 if (status)
2594 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002595
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002596 buflen = (seq->cachethis) ?
2597 session->se_fchannel.maxresp_cached :
2598 session->se_fchannel.maxresp_sz;
2599 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2600 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002601 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002602 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002603 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002604
2605 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002606 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002607 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002608 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002609 if (seq->cachethis)
2610 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002611 else
2612 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002613
2614 cstate->slot = slot;
2615 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002616 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002617
Benny Halevyb85d4c02009-04-03 08:28:08 +03002618out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002619 switch (clp->cl_cb_state) {
2620 case NFSD4_CB_DOWN:
2621 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2622 break;
2623 case NFSD4_CB_FAULT:
2624 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2625 break;
2626 default:
2627 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002628 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002629 if (!list_empty(&clp->cl_revoked))
2630 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002631out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002632 if (conn)
2633 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002634 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002635 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002636out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002637 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002638 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002639}
2640
Trond Myklebustb6076642014-06-30 11:48:35 -04002641void
2642nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2643{
2644 struct nfsd4_compound_state *cs = &resp->cstate;
2645
2646 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002647 if (cs->status != nfserr_replay_cache) {
2648 nfsd4_store_cache_entry(resp);
2649 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2650 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002651 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002652 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002653 } else if (cs->clp)
2654 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002655}
2656
Mi Jinlong345c2842011-10-20 17:51:39 +08002657__be32
2658nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2659{
2660 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002661 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002662 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002663
2664 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002665 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002666 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002667 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002668
2669 if (conf) {
2670 clp = conf;
2671
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002672 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002673 status = nfserr_clientid_busy;
2674 goto out;
2675 }
2676 } else if (unconf)
2677 clp = unconf;
2678 else {
2679 status = nfserr_stale_clientid;
2680 goto out;
2681 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002682 if (!mach_creds_match(clp, rqstp)) {
2683 status = nfserr_wrong_cred;
2684 goto out;
2685 }
Mi Jinlong345c2842011-10-20 17:51:39 +08002686 expire_client(clp);
2687out:
2688 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002689 return status;
2690}
2691
Andy Adamson069b6ad2009-04-03 08:27:58 +03002692__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002693nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2694{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002695 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002696
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002697 if (rc->rca_one_fs) {
2698 if (!cstate->current_fh.fh_dentry)
2699 return nfserr_nofilehandle;
2700 /*
2701 * We don't take advantage of the rca_one_fs case.
2702 * That's OK, it's optional, we can safely ignore it.
2703 */
2704 return nfs_ok;
2705 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002706
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002707 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002708 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002709 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2710 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002711 goto out;
2712
2713 status = nfserr_stale_clientid;
2714 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002715 /*
2716 * The following error isn't really legal.
2717 * But we only get here if the client just explicitly
2718 * destroyed the client. Surely it no longer cares what
2719 * error it gets back on an operation for the dead
2720 * client.
2721 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002722 goto out;
2723
2724 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002725 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002726out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002727 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002728 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002729}
2730
2731__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002732nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2733 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002735 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002737 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002738 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002739 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2740
J. Bruce Fields63db4632012-05-18 21:54:19 -04002741 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002743 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002744 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002745 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002747 if (clp_used_exchangeid(conf))
2748 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002749 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002750 char addr_str[INET6_ADDRSTRLEN];
2751 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2752 sizeof(addr_str));
2753 dprintk("NFSD: setclientid: string in use by client "
2754 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 goto out;
2756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002758 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002759 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002762 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002763 if (new == NULL)
2764 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002765 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002766 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002768 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002769 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002770 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002771 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002772 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2774 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2775 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2776 status = nfs_ok;
2777out:
2778 nfs4_unlock_state();
2779 return status;
2780}
2781
2782
Al Virob37ad282006-10-19 23:28:59 -07002783__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002784nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2785 struct nfsd4_compound_state *cstate,
2786 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787{
NeilBrown21ab45a2005-06-23 22:04:14 -07002788 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2790 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002791 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002792 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002794 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002797
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002798 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002799 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002800 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002801 * We try hard to give out unique clientid's, so if we get an
2802 * attempt to confirm the same clientid with a different cred,
2803 * there's a bug somewhere. Let's charitably assume it's our
2804 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002805 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002806 status = nfserr_serverfault;
2807 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2808 goto out;
2809 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2810 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002811 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002812 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2813 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002815 else /* case 4: client hasn't noticed we rebooted yet? */
2816 status = nfserr_stale_clientid;
2817 goto out;
2818 }
2819 status = nfs_ok;
2820 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002821 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2822 nfsd4_probe_callback(conf);
2823 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002824 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002825 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002826 if (conf) {
2827 status = mark_client_expired(conf);
2828 if (status)
2829 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002830 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002831 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002832 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002833 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 nfs4_unlock_state();
2837 return status;
2838}
2839
J. Bruce Fields32513b42011-10-13 16:00:16 -04002840static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002842 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2843}
2844
2845/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04002846static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04002847{
Trond Myklebustca943212014-07-23 16:17:39 -04002848 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
Trond Myklebust950e0112014-06-30 11:48:31 -04002850 lockdep_assert_held(&state_lock);
2851
J. Bruce Fields32513b42011-10-13 16:00:16 -04002852 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04002853 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002854 INIT_LIST_HEAD(&fp->fi_stateids);
2855 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04002856 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002857 fp->fi_had_conflict = false;
2858 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04002859 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002860 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2861 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04002862 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863}
2864
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002865void
NeilBrowne60d4392005-06-23 22:03:01 -07002866nfsd4_free_slabs(void)
2867{
Christoph Hellwigabf11352014-05-21 07:43:03 -07002868 kmem_cache_destroy(openowner_slab);
2869 kmem_cache_destroy(lockowner_slab);
2870 kmem_cache_destroy(file_slab);
2871 kmem_cache_destroy(stateid_slab);
2872 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002873}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Bryan Schumaker72083392011-11-01 15:24:59 -04002875int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876nfsd4_init_slabs(void)
2877{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002878 openowner_slab = kmem_cache_create("nfsd4_openowners",
2879 sizeof(struct nfs4_openowner), 0, 0, NULL);
2880 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002881 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002882 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002883 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002884 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002885 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07002886 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002887 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002888 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002889 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07002890 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002891 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002892 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002893 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002894 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002895 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002896 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002897 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07002899
2900out_free_stateid_slab:
2901 kmem_cache_destroy(stateid_slab);
2902out_free_file_slab:
2903 kmem_cache_destroy(file_slab);
2904out_free_lockowner_slab:
2905 kmem_cache_destroy(lockowner_slab);
2906out_free_openowner_slab:
2907 kmem_cache_destroy(openowner_slab);
2908out:
NeilBrowne60d4392005-06-23 22:03:01 -07002909 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2910 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911}
2912
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002913static void init_nfs4_replay(struct nfs4_replay *rp)
2914{
2915 rp->rp_status = nfserr_serverfault;
2916 rp->rp_buflen = 0;
2917 rp->rp_buf = rp->rp_ibuf;
2918}
2919
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002920static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921{
2922 struct nfs4_stateowner *sop;
2923
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002924 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002925 if (!sop)
2926 return NULL;
2927
2928 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2929 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002930 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002931 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002933 sop->so_owner.len = owner->len;
2934
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002935 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002936 sop->so_client = clp;
2937 init_nfs4_replay(&sop->so_replay);
2938 return sop;
2939}
2940
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002941static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002942{
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002943 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2944
2945 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002946 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947}
2948
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002949static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04002950alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002951 struct nfsd4_compound_state *cstate)
2952{
Trond Myklebust13d6f662014-06-30 11:48:45 -04002953 struct nfs4_client *clp = cstate->clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002954 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002956 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2957 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002959 oo->oo_owner.so_is_open_owner = 1;
2960 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04002961 oo->oo_flags = NFS4_OO_NEW;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002962 if (nfsd4_has_session(cstate))
2963 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002964 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002965 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002966 INIT_LIST_HEAD(&oo->oo_close_lru);
2967 hash_openowner(oo, clp, strhashval);
2968 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969}
2970
J. Bruce Fields996e0932011-10-17 11:14:48 -04002971static 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 -04002972 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002974 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04002975 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002976 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002977 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002978 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 stp->st_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 stp->st_access_bmap = 0;
2981 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07002982 stp->st_openstp = NULL;
Trond Myklebust1d31a252014-07-10 14:07:25 -04002983 spin_lock(&fp->fi_lock);
2984 list_add(&stp->st_perfile, &fp->fi_stateids);
2985 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986}
2987
2988static void
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002989move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990{
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002991 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2992
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002993 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002995 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002996 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997}
2998
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04003000same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
3001 clientid_t *clid)
3002{
3003 return (sop->so_owner.len == owner->len) &&
3004 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
3005 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006}
3007
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003008static struct nfs4_openowner *
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003009find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
3010 bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011{
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003012 struct nfs4_stateowner *so;
3013 struct nfs4_openowner *oo;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003014 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003016 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003017 if (!so->so_is_open_owner)
3018 continue;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003019 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
3020 oo = openowner(so);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003021 clp = oo->oo_owner.so_client;
3022 if ((bool)clp->cl_minorversion != sessions)
3023 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003024 renew_client(oo->oo_owner.so_client);
3025 return oo;
3026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 }
3028 return NULL;
3029}
3030
3031/* search file_hashtbl[] for file */
3032static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003033find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034{
Trond Myklebustca943212014-07-23 16:17:39 -04003035 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 struct nfs4_file *fp;
3037
Trond Myklebust950e0112014-06-30 11:48:31 -04003038 lockdep_assert_held(&state_lock);
3039
Jeff Layton89876f82013-04-02 09:01:59 -04003040 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003041 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003042 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 }
3046 return NULL;
3047}
3048
Trond Myklebust950e0112014-06-30 11:48:31 -04003049static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003050find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003051{
3052 struct nfs4_file *fp;
3053
3054 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003055 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003056 spin_unlock(&state_lock);
3057 return fp;
3058}
3059
3060static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003061find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003062{
3063 struct nfs4_file *fp;
3064
3065 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003066 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003067 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003068 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003069 fp = new;
3070 }
3071 spin_unlock(&state_lock);
3072
3073 return fp;
3074}
3075
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003076/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 * Called to check deny when READ with all zero stateid or
3078 * WRITE with all zero or all one stateid
3079 */
Al Virob37ad282006-10-19 23:28:59 -07003080static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3082{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003084 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Trond Myklebustca943212014-07-23 16:17:39 -04003086 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003087 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003088 return ret;
3089 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003090 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003091 if (fp->fi_share_deny & deny_type)
3092 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003093 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003094 put_nfs4_file(fp);
3095 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096}
3097
Jeff Layton02e12152014-07-16 10:31:57 -04003098void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099{
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003100 struct nfs4_client *clp = dp->dl_stid.sc_client;
3101 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3102
Jeff Layton650ecc8f2014-07-25 07:34:27 -04003103 block_delegations(&dp->dl_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003104
Jeff Layton02e12152014-07-16 10:31:57 -04003105 /*
3106 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003107 * already holding inode->i_lock.
3108 *
Jeff Laytondff13992014-07-08 14:02:49 -04003109 * If the dl_time != 0, then we know that it has already been
3110 * queued for a lease break. Don't queue it again.
3111 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003112 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003113 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003114 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003115 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003116 }
Jeff Layton02e12152014-07-16 10:31:57 -04003117 spin_unlock(&state_lock);
3118}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
Jeff Layton02e12152014-07-16 10:31:57 -04003120static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3121{
3122 /*
3123 * We're assuming the state code never drops its reference
3124 * without first removing the lease. Since we're in this lease
3125 * callback (and since the lease code is serialized by the kernel
3126 * lock) we know the server hasn't removed the lease yet, we know
3127 * it's safe to take a reference.
3128 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003129 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003130 nfsd4_cb_recall(dp);
3131}
3132
Jeff Layton1c8c6012013-06-21 08:58:15 -04003133/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003134static void nfsd_break_deleg_cb(struct file_lock *fl)
3135{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003136 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3137 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003138
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003139 if (!fp) {
3140 WARN(1, "(%p)->fl_owner NULL\n", fl);
3141 return;
3142 }
3143 if (fp->fi_had_conflict) {
3144 WARN(1, "duplicate break on %p\n", fp);
3145 return;
3146 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003147 /*
3148 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003149 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003150 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003151 */
3152 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
Jeff Layton02e12152014-07-16 10:31:57 -04003154 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003155 fp->fi_had_conflict = true;
3156 /*
3157 * If there are no delegations on the list, then we can't count on this
3158 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3159 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3160 * true should keep any new delegations from being hashed.
3161 */
3162 if (list_empty(&fp->fi_delegations))
3163 fl->fl_break_time = jiffies;
3164 else
3165 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3166 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003167 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168}
3169
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170static
3171int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3172{
3173 if (arg & F_UNLCK)
3174 return lease_modify(onlist, arg);
3175 else
3176 return -EAGAIN;
3177}
3178
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003179static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003180 .lm_break = nfsd_break_deleg_cb,
3181 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182};
3183
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003184static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3185{
3186 if (nfsd4_has_session(cstate))
3187 return nfs_ok;
3188 if (seqid == so->so_seqid - 1)
3189 return nfserr_replay_me;
3190 if (seqid == so->so_seqid)
3191 return nfs_ok;
3192 return nfserr_bad_seqid;
3193}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
Jeff Layton4b24ca72014-06-30 11:48:44 -04003195static __be32 lookup_clientid(clientid_t *clid,
3196 struct nfsd4_compound_state *cstate,
3197 struct nfsd_net *nn)
3198{
3199 struct nfs4_client *found;
3200
3201 if (cstate->clp) {
3202 found = cstate->clp;
3203 if (!same_clid(&found->cl_clientid, clid))
3204 return nfserr_stale_clientid;
3205 return nfs_ok;
3206 }
3207
3208 if (STALE_CLIENTID(clid, nn))
3209 return nfserr_stale_clientid;
3210
3211 /*
3212 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3213 * cached already then we know this is for is for v4.0 and "sessions"
3214 * will be false.
3215 */
3216 WARN_ON_ONCE(cstate->session);
3217 found = find_confirmed_client(clid, false, nn);
3218 if (!found)
3219 return nfserr_expired;
3220
3221 /* Cache the nfs4_client in cstate! */
3222 cstate->clp = found;
3223 atomic_inc(&found->cl_refcount);
3224 return nfs_ok;
3225}
3226
Al Virob37ad282006-10-19 23:28:59 -07003227__be32
Andy Adamson66689582009-04-03 08:28:45 +03003228nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003229 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 clientid_t *clientid = &open->op_clientid;
3232 struct nfs4_client *clp = NULL;
3233 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003234 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003235 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003237 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003239 /*
3240 * In case we need it later, after we've already created the
3241 * file and don't want to risk a further failure:
3242 */
3243 open->op_file = nfsd4_alloc_file();
3244 if (open->op_file == NULL)
3245 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
Trond Myklebust2d91e892014-06-30 11:48:46 -04003247 status = lookup_clientid(clientid, cstate, nn);
3248 if (status)
3249 return status;
3250 clp = cstate->clp;
3251
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003252 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003253 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003254 open->op_openowner = oo;
3255 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003256 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003258 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003259 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003260 release_openowner(oo);
3261 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003262 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003263 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003264 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3265 if (status)
3266 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003267 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003268new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003269 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003270 if (oo == NULL)
3271 return nfserr_jukebox;
3272 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003273alloc_stateid:
3274 open->op_stp = nfs4_alloc_stateid(clp);
3275 if (!open->op_stp)
3276 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003277 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278}
3279
Al Virob37ad282006-10-19 23:28:59 -07003280static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003281nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3282{
3283 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3284 return nfserr_openmode;
3285 else
3286 return nfs_ok;
3287}
3288
Daniel Mackc47d8322011-05-16 16:38:14 +02003289static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003290{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003291 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3292}
3293
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003294static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003295{
3296 struct nfs4_stid *ret;
3297
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003298 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003299 if (!ret)
3300 return NULL;
3301 return delegstateid(ret);
3302}
3303
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003304static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3305{
3306 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3307 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3308}
3309
Al Virob37ad282006-10-19 23:28:59 -07003310static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003311nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003312 struct nfs4_delegation **dp)
3313{
3314 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003315 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07003316
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003317 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07003318 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003319 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003320 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07003321 status = nfs4_check_delegmode(*dp, flags);
3322 if (status)
3323 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003324out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003325 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003326 return nfs_ok;
3327 if (status)
3328 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003329 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003330 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003331}
3332
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003333static struct nfs4_ol_stateid *
3334nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003336 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003337 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
Trond Myklebust1d31a252014-07-10 14:07:25 -04003339 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003340 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 /* ignore lock owners */
3342 if (local->st_stateowner->so_is_open_owner == 0)
3343 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003344 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003345 ret = local;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003346 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003349 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003350 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351}
3352
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003353static inline int nfs4_access_to_access(u32 nfs4_access)
3354{
3355 int flags = 0;
3356
3357 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3358 flags |= NFSD_MAY_READ;
3359 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3360 flags |= NFSD_MAY_WRITE;
3361 return flags;
3362}
3363
Al Virob37ad282006-10-19 23:28:59 -07003364static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3366 struct nfsd4_open *open)
3367{
3368 struct iattr iattr = {
3369 .ia_valid = ATTR_SIZE,
3370 .ia_size = 0,
3371 };
3372 if (!open->op_truncate)
3373 return 0;
3374 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003375 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3377}
3378
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003379static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003380 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3381 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003382{
Trond Myklebustde186432014-07-10 14:07:26 -04003383 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003384 __be32 status;
3385 int oflag = nfs4_access_to_omode(open->op_share_access);
3386 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003387 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003388
Trond Myklebustde186432014-07-10 14:07:26 -04003389 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003390
3391 /*
3392 * Are we trying to set a deny mode that would conflict with
3393 * current access?
3394 */
3395 status = nfs4_file_check_deny(fp, open->op_share_deny);
3396 if (status != nfs_ok) {
3397 spin_unlock(&fp->fi_lock);
3398 goto out;
3399 }
3400
3401 /* set access to the file */
3402 status = nfs4_file_get_access(fp, open->op_share_access);
3403 if (status != nfs_ok) {
3404 spin_unlock(&fp->fi_lock);
3405 goto out;
3406 }
3407
3408 /* Set access bits in stateid */
3409 old_access_bmap = stp->st_access_bmap;
3410 set_access(open->op_share_access, stp);
3411
3412 /* Set new deny mask */
3413 old_deny_bmap = stp->st_deny_bmap;
3414 set_deny(open->op_share_deny, stp);
3415 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3416
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003417 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003418 spin_unlock(&fp->fi_lock);
3419 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003420 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003421 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003422 spin_lock(&fp->fi_lock);
3423 if (!fp->fi_fds[oflag]) {
3424 fp->fi_fds[oflag] = filp;
3425 filp = NULL;
3426 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003427 }
Trond Myklebustde186432014-07-10 14:07:26 -04003428 spin_unlock(&fp->fi_lock);
3429 if (filp)
3430 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003431
3432 status = nfsd4_truncate(rqstp, cur_fh, open);
3433 if (status)
3434 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003435out:
3436 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003437out_put_access:
3438 stp->st_access_bmap = old_access_bmap;
3439 nfs4_file_put_access(fp, open->op_share_access);
3440 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3441 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003442}
3443
Al Virob37ad282006-10-19 23:28:59 -07003444static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003445nfs4_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 -07003446{
Al Virob37ad282006-10-19 23:28:59 -07003447 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003448 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003450 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003451 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003452
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003453 /* test and set deny mode */
3454 spin_lock(&fp->fi_lock);
3455 status = nfs4_file_check_deny(fp, open->op_share_deny);
3456 if (status == nfs_ok) {
3457 old_deny_bmap = stp->st_deny_bmap;
3458 set_deny(open->op_share_deny, stp);
3459 fp->fi_share_deny |=
3460 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3461 }
3462 spin_unlock(&fp->fi_lock);
3463
3464 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003467 status = nfsd4_truncate(rqstp, cur_fh, open);
3468 if (status != nfs_ok)
3469 reset_union_bmap_deny(old_deny_bmap, stp);
3470 return status;
3471}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003474nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003476 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477}
3478
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003479/* Should we give out recallable state?: */
3480static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3481{
3482 if (clp->cl_cb_state == NFSD4_CB_UP)
3483 return true;
3484 /*
3485 * In the sessions case, since we don't have to establish a
3486 * separate connection for callbacks, we assume it's OK
3487 * until we hear otherwise:
3488 */
3489 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3490}
3491
Jeff Laytond564fbe2014-07-16 10:31:58 -04003492static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003493{
3494 struct file_lock *fl;
3495
3496 fl = locks_alloc_lock();
3497 if (!fl)
3498 return NULL;
3499 locks_init_lock(fl);
3500 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003501 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003502 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3503 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003504 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003505 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003506 return fl;
3507}
3508
J. Bruce Fields99c41512013-05-21 16:21:25 -04003509static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003510{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003511 struct nfs4_file *fp = dp->dl_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003512 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003513 struct file *filp;
3514 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003515
Jeff Laytond564fbe2014-07-16 10:31:58 -04003516 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003517 if (!fl)
3518 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003519 filp = find_readable_file(fp);
3520 if (!filp) {
3521 /* We should always have a readable file here */
3522 WARN_ON_ONCE(1);
3523 return -EBADF;
3524 }
3525 fl->fl_file = filp;
3526 status = vfs_setlease(filp, fl->fl_type, &fl);
3527 if (status) {
3528 locks_free_lock(fl);
3529 goto out_fput;
3530 }
Benny Halevycdc97502014-05-30 09:09:30 -04003531 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003532 spin_lock(&fp->fi_lock);
3533 /* Did the lease get broken before we took the lock? */
3534 status = -EAGAIN;
3535 if (fp->fi_had_conflict)
3536 goto out_unlock;
3537 /* Race breaker */
3538 if (fp->fi_lease) {
3539 status = 0;
3540 atomic_inc(&fp->fi_delegees);
3541 hash_delegation_locked(dp, fp);
3542 goto out_unlock;
3543 }
3544 fp->fi_lease = fl;
3545 fp->fi_deleg_file = filp;
3546 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003547 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003548 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003549 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003550 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003551out_unlock:
3552 spin_unlock(&fp->fi_lock);
3553 spin_unlock(&state_lock);
3554out_fput:
3555 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003556 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003557}
3558
Jeff Layton0b266932014-07-25 07:34:25 -04003559static struct nfs4_delegation *
3560nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3561 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003562{
Jeff Layton0b266932014-07-25 07:34:25 -04003563 int status;
3564 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003565
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003566 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003567 return ERR_PTR(-EAGAIN);
3568
3569 dp = alloc_init_deleg(clp, fh);
3570 if (!dp)
3571 return ERR_PTR(-ENOMEM);
3572
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003573 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003574 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003575 spin_lock(&fp->fi_lock);
3576 dp->dl_file = fp;
3577 if (!fp->fi_lease) {
3578 spin_unlock(&fp->fi_lock);
3579 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003580 status = nfs4_setlease(dp);
3581 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003582 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003583 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003584 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003585 status = -EAGAIN;
3586 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003587 }
Benny Halevy931ee562014-05-30 09:09:27 -04003588 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003589 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003590out_unlock:
3591 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003592 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003593out:
3594 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003595 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003596 return ERR_PTR(status);
3597 }
3598 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003599}
3600
Benny Halevy4aa89132012-02-21 14:16:44 -08003601static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3602{
3603 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3604 if (status == -EAGAIN)
3605 open->op_why_no_deleg = WND4_CONTENTION;
3606 else {
3607 open->op_why_no_deleg = WND4_RESOURCE;
3608 switch (open->op_deleg_want) {
3609 case NFS4_SHARE_WANT_READ_DELEG:
3610 case NFS4_SHARE_WANT_WRITE_DELEG:
3611 case NFS4_SHARE_WANT_ANY_DELEG:
3612 break;
3613 case NFS4_SHARE_WANT_CANCEL:
3614 open->op_why_no_deleg = WND4_CANCELLED;
3615 break;
3616 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003617 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003618 }
3619 }
3620}
3621
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622/*
3623 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003624 *
3625 * Note we don't support write delegations, and won't until the vfs has
3626 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 */
3628static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003629nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3630 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631{
3632 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003633 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3634 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003635 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003636 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003638 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003639 open->op_recall = 0;
3640 switch (open->op_claim_type) {
3641 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003642 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003643 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003644 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3645 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003646 break;
3647 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003648 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003649 /*
3650 * Let's not give out any delegations till everyone's
3651 * had the chance to reclaim theirs....
3652 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003653 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003654 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003655 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003656 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003657 /*
3658 * Also, if the file was opened for write or
3659 * create, there's a good chance the client's
3660 * about to write to it, resulting in an
3661 * immediate recall (since we don't support
3662 * write delegations):
3663 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003664 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003665 goto out_no_deleg;
3666 if (open->op_create == NFS4_OPEN_CREATE)
3667 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003668 break;
3669 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003670 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003671 }
Jeff Layton0b266932014-07-25 07:34:25 -04003672 dp = nfs4_set_delegation(clp, fh, stp->st_file);
3673 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003674 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003676 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003678 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003679 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003680 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003681 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003682out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003683 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3684 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003685 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003686 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003687 open->op_recall = 1;
3688 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003689
3690 /* 4.1 client asking for a delegation? */
3691 if (open->op_deleg_want)
3692 nfsd4_open_deleg_none_ext(open, status);
3693 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694}
3695
Benny Halevye27f49c2012-02-21 14:16:54 -08003696static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3697 struct nfs4_delegation *dp)
3698{
3699 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3700 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3701 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3702 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3703 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3704 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3705 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3706 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3707 }
3708 /* Otherwise the client must be confused wanting a delegation
3709 * it already has, therefore we don't return
3710 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3711 */
3712}
3713
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714/*
3715 * called with nfs4_lock_state() held.
3716 */
Al Virob37ad282006-10-19 23:28:59 -07003717__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3719{
Andy Adamson66689582009-04-03 08:28:45 +03003720 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003721 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003723 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003724 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003725 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 /*
3728 * Lookup file; if found, lookup stateid and check open request,
3729 * and check for delegations in the process of being recalled.
3730 * If not found, create the nfs4_file struct
3731 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003732 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04003733 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04003734 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003735 if (status)
3736 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003737 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04003739 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003740 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003741 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003742 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003743 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 }
3745
3746 /*
3747 * OPEN the file, or upgrade an existing OPEN.
3748 * If truncate fails, the OPEN fails.
3749 */
3750 if (stp) {
3751 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003752 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 if (status)
3754 goto out;
3755 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003756 stp = open->op_stp;
3757 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003758 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003759 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3760 if (status) {
3761 release_open_stateid(stp);
3762 goto out;
3763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003765 update_stateid(&stp->st_stid.sc_stateid);
3766 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767
Benny Halevyd24433c2012-02-16 20:57:17 +02003768 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02003769 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3770 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3771 open->op_why_no_deleg = WND4_NOT_WANTED;
3772 goto nodeleg;
3773 }
3774 }
3775
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 /*
3777 * Attempt to hand out a delegation. No error return, because the
3778 * OPEN succeeds even if we fail.
3779 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003780 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003781nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 status = nfs_ok;
3783
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003784 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003785 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003787 /* 4.1 client trying to upgrade/downgrade delegation? */
3788 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003789 open->op_deleg_want)
3790 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003791
NeilBrown13cd2182005-06-23 22:03:10 -07003792 if (fp)
3793 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003794 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003795 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 /*
3797 * To finish the open response, we just need to set the rflags.
3798 */
3799 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003800 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003801 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3803
3804 return status;
3805}
3806
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003807void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3808{
3809 if (open->op_openowner) {
3810 struct nfs4_openowner *oo = open->op_openowner;
3811
3812 if (!list_empty(&oo->oo_owner.so_stateids))
3813 list_del_init(&oo->oo_close_lru);
3814 if (oo->oo_flags & NFS4_OO_NEW) {
3815 if (status) {
3816 release_openowner(oo);
3817 open->op_openowner = NULL;
3818 } else
3819 oo->oo_flags &= ~NFS4_OO_NEW;
3820 }
3821 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003822 if (open->op_file)
3823 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003824 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04003825 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003826}
3827
Al Virob37ad282006-10-19 23:28:59 -07003828__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003829nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3830 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831{
3832 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003833 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003834 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
3836 nfs4_lock_state();
3837 dprintk("process_renew(%08x/%08x): starting\n",
3838 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003839 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003840 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003842 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003844 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003845 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 goto out;
3847 status = nfs_ok;
3848out:
3849 nfs4_unlock_state();
3850 return status;
3851}
3852
NeilBrowna76b4312005-06-23 22:04:01 -07003853static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003854nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07003855{
Jeff Layton33dcc482012-04-10 11:08:48 -04003856 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003857 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04003858 return;
3859
NeilBrowna76b4312005-06-23 22:04:01 -07003860 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003861 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003862 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003863 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003864 /*
3865 * Now that every NFSv4 client has had the chance to recover and
3866 * to see the (possibly new, possibly shorter) lease time, we
3867 * can safely set the next grace time to the current lease time:
3868 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03003869 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003870}
3871
NeilBrownfd39ca92005-06-23 22:04:03 -07003872static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003873nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874{
3875 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003876 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 struct nfs4_delegation *dp;
3878 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003879 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003880 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
3882 nfs4_lock_state();
3883
3884 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003885 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03003886 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003887 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03003888 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 clp = list_entry(pos, struct nfs4_client, cl_lru);
3890 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3891 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003892 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 break;
3894 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003895 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03003896 dprintk("NFSD: client in use (clientid %08x)\n",
3897 clp->cl_clientid.cl_id);
3898 continue;
3899 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003900 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003901 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003902 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03003903 list_for_each_safe(pos, next, &reaplist) {
3904 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 dprintk("NFSD: purging unused client (clientid %08x)\n",
3906 clp->cl_clientid.cl_id);
3907 expire_client(clp);
3908 }
Benny Halevycdc97502014-05-30 09:09:30 -04003909 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003910 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03003912 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3913 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003915 t = dp->dl_time - cutoff;
3916 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 break;
3918 }
Jeff Layton42690672014-07-25 07:34:20 -04003919 unhash_delegation_locked(dp);
3920 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 }
Benny Halevycdc97502014-05-30 09:09:30 -04003922 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04003923 while (!list_empty(&reaplist)) {
3924 dp = list_first_entry(&reaplist, struct nfs4_delegation,
3925 dl_recall_lru);
3926 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003927 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003929 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003930 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3931 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003932 t = oo->oo_time - cutoff;
3933 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 break;
3935 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003936 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04003938 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04003940 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941}
3942
Harvey Harrisona254b242008-02-20 12:49:00 -08003943static struct workqueue_struct *laundry_wq;
3944static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08003945
3946static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003947laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948{
3949 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003950 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3951 work);
3952 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3953 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003955 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003957 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958}
3959
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003960static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07003961{
Trond Myklebustb07c54a2014-07-23 16:17:40 -04003962 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003963 return nfserr_bad_stateid;
3964 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965}
3966
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003968access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003970 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3971 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3972 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973}
3974
3975static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003976access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003978 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3979 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980}
3981
3982static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003983__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984{
Al Virob37ad282006-10-19 23:28:59 -07003985 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986
J. Bruce Fields02921912010-07-29 15:16:59 -04003987 /* For lock stateid's, we test the parent open, not the lock: */
3988 if (stp->st_openstp)
3989 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003990 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003992 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 goto out;
3994 status = nfs_ok;
3995out:
3996 return status;
3997}
3998
Al Virob37ad282006-10-19 23:28:59 -07003999static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004000check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004002 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004004 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004005 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 * conflicting state; so we must wait out the grace period. */
4007 return nfserr_grace;
4008 } else if (flags & WR_STATE)
4009 return nfs4_share_conflict(current_fh,
4010 NFS4_SHARE_DENY_WRITE);
4011 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4012 return nfs4_share_conflict(current_fh,
4013 NFS4_SHARE_DENY_READ);
4014}
4015
4016/*
4017 * Allow READ/WRITE during grace period on recovered state only for files
4018 * that are not able to provide mandatory locking.
4019 */
4020static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004021grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004023 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024}
4025
J. Bruce Fields81b82962011-08-23 11:03:29 -04004026/* Returns true iff a is later than b: */
4027static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4028{
Jim Rees1a9357f2013-05-17 17:33:00 -04004029 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004030}
4031
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004032static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004033{
Andy Adamson66689582009-04-03 08:28:45 +03004034 /*
4035 * When sessions are used the stateid generation number is ignored
4036 * when it is zero.
4037 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004038 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004039 return nfs_ok;
4040
4041 if (in->si_generation == ref->si_generation)
4042 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004043
J. Bruce Fields0836f582008-01-26 19:08:12 -05004044 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004045 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004046 return nfserr_bad_stateid;
4047 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004048 * However, we could see a stateid from the past, even from a
4049 * non-buggy client. For example, if the client sends a lock
4050 * while some IO is outstanding, the lock may bump si_generation
4051 * while the IO is still in flight. The client could avoid that
4052 * situation by waiting for responses on all the IO requests,
4053 * but better performance may result in retrying IO that
4054 * receives an old_stateid error if requests are rarely
4055 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004056 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004057 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004058}
4059
Chuck Lever7df302f2012-05-29 13:56:37 -04004060static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004061{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004062 struct nfs4_stid *s;
4063 struct nfs4_ol_stateid *ols;
4064 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004065
Chuck Lever7df302f2012-05-29 13:56:37 -04004066 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4067 return nfserr_bad_stateid;
4068 /* Client debugging aid. */
4069 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4070 char addr_str[INET6_ADDRSTRLEN];
4071 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4072 sizeof(addr_str));
4073 pr_warn_ratelimited("NFSD: client %s testing state ID "
4074 "with incorrect client ID\n", addr_str);
4075 return nfserr_bad_stateid;
4076 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004077 s = find_stateid(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004078 if (!s)
Chuck Lever7df302f2012-05-29 13:56:37 -04004079 return nfserr_bad_stateid;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004080 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004081 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004082 return status;
J. Bruce Fields23340032013-04-09 17:42:28 -04004083 switch (s->sc_type) {
4084 case NFS4_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004085 return nfs_ok;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004086 case NFS4_REVOKED_DELEG_STID:
4087 return nfserr_deleg_revoked;
J. Bruce Fields23340032013-04-09 17:42:28 -04004088 case NFS4_OPEN_STID:
4089 case NFS4_LOCK_STID:
4090 ols = openlockstateid(s);
4091 if (ols->st_stateowner->so_is_open_owner
4092 && !(openowner(ols->st_stateowner)->oo_flags
4093 & NFS4_OO_CONFIRMED))
4094 return nfserr_bad_stateid;
4095 return nfs_ok;
4096 default:
4097 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004098 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004099 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004100 case NFS4_CLOSED_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004101 return nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004102 }
Bryan Schumaker17456802011-07-13 10:50:48 -04004103}
4104
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004105static __be32
4106nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4107 stateid_t *stateid, unsigned char typemask,
4108 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004109{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004110 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004111
4112 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4113 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004114 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004115 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004116 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004117 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004118 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004119 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004120 if (status)
4121 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004122 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004123 if (!*s)
4124 return nfserr_bad_stateid;
4125 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004126}
4127
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128/*
4129* Checks for stateid operations
4130*/
Al Virob37ad282006-10-19 23:28:59 -07004131__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004132nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004133 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004135 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004136 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004138 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004140 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004141 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004142 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 if (filpp)
4145 *filpp = NULL;
4146
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004147 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 return nfserr_grace;
4149
4150 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004151 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152
Trond Myklebust14bcab12014-04-18 14:44:07 -04004153 nfs4_lock_state();
4154
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004155 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004156 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004157 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004158 if (status)
Trond Myklebust14bcab12014-04-18 14:44:07 -04004159 goto out;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004160 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4161 if (status)
4162 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004163 switch (s->sc_type) {
4164 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004165 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004166 status = nfs4_check_delegmode(dp, flags);
4167 if (status)
4168 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004169 if (filpp) {
Trond Myklebust14bcab12014-04-18 14:44:07 -04004170 file = dp->dl_file->fi_deleg_file;
4171 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004172 WARN_ON_ONCE(1);
4173 status = nfserr_serverfault;
4174 goto out;
4175 }
Trond Myklebustde186432014-07-10 14:07:26 -04004176 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004177 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004178 break;
4179 case NFS4_OPEN_STID:
4180 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004181 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004182 status = nfs4_check_fh(current_fh, stp);
4183 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004185 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004186 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004188 status = nfs4_check_openmode(stp, flags);
4189 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004191 if (filpp) {
4192 if (flags & RD_STATE)
Trond Myklebust14bcab12014-04-18 14:44:07 -04004193 file = find_readable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004194 else
Trond Myklebust14bcab12014-04-18 14:44:07 -04004195 file = find_writeable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004196 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004197 break;
4198 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004199 status = nfserr_bad_stateid;
4200 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 }
4202 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004203 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004204 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205out:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004206 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 return status;
4208}
4209
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004210static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004211nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004212{
J. Bruce Fieldsa1b8ff42014-05-20 15:55:21 -04004213 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4214
4215 if (check_for_locks(stp->st_file, lo))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004216 return nfserr_locks_held;
Jeff Laytonc53530d2014-06-30 11:48:39 -04004217 release_lockowner_if_empty(lo);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004218 return nfs_ok;
4219}
4220
4221/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004222 * Test if the stateid is valid
4223 */
4224__be32
4225nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4226 struct nfsd4_test_stateid *test_stateid)
4227{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004228 struct nfsd4_test_stateid_id *stateid;
4229 struct nfs4_client *cl = cstate->session->se_client;
4230
4231 nfs4_lock_state();
4232 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004233 stateid->ts_id_status =
4234 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004235 nfs4_unlock_state();
4236
Bryan Schumaker17456802011-07-13 10:50:48 -04004237 return nfs_ok;
4238}
4239
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004240__be32
4241nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4242 struct nfsd4_free_stateid *free_stateid)
4243{
4244 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004245 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004246 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004247 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004248 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004249
4250 nfs4_lock_state();
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004251 s = find_stateid(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004252 if (!s)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004253 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004254 switch (s->sc_type) {
4255 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004256 ret = nfserr_locks_held;
4257 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004258 case NFS4_OPEN_STID:
4259 case NFS4_LOCK_STID:
4260 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4261 if (ret)
4262 goto out;
4263 if (s->sc_type == NFS4_LOCK_STID)
4264 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4265 else
4266 ret = nfserr_locks_held;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004267 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004268 case NFS4_REVOKED_DELEG_STID:
4269 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004270 spin_lock(&cl->cl_lock);
4271 list_del_init(&dp->dl_recall_lru);
4272 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004273 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004274 ret = nfs_ok;
4275 break;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004276 default:
4277 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004278 }
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004279out:
4280 nfs4_unlock_state();
4281 return ret;
4282}
4283
NeilBrown4c4cd222005-07-07 17:59:27 -07004284static inline int
4285setlkflg (int type)
4286{
4287 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4288 RD_STATE : WR_STATE;
4289}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004291static __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 -04004292{
4293 struct svc_fh *current_fh = &cstate->current_fh;
4294 struct nfs4_stateowner *sop = stp->st_stateowner;
4295 __be32 status;
4296
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004297 status = nfsd4_check_seqid(cstate, sop, seqid);
4298 if (status)
4299 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004300 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4301 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004302 /*
4303 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004304 * nfserr_replay_me from the previous step, and
4305 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004306 */
4307 return nfserr_bad_stateid;
4308 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4309 if (status)
4310 return status;
4311 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004312}
4313
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314/*
4315 * Checks for sequence id mutating operations.
4316 */
Al Virob37ad282006-10-19 23:28:59 -07004317static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004318nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004319 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004320 struct nfs4_ol_stateid **stpp,
4321 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004323 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004324 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004325 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004327 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4328 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004329
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004331 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004332 if (status)
4333 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004334 stp = openlockstateid(s);
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04004335 if (!nfsd4_has_session(cstate))
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004336 cstate->replay_owner = stp->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004338 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4339 if (!status)
4340 *stpp = stp;
4341 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004342}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004343
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004344static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4345 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004346{
4347 __be32 status;
4348 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004350 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004351 NFS4_OPEN_STID, stpp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004352 if (status)
4353 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004354 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004355 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07004356 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004357 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358}
4359
Al Virob37ad282006-10-19 23:28:59 -07004360__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004361nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004362 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363{
Al Virob37ad282006-10-19 23:28:59 -07004364 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004365 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004366 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004367 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368
Al Viroa6a9f182013-09-16 10:57:01 -04004369 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4370 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004372 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004373 if (status)
4374 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375
4376 nfs4_lock_state();
4377
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004378 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004379 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004380 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004381 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004382 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004383 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004384 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004385 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004386 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004387 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004388 update_stateid(&stp->st_stid.sc_stateid);
4389 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004390 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004391 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004392
Jeff Layton2a4317c2012-03-21 16:42:43 -04004393 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004394 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004396 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004397 if (!cstate->replay_owner)
4398 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 return status;
4400}
4401
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004402static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004404 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004405 return;
Jeff Layton12659652014-07-10 14:07:28 -04004406 nfs4_file_put_access(stp->st_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004407 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004408}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004409
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004410static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4411{
4412 switch (to_access) {
4413 case NFS4_SHARE_ACCESS_READ:
4414 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4415 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4416 break;
4417 case NFS4_SHARE_ACCESS_WRITE:
4418 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4419 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4420 break;
4421 case NFS4_SHARE_ACCESS_BOTH:
4422 break;
4423 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004424 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 }
4426}
4427
Al Virob37ad282006-10-19 23:28:59 -07004428__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004429nfsd4_open_downgrade(struct svc_rqst *rqstp,
4430 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004431 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432{
Al Virob37ad282006-10-19 23:28:59 -07004433 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004434 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004435 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436
Al Viroa6a9f182013-09-16 10:57:01 -04004437 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4438 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004440 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004441 if (od->od_deleg_want)
4442 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4443 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444
4445 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004446 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004447 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004448 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004451 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004452 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 stp->st_access_bmap, od->od_share_access);
4454 goto out;
4455 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004456 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004457 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 stp->st_deny_bmap, od->od_share_deny);
4459 goto out;
4460 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004461 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
Jeff Laytonce0fc432012-05-11 09:45:14 -04004463 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004465 update_stateid(&stp->st_stid.sc_stateid);
4466 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 status = nfs_ok;
4468out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004469 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004470 if (!cstate->replay_owner)
4471 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 return status;
4473}
4474
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004475static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4476{
Trond Myklebustacf92952014-06-30 11:48:37 -04004477 struct nfs4_client *clp = s->st_stid.sc_client;
4478 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4479
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004480 s->st_stid.sc_type = NFS4_CLOSED_STID;
Trond Myklebustacf92952014-06-30 11:48:37 -04004481 unhash_open_stateid(s);
4482
4483 if (clp->cl_minorversion) {
Trond Myklebustacf92952014-06-30 11:48:37 -04004484 if (list_empty(&oo->oo_owner.so_stateids))
4485 release_openowner(oo);
Trond Myklebust60116952014-07-29 21:34:06 -04004486 nfs4_put_stid(&s->st_stid);
Trond Myklebustacf92952014-06-30 11:48:37 -04004487 } else {
Trond Myklebust60116952014-07-29 21:34:06 -04004488 /*
4489 * In the 4.0 case we need to keep the owners around a
4490 * little while to handle CLOSE replay. We still do need
4491 * to release any file access that is held by them
4492 * before returning however.
4493 */
4494 release_all_access(s);
Trond Myklebustf8338832014-07-25 07:34:19 -04004495 if (s->st_file) {
4496 put_nfs4_file(s->st_file);
4497 s->st_file = NULL;
4498 }
Trond Myklebustacf92952014-06-30 11:48:37 -04004499 oo->oo_last_closed_stid = s;
Trond Myklebustacf92952014-06-30 11:48:37 -04004500 if (list_empty(&oo->oo_owner.so_stateids))
4501 move_to_close_lru(oo, clp->net);
4502 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004503}
4504
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505/*
4506 * nfs4_unlock_state() called after encode
4507 */
Al Virob37ad282006-10-19 23:28:59 -07004508__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004509nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004510 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511{
Al Virob37ad282006-10-19 23:28:59 -07004512 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004513 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004514 struct net *net = SVC_NET(rqstp);
4515 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516
Al Viroa6a9f182013-09-16 10:57:01 -04004517 dprintk("NFSD: nfsd4_close on file %pd\n",
4518 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519
4520 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004521 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4522 &close->cl_stateid,
4523 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004524 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004525 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004526 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004528 update_stateid(&stp->st_stid.sc_stateid);
4529 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004531 nfsd4_close_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004533 if (!cstate->replay_owner)
4534 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 return status;
4536}
4537
Al Virob37ad282006-10-19 23:28:59 -07004538__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004539nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4540 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004542 struct nfs4_delegation *dp;
4543 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004544 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004545 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004546 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004548 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004549 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550
4551 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004552 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004553 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004554 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004555 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004556 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004557 if (status)
4558 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004559
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004560 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004562 nfs4_unlock_state();
4563
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 return status;
4565}
4566
4567
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569
Benny Halevy87df4de2008-12-15 19:42:03 +02004570static inline u64
4571end_offset(u64 start, u64 len)
4572{
4573 u64 end;
4574
4575 end = start + len;
4576 return end >= start ? end: NFS4_MAX_UINT64;
4577}
4578
4579/* last octet in a range */
4580static inline u64
4581last_byte_offset(u64 start, u64 len)
4582{
4583 u64 end;
4584
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004585 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004586 end = start + len;
4587 return end > start ? end - 1: NFS4_MAX_UINT64;
4588}
4589
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590/*
4591 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4592 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4593 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4594 * locking, this prevents us from being completely protocol-compliant. The
4595 * real solution to this problem is to start using unsigned file offsets in
4596 * the VFS, but this is a very deep change!
4597 */
4598static inline void
4599nfs4_transform_lock_offset(struct file_lock *lock)
4600{
4601 if (lock->fl_start < 0)
4602 lock->fl_start = OFFSET_MAX;
4603 if (lock->fl_end < 0)
4604 lock->fl_end = OFFSET_MAX;
4605}
4606
NeilBrownd5b90262006-04-10 22:55:22 -07004607/* Hack!: For now, we're defining this just so we can use a pointer to it
4608 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004609static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004610};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
4612static inline void
4613nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4614{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004615 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616
NeilBrownd5b90262006-04-10 22:55:22 -07004617 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004618 lo = (struct nfs4_lockowner *) fl->fl_owner;
4619 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4620 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004621 if (!deny->ld_owner.data)
4622 /* We just don't care that much */
4623 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004624 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4625 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004626 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004627nevermind:
4628 deny->ld_owner.len = 0;
4629 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004630 deny->ld_clientid.cl_boot = 0;
4631 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 }
4633 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004634 deny->ld_length = NFS4_MAX_UINT64;
4635 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4637 deny->ld_type = NFS4_READ_LT;
4638 if (fl->fl_type != F_RDLCK)
4639 deny->ld_type = NFS4_WRITE_LT;
4640}
4641
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004642static struct nfs4_lockowner *
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004643find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4644 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645{
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004646 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4647 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004649 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4650 if (so->so_is_open_owner)
4651 continue;
4652 if (!same_owner_str(so, owner, clid))
4653 continue;
4654 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 }
4656 return NULL;
4657}
4658
4659/*
4660 * Alloc a lock owner structure.
4661 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004662 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004664 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004666static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004667alloc_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 -04004668 struct nfs4_lockowner *lo;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004669 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004671 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4672 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004674 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4675 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07004676 /* It is the openowner seqid that will be incremented in encode in the
4677 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004678 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004679 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004680 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681}
4682
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004683static struct nfs4_ol_stateid *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004684alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp,
4685 struct inode *inode,
4686 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004688 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004689 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690
J. Bruce Fields996e0932011-10-17 11:14:48 -04004691 stp = nfs4_alloc_stateid(clp);
NeilBrown5ac049a2005-06-23 22:03:03 -07004692 if (stp == NULL)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04004693 return NULL;
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004694 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004695 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4696 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07004697 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 stp->st_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004699 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004701 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004702 list_add(&stp->st_locks, &open_stp->st_locks);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004703 spin_lock(&fp->fi_lock);
4704 list_add(&stp->st_perfile, &fp->fi_stateids);
4705 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 return stp;
4707}
4708
Jeff Laytonc53530d2014-06-30 11:48:39 -04004709static struct nfs4_ol_stateid *
4710find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4711{
4712 struct nfs4_ol_stateid *lst;
4713
4714 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
4715 if (lst->st_file == fp)
4716 return lst;
4717 }
4718 return NULL;
4719}
4720
4721
NeilBrownfd39ca92005-06-23 22:04:03 -07004722static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723check_lock_length(u64 offset, u64 length)
4724{
Benny Halevy87df4de2008-12-15 19:42:03 +02004725 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 LOFF_OVERFLOW(offset, length)));
4727}
4728
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004729static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004730{
4731 struct nfs4_file *fp = lock_stp->st_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004732
Jeff Layton7214e862014-07-10 14:07:33 -04004733 lockdep_assert_held(&fp->fi_lock);
4734
Jeff Layton82c5ff12012-05-11 09:45:13 -04004735 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004736 return;
Jeff Layton12659652014-07-10 14:07:28 -04004737 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004738 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004739}
4740
J. Bruce Fields2355c592012-04-25 16:56:22 -04004741static __be32 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, struct nfs4_ol_stateid *ost, struct nfsd4_lock *lock, struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004742{
4743 struct nfs4_file *fi = ost->st_file;
4744 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4745 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004746 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004747 struct nfs4_lockowner *lo;
4748 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004749 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004750
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004751 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004752 if (!lo) {
4753 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4754 &lock->v.new.owner);
4755 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4756 if (lo == NULL)
4757 return nfserr_jukebox;
4758 } else {
4759 /* with an existing lockowner, seqids must be the same */
4760 if (!cstate->minorversion &&
4761 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004762 return nfserr_bad_seqid;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004763 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004764
4765 *lst = find_lock_stateid(lo, fi);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004766 if (*lst == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004767 *lst = alloc_init_lock_stateid(lo, fi, inode, ost);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004768 if (*lst == NULL) {
4769 release_lockowner_if_empty(lo);
4770 return nfserr_jukebox;
4771 }
4772 *new = true;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004773 }
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004774 return nfs_ok;
4775}
4776
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777/*
4778 * LOCK operation
4779 */
Al Virob37ad282006-10-19 23:28:59 -07004780__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004781nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004782 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004784 struct nfs4_openowner *open_sop = NULL;
4785 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004786 struct nfs4_ol_stateid *lock_stp;
Jeff Layton7214e862014-07-10 14:07:33 -04004787 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004788 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004789 struct file_lock *file_lock = NULL;
4790 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004791 __be32 status = 0;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004792 bool new_state = false;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004793 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004794 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004795 struct net *net = SVC_NET(rqstp);
4796 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797
4798 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4799 (long long) lock->lk_offset,
4800 (long long) lock->lk_length);
4801
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 if (check_lock_length(lock->lk_offset, lock->lk_length))
4803 return nfserr_inval;
4804
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004805 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004806 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004807 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4808 return status;
4809 }
4810
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 nfs4_lock_state();
4812
4813 if (lock->lk_is_new) {
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004814 struct nfs4_ol_stateid *open_stp = NULL;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004815
4816 if (nfsd4_has_session(cstate))
4817 /* See rfc 5661 18.10.3: given clientid is ignored: */
4818 memcpy(&lock->v.new.clientid,
4819 &cstate->session->se_client->cl_clientid,
4820 sizeof(clientid_t));
4821
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004823 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004827 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 lock->lk_new_open_seqid,
4829 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004830 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07004831 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004833 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004834 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004835 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004836 &lock->v.new.clientid))
4837 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004838 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4839 &lock_stp, &new_state);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004840 } else
Benny Halevydd453df2009-04-03 08:28:41 +03004841 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004842 lock->lk_old_lock_seqid,
4843 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004844 NFS4_LOCK_STID, &lock_stp, nn);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004845 if (status)
4846 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004847 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004849 lkflg = setlkflg(lock->lk_type);
4850 status = nfs4_check_openmode(lock_stp, lkflg);
4851 if (status)
4852 goto out;
4853
NeilBrown0dd395d2005-07-07 17:59:15 -07004854 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004855 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004856 goto out;
4857 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004858 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004859 goto out;
4860
Jeff Layton21179d82012-08-21 08:03:32 -04004861 file_lock = locks_alloc_lock();
4862 if (!file_lock) {
4863 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4864 status = nfserr_jukebox;
4865 goto out;
4866 }
4867
Jeff Layton7214e862014-07-10 14:07:33 -04004868 fp = lock_stp->st_file;
Jeff Layton21179d82012-08-21 08:03:32 -04004869 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 switch (lock->lk_type) {
4871 case NFS4_READ_LT:
4872 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04004873 spin_lock(&fp->fi_lock);
4874 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004875 if (filp)
4876 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04004877 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04004878 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004879 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 case NFS4_WRITE_LT:
4881 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04004882 spin_lock(&fp->fi_lock);
4883 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004884 if (filp)
4885 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04004886 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04004887 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004888 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 default:
4890 status = nfserr_inval;
4891 goto out;
4892 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004893 if (!filp) {
4894 status = nfserr_openmode;
4895 goto out;
4896 }
Jeff Layton21179d82012-08-21 08:03:32 -04004897 file_lock->fl_owner = (fl_owner_t)lock_sop;
4898 file_lock->fl_pid = current->tgid;
4899 file_lock->fl_file = filp;
4900 file_lock->fl_flags = FL_POSIX;
4901 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4902 file_lock->fl_start = lock->lk_offset;
4903 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4904 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905
Jeff Layton21179d82012-08-21 08:03:32 -04004906 conflock = locks_alloc_lock();
4907 if (!conflock) {
4908 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4909 status = nfserr_jukebox;
4910 goto out;
4911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912
Jeff Layton21179d82012-08-21 08:03:32 -04004913 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004914 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004916 update_stateid(&lock_stp->st_stid.sc_stateid);
4917 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004919 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004920 break;
4921 case (EAGAIN): /* conflock holds conflicting lock */
4922 status = nfserr_denied;
4923 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04004924 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004925 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 case (EDEADLK):
4927 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004928 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004929 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004930 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004931 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004932 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934out:
Trond Myklebustde186432014-07-10 14:07:26 -04004935 if (filp)
4936 fput(filp);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004937 if (status && new_state)
Jeff Laytonc53530d2014-06-30 11:48:39 -04004938 release_lock_stateid(lock_stp);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004939 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004940 if (!cstate->replay_owner)
4941 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004942 if (file_lock)
4943 locks_free_lock(file_lock);
4944 if (conflock)
4945 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 return status;
4947}
4948
4949/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004950 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4951 * so we do a temporary open here just to get an open file to pass to
4952 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4953 * inode operation.)
4954 */
Al Viro04da6e92012-04-13 00:00:04 -04004955static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004956{
4957 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04004958 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4959 if (!err) {
4960 err = nfserrno(vfs_test_lock(file, lock));
4961 nfsd_close(file);
4962 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004963 return err;
4964}
4965
4966/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 * LOCKT operation
4968 */
Al Virob37ad282006-10-19 23:28:59 -07004969__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004970nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4971 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972{
Jeff Layton21179d82012-08-21 08:03:32 -04004973 struct file_lock *file_lock = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004974 struct nfs4_lockowner *lo;
Al Virob37ad282006-10-19 23:28:59 -07004975 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004976 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004978 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 return nfserr_grace;
4980
4981 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4982 return nfserr_inval;
4983
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 nfs4_lock_state();
4985
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004986 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004987 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004988 if (status)
4989 goto out;
4990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004992 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994
Jeff Layton21179d82012-08-21 08:03:32 -04004995 file_lock = locks_alloc_lock();
4996 if (!file_lock) {
4997 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4998 status = nfserr_jukebox;
4999 goto out;
5000 }
5001 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 switch (lockt->lt_type) {
5003 case NFS4_READ_LT:
5004 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005005 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 break;
5007 case NFS4_WRITE_LT:
5008 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005009 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 break;
5011 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005012 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 status = nfserr_inval;
5014 goto out;
5015 }
5016
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005017 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005018 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005019 file_lock->fl_owner = (fl_owner_t)lo;
5020 file_lock->fl_pid = current->tgid;
5021 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022
Jeff Layton21179d82012-08-21 08:03:32 -04005023 file_lock->fl_start = lockt->lt_offset;
5024 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025
Jeff Layton21179d82012-08-21 08:03:32 -04005026 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027
Jeff Layton21179d82012-08-21 08:03:32 -04005028 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005029 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005030 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005031
Jeff Layton21179d82012-08-21 08:03:32 -04005032 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005034 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 }
5036out:
5037 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005038 if (file_lock)
5039 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 return status;
5041}
5042
Al Virob37ad282006-10-19 23:28:59 -07005043__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005044nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005045 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005047 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005049 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005050 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005051 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005052 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5053
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5055 (long long) locku->lu_offset,
5056 (long long) locku->lu_length);
5057
5058 if (check_lock_length(locku->lu_offset, locku->lu_length))
5059 return nfserr_inval;
5060
5061 nfs4_lock_state();
5062
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005063 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005064 &locku->lu_stateid, NFS4_LOCK_STID,
5065 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005066 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005068 filp = find_any_file(stp->st_file);
5069 if (!filp) {
5070 status = nfserr_lock_range;
5071 goto out;
5072 }
Jeff Layton21179d82012-08-21 08:03:32 -04005073 file_lock = locks_alloc_lock();
5074 if (!file_lock) {
5075 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5076 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005077 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005078 }
5079 locks_init_lock(file_lock);
5080 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005081 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005082 file_lock->fl_pid = current->tgid;
5083 file_lock->fl_file = filp;
5084 file_lock->fl_flags = FL_POSIX;
5085 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5086 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087
Jeff Layton21179d82012-08-21 08:03:32 -04005088 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5089 locku->lu_length);
5090 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091
Jeff Layton21179d82012-08-21 08:03:32 -04005092 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005093 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005094 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 goto out_nfserr;
5096 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005097 update_stateid(&stp->st_stid.sc_stateid);
5098 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005099fput:
5100 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005102 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields71c3bcd2011-09-27 21:42:29 -04005103 if (!cstate->replay_owner)
5104 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005105 if (file_lock)
5106 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 return status;
5108
5109out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005110 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005111 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112}
5113
5114/*
5115 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005116 * true: locks held by lockowner
5117 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005119static bool
5120check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121{
5122 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005123 int status = false;
5124 struct file *filp = find_any_file(fp);
5125 struct inode *inode;
5126
5127 if (!filp) {
5128 /* Any valid lock stateid should have some sort of access */
5129 WARN_ON_ONCE(1);
5130 return status;
5131 }
5132
5133 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134
Jeff Layton1c8c6012013-06-21 08:58:15 -04005135 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005137 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005138 status = true;
5139 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005142 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005143 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 return status;
5145}
5146
Al Virob37ad282006-10-19 23:28:59 -07005147__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005148nfsd4_release_lockowner(struct svc_rqst *rqstp,
5149 struct nfsd4_compound_state *cstate,
5150 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151{
5152 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Laytonfd449072014-06-30 11:48:41 -04005153 struct nfs4_stateowner *sop = NULL, *tmp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005154 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005155 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 struct xdr_netobj *owner = &rlockowner->rl_owner;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005157 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07005158 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005159 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160
5161 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5162 clid->cl_boot, clid->cl_id);
5163
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 nfs4_lock_state();
5165
Jeff Layton4b24ca72014-06-30 11:48:44 -04005166 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005167 if (status)
5168 goto out;
5169
NeilBrown3e9e3db2005-06-23 22:04:20 -07005170 status = nfserr_locks_held;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05005171
Jeff Laytonfd449072014-06-30 11:48:41 -04005172 /* Find the matching lock stateowner */
5173 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
5174 if (tmp->so_is_open_owner)
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005175 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005176 if (same_owner_str(tmp, owner, clid)) {
5177 sop = tmp;
5178 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07005180 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005181
5182 /* No matching owner found, maybe a replay? Just declare victory... */
5183 if (!sop) {
5184 status = nfs_ok;
5185 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005187
5188 lo = lockowner(sop);
5189 /* see if there are still any locks associated with it */
5190 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5191 if (check_for_locks(stp->st_file, lo))
5192 goto out;
5193 }
5194
5195 status = nfs_ok;
5196 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197out:
5198 nfs4_unlock_state();
5199 return status;
5200}
5201
5202static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005203alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204{
NeilBrowna55370a2005-06-23 22:03:52 -07005205 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206}
5207
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005208bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005209nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005210{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005211 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005212
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005213 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005214 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005215}
5216
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217/*
5218 * failure => all reset bets are off, nfserr_no_grace...
5219 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005220struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005221nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222{
5223 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005224 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225
NeilBrowna55370a2005-06-23 22:03:52 -07005226 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5227 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005228 if (crp) {
5229 strhashval = clientstr_hashval(name);
5230 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005231 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005232 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005233 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005234 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005235 }
5236 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237}
5238
Jeff Layton2a4317c2012-03-21 16:42:43 -04005239void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005240nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005241{
5242 list_del(&crp->cr_strhash);
5243 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005244 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005245}
5246
5247void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005248nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249{
5250 struct nfs4_client_reclaim *crp = NULL;
5251 int i;
5252
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005254 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5255 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005257 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 }
5259 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005260 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261}
5262
5263/*
5264 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005265struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005266nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267{
5268 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 struct nfs4_client_reclaim *crp = NULL;
5270
Jeff Layton278c9312012-11-12 15:00:52 -05005271 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272
Jeff Layton278c9312012-11-12 15:00:52 -05005273 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005274 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005275 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 return crp;
5277 }
5278 }
5279 return NULL;
5280}
5281
5282/*
5283* Called from OPEN. Look for clientid in reclaim list.
5284*/
Al Virob37ad282006-10-19 23:28:59 -07005285__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005286nfs4_check_open_reclaim(clientid_t *clid,
5287 struct nfsd4_compound_state *cstate,
5288 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005290 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005291
5292 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005293 status = lookup_clientid(clid, cstate, nn);
5294 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005295 return nfserr_reclaim_bad;
5296
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005297 if (nfsd4_client_record_check(cstate->clp))
5298 return nfserr_reclaim_bad;
5299
5300 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301}
5302
Bryan Schumaker65178db2011-11-01 13:35:21 -04005303#ifdef CONFIG_NFSD_FAULT_INJECTION
5304
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005305u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5306{
J. Bruce Fields221a6872013-04-01 22:23:49 -04005307 if (mark_client_expired(clp))
5308 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005309 expire_client(clp);
5310 return 1;
5311}
5312
Bryan Schumaker184c1842012-11-29 11:40:44 -05005313u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5314{
5315 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005316 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005317 printk(KERN_INFO "NFS Client: %s\n", buf);
5318 return 1;
5319}
5320
5321static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5322 const char *type)
5323{
5324 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005325 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005326 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5327}
5328
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005329static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5330 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005331{
5332 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005333 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005334 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005335 u64 count = 0;
5336
5337 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005338 list_for_each_entry_safe(stp, st_next,
5339 &oop->oo_owner.so_stateids, st_perstateowner) {
5340 list_for_each_entry_safe(lst, lst_next,
5341 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005342 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005343 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005344 if (++count == max)
5345 return count;
5346 }
5347 }
5348 }
5349
5350 return count;
5351}
5352
5353u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5354{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005355 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005356}
5357
Bryan Schumaker184c1842012-11-29 11:40:44 -05005358u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5359{
5360 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5361 nfsd_print_count(clp, count, "locked files");
5362 return count;
5363}
5364
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005365static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5366{
5367 struct nfs4_openowner *oop, *next;
5368 u64 count = 0;
5369
5370 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5371 if (func)
5372 func(oop);
5373 if (++count == max)
5374 break;
5375 }
5376
5377 return count;
5378}
5379
5380u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5381{
5382 return nfsd_foreach_client_open(clp, max, release_openowner);
5383}
5384
Bryan Schumaker184c1842012-11-29 11:40:44 -05005385u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5386{
5387 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5388 nfsd_print_count(clp, count, "open files");
5389 return count;
5390}
5391
Bryan Schumaker269de302012-11-29 11:40:42 -05005392static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5393 struct list_head *victims)
5394{
5395 struct nfs4_delegation *dp, *next;
5396 u64 count = 0;
5397
Benny Halevycdc97502014-05-30 09:09:30 -04005398 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005399 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005400 if (victims) {
5401 /*
5402 * It's not safe to mess with delegations that have a
5403 * non-zero dl_time. They might have already been broken
5404 * and could be processed by the laundromat outside of
5405 * the state_lock. Just leave them be.
5406 */
5407 if (dp->dl_time != 0)
5408 continue;
5409
Jeff Layton42690672014-07-25 07:34:20 -04005410 unhash_delegation_locked(dp);
5411 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005412 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005413 if (++count == max)
5414 break;
5415 }
5416 return count;
5417}
5418
5419u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5420{
5421 struct nfs4_delegation *dp, *next;
5422 LIST_HEAD(victims);
5423 u64 count;
5424
Benny Halevycdc97502014-05-30 09:09:30 -04005425 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005426 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005427 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005428
Jeff Layton2d4a5322014-07-25 07:34:21 -04005429 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5430 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005431 revoke_delegation(dp);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005432 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005433
5434 return count;
5435}
5436
5437u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5438{
Jeff Laytondff13992014-07-08 14:02:49 -04005439 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005440 LIST_HEAD(victims);
5441 u64 count;
5442
Benny Halevycdc97502014-05-30 09:09:30 -04005443 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005444 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005445 while (!list_empty(&victims)) {
5446 dp = list_first_entry(&victims, struct nfs4_delegation,
5447 dl_recall_lru);
5448 list_del_init(&dp->dl_recall_lru);
5449 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005450 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005451 }
Benny Halevycdc97502014-05-30 09:09:30 -04005452 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005453
5454 return count;
5455}
5456
Bryan Schumaker184c1842012-11-29 11:40:44 -05005457u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5458{
5459 u64 count = 0;
5460
Benny Halevycdc97502014-05-30 09:09:30 -04005461 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005462 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005463 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005464
5465 nfsd_print_count(clp, count, "delegations");
5466 return count;
5467}
5468
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005469u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005470{
5471 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005472 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005473 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005474
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005475 if (!nfsd_netns_ready(nn))
5476 return 0;
5477
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005478 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005479 count += func(clp, max - count);
5480 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005481 break;
5482 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005483
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005484 return count;
5485}
5486
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005487struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5488{
5489 struct nfs4_client *clp;
5490 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5491
5492 if (!nfsd_netns_ready(nn))
5493 return NULL;
5494
5495 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5496 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5497 return clp;
5498 }
5499 return NULL;
5500}
5501
Bryan Schumaker65178db2011-11-01 13:35:21 -04005502#endif /* CONFIG_NFSD_FAULT_INJECTION */
5503
Meelap Shahc2f1a552007-07-17 04:04:39 -07005504/*
5505 * Since the lifetime of a delegation isn't limited to that of an open, a
5506 * client may quite reasonably hang on to a delegation as long as it has
5507 * the inode cached. This becomes an obvious problem the first time a
5508 * client's inode cache approaches the size of the server's total memory.
5509 *
5510 * For now we avoid this problem by imposing a hard limit on the number
5511 * of delegations, which varies according to the server's memory size.
5512 */
5513static void
5514set_max_delegations(void)
5515{
5516 /*
5517 * Allow at most 4 delegations per megabyte of RAM. Quick
5518 * estimates suggest that in the worst case (where every delegation
5519 * is for a different inode), a delegation could take about 1.5K,
5520 * giving a worst case usage of about 6% of memory.
5521 */
5522 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5523}
5524
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005525static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005526{
5527 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5528 int i;
5529
5530 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5531 CLIENT_HASH_SIZE, GFP_KERNEL);
5532 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005533 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005534 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5535 CLIENT_HASH_SIZE, GFP_KERNEL);
5536 if (!nn->unconf_id_hashtbl)
5537 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005538 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5539 OWNER_HASH_SIZE, GFP_KERNEL);
5540 if (!nn->ownerstr_hashtbl)
5541 goto err_ownerstr;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005542 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5543 SESSION_HASH_SIZE, GFP_KERNEL);
5544 if (!nn->sessionid_hashtbl)
5545 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005546
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005547 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005548 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005549 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005550 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005551 for (i = 0; i < OWNER_HASH_SIZE; i++)
5552 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005553 for (i = 0; i < SESSION_HASH_SIZE; i++)
5554 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005555 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005556 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005557 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005558 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005559 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005560 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005561
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005562 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005563 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005564
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005565 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005566
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005567err_sessionid:
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03005568 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005569err_ownerstr:
5570 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005571err_unconf_id:
5572 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005573err:
5574 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005575}
5576
5577static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005578nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005579{
5580 int i;
5581 struct nfs4_client *clp = NULL;
5582 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5583
5584 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5585 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5586 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5587 destroy_client(clp);
5588 }
5589 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005590
Kinglong Mee2b905632014-03-26 22:09:30 +08005591 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5592 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5593 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5594 destroy_client(clp);
5595 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005596 }
5597
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005598 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005599 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005600 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005601 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005602 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005603}
5604
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005605int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005606nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005607{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005608 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005609 int ret;
5610
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005611 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005612 if (ret)
5613 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005614 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005615 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005616 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005617 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005618 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005619 nn->nfsd4_grace, net);
5620 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005621 return 0;
5622}
5623
5624/* initialization to perform when the nfsd service is started: */
5625
5626int
5627nfs4_state_start(void)
5628{
5629 int ret;
5630
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005631 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005632 if (ret)
5633 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005634 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005635 if (laundry_wq == NULL) {
5636 ret = -ENOMEM;
5637 goto out_recovery;
5638 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005639 ret = nfsd4_create_callback_queue();
5640 if (ret)
5641 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005642
Meelap Shahc2f1a552007-07-17 04:04:39 -07005643 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005644
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005645 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005646
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005647out_free_laundry:
5648 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005649out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005650 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651}
5652
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005653void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005654nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005658 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005660 cancel_delayed_work_sync(&nn->laundromat_work);
5661 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005662
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005663 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04005665 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005666 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005668 unhash_delegation_locked(dp);
5669 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 }
Benny Halevycdc97502014-05-30 09:09:30 -04005671 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 list_for_each_safe(pos, next, &reaplist) {
5673 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005674 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04005675 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 }
5677
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005678 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005679 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005680 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681}
5682
5683void
5684nfs4_state_shutdown(void)
5685{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005686 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005687 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005689
5690static void
5691get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5692{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005693 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5694 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005695}
5696
5697static void
5698put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5699{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005700 if (cstate->minorversion) {
5701 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5702 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5703 }
5704}
5705
5706void
5707clear_current_stateid(struct nfsd4_compound_state *cstate)
5708{
5709 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005710}
5711
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005712/*
5713 * functions to set current state id
5714 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005715void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005716nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5717{
5718 put_stateid(cstate, &odp->od_stateid);
5719}
5720
5721void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005722nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5723{
5724 put_stateid(cstate, &open->op_stateid);
5725}
5726
5727void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005728nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5729{
5730 put_stateid(cstate, &close->cl_stateid);
5731}
5732
5733void
5734nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5735{
5736 put_stateid(cstate, &lock->lk_resp_stateid);
5737}
5738
5739/*
5740 * functions to consume current state id
5741 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005742
5743void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005744nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5745{
5746 get_stateid(cstate, &odp->od_stateid);
5747}
5748
5749void
5750nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5751{
5752 get_stateid(cstate, &drp->dr_stateid);
5753}
5754
5755void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005756nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5757{
5758 get_stateid(cstate, &fsp->fr_stateid);
5759}
5760
5761void
5762nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5763{
5764 get_stateid(cstate, &setattr->sa_stateid);
5765}
5766
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005767void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005768nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5769{
5770 get_stateid(cstate, &close->cl_stateid);
5771}
5772
5773void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005774nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005775{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005776 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005777}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01005778
5779void
5780nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5781{
5782 get_stateid(cstate, &read->rd_stateid);
5783}
5784
5785void
5786nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5787{
5788 get_stateid(cstate, &write->wr_stateid);
5789}