blob: 4e1d7268b004faff8f79730003757483689c715e [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>
Daniel Borkmann87545892014-12-10 16:33:11 +010044#include <linux/jhash.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
J. Bruce Fields8b671b82009-02-22 14:51:34 -080078/*
79 * Currently used for the del_recall_lru and file hash table. In an
80 * effort to decrease the scope of the client_mutex, this spinlock may
81 * eventually cover more:
82 */
Benny Halevycdc97502014-05-30 09:09:30 -040083static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080084
Jeff Laytonb401be222014-07-29 21:34:33 -040085/*
86 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
87 * the refcount on the open stateid to drop.
88 */
89static DECLARE_WAIT_QUEUE_HEAD(close_wq);
90
Christoph Hellwigabf11352014-05-21 07:43:03 -070091static struct kmem_cache *openowner_slab;
92static struct kmem_cache *lockowner_slab;
93static struct kmem_cache *file_slab;
94static struct kmem_cache *stateid_slab;
95static struct kmem_cache *deleg_slab;
NeilBrowne60d4392005-06-23 22:03:01 -070096
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -040097static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -080098
Christoph Hellwig0162ac22014-09-24 12:19:19 +020099static struct nfsd4_callback_ops nfsd4_cb_recall_ops;
100
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400101static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800102{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400103 return ses->se_flags & NFS4_SESSION_DEAD;
104}
105
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400106static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
107{
108 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400109 return nfserr_jukebox;
110 ses->se_flags |= NFS4_SESSION_DEAD;
111 return nfs_ok;
112}
113
J. Bruce Fields221a6872013-04-01 22:23:49 -0400114static bool is_client_expired(struct nfs4_client *clp)
115{
116 return clp->cl_time == 0;
117}
118
J. Bruce Fields221a6872013-04-01 22:23:49 -0400119static __be32 get_client_locked(struct nfs4_client *clp)
120{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400121 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
122
123 lockdep_assert_held(&nn->client_lock);
124
J. Bruce Fields221a6872013-04-01 22:23:49 -0400125 if (is_client_expired(clp))
126 return nfserr_expired;
127 atomic_inc(&clp->cl_refcount);
128 return nfs_ok;
129}
130
131/* must be called under the client_lock */
132static inline void
133renew_client_locked(struct nfs4_client *clp)
134{
135 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
136
137 if (is_client_expired(clp)) {
138 WARN_ON(1);
139 printk("%s: client (clientid %08x/%08x) already expired\n",
140 __func__,
141 clp->cl_clientid.cl_boot,
142 clp->cl_clientid.cl_id);
143 return;
144 }
145
146 dprintk("renewing client (clientid %08x/%08x)\n",
147 clp->cl_clientid.cl_boot,
148 clp->cl_clientid.cl_id);
149 list_move_tail(&clp->cl_lru, &nn->client_lru);
150 clp->cl_time = get_seconds();
151}
152
153static inline void
154renew_client(struct nfs4_client *clp)
155{
156 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
157
158 spin_lock(&nn->client_lock);
159 renew_client_locked(clp);
160 spin_unlock(&nn->client_lock);
161}
162
Fengguang Wuba138432013-04-16 22:14:15 -0400163static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400164{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400165 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
166
167 lockdep_assert_held(&nn->client_lock);
168
J. Bruce Fields221a6872013-04-01 22:23:49 -0400169 if (!atomic_dec_and_test(&clp->cl_refcount))
170 return;
171 if (!is_client_expired(clp))
172 renew_client_locked(clp);
173}
174
Jeff Layton4b24ca72014-06-30 11:48:44 -0400175static void put_client_renew(struct nfs4_client *clp)
176{
177 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
178
Jeff Laytond6c249b2014-07-08 14:02:50 -0400179 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
180 return;
181 if (!is_client_expired(clp))
182 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400183 spin_unlock(&nn->client_lock);
184}
185
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400186static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
187{
188 __be32 status;
189
190 if (is_session_dead(ses))
191 return nfserr_badsession;
192 status = get_client_locked(ses->se_client);
193 if (status)
194 return status;
195 atomic_inc(&ses->se_ref);
196 return nfs_ok;
197}
198
199static void nfsd4_put_session_locked(struct nfsd4_session *ses)
200{
201 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400202 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
203
204 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400205
206 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
207 free_session(ses);
208 put_client_renew_locked(clp);
209}
210
211static void nfsd4_put_session(struct nfsd4_session *ses)
212{
213 struct nfs4_client *clp = ses->se_client;
214 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
215
216 spin_lock(&nn->client_lock);
217 nfsd4_put_session_locked(ses);
218 spin_unlock(&nn->client_lock);
219}
220
Kinglong Meeb5971af2014-08-22 10:18:43 -0400221static inline struct nfs4_stateowner *
222nfs4_get_stateowner(struct nfs4_stateowner *sop)
223{
224 atomic_inc(&sop->so_count);
225 return sop;
226}
227
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400228static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400229same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400230{
231 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400232 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400233}
234
235static struct nfs4_openowner *
236find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400237 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400238{
239 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400240
Trond Myklebustd4f04892014-07-29 21:34:36 -0400241 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400242
Trond Myklebustd4f04892014-07-29 21:34:36 -0400243 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
244 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400245 if (!so->so_is_open_owner)
246 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400247 if (same_owner_str(so, &open->op_owner))
248 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400249 }
250 return NULL;
251}
252
253static struct nfs4_openowner *
254find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400255 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400256{
257 struct nfs4_openowner *oo;
258
Trond Myklebustd4f04892014-07-29 21:34:36 -0400259 spin_lock(&clp->cl_lock);
260 oo = find_openstateowner_str_locked(hashval, open, clp);
261 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400262 return oo;
263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265static inline u32
266opaque_hashval(const void *ptr, int nbytes)
267{
268 unsigned char *cptr = (unsigned char *) ptr;
269
270 u32 x = 0;
271 while (nbytes--) {
272 x *= 37;
273 x += *cptr++;
274 }
275 return x;
276}
277
J. Bruce Fields32513b42011-10-13 16:00:16 -0400278static void nfsd4_free_file(struct nfs4_file *f)
279{
280 kmem_cache_free(file_slab, f);
281}
282
NeilBrown13cd2182005-06-23 22:03:10 -0700283static inline void
284put_nfs4_file(struct nfs4_file *fi)
285{
Jeff Layton02e12152014-07-16 10:31:57 -0400286 might_lock(&state_lock);
287
Benny Halevycdc97502014-05-30 09:09:30 -0400288 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton89876f82013-04-02 09:01:59 -0400289 hlist_del(&fi->fi_hash);
Benny Halevycdc97502014-05-30 09:09:30 -0400290 spin_unlock(&state_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400291 nfsd4_free_file(fi);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800292 }
NeilBrown13cd2182005-06-23 22:03:10 -0700293}
294
295static inline void
296get_nfs4_file(struct nfs4_file *fi)
297{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800298 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700299}
300
Trond Myklebustde186432014-07-10 14:07:26 -0400301static struct file *
302__nfs4_get_fd(struct nfs4_file *f, int oflag)
303{
304 if (f->fi_fds[oflag])
305 return get_file(f->fi_fds[oflag]);
306 return NULL;
307}
308
309static struct file *
310find_writeable_file_locked(struct nfs4_file *f)
311{
312 struct file *ret;
313
314 lockdep_assert_held(&f->fi_lock);
315
316 ret = __nfs4_get_fd(f, O_WRONLY);
317 if (!ret)
318 ret = __nfs4_get_fd(f, O_RDWR);
319 return ret;
320}
321
322static struct file *
323find_writeable_file(struct nfs4_file *f)
324{
325 struct file *ret;
326
327 spin_lock(&f->fi_lock);
328 ret = find_writeable_file_locked(f);
329 spin_unlock(&f->fi_lock);
330
331 return ret;
332}
333
334static struct file *find_readable_file_locked(struct nfs4_file *f)
335{
336 struct file *ret;
337
338 lockdep_assert_held(&f->fi_lock);
339
340 ret = __nfs4_get_fd(f, O_RDONLY);
341 if (!ret)
342 ret = __nfs4_get_fd(f, O_RDWR);
343 return ret;
344}
345
346static struct file *
347find_readable_file(struct nfs4_file *f)
348{
349 struct file *ret;
350
351 spin_lock(&f->fi_lock);
352 ret = find_readable_file_locked(f);
353 spin_unlock(&f->fi_lock);
354
355 return ret;
356}
357
358static struct file *
359find_any_file(struct nfs4_file *f)
360{
361 struct file *ret;
362
363 spin_lock(&f->fi_lock);
364 ret = __nfs4_get_fd(f, O_RDWR);
365 if (!ret) {
366 ret = __nfs4_get_fd(f, O_WRONLY);
367 if (!ret)
368 ret = __nfs4_get_fd(f, O_RDONLY);
369 }
370 spin_unlock(&f->fi_lock);
371 return ret;
372}
373
Trond Myklebust02a35082014-07-25 07:34:22 -0400374static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800375unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700376
377/*
378 * Open owner state (share locks)
379 */
380
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500381/* hash tables for lock and open owners */
382#define OWNER_HASH_BITS 8
383#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
384#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700385
Trond Myklebustd4f04892014-07-29 21:34:36 -0400386static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400387{
388 unsigned int ret;
389
390 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500391 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400392}
NeilBrownef0f3392006-04-10 22:55:41 -0700393
NeilBrownef0f3392006-04-10 22:55:41 -0700394/* hash table for nfs4_file */
395#define FILE_HASH_BITS 8
396#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800397
Trond Myklebustca943212014-07-23 16:17:39 -0400398static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400399{
Trond Myklebustca943212014-07-23 16:17:39 -0400400 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
401}
402
403static unsigned int file_hashval(struct knfsd_fh *fh)
404{
405 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
406}
407
408static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
409{
410 return fh1->fh_size == fh2->fh_size &&
411 !memcmp(fh1->fh_base.fh_pad,
412 fh2->fh_base.fh_pad,
413 fh1->fh_size);
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400414}
415
Jeff Layton89876f82013-04-02 09:01:59 -0400416static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700417
Jeff Layton12659652014-07-10 14:07:28 -0400418static void
419__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400420{
Jeff Layton7214e862014-07-10 14:07:33 -0400421 lockdep_assert_held(&fp->fi_lock);
422
Jeff Layton12659652014-07-10 14:07:28 -0400423 if (access & NFS4_SHARE_ACCESS_WRITE)
424 atomic_inc(&fp->fi_access[O_WRONLY]);
425 if (access & NFS4_SHARE_ACCESS_READ)
426 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400427}
428
Jeff Layton12659652014-07-10 14:07:28 -0400429static __be32
430nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400431{
Jeff Layton7214e862014-07-10 14:07:33 -0400432 lockdep_assert_held(&fp->fi_lock);
433
Jeff Layton12659652014-07-10 14:07:28 -0400434 /* Does this access mode make sense? */
435 if (access & ~NFS4_SHARE_ACCESS_BOTH)
436 return nfserr_inval;
437
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400438 /* Does it conflict with a deny mode already set? */
439 if ((access & fp->fi_share_deny) != 0)
440 return nfserr_share_denied;
441
Jeff Layton12659652014-07-10 14:07:28 -0400442 __nfs4_file_get_access(fp, access);
443 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400444}
445
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400446static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
447{
448 /* Common case is that there is no deny mode. */
449 if (deny) {
450 /* Does this deny mode make sense? */
451 if (deny & ~NFS4_SHARE_DENY_BOTH)
452 return nfserr_inval;
453
454 if ((deny & NFS4_SHARE_DENY_READ) &&
455 atomic_read(&fp->fi_access[O_RDONLY]))
456 return nfserr_share_denied;
457
458 if ((deny & NFS4_SHARE_DENY_WRITE) &&
459 atomic_read(&fp->fi_access[O_WRONLY]))
460 return nfserr_share_denied;
461 }
462 return nfs_ok;
463}
464
J. Bruce Fields998db522010-08-07 09:21:41 -0400465static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400466{
Trond Myklebustde186432014-07-10 14:07:26 -0400467 might_lock(&fp->fi_lock);
468
469 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
470 struct file *f1 = NULL;
471 struct file *f2 = NULL;
472
Jeff Layton6d338b52014-07-10 14:07:29 -0400473 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400474 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400475 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400476 spin_unlock(&fp->fi_lock);
477 if (f1)
478 fput(f1);
479 if (f2)
480 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400481 }
482}
483
Jeff Layton12659652014-07-10 14:07:28 -0400484static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400485{
Jeff Layton12659652014-07-10 14:07:28 -0400486 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
487
488 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400489 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400490 if (access & NFS4_SHARE_ACCESS_READ)
491 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400492}
493
Trond Myklebust60116952014-07-29 21:34:06 -0400494static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
495 struct kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400496{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500497 struct nfs4_stid *stid;
498 int new_id;
499
Trond Myklebustf8338832014-07-25 07:34:19 -0400500 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500501 if (!stid)
502 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400503
Jeff Layton4770d722014-07-29 21:34:10 -0400504 idr_preload(GFP_KERNEL);
505 spin_lock(&cl->cl_lock);
506 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
507 spin_unlock(&cl->cl_lock);
508 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700509 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500510 goto out_free;
511 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500512 stid->sc_stateid.si_opaque.so_id = new_id;
513 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
514 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400515 atomic_set(&stid->sc_count, 1);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500516
J. Bruce Fields996e0932011-10-17 11:14:48 -0400517 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500518 * It shouldn't be a problem to reuse an opaque stateid value.
519 * I don't think it is for 4.1. But with 4.0 I worry that, for
520 * example, a stray write retransmission could be accepted by
521 * the server when it should have been rejected. Therefore,
522 * adopt a trick from the sctp code to attempt to maximize the
523 * amount of time until an id is reused, by ensuring they always
524 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400525 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500526 return stid;
527out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800528 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500529 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400530}
531
Jeff Laytonb49e0842014-07-29 21:34:11 -0400532static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400533{
Trond Myklebust60116952014-07-29 21:34:06 -0400534 struct nfs4_stid *stid;
535 struct nfs4_ol_stateid *stp;
536
537 stid = nfs4_alloc_stid(clp, stateid_slab);
538 if (!stid)
539 return NULL;
540
541 stp = openlockstateid(stid);
542 stp->st_stid.sc_free = nfs4_free_ol_stateid;
543 return stp;
544}
545
546static void nfs4_free_deleg(struct nfs4_stid *stid)
547{
Trond Myklebust60116952014-07-29 21:34:06 -0400548 kmem_cache_free(deleg_slab, stid);
549 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400550}
551
NeilBrown6282cd52014-06-04 17:39:26 +1000552/*
553 * When we recall a delegation, we should be careful not to hand it
554 * out again straight away.
555 * To ensure this we keep a pair of bloom filters ('new' and 'old')
556 * in which the filehandles of recalled delegations are "stored".
557 * If a filehandle appear in either filter, a delegation is blocked.
558 * When a delegation is recalled, the filehandle is stored in the "new"
559 * filter.
560 * Every 30 seconds we swap the filters and clear the "new" one,
561 * unless both are empty of course.
562 *
563 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
564 * low 3 bytes as hash-table indices.
565 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400566 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000567 * is used to manage concurrent access. Testing does not need the lock
568 * except when swapping the two filters.
569 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400570static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000571static struct bloom_pair {
572 int entries, old_entries;
573 time_t swap_time;
574 int new; /* index into 'set' */
575 DECLARE_BITMAP(set[2], 256);
576} blocked_delegations;
577
578static int delegation_blocked(struct knfsd_fh *fh)
579{
580 u32 hash;
581 struct bloom_pair *bd = &blocked_delegations;
582
583 if (bd->entries == 0)
584 return 0;
585 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400586 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000587 if (seconds_since_boot() - bd->swap_time > 30) {
588 bd->entries -= bd->old_entries;
589 bd->old_entries = bd->entries;
590 memset(bd->set[bd->new], 0,
591 sizeof(bd->set[0]));
592 bd->new = 1-bd->new;
593 bd->swap_time = seconds_since_boot();
594 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400595 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000596 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100597 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000598 if (test_bit(hash&255, bd->set[0]) &&
599 test_bit((hash>>8)&255, bd->set[0]) &&
600 test_bit((hash>>16)&255, bd->set[0]))
601 return 1;
602
603 if (test_bit(hash&255, bd->set[1]) &&
604 test_bit((hash>>8)&255, bd->set[1]) &&
605 test_bit((hash>>16)&255, bd->set[1]))
606 return 1;
607
608 return 0;
609}
610
611static void block_delegations(struct knfsd_fh *fh)
612{
613 u32 hash;
614 struct bloom_pair *bd = &blocked_delegations;
615
Daniel Borkmann87545892014-12-10 16:33:11 +0100616 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000617
Jeff Laytonf54fe962014-07-25 07:34:26 -0400618 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000619 __set_bit(hash&255, bd->set[bd->new]);
620 __set_bit((hash>>8)&255, bd->set[bd->new]);
621 __set_bit((hash>>16)&255, bd->set[bd->new]);
622 if (bd->entries == 0)
623 bd->swap_time = seconds_since_boot();
624 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400625 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000626}
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628static struct nfs4_delegation *
Jeff Laytonf9416e22014-07-25 07:34:23 -0400629alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400632 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400635 n = atomic_long_inc_return(&num_delegations);
636 if (n < 0 || n > max_delegations)
637 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000638 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400639 goto out_dec;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400640 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700641 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400642 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400643
644 dp->dl_stid.sc_free = nfs4_free_deleg;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400645 /*
646 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400647 * meaning of seqid 0 isn't meaningful, really, but let's avoid
648 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400649 */
650 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700651 INIT_LIST_HEAD(&dp->dl_perfile);
652 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 INIT_LIST_HEAD(&dp->dl_recall_lru);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400654 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200655 dp->dl_retries = 1;
656 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200657 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400659out_dec:
660 atomic_long_dec(&num_delegations);
661 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
664void
Trond Myklebust60116952014-07-29 21:34:06 -0400665nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Trond Myklebust11b91642014-07-29 21:34:08 -0400667 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400668 struct nfs4_client *clp = s->sc_client;
669
Jeff Layton4770d722014-07-29 21:34:10 -0400670 might_lock(&clp->cl_lock);
671
Jeff Laytonb401be222014-07-29 21:34:33 -0400672 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
673 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400674 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400675 }
Trond Myklebust60116952014-07-29 21:34:06 -0400676 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400677 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400678 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400679 if (fp)
680 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500683static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Jeff Layton6bcc0342014-08-09 10:22:40 -0400685 struct file *filp = NULL;
Jeff Layton417c6622014-07-21 09:34:57 -0400686
Jeff Layton6bcc0342014-08-09 10:22:40 -0400687 spin_lock(&fp->fi_lock);
Jeff Layton0c637be2014-08-22 12:05:43 -0400688 if (fp->fi_deleg_file && atomic_dec_and_test(&fp->fi_delegees))
Jeff Layton6bcc0342014-08-09 10:22:40 -0400689 swap(filp, fp->fi_deleg_file);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400690 spin_unlock(&fp->fi_lock);
691
692 if (filp) {
Jeff Laytone6f5c782014-08-22 10:40:25 -0400693 vfs_setlease(filp, F_UNLCK, NULL, NULL);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400694 fput(filp);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400698static void unhash_stid(struct nfs4_stid *s)
699{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500700 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400701}
702
Benny Halevy931ee562014-05-30 09:09:27 -0400703static void
704hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
705{
Benny Halevycdc97502014-05-30 09:09:30 -0400706 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400707 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400708
Trond Myklebust67cb1272014-07-29 21:34:17 -0400709 atomic_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400710 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400711 list_add(&dp->dl_perfile, &fp->fi_delegations);
712 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
713}
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715static void
Jeff Layton42690672014-07-25 07:34:20 -0400716unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Trond Myklebust11b91642014-07-29 21:34:08 -0400718 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400719
Jeff Layton42690672014-07-25 07:34:20 -0400720 lockdep_assert_held(&state_lock);
721
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400722 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400723 /* Ensure that deleg break won't try to requeue it */
724 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400725 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400726 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400728 list_del_init(&dp->dl_perfile);
729 spin_unlock(&fp->fi_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400730}
731
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400732static void destroy_delegation(struct nfs4_delegation *dp)
733{
Jeff Layton42690672014-07-25 07:34:20 -0400734 spin_lock(&state_lock);
735 unhash_delegation_locked(dp);
736 spin_unlock(&state_lock);
Jeff Laytonafbda402014-08-09 10:22:41 -0400737 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -0400738 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400739}
740
741static void revoke_delegation(struct nfs4_delegation *dp)
742{
743 struct nfs4_client *clp = dp->dl_stid.sc_client;
744
Jeff Layton2d4a5322014-07-25 07:34:21 -0400745 WARN_ON(!list_empty(&dp->dl_recall_lru));
746
Jeff Laytonafbda402014-08-09 10:22:41 -0400747 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
748
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400749 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400750 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400751 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400752 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400753 spin_lock(&clp->cl_lock);
754 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
755 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400756 }
757}
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759/*
760 * SETCLIENTID state
761 */
762
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400763static unsigned int clientid_hashval(u32 id)
764{
765 return id & CLIENT_HASH_MASK;
766}
767
768static unsigned int clientstr_hashval(const char *name)
769{
770 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
771}
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400774 * We store the NONE, READ, WRITE, and BOTH bits separately in the
775 * st_{access,deny}_bmap field of the stateid, in order to track not
776 * only what share bits are currently in force, but also what
777 * combinations of share bits previous opens have used. This allows us
778 * to enforce the recommendation of rfc 3530 14.2.19 that the server
779 * return an error if the client attempt to downgrade to a combination
780 * of share bits not explicable by closing some of its previous opens.
781 *
782 * XXX: This enforcement is actually incomplete, since we don't keep
783 * track of access/deny bit combinations; so, e.g., we allow:
784 *
785 * OPEN allow read, deny write
786 * OPEN allow both, deny none
787 * DOWNGRADE allow read, deny none
788 *
789 * which we should reject.
790 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400791static unsigned int
792bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400793 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400794 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400795
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400796 for (i = 1; i < 4; i++) {
797 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400798 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400799 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400800 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400801}
802
Jeff Layton82c5ff12012-05-11 09:45:13 -0400803/* set share access for a given stateid */
804static inline void
805set_access(u32 access, struct nfs4_ol_stateid *stp)
806{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400807 unsigned char mask = 1 << access;
808
809 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
810 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400811}
812
813/* clear share access for a given stateid */
814static inline void
815clear_access(u32 access, struct nfs4_ol_stateid *stp)
816{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400817 unsigned char mask = 1 << access;
818
819 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
820 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400821}
822
823/* test whether a given stateid has access */
824static inline bool
825test_access(u32 access, struct nfs4_ol_stateid *stp)
826{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400827 unsigned char mask = 1 << access;
828
829 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400830}
831
Jeff Laytonce0fc432012-05-11 09:45:14 -0400832/* set share deny for a given stateid */
833static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400834set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400835{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400836 unsigned char mask = 1 << deny;
837
838 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
839 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400840}
841
842/* clear share deny for a given stateid */
843static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400844clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400845{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400846 unsigned char mask = 1 << deny;
847
848 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
849 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400850}
851
852/* test whether a given stateid is denying specific access */
853static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400854test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400855{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400856 unsigned char mask = 1 << deny;
857
858 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400859}
860
861static int nfs4_access_to_omode(u32 access)
862{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400863 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400864 case NFS4_SHARE_ACCESS_READ:
865 return O_RDONLY;
866 case NFS4_SHARE_ACCESS_WRITE:
867 return O_WRONLY;
868 case NFS4_SHARE_ACCESS_BOTH:
869 return O_RDWR;
870 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500871 WARN_ON_ONCE(1);
872 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400873}
874
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400875/*
876 * A stateid that had a deny mode associated with it is being released
877 * or downgraded. Recalculate the deny mode on the file.
878 */
879static void
880recalculate_deny_mode(struct nfs4_file *fp)
881{
882 struct nfs4_ol_stateid *stp;
883
884 spin_lock(&fp->fi_lock);
885 fp->fi_share_deny = 0;
886 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
887 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
888 spin_unlock(&fp->fi_lock);
889}
890
891static void
892reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
893{
894 int i;
895 bool change = false;
896
897 for (i = 1; i < 4; i++) {
898 if ((i & deny) != i) {
899 change = true;
900 clear_deny(i, stp);
901 }
902 }
903
904 /* Recalculate per-file deny mode if there was a change */
905 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -0400906 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400907}
908
Jeff Layton82c5ff12012-05-11 09:45:13 -0400909/* release all access and file references for a given stateid */
910static void
911release_all_access(struct nfs4_ol_stateid *stp)
912{
913 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -0400914 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400915
916 if (fp && stp->st_deny_bmap != 0)
917 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400918
919 for (i = 1; i < 4; i++) {
920 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -0400921 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400922 clear_access(i, stp);
923 }
924}
925
Jeff Layton6b180f02014-07-29 21:34:26 -0400926static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
927{
Jeff Laytona819ecc2014-07-29 21:34:38 -0400928 struct nfs4_client *clp = sop->so_client;
929
930 might_lock(&clp->cl_lock);
931
932 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -0400933 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -0400934 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -0400935 spin_unlock(&clp->cl_lock);
Jeff Layton6b180f02014-07-29 21:34:26 -0400936 kfree(sop->so_owner.data);
937 sop->so_ops->so_free(sop);
938}
939
Jeff Layton4ae098d2014-07-29 21:34:43 -0400940static void unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500941{
Trond Myklebust11b91642014-07-29 21:34:08 -0400942 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -0400943
Jeff Layton1c755dc2014-07-29 21:34:12 -0400944 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
945
Trond Myklebust1d31a252014-07-10 14:07:25 -0400946 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500947 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400948 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500949 list_del(&stp->st_perstateowner);
950}
951
Trond Myklebust60116952014-07-29 21:34:06 -0400952static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500953{
Trond Myklebust60116952014-07-29 21:34:06 -0400954 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400955
Trond Myklebust60116952014-07-29 21:34:06 -0400956 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -0400957 if (stp->st_stateowner)
958 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -0400959 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500960}
961
Jeff Laytonb49e0842014-07-29 21:34:11 -0400962static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500963{
Jeff Laytonb49e0842014-07-29 21:34:11 -0400964 struct nfs4_ol_stateid *stp = openlockstateid(stid);
965 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500966 struct file *file;
967
Jeff Laytonb49e0842014-07-29 21:34:11 -0400968 file = find_any_file(stp->st_stid.sc_file);
969 if (file)
970 filp_close(file, (fl_owner_t)lo);
971 nfs4_free_ol_stateid(stid);
972}
973
Jeff Layton2c41beb2014-07-29 21:34:41 -0400974/*
975 * Put the persistent reference to an already unhashed generic stateid, while
976 * holding the cl_lock. If it's the last reference, then put it onto the
977 * reaplist for later destruction.
978 */
979static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
980 struct list_head *reaplist)
981{
982 struct nfs4_stid *s = &stp->st_stid;
983 struct nfs4_client *clp = s->sc_client;
984
985 lockdep_assert_held(&clp->cl_lock);
986
987 WARN_ON_ONCE(!list_empty(&stp->st_locks));
988
989 if (!atomic_dec_and_test(&s->sc_count)) {
990 wake_up_all(&close_wq);
991 return;
992 }
993
994 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
995 list_add(&stp->st_locks, reaplist);
996}
997
Jeff Layton3c1c9952014-07-29 21:34:39 -0400998static void unhash_lock_stateid(struct nfs4_ol_stateid *stp)
999{
1000 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1001
1002 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
1003
1004 list_del_init(&stp->st_locks);
Jeff Layton4ae098d2014-07-29 21:34:43 -04001005 unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001006 unhash_stid(&stp->st_stid);
1007}
1008
Jeff Layton5adfd882014-07-29 21:34:31 -04001009static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001010{
Jeff Layton1c755dc2014-07-29 21:34:12 -04001011 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1012
1013 spin_lock(&oo->oo_owner.so_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001014 unhash_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001015 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04001016 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001017}
1018
Trond Myklebustc58c6612014-07-29 21:34:35 -04001019static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001020{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001021 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001022
Trond Myklebustd4f04892014-07-29 21:34:36 -04001023 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001024
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001025 list_del_init(&lo->lo_owner.so_strhash);
1026}
1027
Jeff Layton2c41beb2014-07-29 21:34:41 -04001028/*
1029 * Free a list of generic stateids that were collected earlier after being
1030 * fully unhashed.
1031 */
1032static void
1033free_ol_stateid_reaplist(struct list_head *reaplist)
1034{
1035 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001036 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001037
1038 might_sleep();
1039
1040 while (!list_empty(reaplist)) {
1041 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1042 st_locks);
1043 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001044 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001045 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001046 if (fp)
1047 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001048 }
1049}
1050
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001051static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001052{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001053 struct nfs4_client *clp = lo->lo_owner.so_client;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001054 struct nfs4_ol_stateid *stp;
1055 struct list_head reaplist;
1056
1057 INIT_LIST_HEAD(&reaplist);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001058
Trond Myklebustd4f04892014-07-29 21:34:36 -04001059 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001060 unhash_lockowner_locked(lo);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001061 while (!list_empty(&lo->lo_owner.so_stateids)) {
1062 stp = list_first_entry(&lo->lo_owner.so_stateids,
1063 struct nfs4_ol_stateid, st_perstateowner);
1064 unhash_lock_stateid(stp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001065 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001066 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001067 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001068 free_ol_stateid_reaplist(&reaplist);
Jeff Layton6b180f02014-07-29 21:34:26 -04001069 nfs4_put_stateowner(&lo->lo_owner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001070}
1071
Jeff Laytond83017f2014-07-29 21:34:42 -04001072static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1073 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001074{
1075 struct nfs4_ol_stateid *stp;
1076
1077 while (!list_empty(&open_stp->st_locks)) {
1078 stp = list_entry(open_stp->st_locks.next,
1079 struct nfs4_ol_stateid, st_locks);
Jeff Laytond83017f2014-07-29 21:34:42 -04001080 unhash_lock_stateid(stp);
1081 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001082 }
1083}
1084
Jeff Laytond83017f2014-07-29 21:34:42 -04001085static void unhash_open_stateid(struct nfs4_ol_stateid *stp,
1086 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001087{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001088 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1089
Jeff Layton4ae098d2014-07-29 21:34:43 -04001090 unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001091 release_open_stateid_locks(stp, reaplist);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001092}
1093
1094static void release_open_stateid(struct nfs4_ol_stateid *stp)
1095{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001096 LIST_HEAD(reaplist);
1097
1098 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04001099 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001100 put_ol_stateid_locked(stp, &reaplist);
1101 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1102 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001103}
1104
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001105static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001106{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001107 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001108
Trond Myklebustd4f04892014-07-29 21:34:36 -04001109 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001110
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001111 list_del_init(&oo->oo_owner.so_strhash);
1112 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001113}
1114
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001115static void release_last_closed_stateid(struct nfs4_openowner *oo)
1116{
Jeff Layton217526e2014-07-30 08:27:11 -04001117 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1118 nfsd_net_id);
1119 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001120
Jeff Layton217526e2014-07-30 08:27:11 -04001121 spin_lock(&nn->client_lock);
1122 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001123 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001124 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001125 oo->oo_last_closed_stid = NULL;
1126 }
Jeff Layton217526e2014-07-30 08:27:11 -04001127 spin_unlock(&nn->client_lock);
1128 if (s)
1129 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001130}
1131
Jeff Layton2c41beb2014-07-29 21:34:41 -04001132static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001133{
1134 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001135 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001136 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001137
Jeff Layton2c41beb2014-07-29 21:34:41 -04001138 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001139
Trond Myklebustd4f04892014-07-29 21:34:36 -04001140 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001141 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001142 while (!list_empty(&oo->oo_owner.so_stateids)) {
1143 stp = list_first_entry(&oo->oo_owner.so_stateids,
1144 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytond83017f2014-07-29 21:34:42 -04001145 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001146 put_ol_stateid_locked(stp, &reaplist);
1147 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001148 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001149 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001150 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001151 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001152}
1153
Marc Eshel5282fd72009-04-03 08:27:52 +03001154static inline int
1155hash_sessionid(struct nfs4_sessionid *sessionid)
1156{
1157 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1158
1159 return sid->sequence % SESSION_HASH_SIZE;
1160}
1161
Trond Myklebust8f199b82012-03-20 15:11:17 -04001162#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001163static inline void
1164dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1165{
1166 u32 *ptr = (u32 *)(&sessionid->data[0]);
1167 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1168}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001169#else
1170static inline void
1171dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1172{
1173}
1174#endif
1175
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001176/*
1177 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1178 * won't be used for replay.
1179 */
1180void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1181{
1182 struct nfs4_stateowner *so = cstate->replay_owner;
1183
1184 if (nfserr == nfserr_replay_me)
1185 return;
1186
1187 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001188 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001189 return;
1190 }
1191 if (!so)
1192 return;
1193 if (so->so_is_open_owner)
1194 release_last_closed_stateid(openowner(so));
1195 so->so_seqid++;
1196 return;
1197}
Marc Eshel5282fd72009-04-03 08:27:52 +03001198
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001199static void
1200gen_sessionid(struct nfsd4_session *ses)
1201{
1202 struct nfs4_client *clp = ses->se_client;
1203 struct nfsd4_sessionid *sid;
1204
1205 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1206 sid->clientid = clp->cl_clientid;
1207 sid->sequence = current_sessionid++;
1208 sid->reserved = 0;
1209}
1210
1211/*
Andy Adamsona6496372009-08-28 08:45:01 -04001212 * The protocol defines ca_maxresponssize_cached to include the size of
1213 * the rpc header, but all we need to cache is the data starting after
1214 * the end of the initial SEQUENCE operation--the rest we regenerate
1215 * each time. Therefore we can advertise a ca_maxresponssize_cached
1216 * value that is the number of bytes in our cache plus a few additional
1217 * bytes. In order to stay on the safe side, and not promise more than
1218 * we can cache, those additional bytes must be the minimum possible: 24
1219 * bytes of rpc header (xid through accept state, with AUTH_NULL
1220 * verifier), 12 for the compound header (with zero-length tag), and 44
1221 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001222 */
Andy Adamsona6496372009-08-28 08:45:01 -04001223#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1224
Andy Adamson557ce262009-08-28 08:45:04 -04001225static void
1226free_session_slots(struct nfsd4_session *ses)
1227{
1228 int i;
1229
1230 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1231 kfree(ses->se_slots[i]);
1232}
1233
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001234/*
1235 * We don't actually need to cache the rpc and session headers, so we
1236 * can allocate a little less for each slot:
1237 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001238static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001239{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001240 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001241
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001242 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1243 size = 0;
1244 else
1245 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1246 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001247}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001248
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001249/*
1250 * XXX: If we run out of reserved DRC memory we could (up to a point)
1251 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001252 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001253 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001254static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001255{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001256 u32 slotsize = slot_bytes(ca);
1257 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001258 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001259
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001260 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001261 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1262 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001263 num = min_t(int, num, avail / slotsize);
1264 nfsd_drc_mem_used += num * slotsize;
1265 spin_unlock(&nfsd_drc_lock);
1266
1267 return num;
1268}
1269
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001270static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001271{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001272 int slotsize = slot_bytes(ca);
1273
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001274 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001275 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001276 spin_unlock(&nfsd_drc_lock);
1277}
1278
Kinglong Mee60810e52014-01-01 00:35:47 +08001279static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1280 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001281{
Kinglong Mee60810e52014-01-01 00:35:47 +08001282 int numslots = fattrs->maxreqs;
1283 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001284 struct nfsd4_session *new;
1285 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001286
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001287 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001288 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1289 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001290
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001291 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001292 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001293 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001294 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001295 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001296 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001297 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001298 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001299 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001300
1301 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1302 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1303
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001304 return new;
1305out_free:
1306 while (i--)
1307 kfree(new->se_slots[i]);
1308 kfree(new);
1309 return NULL;
1310}
1311
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001312static void free_conn(struct nfsd4_conn *c)
1313{
1314 svc_xprt_put(c->cn_xprt);
1315 kfree(c);
1316}
1317
1318static void nfsd4_conn_lost(struct svc_xpt_user *u)
1319{
1320 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1321 struct nfs4_client *clp = c->cn_session->se_client;
1322
1323 spin_lock(&clp->cl_lock);
1324 if (!list_empty(&c->cn_persession)) {
1325 list_del(&c->cn_persession);
1326 free_conn(c);
1327 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001328 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001329 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001330}
1331
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001332static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001333{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001334 struct nfsd4_conn *conn;
1335
1336 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1337 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001338 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001339 svc_xprt_get(rqstp->rq_xprt);
1340 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001341 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001342 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1343 return conn;
1344}
1345
J. Bruce Fields328ead22010-09-29 16:11:06 -04001346static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1347{
1348 conn->cn_session = ses;
1349 list_add(&conn->cn_persession, &ses->se_conns);
1350}
1351
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001352static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1353{
1354 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001355
1356 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001357 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001358 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001359}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001360
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001361static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001362{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001363 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001364 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001365}
1366
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001367static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001368{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001369 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001370
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001371 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001372 ret = nfsd4_register_conn(conn);
1373 if (ret)
1374 /* oops; xprt is already down: */
1375 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001376 /* We may have gained or lost a callback channel: */
1377 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001378}
1379
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001380static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001381{
1382 u32 dir = NFS4_CDFC4_FORE;
1383
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001384 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001385 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001386 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001387}
1388
1389/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001390static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001391{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001392 struct nfs4_client *clp = s->se_client;
1393 struct nfsd4_conn *c;
1394
1395 spin_lock(&clp->cl_lock);
1396 while (!list_empty(&s->se_conns)) {
1397 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1398 list_del_init(&c->cn_persession);
1399 spin_unlock(&clp->cl_lock);
1400
1401 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1402 free_conn(c);
1403
1404 spin_lock(&clp->cl_lock);
1405 }
1406 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001407}
1408
J. Bruce Fields1377b692012-09-11 21:42:40 -04001409static void __free_session(struct nfsd4_session *ses)
1410{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001411 free_session_slots(ses);
1412 kfree(ses);
1413}
1414
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001415static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001416{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001417 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001418 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001419 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001420}
Andy Adamson557ce262009-08-28 08:45:04 -04001421
Fengguang Wu135ae822012-11-10 07:20:25 -05001422static 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 -04001423{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001424 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001425 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001426
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001427 new->se_client = clp;
1428 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001429
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001430 INIT_LIST_HEAD(&new->se_conns);
1431
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001432 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001433 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001434 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001435 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001436 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001437 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001438 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001439 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001440 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001441 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001442
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001443 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001444 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001445 /*
1446 * This is a little silly; with sessions there's no real
1447 * use for the callback address. Use the peer address
1448 * as a reasonable default for now, but consider fixing
1449 * the rpc client not to require an address in the
1450 * future:
1451 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001452 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1453 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001454 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001455}
1456
Benny Halevy9089f1b2010-05-12 00:12:26 +03001457/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001458static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001459__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001460{
1461 struct nfsd4_session *elem;
1462 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001463 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001464
Trond Myklebust0a880a22014-07-30 08:27:10 -04001465 lockdep_assert_held(&nn->client_lock);
1466
Marc Eshel5282fd72009-04-03 08:27:52 +03001467 dump_sessionid(__func__, sessionid);
1468 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001469 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001470 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001471 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1472 NFS4_MAX_SESSIONID_LEN)) {
1473 return elem;
1474 }
1475 }
1476
1477 dprintk("%s: session not found\n", __func__);
1478 return NULL;
1479}
1480
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001481static struct nfsd4_session *
1482find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1483 __be32 *ret)
1484{
1485 struct nfsd4_session *session;
1486 __be32 status = nfserr_badsession;
1487
1488 session = __find_in_sessionid_hashtbl(sessionid, net);
1489 if (!session)
1490 goto out;
1491 status = nfsd4_get_session_locked(session);
1492 if (status)
1493 session = NULL;
1494out:
1495 *ret = status;
1496 return session;
1497}
1498
Benny Halevy9089f1b2010-05-12 00:12:26 +03001499/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001500static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001501unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001502{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001503 struct nfs4_client *clp = ses->se_client;
1504 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1505
1506 lockdep_assert_held(&nn->client_lock);
1507
Andy Adamson7116ed62009-04-03 08:27:43 +03001508 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001509 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001510 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001511 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001512}
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1515static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001516STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001518 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001520 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001521 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return 1;
1523}
1524
1525/*
1526 * XXX Should we use a slab cache ?
1527 * This type of memory management is somewhat inefficient, but we use it
1528 * anyway since SETCLIENTID is not a common operation.
1529 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001530static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
1532 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001533 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001535 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1536 if (clp == NULL)
1537 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001538 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001539 if (clp->cl_name.data == NULL)
1540 goto err_no_name;
1541 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1542 OWNER_HASH_SIZE, GFP_KERNEL);
1543 if (!clp->cl_ownerstr_hashtbl)
1544 goto err_no_hashtbl;
1545 for (i = 0; i < OWNER_HASH_SIZE; i++)
1546 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001547 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001548 INIT_LIST_HEAD(&clp->cl_sessions);
1549 idr_init(&clp->cl_stateids);
1550 atomic_set(&clp->cl_refcount, 0);
1551 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1552 INIT_LIST_HEAD(&clp->cl_idhash);
1553 INIT_LIST_HEAD(&clp->cl_openowners);
1554 INIT_LIST_HEAD(&clp->cl_delegations);
1555 INIT_LIST_HEAD(&clp->cl_lru);
1556 INIT_LIST_HEAD(&clp->cl_callbacks);
1557 INIT_LIST_HEAD(&clp->cl_revoked);
1558 spin_lock_init(&clp->cl_lock);
1559 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001561err_no_hashtbl:
1562 kfree(clp->cl_name.data);
1563err_no_name:
1564 kfree(clp);
1565 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001568static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569free_client(struct nfs4_client *clp)
1570{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001571 while (!list_empty(&clp->cl_sessions)) {
1572 struct nfsd4_session *ses;
1573 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1574 se_perclnt);
1575 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001576 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1577 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001578 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001579 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001580 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001581 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001583 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 kfree(clp);
1585}
1586
Benny Halevy84d38ac2010-05-12 00:13:16 +03001587/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001588static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001589unhash_client_locked(struct nfs4_client *clp)
1590{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001591 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001592 struct nfsd4_session *ses;
1593
Trond Myklebust0a880a22014-07-30 08:27:10 -04001594 lockdep_assert_held(&nn->client_lock);
1595
Trond Myklebust4beb3452014-07-30 08:27:02 -04001596 /* Mark the client as expired! */
1597 clp->cl_time = 0;
1598 /* Make it invisible */
1599 if (!list_empty(&clp->cl_idhash)) {
1600 list_del_init(&clp->cl_idhash);
1601 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1602 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1603 else
1604 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1605 }
1606 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001607 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001608 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1609 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001610 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001611}
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001614unhash_client(struct nfs4_client *clp)
1615{
1616 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1617
1618 spin_lock(&nn->client_lock);
1619 unhash_client_locked(clp);
1620 spin_unlock(&nn->client_lock);
1621}
1622
Jeff Layton97403d92014-07-30 08:27:12 -04001623static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1624{
1625 if (atomic_read(&clp->cl_refcount))
1626 return nfserr_jukebox;
1627 unhash_client_locked(clp);
1628 return nfs_ok;
1629}
1630
Trond Myklebust4beb3452014-07-30 08:27:02 -04001631static void
1632__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001634 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 struct list_head reaplist;
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001639 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001640 while (!list_empty(&clp->cl_delegations)) {
1641 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001642 unhash_delegation_locked(dp);
1643 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 }
Benny Halevycdc97502014-05-30 09:09:30 -04001645 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 while (!list_empty(&reaplist)) {
1647 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001648 list_del_init(&dp->dl_recall_lru);
Jeff Laytonafbda402014-08-09 10:22:41 -04001649 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04001650 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001652 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001653 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001654 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001655 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001656 }
NeilBrownea1da632005-06-23 22:04:17 -07001657 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001658 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04001659 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001660 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001662 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001663 if (clp->cl_cb_conn.cb_xprt)
1664 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001665 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666}
1667
Trond Myklebust4beb3452014-07-30 08:27:02 -04001668static void
1669destroy_client(struct nfs4_client *clp)
1670{
1671 unhash_client(clp);
1672 __destroy_client(clp);
1673}
1674
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001675static void expire_client(struct nfs4_client *clp)
1676{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001677 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001678 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001679 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001680}
1681
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001682static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1683{
1684 memcpy(target->cl_verifier.data, source->data,
1685 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001688static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1689{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1691 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1692}
1693
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001694static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001695{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001696 if (source->cr_principal) {
1697 target->cr_principal =
1698 kstrdup(source->cr_principal, GFP_KERNEL);
1699 if (target->cr_principal == NULL)
1700 return -ENOMEM;
1701 } else
1702 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001703 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 target->cr_uid = source->cr_uid;
1705 target->cr_gid = source->cr_gid;
1706 target->cr_group_info = source->cr_group_info;
1707 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001708 target->cr_gss_mech = source->cr_gss_mech;
1709 if (source->cr_gss_mech)
1710 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001711 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}
1713
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001714static long long
1715compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1716{
1717 long long res;
1718
1719 res = o1->len - o2->len;
1720 if (res)
1721 return res;
1722 return (long long)memcmp(o1->data, o2->data, o1->len);
1723}
1724
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001725static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001726{
NeilBrowna55370a2005-06-23 22:03:52 -07001727 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
1730static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001731same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1732{
1733 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734}
1735
1736static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001737same_clid(clientid_t *cl1, clientid_t *cl2)
1738{
1739 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740}
1741
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001742static bool groups_equal(struct group_info *g1, struct group_info *g2)
1743{
1744 int i;
1745
1746 if (g1->ngroups != g2->ngroups)
1747 return false;
1748 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001749 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001750 return false;
1751 return true;
1752}
1753
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001754/*
1755 * RFC 3530 language requires clid_inuse be returned when the
1756 * "principal" associated with a requests differs from that previously
1757 * used. We use uid, gid's, and gss principal string as our best
1758 * approximation. We also don't want to allow non-gss use of a client
1759 * established using gss: in theory cr_principal should catch that
1760 * change, but in practice cr_principal can be null even in the gss case
1761 * since gssd doesn't always pass down a principal string.
1762 */
1763static bool is_gss_cred(struct svc_cred *cr)
1764{
1765 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1766 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1767}
1768
1769
Vivek Trivedi5559b502012-07-24 21:18:20 +05301770static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001771same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1772{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001773 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001774 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1775 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001776 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1777 return false;
1778 if (cr1->cr_principal == cr2->cr_principal)
1779 return true;
1780 if (!cr1->cr_principal || !cr2->cr_principal)
1781 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301782 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
1784
J. Bruce Fields57266a62013-04-13 14:27:29 -04001785static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1786{
1787 struct svc_cred *cr = &rqstp->rq_cred;
1788 u32 service;
1789
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001790 if (!cr->cr_gss_mech)
1791 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001792 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1793 return service == RPC_GSS_SVC_INTEGRITY ||
1794 service == RPC_GSS_SVC_PRIVACY;
1795}
1796
1797static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1798{
1799 struct svc_cred *cr = &rqstp->rq_cred;
1800
1801 if (!cl->cl_mach_cred)
1802 return true;
1803 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1804 return false;
1805 if (!svc_rqst_integrity_protected(rqstp))
1806 return false;
1807 if (!cr->cr_principal)
1808 return false;
1809 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1810}
1811
Jeff Layton294ac322014-07-30 08:27:15 -04001812static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001813{
Chuck Leverab4684d2012-03-02 17:13:50 -05001814 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Jeff Laytonf4199922014-06-17 07:44:11 -04001816 /*
1817 * This is opaque to client, so no need to byte-swap. Use
1818 * __force to keep sparse happy
1819 */
1820 verf[0] = (__force __be32)get_seconds();
Jeff Layton294ac322014-07-30 08:27:15 -04001821 verf[1] = (__force __be32)nn->clientid_counter;
Chuck Leverab4684d2012-03-02 17:13:50 -05001822 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}
1824
Jeff Layton294ac322014-07-30 08:27:15 -04001825static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1826{
1827 clp->cl_clientid.cl_boot = nn->boot_time;
1828 clp->cl_clientid.cl_id = nn->clientid_counter++;
1829 gen_confirm(clp, nn);
1830}
1831
Jeff Layton4770d722014-07-29 21:34:10 -04001832static struct nfs4_stid *
1833find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001834{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001835 struct nfs4_stid *ret;
1836
1837 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1838 if (!ret || !ret->sc_type)
1839 return NULL;
1840 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001841}
1842
Jeff Layton4770d722014-07-29 21:34:10 -04001843static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001844find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001845{
1846 struct nfs4_stid *s;
1847
Jeff Layton4770d722014-07-29 21:34:10 -04001848 spin_lock(&cl->cl_lock);
1849 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001850 if (s != NULL) {
1851 if (typemask & s->sc_type)
1852 atomic_inc(&s->sc_count);
1853 else
1854 s = NULL;
1855 }
Jeff Layton4770d722014-07-29 21:34:10 -04001856 spin_unlock(&cl->cl_lock);
1857 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001858}
1859
Jeff Layton2216d442012-11-12 15:00:57 -05001860static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001861 struct svc_rqst *rqstp, nfs4_verifier *verf)
1862{
1863 struct nfs4_client *clp;
1864 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001865 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001866 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001867
1868 clp = alloc_client(name);
1869 if (clp == NULL)
1870 return NULL;
1871
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001872 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1873 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001874 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001875 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001876 }
Christoph Hellwig0162ac22014-09-24 12:19:19 +02001877 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Benny Halevy07cd4902010-05-12 00:13:41 +03001878 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001879 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001880 copy_verf(clp, verf);
1881 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001882 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001883 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001884 return clp;
1885}
1886
NeilBrownfd39ca92005-06-23 22:04:03 -07001887static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001888add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1889{
1890 struct rb_node **new = &(root->rb_node), *parent = NULL;
1891 struct nfs4_client *clp;
1892
1893 while (*new) {
1894 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1895 parent = *new;
1896
1897 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1898 new = &((*new)->rb_left);
1899 else
1900 new = &((*new)->rb_right);
1901 }
1902
1903 rb_link_node(&new_clp->cl_namenode, parent, new);
1904 rb_insert_color(&new_clp->cl_namenode, root);
1905}
1906
1907static struct nfs4_client *
1908find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1909{
1910 long long cmp;
1911 struct rb_node *node = root->rb_node;
1912 struct nfs4_client *clp;
1913
1914 while (node) {
1915 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1916 cmp = compare_blob(&clp->cl_name, name);
1917 if (cmp > 0)
1918 node = node->rb_left;
1919 else if (cmp < 0)
1920 node = node->rb_right;
1921 else
1922 return clp;
1923 }
1924 return NULL;
1925}
1926
1927static void
1928add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
1930 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001931 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Trond Myklebust0a880a22014-07-30 08:27:10 -04001933 lockdep_assert_held(&nn->client_lock);
1934
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001935 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001936 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001938 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001939 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940}
1941
NeilBrownfd39ca92005-06-23 22:04:03 -07001942static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943move_to_confirmed(struct nfs4_client *clp)
1944{
1945 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001946 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Trond Myklebust0a880a22014-07-30 08:27:10 -04001948 lockdep_assert_held(&nn->client_lock);
1949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001951 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001952 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001953 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001954 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001955 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956}
1957
1958static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001959find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
1961 struct nfs4_client *clp;
1962 unsigned int idhashval = clientid_hashval(clid->cl_id);
1963
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001964 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001965 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001966 if ((bool)clp->cl_minorversion != sessions)
1967 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001968 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
1972 return NULL;
1973}
1974
1975static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001976find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1977{
1978 struct list_head *tbl = nn->conf_id_hashtbl;
1979
Trond Myklebust0a880a22014-07-30 08:27:10 -04001980 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001981 return find_client_in_id_table(tbl, clid, sessions);
1982}
1983
1984static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001985find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001987 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Trond Myklebust0a880a22014-07-30 08:27:10 -04001989 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001990 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991}
1992
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001993static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001994{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001995 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001996}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001997
NeilBrown28ce6052005-06-23 22:03:56 -07001998static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001999find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002000{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002001 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002002 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002003}
2004
2005static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002006find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002007{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002008 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002009 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002010}
2011
NeilBrownfd39ca92005-06-23 22:04:03 -07002012static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002013gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002015 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002016 struct sockaddr *sa = svc_addr(rqstp);
2017 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002018 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Jeff Layton7077ecb2009-08-14 12:57:58 -04002020 /* Currently, we only support tcp and tcp6 for the callback channel */
2021 if (se->se_callback_netid_len == 3 &&
2022 !memcmp(se->se_callback_netid_val, "tcp", 3))
2023 expected_family = AF_INET;
2024 else if (se->se_callback_netid_len == 4 &&
2025 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2026 expected_family = AF_INET6;
2027 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 goto out_err;
2029
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002030 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002031 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002032 (struct sockaddr *)&conn->cb_addr,
2033 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002034
J. Bruce Fields07263f12010-05-31 19:09:40 -04002035 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002037
J. Bruce Fields07263f12010-05-31 19:09:40 -04002038 if (conn->cb_addr.ss_family == AF_INET6)
2039 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002040
J. Bruce Fields07263f12010-05-31 19:09:40 -04002041 conn->cb_prog = se->se_callback_prog;
2042 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002043 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 return;
2045out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002046 conn->cb_addr.ss_family = AF_UNSPEC;
2047 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002048 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 "will not receive delegations\n",
2050 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 return;
2053}
2054
Andy Adamson074fe892009-04-03 08:28:15 +03002055/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002056 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002057 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002058static void
Andy Adamson074fe892009-04-03 08:28:15 +03002059nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2060{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002061 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002062 struct nfsd4_slot *slot = resp->cstate.slot;
2063 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002064
Andy Adamson557ce262009-08-28 08:45:04 -04002065 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002066
Andy Adamson557ce262009-08-28 08:45:04 -04002067 slot->sl_opcnt = resp->opcnt;
2068 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002069
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002070 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002071 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002072 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002073 return;
2074 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002075 base = resp->cstate.data_offset;
2076 slot->sl_datalen = buf->len - base;
2077 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04002078 WARN("%s: sessions DRC could not cache compound\n", __func__);
2079 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002080}
2081
2082/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002083 * Encode the replay sequence operation from the slot values.
2084 * If cachethis is FALSE encode the uncached rep error on the next
2085 * operation which sets resp->p and increments resp->opcnt for
2086 * nfs4svc_encode_compoundres.
2087 *
Andy Adamson074fe892009-04-03 08:28:15 +03002088 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002089static __be32
2090nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2091 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002092{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002093 struct nfsd4_op *op;
2094 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002095
Andy Adamsonabfabf82009-07-23 19:02:18 -04002096 /* Encode the replayed sequence operation */
2097 op = &args->ops[resp->opcnt - 1];
2098 nfsd4_encode_operation(resp, op);
2099
2100 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002101 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002102 op = &args->ops[resp->opcnt++];
2103 op->status = nfserr_retry_uncached_rep;
2104 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002105 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002106 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002107}
2108
2109/*
Andy Adamson557ce262009-08-28 08:45:04 -04002110 * The sequence operation is not cached because we can use the slot and
2111 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002112 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002113static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002114nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2115 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002116{
Andy Adamson557ce262009-08-28 08:45:04 -04002117 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002118 struct xdr_stream *xdr = &resp->xdr;
2119 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002120 __be32 status;
2121
Andy Adamson557ce262009-08-28 08:45:04 -04002122 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002123
Andy Adamsonabfabf82009-07-23 19:02:18 -04002124 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002125 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002126 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002127
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002128 p = xdr_reserve_space(xdr, slot->sl_datalen);
2129 if (!p) {
2130 WARN_ON_ONCE(1);
2131 return nfserr_serverfault;
2132 }
2133 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2134 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002135
Andy Adamson557ce262009-08-28 08:45:04 -04002136 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002137 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002138}
2139
Andy Adamson0733d212009-04-03 08:28:01 +03002140/*
2141 * Set the exchange_id flags returned by the server.
2142 */
2143static void
2144nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2145{
2146 /* pNFS is not supported */
2147 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2148
2149 /* Referrals are supported, Migration is not. */
2150 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2151
2152 /* set the wire flags to return to client. */
2153 clid->flags = new->cl_exchange_flags;
2154}
2155
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002156static bool client_has_state(struct nfs4_client *clp)
2157{
2158 /*
2159 * Note clp->cl_openowners check isn't quite right: there's no
2160 * need to count owners without stateid's.
2161 *
2162 * Also note we should probably be using this in 4.0 case too.
2163 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002164 return !list_empty(&clp->cl_openowners)
2165 || !list_empty(&clp->cl_delegations)
2166 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002167}
2168
Al Virob37ad282006-10-19 23:28:59 -07002169__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002170nfsd4_exchange_id(struct svc_rqst *rqstp,
2171 struct nfsd4_compound_state *cstate,
2172 struct nfsd4_exchange_id *exid)
2173{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002174 struct nfs4_client *conf, *new;
2175 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002176 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002177 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002178 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002179 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002180 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002181 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002182
Jeff Layton363168b2009-08-14 12:57:56 -04002183 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002184 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002185 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002186 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002187 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002188
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002189 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002190 return nfserr_inval;
2191
Andy Adamson0733d212009-04-03 08:28:01 +03002192 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002193 case SP4_MACH_CRED:
2194 if (!svc_rqst_integrity_protected(rqstp))
2195 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002196 case SP4_NONE:
2197 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002198 default: /* checked by xdr code */
2199 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002200 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002201 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002202 }
2203
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002204 new = create_client(exid->clname, rqstp, &verf);
2205 if (new == NULL)
2206 return nfserr_jukebox;
2207
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002208 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002209 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002210 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002211 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002212 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2213 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2214
J. Bruce Fields136e6582012-05-12 20:37:23 -04002215 if (update) {
2216 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002217 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002218 goto out;
2219 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002220 if (!mach_creds_match(conf, rqstp)) {
2221 status = nfserr_wrong_cred;
2222 goto out;
2223 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002224 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002225 status = nfserr_perm;
2226 goto out;
2227 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002228 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002229 status = nfserr_not_same;
2230 goto out;
2231 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002232 /* case 6 */
2233 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002234 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002235 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002236 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002237 if (client_has_state(conf)) {
2238 status = nfserr_clid_inuse;
2239 goto out;
2240 }
Andy Adamson0733d212009-04-03 08:28:01 +03002241 goto out_new;
2242 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002243 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002244 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002245 goto out_copy;
2246 }
2247 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002248 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002249 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002250 }
2251
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002252 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002253 status = nfserr_noent;
2254 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002255 }
2256
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002257 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002258 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002259 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002260
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002261 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002262out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002263 if (conf) {
2264 status = mark_client_expired_locked(conf);
2265 if (status)
2266 goto out;
2267 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002268 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002269 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002270
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002271 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002272 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002273 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002274out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002275 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2276 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002277
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002278 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2279 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002280
2281 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002282 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002283 status = nfs_ok;
2284
2285out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002286 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002287 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002288 expire_client(new);
2289 if (unconf)
2290 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002291 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002292}
2293
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002294static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002295check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002296{
Andy Adamson88e588d2009-07-23 19:02:15 -04002297 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2298 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002299
2300 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002301 if (slot_inuse) {
2302 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002303 return nfserr_jukebox;
2304 else
2305 return nfserr_seq_misordered;
2306 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002307 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002308 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002309 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002310 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002311 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002312 return nfserr_seq_misordered;
2313}
2314
Andy Adamson49557cc2009-07-23 19:02:16 -04002315/*
2316 * Cache the create session result into the create session single DRC
2317 * slot cache by saving the xdr structure. sl_seqid has been set.
2318 * Do this for solo or embedded create session operations.
2319 */
2320static void
2321nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002322 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002323{
2324 slot->sl_status = nfserr;
2325 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2326}
2327
2328static __be32
2329nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2330 struct nfsd4_clid_slot *slot)
2331{
2332 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2333 return slot->sl_status;
2334}
2335
Mi Jinlong1b74c252011-07-14 14:50:17 +08002336#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2337 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2338 1 + /* MIN tag is length with zero, only length */ \
2339 3 + /* version, opcount, opcode */ \
2340 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2341 /* seqid, slotID, slotID, cache */ \
2342 4 ) * sizeof(__be32))
2343
2344#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2345 2 + /* verifier: AUTH_NULL, length 0 */\
2346 1 + /* status */ \
2347 1 + /* MIN tag is length with zero, only length */ \
2348 3 + /* opcount, opcode, opstatus*/ \
2349 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2350 /* seqid, slotID, slotID, slotID, status */ \
2351 5 ) * sizeof(__be32))
2352
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002353static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002354{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002355 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2356
J. Bruce Fields373cd402013-04-08 15:42:12 -04002357 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2358 return nfserr_toosmall;
2359 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2360 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002361 ca->headerpadsz = 0;
2362 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2363 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2364 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2365 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2366 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2367 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2368 /*
2369 * Note decreasing slot size below client's request may make it
2370 * difficult for client to function correctly, whereas
2371 * decreasing the number of slots will (just?) affect
2372 * performance. When short on memory we therefore prefer to
2373 * decrease number of slots instead of their size. Clients that
2374 * request larger slots than they need will get poor results:
2375 */
2376 ca->maxreqs = nfsd4_get_drc_mem(ca);
2377 if (!ca->maxreqs)
2378 return nfserr_jukebox;
2379
J. Bruce Fields373cd402013-04-08 15:42:12 -04002380 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002381}
2382
Kinglong Mee8a891632013-12-23 18:11:02 +08002383#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2384 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2385#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2386 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2387
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002388static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2389{
2390 ca->headerpadsz = 0;
2391
2392 /*
2393 * These RPC_MAX_HEADER macros are overkill, especially since we
2394 * don't even do gss on the backchannel yet. But this is still
2395 * less than 1k. Tighten up this estimate in the unlikely event
2396 * it turns out to be a problem for some client:
2397 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002398 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002399 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002400 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002401 return nfserr_toosmall;
2402 ca->maxresp_cached = 0;
2403 if (ca->maxops < 2)
2404 return nfserr_toosmall;
2405
2406 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002407}
2408
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002409static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2410{
2411 switch (cbs->flavor) {
2412 case RPC_AUTH_NULL:
2413 case RPC_AUTH_UNIX:
2414 return nfs_ok;
2415 default:
2416 /*
2417 * GSS case: the spec doesn't allow us to return this
2418 * error. But it also doesn't allow us not to support
2419 * GSS.
2420 * I'd rather this fail hard than return some error the
2421 * client might think it can already handle:
2422 */
2423 return nfserr_encr_alg_unsupp;
2424 }
2425}
2426
Andy Adamson069b6ad2009-04-03 08:27:58 +03002427__be32
2428nfsd4_create_session(struct svc_rqst *rqstp,
2429 struct nfsd4_compound_state *cstate,
2430 struct nfsd4_create_session *cr_ses)
2431{
Jeff Layton363168b2009-08-14 12:57:56 -04002432 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002433 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002434 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002435 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002436 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002437 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002438 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002439 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002440
Mi Jinlonga62573d2011-03-23 17:57:07 +08002441 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2442 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002443 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2444 if (status)
2445 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002446 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002447 if (status)
2448 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002449 status = check_backchannel_attrs(&cr_ses->back_channel);
2450 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002451 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002452 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002453 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002454 if (!new)
2455 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002456 conn = alloc_conn_from_crses(rqstp, cr_ses);
2457 if (!conn)
2458 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002459
Jeff Laytond20c11d2014-07-30 08:27:07 -04002460 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002461 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002462 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002463 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002464
2465 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002466 status = nfserr_wrong_cred;
2467 if (!mach_creds_match(conf, rqstp))
2468 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002469 cs_slot = &conf->cl_cs_slot;
2470 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002471 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002472 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002473 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002474 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002475 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002476 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002477 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002478 } else if (unconf) {
2479 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002480 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002481 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002482 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002483 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002484 status = nfserr_wrong_cred;
2485 if (!mach_creds_match(unconf, rqstp))
2486 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002487 cs_slot = &unconf->cl_cs_slot;
2488 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002489 if (status) {
2490 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002491 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002492 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002493 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002494 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002495 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002496 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002497 if (status) {
2498 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002499 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002500 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002501 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002502 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002503 conf = unconf;
2504 } else {
2505 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002506 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002507 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002508 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002509 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002510 * We do not support RDMA or persistent sessions
2511 */
2512 cr_ses->flags &= ~SESSION4_PERSIST;
2513 cr_ses->flags &= ~SESSION4_RDMA;
2514
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002515 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002516 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002517
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002518 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002519 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002520 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002521 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002522
Jeff Laytond20c11d2014-07-30 08:27:07 -04002523 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002524 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002525 spin_unlock(&nn->client_lock);
2526 /* init connection and backchannel */
2527 nfsd4_init_conn(rqstp, conn, new);
2528 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002529 if (old)
2530 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002531 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002532out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002533 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002534 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002535 if (old)
2536 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002537out_free_session:
2538 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002539out_release_drc_mem:
2540 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002541 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002542}
2543
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002544static __be32 nfsd4_map_bcts_dir(u32 *dir)
2545{
2546 switch (*dir) {
2547 case NFS4_CDFC4_FORE:
2548 case NFS4_CDFC4_BACK:
2549 return nfs_ok;
2550 case NFS4_CDFC4_FORE_OR_BOTH:
2551 case NFS4_CDFC4_BACK_OR_BOTH:
2552 *dir = NFS4_CDFC4_BOTH;
2553 return nfs_ok;
2554 };
2555 return nfserr_inval;
2556}
2557
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002558__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2559{
2560 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002561 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002562 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002563
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002564 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2565 if (status)
2566 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002567 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002568 session->se_cb_prog = bc->bc_cb_program;
2569 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002570 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002571
2572 nfsd4_probe_callback(session->se_client);
2573
2574 return nfs_ok;
2575}
2576
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002577__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2578 struct nfsd4_compound_state *cstate,
2579 struct nfsd4_bind_conn_to_session *bcts)
2580{
2581 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002582 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002583 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002584 struct net *net = SVC_NET(rqstp);
2585 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002586
2587 if (!nfsd4_last_compound_op(rqstp))
2588 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002589 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002590 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002591 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002592 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002593 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002594 status = nfserr_wrong_cred;
2595 if (!mach_creds_match(session->se_client, rqstp))
2596 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002597 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002598 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002599 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002600 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002601 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002602 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002603 goto out;
2604 nfsd4_init_conn(rqstp, conn, session);
2605 status = nfs_ok;
2606out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002607 nfsd4_put_session(session);
2608out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002609 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002610}
2611
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002612static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2613{
2614 if (!session)
2615 return 0;
2616 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2617}
2618
Andy Adamson069b6ad2009-04-03 08:27:58 +03002619__be32
2620nfsd4_destroy_session(struct svc_rqst *r,
2621 struct nfsd4_compound_state *cstate,
2622 struct nfsd4_destroy_session *sessionid)
2623{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002624 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002625 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002626 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002627 struct net *net = SVC_NET(r);
2628 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002629
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002630 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002631 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002632 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002633 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002634 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002635 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002636 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002637 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002638 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002639 if (!ses)
2640 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002641 status = nfserr_wrong_cred;
2642 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002643 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002644 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002645 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002646 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002647 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002648 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002649
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002650 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002651
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002652 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002653 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002654out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002655 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002656out_client_lock:
2657 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002658out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03002659 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002660}
2661
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002662static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002663{
2664 struct nfsd4_conn *c;
2665
2666 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002667 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002668 return c;
2669 }
2670 }
2671 return NULL;
2672}
2673
J. Bruce Fields57266a62013-04-13 14:27:29 -04002674static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002675{
2676 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002677 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002678 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002679 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002680
2681 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002682 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002683 if (c)
2684 goto out_free;
2685 status = nfserr_conn_not_bound_to_session;
2686 if (clp->cl_mach_cred)
2687 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002688 __nfsd4_hash_conn(new, ses);
2689 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002690 ret = nfsd4_register_conn(new);
2691 if (ret)
2692 /* oops; xprt is already down: */
2693 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002694 return nfs_ok;
2695out_free:
2696 spin_unlock(&clp->cl_lock);
2697 free_conn(new);
2698 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002699}
2700
Mi Jinlong868b89c2011-04-27 09:09:58 +08002701static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2702{
2703 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2704
2705 return args->opcnt > session->se_fchannel.maxops;
2706}
2707
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002708static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2709 struct nfsd4_session *session)
2710{
2711 struct xdr_buf *xb = &rqstp->rq_arg;
2712
2713 return xb->len > session->se_fchannel.maxreq_sz;
2714}
2715
Andy Adamson069b6ad2009-04-03 08:27:58 +03002716__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002717nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002718 struct nfsd4_compound_state *cstate,
2719 struct nfsd4_sequence *seq)
2720{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002721 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002722 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002723 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002724 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002725 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002726 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002727 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002728 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002729 struct net *net = SVC_NET(rqstp);
2730 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002731
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002732 if (resp->opcnt != 1)
2733 return nfserr_sequence_pos;
2734
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002735 /*
2736 * Will be either used or freed by nfsd4_sequence_check_conn
2737 * below.
2738 */
2739 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2740 if (!conn)
2741 return nfserr_jukebox;
2742
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002743 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002744 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002745 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002746 goto out_no_session;
2747 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002748
Mi Jinlong868b89c2011-04-27 09:09:58 +08002749 status = nfserr_too_many_ops;
2750 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002751 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002752
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002753 status = nfserr_req_too_big;
2754 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002755 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002756
Benny Halevyb85d4c02009-04-03 08:28:08 +03002757 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002758 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002759 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002760
Andy Adamson557ce262009-08-28 08:45:04 -04002761 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002762 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2763
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002764 /* We do not negotiate the number of slots yet, so set the
2765 * maxslots to the session maxreqs which is used to encode
2766 * sr_highest_slotid and the sr_target_slot id to maxslots */
2767 seq->maxslots = session->se_fchannel.maxreqs;
2768
J. Bruce Fields73e79482012-02-13 16:39:00 -05002769 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2770 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002771 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002772 status = nfserr_seq_misordered;
2773 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002774 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002775 cstate->slot = slot;
2776 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002777 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002778 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002779 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002780 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002781 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002782 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002783 }
2784 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002785 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002786
J. Bruce Fields57266a62013-04-13 14:27:29 -04002787 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002788 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002789 if (status)
2790 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002791
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002792 buflen = (seq->cachethis) ?
2793 session->se_fchannel.maxresp_cached :
2794 session->se_fchannel.maxresp_sz;
2795 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2796 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002797 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002798 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002799 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002800
2801 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002802 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002803 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002804 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002805 if (seq->cachethis)
2806 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002807 else
2808 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002809
2810 cstate->slot = slot;
2811 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002812 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002813
Benny Halevyb85d4c02009-04-03 08:28:08 +03002814out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002815 switch (clp->cl_cb_state) {
2816 case NFSD4_CB_DOWN:
2817 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2818 break;
2819 case NFSD4_CB_FAULT:
2820 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2821 break;
2822 default:
2823 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002824 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002825 if (!list_empty(&clp->cl_revoked))
2826 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002827out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002828 if (conn)
2829 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002830 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002831 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002832out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002833 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002834 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002835}
2836
Trond Myklebustb6076642014-06-30 11:48:35 -04002837void
2838nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2839{
2840 struct nfsd4_compound_state *cs = &resp->cstate;
2841
2842 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002843 if (cs->status != nfserr_replay_cache) {
2844 nfsd4_store_cache_entry(resp);
2845 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2846 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002847 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002848 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002849 } else if (cs->clp)
2850 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002851}
2852
Mi Jinlong345c2842011-10-20 17:51:39 +08002853__be32
2854nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2855{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002856 struct nfs4_client *conf, *unconf;
2857 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002858 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002859 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002860
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002861 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002862 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002863 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002864 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002865
2866 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002867 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002868 status = nfserr_clientid_busy;
2869 goto out;
2870 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04002871 status = mark_client_expired_locked(conf);
2872 if (status)
2873 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002874 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08002875 } else if (unconf)
2876 clp = unconf;
2877 else {
2878 status = nfserr_stale_clientid;
2879 goto out;
2880 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002881 if (!mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002882 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002883 status = nfserr_wrong_cred;
2884 goto out;
2885 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002886 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002887out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002888 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002889 if (clp)
2890 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002891 return status;
2892}
2893
Andy Adamson069b6ad2009-04-03 08:27:58 +03002894__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002895nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2896{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002897 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002898
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002899 if (rc->rca_one_fs) {
2900 if (!cstate->current_fh.fh_dentry)
2901 return nfserr_nofilehandle;
2902 /*
2903 * We don't take advantage of the rca_one_fs case.
2904 * That's OK, it's optional, we can safely ignore it.
2905 */
2906 return nfs_ok;
2907 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002908
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002909 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002910 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2911 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002912 goto out;
2913
2914 status = nfserr_stale_clientid;
2915 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002916 /*
2917 * The following error isn't really legal.
2918 * But we only get here if the client just explicitly
2919 * destroyed the client. Surely it no longer cares what
2920 * error it gets back on an operation for the dead
2921 * client.
2922 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002923 goto out;
2924
2925 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002926 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002927out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002928 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002929}
2930
2931__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002932nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2933 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002935 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002937 struct nfs4_client *conf, *new;
2938 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002939 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002940 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2941
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002942 new = create_client(clname, rqstp, &clverifier);
2943 if (new == NULL)
2944 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002945 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002946 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002947 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002948 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002949 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002951 if (clp_used_exchangeid(conf))
2952 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002953 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002954 char addr_str[INET6_ADDRSTRLEN];
2955 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2956 sizeof(addr_str));
2957 dprintk("NFSD: setclientid: string in use by client "
2958 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 goto out;
2960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002962 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002963 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002964 unhash_client_locked(unconf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002965 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002966 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002968 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002969 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002970 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002971 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002972 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2974 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2975 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002976 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 status = nfs_ok;
2978out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002979 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002980 if (new)
2981 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002982 if (unconf)
2983 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 return status;
2985}
2986
2987
Al Virob37ad282006-10-19 23:28:59 -07002988__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002989nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2990 struct nfsd4_compound_state *cstate,
2991 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992{
NeilBrown21ab45a2005-06-23 22:04:14 -07002993 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002994 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2996 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002997 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002998 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003000 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003002
Jeff Laytond20c11d2014-07-30 08:27:07 -04003003 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003004 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003005 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003006 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003007 * We try hard to give out unique clientid's, so if we get an
3008 * attempt to confirm the same clientid with a different cred,
3009 * there's a bug somewhere. Let's charitably assume it's our
3010 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003011 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04003012 status = nfserr_serverfault;
3013 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3014 goto out;
3015 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3016 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003017 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003018 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3019 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003021 else /* case 4: client hasn't noticed we rebooted yet? */
3022 status = nfserr_stale_clientid;
3023 goto out;
3024 }
3025 status = nfs_ok;
3026 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003027 old = unconf;
3028 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003029 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003030 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003031 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3032 if (old) {
3033 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003034 if (status) {
3035 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003036 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003037 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003038 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003039 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003040 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003041 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003042 get_client_locked(conf);
3043 spin_unlock(&nn->client_lock);
3044 nfsd4_probe_callback(conf);
3045 spin_lock(&nn->client_lock);
3046 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003048 spin_unlock(&nn->client_lock);
3049 if (old)
3050 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 return status;
3052}
3053
J. Bruce Fields32513b42011-10-13 16:00:16 -04003054static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003056 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3057}
3058
3059/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003060static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003061{
Trond Myklebustca943212014-07-23 16:17:39 -04003062 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
Trond Myklebust950e0112014-06-30 11:48:31 -04003064 lockdep_assert_held(&state_lock);
3065
J. Bruce Fields32513b42011-10-13 16:00:16 -04003066 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003067 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003068 INIT_LIST_HEAD(&fp->fi_stateids);
3069 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003070 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003071 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003072 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003073 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003074 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3075 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04003076 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077}
3078
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003079void
NeilBrowne60d4392005-06-23 22:03:01 -07003080nfsd4_free_slabs(void)
3081{
Christoph Hellwigabf11352014-05-21 07:43:03 -07003082 kmem_cache_destroy(openowner_slab);
3083 kmem_cache_destroy(lockowner_slab);
3084 kmem_cache_destroy(file_slab);
3085 kmem_cache_destroy(stateid_slab);
3086 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003087}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
Bryan Schumaker72083392011-11-01 15:24:59 -04003089int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090nfsd4_init_slabs(void)
3091{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003092 openowner_slab = kmem_cache_create("nfsd4_openowners",
3093 sizeof(struct nfs4_openowner), 0, 0, NULL);
3094 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003095 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003096 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003097 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003098 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003099 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003100 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003101 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003102 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003103 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003104 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003105 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003106 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003107 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003108 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003109 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003110 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003111 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003113
3114out_free_stateid_slab:
3115 kmem_cache_destroy(stateid_slab);
3116out_free_file_slab:
3117 kmem_cache_destroy(file_slab);
3118out_free_lockowner_slab:
3119 kmem_cache_destroy(lockowner_slab);
3120out_free_openowner_slab:
3121 kmem_cache_destroy(openowner_slab);
3122out:
NeilBrowne60d4392005-06-23 22:03:01 -07003123 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3124 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125}
3126
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003127static void init_nfs4_replay(struct nfs4_replay *rp)
3128{
3129 rp->rp_status = nfserr_serverfault;
3130 rp->rp_buflen = 0;
3131 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003132 mutex_init(&rp->rp_mutex);
3133}
3134
3135static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3136 struct nfs4_stateowner *so)
3137{
3138 if (!nfsd4_has_session(cstate)) {
3139 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003140 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04003141 }
3142}
3143
3144void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3145{
3146 struct nfs4_stateowner *so = cstate->replay_owner;
3147
3148 if (so != NULL) {
3149 cstate->replay_owner = NULL;
3150 mutex_unlock(&so->so_replay.rp_mutex);
3151 nfs4_put_stateowner(so);
3152 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003153}
3154
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003155static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156{
3157 struct nfs4_stateowner *sop;
3158
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003159 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003160 if (!sop)
3161 return NULL;
3162
3163 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3164 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003165 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003166 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003168 sop->so_owner.len = owner->len;
3169
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003170 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003171 sop->so_client = clp;
3172 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003173 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003174 return sop;
3175}
3176
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003177static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003178{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003179 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003180
Trond Myklebustd4f04892014-07-29 21:34:36 -04003181 list_add(&oo->oo_owner.so_strhash,
3182 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003183 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184}
3185
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003186static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3187{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003188 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003189}
3190
Jeff Layton6b180f02014-07-29 21:34:26 -04003191static void nfs4_free_openowner(struct nfs4_stateowner *so)
3192{
3193 struct nfs4_openowner *oo = openowner(so);
3194
3195 kmem_cache_free(openowner_slab, oo);
3196}
3197
3198static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003199 .so_unhash = nfs4_unhash_openowner,
3200 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003201};
3202
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003203static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003204alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003205 struct nfsd4_compound_state *cstate)
3206{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003207 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003208 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003210 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3211 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003213 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003214 oo->oo_owner.so_is_open_owner = 1;
3215 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003216 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003217 if (nfsd4_has_session(cstate))
3218 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003219 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003220 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003221 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003222 spin_lock(&clp->cl_lock);
3223 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003224 if (ret == NULL) {
3225 hash_openowner(oo, clp, strhashval);
3226 ret = oo;
3227 } else
3228 nfs4_free_openowner(&oo->oo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003229 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003230 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231}
3232
J. Bruce Fields996e0932011-10-17 11:14:48 -04003233static 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 -04003234 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003236 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003237 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003238 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003239 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07003240 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003241 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 stp->st_access_bmap = 0;
3243 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003244 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003245 spin_lock(&oo->oo_owner.so_client->cl_lock);
3246 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003247 spin_lock(&fp->fi_lock);
3248 list_add(&stp->st_perfile, &fp->fi_stateids);
3249 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003250 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251}
3252
Jeff Laytond3134b12014-07-29 21:34:32 -04003253/*
3254 * In the 4.0 case we need to keep the owners around a little while to handle
3255 * CLOSE replay. We still do need to release any file access that is held by
3256 * them before returning however.
3257 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003259move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260{
Jeff Layton217526e2014-07-30 08:27:11 -04003261 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003262 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3263 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3264 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003265
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003266 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
Jeff Laytonb401be222014-07-29 21:34:33 -04003268 /*
3269 * We know that we hold one reference via nfsd4_close, and another
3270 * "persistent" reference for the client. If the refcount is higher
3271 * than 2, then there are still calls in progress that are using this
3272 * stateid. We can't put the sc_file reference until they are finished.
3273 * Wait for the refcount to drop to 2. Since it has been unhashed,
3274 * there should be no danger of the refcount going back up again at
3275 * this point.
3276 */
3277 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3278
Jeff Laytond3134b12014-07-29 21:34:32 -04003279 release_all_access(s);
3280 if (s->st_stid.sc_file) {
3281 put_nfs4_file(s->st_stid.sc_file);
3282 s->st_stid.sc_file = NULL;
3283 }
Jeff Layton217526e2014-07-30 08:27:11 -04003284
3285 spin_lock(&nn->client_lock);
3286 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003287 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003288 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003289 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003290 spin_unlock(&nn->client_lock);
3291 if (last)
3292 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293}
3294
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295/* search file_hashtbl[] for file */
3296static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003297find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298{
Trond Myklebustca943212014-07-23 16:17:39 -04003299 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 struct nfs4_file *fp;
3301
Trond Myklebust950e0112014-06-30 11:48:31 -04003302 lockdep_assert_held(&state_lock);
3303
Jeff Layton89876f82013-04-02 09:01:59 -04003304 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003305 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003306 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 }
3310 return NULL;
3311}
3312
Trond Myklebust950e0112014-06-30 11:48:31 -04003313static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003314find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003315{
3316 struct nfs4_file *fp;
3317
3318 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003319 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003320 spin_unlock(&state_lock);
3321 return fp;
3322}
3323
3324static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003325find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003326{
3327 struct nfs4_file *fp;
3328
3329 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003330 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003331 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003332 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003333 fp = new;
3334 }
3335 spin_unlock(&state_lock);
3336
3337 return fp;
3338}
3339
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003340/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 * Called to check deny when READ with all zero stateid or
3342 * WRITE with all zero or all one stateid
3343 */
Al Virob37ad282006-10-19 23:28:59 -07003344static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3346{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003348 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
Trond Myklebustca943212014-07-23 16:17:39 -04003350 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003351 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003352 return ret;
3353 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003354 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003355 if (fp->fi_share_deny & deny_type)
3356 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003357 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003358 put_nfs4_file(fp);
3359 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360}
3361
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003362static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003364 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04003365 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3366 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003367
Trond Myklebust11b91642014-07-29 21:34:08 -04003368 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003369
Jeff Layton02e12152014-07-16 10:31:57 -04003370 /*
3371 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003372 * already holding inode->i_lock.
3373 *
Jeff Laytondff13992014-07-08 14:02:49 -04003374 * If the dl_time != 0, then we know that it has already been
3375 * queued for a lease break. Don't queue it again.
3376 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003377 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003378 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003379 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003380 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003381 }
Jeff Layton02e12152014-07-16 10:31:57 -04003382 spin_unlock(&state_lock);
3383}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003385static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3386 struct rpc_task *task)
3387{
3388 struct nfs4_delegation *dp = cb_to_delegation(cb);
3389
3390 switch (task->tk_status) {
3391 case 0:
3392 return 1;
3393 case -EBADHANDLE:
3394 case -NFS4ERR_BAD_STATEID:
3395 /*
3396 * Race: client probably got cb_recall before open reply
3397 * granting delegation.
3398 */
3399 if (dp->dl_retries--) {
3400 rpc_delay(task, 2 * HZ);
3401 return 0;
3402 }
3403 /*FALLTHRU*/
3404 default:
3405 return -1;
3406 }
3407}
3408
3409static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3410{
3411 struct nfs4_delegation *dp = cb_to_delegation(cb);
3412
3413 nfs4_put_stid(&dp->dl_stid);
3414}
3415
3416static struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3417 .prepare = nfsd4_cb_recall_prepare,
3418 .done = nfsd4_cb_recall_done,
3419 .release = nfsd4_cb_recall_release,
3420};
3421
Jeff Layton02e12152014-07-16 10:31:57 -04003422static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3423{
3424 /*
3425 * We're assuming the state code never drops its reference
3426 * without first removing the lease. Since we're in this lease
3427 * callback (and since the lease code is serialized by the kernel
3428 * lock) we know the server hasn't removed the lease yet, we know
3429 * it's safe to take a reference.
3430 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003431 atomic_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02003432 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003433}
3434
Jeff Layton1c8c6012013-06-21 08:58:15 -04003435/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003436static bool
3437nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003438{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003439 bool ret = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003440 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3441 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003442
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003443 if (!fp) {
3444 WARN(1, "(%p)->fl_owner NULL\n", fl);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003445 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003446 }
3447 if (fp->fi_had_conflict) {
3448 WARN(1, "duplicate break on %p\n", fp);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003449 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003450 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003451 /*
3452 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003453 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003454 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003455 */
3456 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
Jeff Layton02e12152014-07-16 10:31:57 -04003458 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003459 fp->fi_had_conflict = true;
3460 /*
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003461 * If there are no delegations on the list, then return true
3462 * so that the lease code will go ahead and delete it.
Jeff Layton417c6622014-07-21 09:34:57 -04003463 */
3464 if (list_empty(&fp->fi_delegations))
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003465 ret = true;
Jeff Layton417c6622014-07-21 09:34:57 -04003466 else
3467 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3468 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003469 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003470 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471}
3472
Jeff Laytonc45198e2014-09-01 07:12:07 -04003473static int
3474nfsd_change_deleg_cb(struct file_lock **onlist, int arg, struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475{
3476 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04003477 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 else
3479 return -EAGAIN;
3480}
3481
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003482static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003483 .lm_break = nfsd_break_deleg_cb,
3484 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485};
3486
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003487static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3488{
3489 if (nfsd4_has_session(cstate))
3490 return nfs_ok;
3491 if (seqid == so->so_seqid - 1)
3492 return nfserr_replay_me;
3493 if (seqid == so->so_seqid)
3494 return nfs_ok;
3495 return nfserr_bad_seqid;
3496}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
Jeff Layton4b24ca72014-06-30 11:48:44 -04003498static __be32 lookup_clientid(clientid_t *clid,
3499 struct nfsd4_compound_state *cstate,
3500 struct nfsd_net *nn)
3501{
3502 struct nfs4_client *found;
3503
3504 if (cstate->clp) {
3505 found = cstate->clp;
3506 if (!same_clid(&found->cl_clientid, clid))
3507 return nfserr_stale_clientid;
3508 return nfs_ok;
3509 }
3510
3511 if (STALE_CLIENTID(clid, nn))
3512 return nfserr_stale_clientid;
3513
3514 /*
3515 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3516 * cached already then we know this is for is for v4.0 and "sessions"
3517 * will be false.
3518 */
3519 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003520 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003521 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003522 if (!found) {
3523 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003524 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003525 }
3526 atomic_inc(&found->cl_refcount);
3527 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003528
3529 /* Cache the nfs4_client in cstate! */
3530 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003531 return nfs_ok;
3532}
3533
Al Virob37ad282006-10-19 23:28:59 -07003534__be32
Andy Adamson66689582009-04-03 08:28:45 +03003535nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003536 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 clientid_t *clientid = &open->op_clientid;
3539 struct nfs4_client *clp = NULL;
3540 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003541 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003542 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003544 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003546 /*
3547 * In case we need it later, after we've already created the
3548 * file and don't want to risk a further failure:
3549 */
3550 open->op_file = nfsd4_alloc_file();
3551 if (open->op_file == NULL)
3552 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
Trond Myklebust2d91e892014-06-30 11:48:46 -04003554 status = lookup_clientid(clientid, cstate, nn);
3555 if (status)
3556 return status;
3557 clp = cstate->clp;
3558
Trond Myklebustd4f04892014-07-29 21:34:36 -04003559 strhashval = ownerstr_hashval(&open->op_owner);
3560 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003561 open->op_openowner = oo;
3562 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003563 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003565 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003566 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003567 release_openowner(oo);
3568 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003569 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003570 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003571 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3572 if (status)
3573 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003574 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003575new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003576 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003577 if (oo == NULL)
3578 return nfserr_jukebox;
3579 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003580alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003581 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003582 if (!open->op_stp)
3583 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003584 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585}
3586
Al Virob37ad282006-10-19 23:28:59 -07003587static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003588nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3589{
3590 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3591 return nfserr_openmode;
3592 else
3593 return nfs_ok;
3594}
3595
Daniel Mackc47d8322011-05-16 16:38:14 +02003596static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003597{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003598 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3599}
3600
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003601static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003602{
3603 struct nfs4_stid *ret;
3604
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003605 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003606 if (!ret)
3607 return NULL;
3608 return delegstateid(ret);
3609}
3610
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003611static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3612{
3613 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3614 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3615}
3616
Al Virob37ad282006-10-19 23:28:59 -07003617static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003618nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003619 struct nfs4_delegation **dp)
3620{
3621 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003622 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003623 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003624
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003625 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3626 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003627 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003628 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003629 status = nfs4_check_delegmode(deleg, flags);
3630 if (status) {
3631 nfs4_put_stid(&deleg->dl_stid);
3632 goto out;
3633 }
3634 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003635out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003636 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003637 return nfs_ok;
3638 if (status)
3639 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003640 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003641 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003642}
3643
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003644static struct nfs4_ol_stateid *
3645nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003647 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003648 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649
Trond Myklebust1d31a252014-07-10 14:07:25 -04003650 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003651 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 /* ignore lock owners */
3653 if (local->st_stateowner->so_is_open_owner == 0)
3654 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003655 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003656 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003657 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003658 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003661 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003662 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663}
3664
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003665static inline int nfs4_access_to_access(u32 nfs4_access)
3666{
3667 int flags = 0;
3668
3669 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3670 flags |= NFSD_MAY_READ;
3671 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3672 flags |= NFSD_MAY_WRITE;
3673 return flags;
3674}
3675
Al Virob37ad282006-10-19 23:28:59 -07003676static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3678 struct nfsd4_open *open)
3679{
3680 struct iattr iattr = {
3681 .ia_valid = ATTR_SIZE,
3682 .ia_size = 0,
3683 };
3684 if (!open->op_truncate)
3685 return 0;
3686 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003687 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3689}
3690
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003691static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003692 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3693 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003694{
Trond Myklebustde186432014-07-10 14:07:26 -04003695 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003696 __be32 status;
3697 int oflag = nfs4_access_to_omode(open->op_share_access);
3698 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003699 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003700
Trond Myklebustde186432014-07-10 14:07:26 -04003701 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003702
3703 /*
3704 * Are we trying to set a deny mode that would conflict with
3705 * current access?
3706 */
3707 status = nfs4_file_check_deny(fp, open->op_share_deny);
3708 if (status != nfs_ok) {
3709 spin_unlock(&fp->fi_lock);
3710 goto out;
3711 }
3712
3713 /* set access to the file */
3714 status = nfs4_file_get_access(fp, open->op_share_access);
3715 if (status != nfs_ok) {
3716 spin_unlock(&fp->fi_lock);
3717 goto out;
3718 }
3719
3720 /* Set access bits in stateid */
3721 old_access_bmap = stp->st_access_bmap;
3722 set_access(open->op_share_access, stp);
3723
3724 /* Set new deny mask */
3725 old_deny_bmap = stp->st_deny_bmap;
3726 set_deny(open->op_share_deny, stp);
3727 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3728
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003729 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003730 spin_unlock(&fp->fi_lock);
3731 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003732 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003733 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003734 spin_lock(&fp->fi_lock);
3735 if (!fp->fi_fds[oflag]) {
3736 fp->fi_fds[oflag] = filp;
3737 filp = NULL;
3738 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003739 }
Trond Myklebustde186432014-07-10 14:07:26 -04003740 spin_unlock(&fp->fi_lock);
3741 if (filp)
3742 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003743
3744 status = nfsd4_truncate(rqstp, cur_fh, open);
3745 if (status)
3746 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003747out:
3748 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003749out_put_access:
3750 stp->st_access_bmap = old_access_bmap;
3751 nfs4_file_put_access(fp, open->op_share_access);
3752 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3753 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003754}
3755
Al Virob37ad282006-10-19 23:28:59 -07003756static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003757nfs4_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 -07003758{
Al Virob37ad282006-10-19 23:28:59 -07003759 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003760 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003762 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003763 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003764
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003765 /* test and set deny mode */
3766 spin_lock(&fp->fi_lock);
3767 status = nfs4_file_check_deny(fp, open->op_share_deny);
3768 if (status == nfs_ok) {
3769 old_deny_bmap = stp->st_deny_bmap;
3770 set_deny(open->op_share_deny, stp);
3771 fp->fi_share_deny |=
3772 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3773 }
3774 spin_unlock(&fp->fi_lock);
3775
3776 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003779 status = nfsd4_truncate(rqstp, cur_fh, open);
3780 if (status != nfs_ok)
3781 reset_union_bmap_deny(old_deny_bmap, stp);
3782 return status;
3783}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003786nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003788 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789}
3790
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003791/* Should we give out recallable state?: */
3792static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3793{
3794 if (clp->cl_cb_state == NFSD4_CB_UP)
3795 return true;
3796 /*
3797 * In the sessions case, since we don't have to establish a
3798 * separate connection for callbacks, we assume it's OK
3799 * until we hear otherwise:
3800 */
3801 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3802}
3803
Jeff Laytond564fbe2014-07-16 10:31:58 -04003804static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003805{
3806 struct file_lock *fl;
3807
3808 fl = locks_alloc_lock();
3809 if (!fl)
3810 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003811 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003812 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003813 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3814 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003815 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003816 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003817 return fl;
3818}
3819
J. Bruce Fields99c41512013-05-21 16:21:25 -04003820static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003821{
Trond Myklebust11b91642014-07-29 21:34:08 -04003822 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton415b96c2014-08-22 12:26:36 -04003823 struct file_lock *fl, *ret;
Jeff Layton417c6622014-07-21 09:34:57 -04003824 struct file *filp;
3825 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003826
Jeff Laytond564fbe2014-07-16 10:31:58 -04003827 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003828 if (!fl)
3829 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003830 filp = find_readable_file(fp);
3831 if (!filp) {
3832 /* We should always have a readable file here */
3833 WARN_ON_ONCE(1);
3834 return -EBADF;
3835 }
3836 fl->fl_file = filp;
Jeff Layton415b96c2014-08-22 12:26:36 -04003837 ret = fl;
Jeff Laytone6f5c782014-08-22 10:40:25 -04003838 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04003839 if (fl)
Jeff Layton417c6622014-07-21 09:34:57 -04003840 locks_free_lock(fl);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04003841 if (status)
Jeff Layton417c6622014-07-21 09:34:57 -04003842 goto out_fput;
Benny Halevycdc97502014-05-30 09:09:30 -04003843 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003844 spin_lock(&fp->fi_lock);
3845 /* Did the lease get broken before we took the lock? */
3846 status = -EAGAIN;
3847 if (fp->fi_had_conflict)
3848 goto out_unlock;
3849 /* Race breaker */
Jeff Layton0c637be2014-08-22 12:05:43 -04003850 if (fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04003851 status = 0;
3852 atomic_inc(&fp->fi_delegees);
3853 hash_delegation_locked(dp, fp);
3854 goto out_unlock;
3855 }
Jeff Layton417c6622014-07-21 09:34:57 -04003856 fp->fi_deleg_file = filp;
3857 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003858 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003859 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003860 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003861 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003862out_unlock:
3863 spin_unlock(&fp->fi_lock);
3864 spin_unlock(&state_lock);
3865out_fput:
3866 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003867 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003868}
3869
Jeff Layton0b266932014-07-25 07:34:25 -04003870static struct nfs4_delegation *
3871nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3872 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003873{
Jeff Layton0b266932014-07-25 07:34:25 -04003874 int status;
3875 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003876
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003877 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003878 return ERR_PTR(-EAGAIN);
3879
3880 dp = alloc_init_deleg(clp, fh);
3881 if (!dp)
3882 return ERR_PTR(-ENOMEM);
3883
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003884 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003885 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003886 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003887 dp->dl_stid.sc_file = fp;
Jeff Layton0c637be2014-08-22 12:05:43 -04003888 if (!fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04003889 spin_unlock(&fp->fi_lock);
3890 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003891 status = nfs4_setlease(dp);
3892 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003893 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003894 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003895 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003896 status = -EAGAIN;
3897 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003898 }
Benny Halevy931ee562014-05-30 09:09:27 -04003899 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003900 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003901out_unlock:
3902 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003903 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003904out:
3905 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003906 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003907 return ERR_PTR(status);
3908 }
3909 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003910}
3911
Benny Halevy4aa89132012-02-21 14:16:44 -08003912static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3913{
3914 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3915 if (status == -EAGAIN)
3916 open->op_why_no_deleg = WND4_CONTENTION;
3917 else {
3918 open->op_why_no_deleg = WND4_RESOURCE;
3919 switch (open->op_deleg_want) {
3920 case NFS4_SHARE_WANT_READ_DELEG:
3921 case NFS4_SHARE_WANT_WRITE_DELEG:
3922 case NFS4_SHARE_WANT_ANY_DELEG:
3923 break;
3924 case NFS4_SHARE_WANT_CANCEL:
3925 open->op_why_no_deleg = WND4_CANCELLED;
3926 break;
3927 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003928 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003929 }
3930 }
3931}
3932
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933/*
3934 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003935 *
3936 * Note we don't support write delegations, and won't until the vfs has
3937 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 */
3939static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003940nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3941 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942{
3943 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003944 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3945 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003946 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003947 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003949 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003950 open->op_recall = 0;
3951 switch (open->op_claim_type) {
3952 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003953 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003954 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003955 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3956 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003957 break;
3958 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003959 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003960 /*
3961 * Let's not give out any delegations till everyone's
3962 * had the chance to reclaim theirs....
3963 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003964 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003965 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003966 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003967 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003968 /*
3969 * Also, if the file was opened for write or
3970 * create, there's a good chance the client's
3971 * about to write to it, resulting in an
3972 * immediate recall (since we don't support
3973 * write delegations):
3974 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003975 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003976 goto out_no_deleg;
3977 if (open->op_create == NFS4_OPEN_CREATE)
3978 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003979 break;
3980 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003981 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003982 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003983 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003984 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003985 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003987 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003989 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003990 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003991 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003992 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003993 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003994out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003995 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3996 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003997 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003998 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003999 open->op_recall = 1;
4000 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04004001
4002 /* 4.1 client asking for a delegation? */
4003 if (open->op_deleg_want)
4004 nfsd4_open_deleg_none_ext(open, status);
4005 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006}
4007
Benny Halevye27f49c2012-02-21 14:16:54 -08004008static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4009 struct nfs4_delegation *dp)
4010{
4011 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4012 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4013 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4014 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4015 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4016 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4017 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4018 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4019 }
4020 /* Otherwise the client must be confused wanting a delegation
4021 * it already has, therefore we don't return
4022 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4023 */
4024}
4025
Al Virob37ad282006-10-19 23:28:59 -07004026__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4028{
Andy Adamson66689582009-04-03 08:28:45 +03004029 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004030 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004032 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004033 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004034 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 /*
4037 * Lookup file; if found, lookup stateid and check open request,
4038 * and check for delegations in the process of being recalled.
4039 * If not found, create the nfs4_file struct
4040 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004041 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004042 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004043 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004044 if (status)
4045 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04004046 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004048 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004049 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004050 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004051 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004052 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 }
4054
4055 /*
4056 * OPEN the file, or upgrade an existing OPEN.
4057 * If truncate fails, the OPEN fails.
4058 */
4059 if (stp) {
4060 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004061 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 if (status)
4063 goto out;
4064 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004065 stp = open->op_stp;
4066 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04004067 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004068 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4069 if (status) {
4070 release_open_stateid(stp);
4071 goto out;
4072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004074 update_stateid(&stp->st_stid.sc_stateid);
4075 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076
Benny Halevyd24433c2012-02-16 20:57:17 +02004077 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004078 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4079 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4080 open->op_why_no_deleg = WND4_NOT_WANTED;
4081 goto nodeleg;
4082 }
4083 }
4084
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 /*
4086 * Attempt to hand out a delegation. No error return, because the
4087 * OPEN succeeds even if we fail.
4088 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004089 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004090nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 status = nfs_ok;
4092
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004093 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004094 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004096 /* 4.1 client trying to upgrade/downgrade delegation? */
4097 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004098 open->op_deleg_want)
4099 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004100
NeilBrown13cd2182005-06-23 22:03:10 -07004101 if (fp)
4102 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004103 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05004104 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 /*
4106 * To finish the open response, we just need to set the rflags.
4107 */
4108 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004109 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004110 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004112 if (dp)
4113 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004114 if (stp)
4115 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116
4117 return status;
4118}
4119
Jeff Layton58fb12e2014-07-29 21:34:27 -04004120void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4121 struct nfsd4_open *open, __be32 status)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004122{
4123 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004124 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004125
Jeff Laytond3134b12014-07-29 21:34:32 -04004126 nfsd4_cstate_assign_replay(cstate, so);
4127 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004128 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004129 if (open->op_file)
4130 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004131 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004132 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004133}
4134
Al Virob37ad282006-10-19 23:28:59 -07004135__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004136nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4137 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138{
4139 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004140 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004141 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 dprintk("process_renew(%08x/%08x): starting\n",
4144 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004145 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004146 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004148 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004150 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004151 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 goto out;
4153 status = nfs_ok;
4154out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 return status;
4156}
4157
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04004158void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004159nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004160{
Jeff Layton33dcc482012-04-10 11:08:48 -04004161 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004162 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004163 return;
4164
NeilBrowna76b4312005-06-23 22:04:01 -07004165 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004166 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04004167 /*
4168 * If the server goes down again right now, an NFSv4
4169 * client will still be allowed to reclaim after it comes back up,
4170 * even if it hasn't yet had a chance to reclaim state this time.
4171 *
4172 */
Jeff Layton919b8042014-09-12 16:40:20 -04004173 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004174 /*
4175 * At this point, NFSv4 clients can still reclaim. But if the
4176 * server crashes, any that have not yet reclaimed will be out
4177 * of luck on the next boot.
4178 *
4179 * (NFSv4.1+ clients are considered to have reclaimed once they
4180 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4181 * have reclaimed after their first OPEN.)
4182 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004183 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004184 /*
4185 * At this point, and once lockd and/or any other containers
4186 * exit their grace period, further reclaims will fail and
4187 * regular locking can resume.
4188 */
NeilBrowna76b4312005-06-23 22:04:01 -07004189}
4190
NeilBrownfd39ca92005-06-23 22:04:03 -07004191static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004192nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193{
4194 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004195 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004197 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004199 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004200 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004203 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004204 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004205 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004206 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 clp = list_entry(pos, struct nfs4_client, cl_lru);
4208 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4209 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004210 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 break;
4212 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004213 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004214 dprintk("NFSD: client in use (clientid %08x)\n",
4215 clp->cl_clientid.cl_id);
4216 continue;
4217 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004218 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004219 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004220 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004221 list_for_each_safe(pos, next, &reaplist) {
4222 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 dprintk("NFSD: purging unused client (clientid %08x)\n",
4224 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004225 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 expire_client(clp);
4227 }
Benny Halevycdc97502014-05-30 09:09:30 -04004228 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004229 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004231 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4232 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004234 t = dp->dl_time - cutoff;
4235 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 break;
4237 }
Jeff Layton42690672014-07-25 07:34:20 -04004238 unhash_delegation_locked(dp);
4239 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 }
Benny Halevycdc97502014-05-30 09:09:30 -04004241 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004242 while (!list_empty(&reaplist)) {
4243 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4244 dl_recall_lru);
4245 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004246 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 }
Jeff Layton217526e2014-07-30 08:27:11 -04004248
4249 spin_lock(&nn->client_lock);
4250 while (!list_empty(&nn->close_lru)) {
4251 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4252 oo_close_lru);
4253 if (time_after((unsigned long)oo->oo_time,
4254 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004255 t = oo->oo_time - cutoff;
4256 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 break;
4258 }
Jeff Layton217526e2014-07-30 08:27:11 -04004259 list_del_init(&oo->oo_close_lru);
4260 stp = oo->oo_last_closed_stid;
4261 oo->oo_last_closed_stid = NULL;
4262 spin_unlock(&nn->client_lock);
4263 nfs4_put_stid(&stp->st_stid);
4264 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 }
Jeff Layton217526e2014-07-30 08:27:11 -04004266 spin_unlock(&nn->client_lock);
4267
Jeff Laytona832e7a2014-05-30 09:09:26 -04004268 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004269 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270}
4271
Harvey Harrisona254b242008-02-20 12:49:00 -08004272static struct workqueue_struct *laundry_wq;
4273static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004274
4275static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004276laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277{
4278 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004279 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4280 work);
4281 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4282 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004284 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004286 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287}
4288
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004289static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004290{
Trond Myklebust11b91642014-07-29 21:34:08 -04004291 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004292 return nfserr_bad_stateid;
4293 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294}
4295
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004297access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004299 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4300 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4301 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302}
4303
4304static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004305access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004307 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4308 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309}
4310
4311static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004312__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313{
Al Virob37ad282006-10-19 23:28:59 -07004314 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315
J. Bruce Fields02921912010-07-29 15:16:59 -04004316 /* For lock stateid's, we test the parent open, not the lock: */
4317 if (stp->st_openstp)
4318 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004319 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004321 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 goto out;
4323 status = nfs_ok;
4324out:
4325 return status;
4326}
4327
Al Virob37ad282006-10-19 23:28:59 -07004328static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004329check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004331 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004333 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004334 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 * conflicting state; so we must wait out the grace period. */
4336 return nfserr_grace;
4337 } else if (flags & WR_STATE)
4338 return nfs4_share_conflict(current_fh,
4339 NFS4_SHARE_DENY_WRITE);
4340 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4341 return nfs4_share_conflict(current_fh,
4342 NFS4_SHARE_DENY_READ);
4343}
4344
4345/*
4346 * Allow READ/WRITE during grace period on recovered state only for files
4347 * that are not able to provide mandatory locking.
4348 */
4349static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004350grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004352 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353}
4354
J. Bruce Fields81b82962011-08-23 11:03:29 -04004355/* Returns true iff a is later than b: */
4356static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4357{
Jim Rees1a9357f2013-05-17 17:33:00 -04004358 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004359}
4360
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004361static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004362{
Andy Adamson66689582009-04-03 08:28:45 +03004363 /*
4364 * When sessions are used the stateid generation number is ignored
4365 * when it is zero.
4366 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004367 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004368 return nfs_ok;
4369
4370 if (in->si_generation == ref->si_generation)
4371 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004372
J. Bruce Fields0836f582008-01-26 19:08:12 -05004373 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004374 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004375 return nfserr_bad_stateid;
4376 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004377 * However, we could see a stateid from the past, even from a
4378 * non-buggy client. For example, if the client sends a lock
4379 * while some IO is outstanding, the lock may bump si_generation
4380 * while the IO is still in flight. The client could avoid that
4381 * situation by waiting for responses on all the IO requests,
4382 * but better performance may result in retrying IO that
4383 * receives an old_stateid error if requests are rarely
4384 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004385 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004386 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004387}
4388
Chuck Lever7df302f2012-05-29 13:56:37 -04004389static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004390{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004391 struct nfs4_stid *s;
4392 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004393 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004394
Chuck Lever7df302f2012-05-29 13:56:37 -04004395 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004396 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004397 /* Client debugging aid. */
4398 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4399 char addr_str[INET6_ADDRSTRLEN];
4400 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4401 sizeof(addr_str));
4402 pr_warn_ratelimited("NFSD: client %s testing state ID "
4403 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004404 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004405 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004406 spin_lock(&cl->cl_lock);
4407 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004408 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004409 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004410 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004411 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004412 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004413 switch (s->sc_type) {
4414 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004415 status = nfs_ok;
4416 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004417 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004418 status = nfserr_deleg_revoked;
4419 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004420 case NFS4_OPEN_STID:
4421 case NFS4_LOCK_STID:
4422 ols = openlockstateid(s);
4423 if (ols->st_stateowner->so_is_open_owner
4424 && !(openowner(ols->st_stateowner)->oo_flags
4425 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004426 status = nfserr_bad_stateid;
4427 else
4428 status = nfs_ok;
4429 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004430 default:
4431 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004432 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004433 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004434 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004435 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004436 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004437out_unlock:
4438 spin_unlock(&cl->cl_lock);
4439 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004440}
4441
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004442static __be32
4443nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4444 stateid_t *stateid, unsigned char typemask,
4445 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004446{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004447 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004448
4449 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4450 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004451 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004452 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004453 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004454 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004455 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004456 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004457 if (status)
4458 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004459 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004460 if (!*s)
4461 return nfserr_bad_stateid;
4462 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004463}
4464
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465/*
4466* Checks for stateid operations
4467*/
Al Virob37ad282006-10-19 23:28:59 -07004468__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004469nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004470 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004472 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004473 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004475 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004477 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004478 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004479 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 if (filpp)
4482 *filpp = NULL;
4483
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004484 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 return nfserr_grace;
4486
4487 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004488 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004490 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004491 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004492 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004493 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04004494 return status;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004495 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4496 if (status)
4497 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004498 switch (s->sc_type) {
4499 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004500 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004501 status = nfs4_check_delegmode(dp, flags);
4502 if (status)
4503 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004504 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004505 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004506 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004507 WARN_ON_ONCE(1);
4508 status = nfserr_serverfault;
4509 goto out;
4510 }
Trond Myklebustde186432014-07-10 14:07:26 -04004511 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004512 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004513 break;
4514 case NFS4_OPEN_STID:
4515 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004516 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004517 status = nfs4_check_fh(current_fh, stp);
4518 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004520 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004521 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004523 status = nfs4_check_openmode(stp, flags);
4524 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004526 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004527 struct nfs4_file *fp = stp->st_stid.sc_file;
4528
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004529 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004530 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004531 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004532 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004533 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004534 break;
4535 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004536 status = nfserr_bad_stateid;
4537 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 }
4539 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004540 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004541 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004543 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 return status;
4545}
4546
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004547/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004548 * Test if the stateid is valid
4549 */
4550__be32
4551nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4552 struct nfsd4_test_stateid *test_stateid)
4553{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004554 struct nfsd4_test_stateid_id *stateid;
4555 struct nfs4_client *cl = cstate->session->se_client;
4556
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004557 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004558 stateid->ts_id_status =
4559 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004560
Bryan Schumaker17456802011-07-13 10:50:48 -04004561 return nfs_ok;
4562}
4563
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004564__be32
4565nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4566 struct nfsd4_free_stateid *free_stateid)
4567{
4568 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004569 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004570 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004571 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004572 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004573 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004574
Jeff Layton1af71cc2014-07-29 21:34:14 -04004575 spin_lock(&cl->cl_lock);
4576 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004577 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004578 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004579 switch (s->sc_type) {
4580 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004581 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004582 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004583 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004584 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4585 if (ret)
4586 break;
4587 ret = nfserr_locks_held;
4588 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004589 case NFS4_LOCK_STID:
4590 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4591 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004592 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004593 stp = openlockstateid(s);
4594 ret = nfserr_locks_held;
4595 if (check_for_locks(stp->st_stid.sc_file,
4596 lockowner(stp->st_stateowner)))
4597 break;
4598 unhash_lock_stateid(stp);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004599 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004600 nfs4_put_stid(s);
4601 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004602 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004603 case NFS4_REVOKED_DELEG_STID:
4604 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004605 list_del_init(&dp->dl_recall_lru);
4606 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004607 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004608 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004609 goto out;
4610 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004611 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004612out_unlock:
4613 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004614out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004615 return ret;
4616}
4617
NeilBrown4c4cd222005-07-07 17:59:27 -07004618static inline int
4619setlkflg (int type)
4620{
4621 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4622 RD_STATE : WR_STATE;
4623}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004625static __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 -04004626{
4627 struct svc_fh *current_fh = &cstate->current_fh;
4628 struct nfs4_stateowner *sop = stp->st_stateowner;
4629 __be32 status;
4630
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004631 status = nfsd4_check_seqid(cstate, sop, seqid);
4632 if (status)
4633 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004634 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4635 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004636 /*
4637 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004638 * nfserr_replay_me from the previous step, and
4639 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004640 */
4641 return nfserr_bad_stateid;
4642 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4643 if (status)
4644 return status;
4645 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004646}
4647
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648/*
4649 * Checks for sequence id mutating operations.
4650 */
Al Virob37ad282006-10-19 23:28:59 -07004651static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004652nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004653 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004654 struct nfs4_ol_stateid **stpp,
4655 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004657 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004658 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004659 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004661 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4662 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004663
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004665 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004666 if (status)
4667 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004668 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004669 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004671 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004672 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004673 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004674 else
4675 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004676 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004677}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004678
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004679static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4680 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004681{
4682 __be32 status;
4683 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004684 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004686 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004687 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004688 if (status)
4689 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004690 oo = openowner(stp->st_stateowner);
4691 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4692 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004693 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004694 }
4695 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004696 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697}
4698
Al Virob37ad282006-10-19 23:28:59 -07004699__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004700nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004701 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702{
Al Virob37ad282006-10-19 23:28:59 -07004703 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004704 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004705 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004706 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707
Al Viroa6a9f182013-09-16 10:57:01 -04004708 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4709 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004711 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004712 if (status)
4713 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004715 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004716 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004717 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004718 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004719 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004720 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004721 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004722 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004723 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004724 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004725 update_stateid(&stp->st_stid.sc_stateid);
4726 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004727 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004728 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004729
Jeff Layton2a4317c2012-03-21 16:42:43 -04004730 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004731 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004732put_stateid:
4733 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004735 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 return status;
4737}
4738
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004739static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004741 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004742 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004743 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004744 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004745}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004746
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004747static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4748{
4749 switch (to_access) {
4750 case NFS4_SHARE_ACCESS_READ:
4751 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4752 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4753 break;
4754 case NFS4_SHARE_ACCESS_WRITE:
4755 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4756 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4757 break;
4758 case NFS4_SHARE_ACCESS_BOTH:
4759 break;
4760 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004761 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 }
4763}
4764
Al Virob37ad282006-10-19 23:28:59 -07004765__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004766nfsd4_open_downgrade(struct svc_rqst *rqstp,
4767 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004768 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769{
Al Virob37ad282006-10-19 23:28:59 -07004770 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004771 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004772 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773
Al Viroa6a9f182013-09-16 10:57:01 -04004774 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4775 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004777 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004778 if (od->od_deleg_want)
4779 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4780 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004782 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004783 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004784 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004787 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004788 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004790 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004792 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004793 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004795 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004797 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
Jeff Laytonce0fc432012-05-11 09:45:14 -04004799 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004801 update_stateid(&stp->st_stid.sc_stateid);
4802 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004804put_stateid:
4805 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004807 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 return status;
4809}
4810
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004811static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4812{
Trond Myklebustacf92952014-06-30 11:48:37 -04004813 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytond83017f2014-07-29 21:34:42 -04004814 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004815
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004816 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04004817 spin_lock(&clp->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04004818 unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004819
Jeff Laytond83017f2014-07-29 21:34:42 -04004820 if (clp->cl_minorversion) {
4821 put_ol_stateid_locked(s, &reaplist);
4822 spin_unlock(&clp->cl_lock);
4823 free_ol_stateid_reaplist(&reaplist);
4824 } else {
4825 spin_unlock(&clp->cl_lock);
4826 free_ol_stateid_reaplist(&reaplist);
Jeff Laytond3134b12014-07-29 21:34:32 -04004827 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04004828 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004829}
4830
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831/*
4832 * nfs4_unlock_state() called after encode
4833 */
Al Virob37ad282006-10-19 23:28:59 -07004834__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004835nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004836 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837{
Al Virob37ad282006-10-19 23:28:59 -07004838 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004839 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004840 struct net *net = SVC_NET(rqstp);
4841 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842
Al Viroa6a9f182013-09-16 10:57:01 -04004843 dprintk("NFSD: nfsd4_close on file %pd\n",
4844 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004846 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4847 &close->cl_stateid,
4848 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004849 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004850 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004851 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004853 update_stateid(&stp->st_stid.sc_stateid);
4854 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004856 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004857
4858 /* put reference from nfs4_preprocess_seqid_op */
4859 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 return status;
4862}
4863
Al Virob37ad282006-10-19 23:28:59 -07004864__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004865nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4866 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004868 struct nfs4_delegation *dp;
4869 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004870 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004871 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004872 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004874 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004875 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004877 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004878 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004879 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004880 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004881 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004882 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004883 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004884
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004885 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004886put_stateid:
4887 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888out:
4889 return status;
4890}
4891
4892
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894
Benny Halevy87df4de2008-12-15 19:42:03 +02004895static inline u64
4896end_offset(u64 start, u64 len)
4897{
4898 u64 end;
4899
4900 end = start + len;
4901 return end >= start ? end: NFS4_MAX_UINT64;
4902}
4903
4904/* last octet in a range */
4905static inline u64
4906last_byte_offset(u64 start, u64 len)
4907{
4908 u64 end;
4909
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004910 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004911 end = start + len;
4912 return end > start ? end - 1: NFS4_MAX_UINT64;
4913}
4914
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915/*
4916 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4917 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4918 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4919 * locking, this prevents us from being completely protocol-compliant. The
4920 * real solution to this problem is to start using unsigned file offsets in
4921 * the VFS, but this is a very deep change!
4922 */
4923static inline void
4924nfs4_transform_lock_offset(struct file_lock *lock)
4925{
4926 if (lock->fl_start < 0)
4927 lock->fl_start = OFFSET_MAX;
4928 if (lock->fl_end < 0)
4929 lock->fl_end = OFFSET_MAX;
4930}
4931
Kinglong Meeaef95832014-08-22 10:18:44 -04004932static void nfsd4_fl_get_owner(struct file_lock *dst, struct file_lock *src)
4933{
4934 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)src->fl_owner;
4935 dst->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lo->lo_owner));
4936}
4937
4938static void nfsd4_fl_put_owner(struct file_lock *fl)
4939{
4940 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
4941
4942 if (lo) {
4943 nfs4_put_stateowner(&lo->lo_owner);
4944 fl->fl_owner = NULL;
4945 }
4946}
4947
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004948static const struct lock_manager_operations nfsd_posix_mng_ops = {
Kinglong Meeaef95832014-08-22 10:18:44 -04004949 .lm_get_owner = nfsd4_fl_get_owner,
4950 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07004951};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952
4953static inline void
4954nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4955{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004956 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
NeilBrownd5b90262006-04-10 22:55:22 -07004958 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004959 lo = (struct nfs4_lockowner *) fl->fl_owner;
4960 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4961 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004962 if (!deny->ld_owner.data)
4963 /* We just don't care that much */
4964 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004965 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4966 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004967 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004968nevermind:
4969 deny->ld_owner.len = 0;
4970 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004971 deny->ld_clientid.cl_boot = 0;
4972 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 }
4974 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004975 deny->ld_length = NFS4_MAX_UINT64;
4976 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4978 deny->ld_type = NFS4_READ_LT;
4979 if (fl->fl_type != F_RDLCK)
4980 deny->ld_type = NFS4_WRITE_LT;
4981}
4982
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004983static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004984find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004985 struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004987 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004988 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989
Trond Myklebust0a880a22014-07-30 08:27:10 -04004990 lockdep_assert_held(&clp->cl_lock);
4991
Trond Myklebustd4f04892014-07-29 21:34:36 -04004992 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4993 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004994 if (so->so_is_open_owner)
4995 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04004996 if (same_owner_str(so, owner))
4997 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 }
4999 return NULL;
5000}
5001
Trond Myklebustc58c6612014-07-29 21:34:35 -04005002static struct nfs4_lockowner *
5003find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005004 struct nfs4_client *clp)
Trond Myklebustc58c6612014-07-29 21:34:35 -04005005{
5006 struct nfs4_lockowner *lo;
5007
Trond Myklebustd4f04892014-07-29 21:34:36 -04005008 spin_lock(&clp->cl_lock);
5009 lo = find_lockowner_str_locked(clid, owner, clp);
5010 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005011 return lo;
5012}
5013
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005014static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5015{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005016 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005017}
5018
Jeff Layton6b180f02014-07-29 21:34:26 -04005019static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5020{
5021 struct nfs4_lockowner *lo = lockowner(sop);
5022
5023 kmem_cache_free(lockowner_slab, lo);
5024}
5025
5026static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005027 .so_unhash = nfs4_unhash_lockowner,
5028 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005029};
5030
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031/*
5032 * Alloc a lock owner structure.
5033 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005034 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005036 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005038static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005039alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5040 struct nfs4_ol_stateid *open_stp,
5041 struct nfsd4_lock *lock)
5042{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005043 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005045 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5046 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005048 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5049 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005050 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005051 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005052 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005053 ret = find_lockowner_str_locked(&clp->cl_clientid,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005054 &lock->lk_new_owner, clp);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005055 if (ret == NULL) {
5056 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005057 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005058 ret = lo;
5059 } else
5060 nfs4_free_lockowner(&lo->lo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005061 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005062 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063}
5064
Jeff Layton356a95e2014-07-29 21:34:13 -04005065static void
5066init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5067 struct nfs4_file *fp, struct inode *inode,
5068 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005070 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071
Jeff Layton356a95e2014-07-29 21:34:13 -04005072 lockdep_assert_held(&clp->cl_lock);
5073
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005074 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005075 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005076 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07005077 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005078 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04005079 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005080 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005082 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005083 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005084 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005085 spin_lock(&fp->fi_lock);
5086 list_add(&stp->st_perfile, &fp->fi_stateids);
5087 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088}
5089
Jeff Laytonc53530d2014-06-30 11:48:39 -04005090static struct nfs4_ol_stateid *
5091find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5092{
5093 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005094 struct nfs4_client *clp = lo->lo_owner.so_client;
5095
5096 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005097
5098 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005099 if (lst->st_stid.sc_file == fp) {
5100 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005101 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005102 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005103 }
5104 return NULL;
5105}
5106
Jeff Layton356a95e2014-07-29 21:34:13 -04005107static struct nfs4_ol_stateid *
5108find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5109 struct inode *inode, struct nfs4_ol_stateid *ost,
5110 bool *new)
5111{
5112 struct nfs4_stid *ns = NULL;
5113 struct nfs4_ol_stateid *lst;
5114 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5115 struct nfs4_client *clp = oo->oo_owner.so_client;
5116
5117 spin_lock(&clp->cl_lock);
5118 lst = find_lock_stateid(lo, fi);
5119 if (lst == NULL) {
5120 spin_unlock(&clp->cl_lock);
5121 ns = nfs4_alloc_stid(clp, stateid_slab);
5122 if (ns == NULL)
5123 return NULL;
5124
5125 spin_lock(&clp->cl_lock);
5126 lst = find_lock_stateid(lo, fi);
5127 if (likely(!lst)) {
5128 lst = openlockstateid(ns);
5129 init_lock_stateid(lst, lo, fi, inode, ost);
5130 ns = NULL;
5131 *new = true;
5132 }
5133 }
5134 spin_unlock(&clp->cl_lock);
5135 if (ns)
5136 nfs4_put_stid(ns);
5137 return lst;
5138}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005139
NeilBrownfd39ca92005-06-23 22:04:03 -07005140static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141check_lock_length(u64 offset, u64 length)
5142{
Benny Halevy87df4de2008-12-15 19:42:03 +02005143 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 LOFF_OVERFLOW(offset, length)));
5145}
5146
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005147static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005148{
Trond Myklebust11b91642014-07-29 21:34:08 -04005149 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005150
Jeff Layton7214e862014-07-10 14:07:33 -04005151 lockdep_assert_held(&fp->fi_lock);
5152
Jeff Layton82c5ff12012-05-11 09:45:13 -04005153 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005154 return;
Jeff Layton12659652014-07-10 14:07:28 -04005155 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005156 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005157}
5158
Jeff Layton356a95e2014-07-29 21:34:13 -04005159static __be32
5160lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5161 struct nfs4_ol_stateid *ost,
5162 struct nfsd4_lock *lock,
5163 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005164{
Jeff Layton5db1c032014-07-29 21:34:28 -04005165 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005166 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005167 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5168 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005169 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005170 struct nfs4_lockowner *lo;
5171 unsigned int strhashval;
5172
Trond Myklebustd4f04892014-07-29 21:34:36 -04005173 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005174 if (!lo) {
Trond Myklebustd4f04892014-07-29 21:34:36 -04005175 strhashval = ownerstr_hashval(&lock->v.new.owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005176 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5177 if (lo == NULL)
5178 return nfserr_jukebox;
5179 } else {
5180 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005181 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005182 if (!cstate->minorversion &&
5183 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005184 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005185 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005186
Jeff Layton356a95e2014-07-29 21:34:13 -04005187 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005188 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005189 status = nfserr_jukebox;
5190 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005191 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005192 status = nfs_ok;
5193out:
5194 nfs4_put_stateowner(&lo->lo_owner);
5195 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005196}
5197
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198/*
5199 * LOCK operation
5200 */
Al Virob37ad282006-10-19 23:28:59 -07005201__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005202nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005203 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005205 struct nfs4_openowner *open_sop = NULL;
5206 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005207 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005208 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005209 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005210 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005211 struct file_lock *file_lock = NULL;
5212 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005213 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005214 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005215 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005216 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005217 struct net *net = SVC_NET(rqstp);
5218 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219
5220 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5221 (long long) lock->lk_offset,
5222 (long long) lock->lk_length);
5223
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 if (check_lock_length(lock->lk_offset, lock->lk_length))
5225 return nfserr_inval;
5226
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005227 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005228 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005229 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5230 return status;
5231 }
5232
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005234 if (nfsd4_has_session(cstate))
5235 /* See rfc 5661 18.10.3: given clientid is ignored: */
5236 memcpy(&lock->v.new.clientid,
5237 &cstate->session->se_client->cl_clientid,
5238 sizeof(clientid_t));
5239
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005241 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005245 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 lock->lk_new_open_seqid,
5247 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005248 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005249 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005251 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005252 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005253 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005254 &lock->v.new.clientid))
5255 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005256 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005257 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005258 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005259 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005260 lock->lk_old_lock_seqid,
5261 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005262 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005263 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005264 if (status)
5265 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005266 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005268 lkflg = setlkflg(lock->lk_type);
5269 status = nfs4_check_openmode(lock_stp, lkflg);
5270 if (status)
5271 goto out;
5272
NeilBrown0dd395d2005-07-07 17:59:15 -07005273 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005274 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005275 goto out;
5276 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005277 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005278 goto out;
5279
Jeff Layton21179d82012-08-21 08:03:32 -04005280 file_lock = locks_alloc_lock();
5281 if (!file_lock) {
5282 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5283 status = nfserr_jukebox;
5284 goto out;
5285 }
5286
Trond Myklebust11b91642014-07-29 21:34:08 -04005287 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288 switch (lock->lk_type) {
5289 case NFS4_READ_LT:
5290 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005291 spin_lock(&fp->fi_lock);
5292 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005293 if (filp)
5294 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005295 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005296 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005297 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 case NFS4_WRITE_LT:
5299 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005300 spin_lock(&fp->fi_lock);
5301 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005302 if (filp)
5303 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005304 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005305 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005306 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 default:
5308 status = nfserr_inval;
5309 goto out;
5310 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005311 if (!filp) {
5312 status = nfserr_openmode;
5313 goto out;
5314 }
Kinglong Meeaef95832014-08-22 10:18:44 -04005315
5316 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04005317 file_lock->fl_pid = current->tgid;
5318 file_lock->fl_file = filp;
5319 file_lock->fl_flags = FL_POSIX;
5320 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5321 file_lock->fl_start = lock->lk_offset;
5322 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5323 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324
Jeff Layton21179d82012-08-21 08:03:32 -04005325 conflock = locks_alloc_lock();
5326 if (!conflock) {
5327 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5328 status = nfserr_jukebox;
5329 goto out;
5330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331
Jeff Layton21179d82012-08-21 08:03:32 -04005332 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005333 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005335 update_stateid(&lock_stp->st_stid.sc_stateid);
5336 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005338 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005339 break;
5340 case (EAGAIN): /* conflock holds conflicting lock */
5341 status = nfserr_denied;
5342 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005343 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005344 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 case (EDEADLK):
5346 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005347 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005348 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005349 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005350 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005351 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353out:
Trond Myklebustde186432014-07-10 14:07:26 -04005354 if (filp)
5355 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005356 if (lock_stp) {
5357 /* Bump seqid manually if the 4.0 replay owner is openowner */
5358 if (cstate->replay_owner &&
5359 cstate->replay_owner != &lock_sop->lo_owner &&
5360 seqid_mutating_err(ntohl(status)))
5361 lock_sop->lo_owner.so_seqid++;
5362
5363 /*
5364 * If this is a new, never-before-used stateid, and we are
5365 * returning an error, then just go ahead and release it.
5366 */
5367 if (status && new)
5368 release_lock_stateid(lock_stp);
5369
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005370 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005371 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005372 if (open_stp)
5373 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005374 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005375 if (file_lock)
5376 locks_free_lock(file_lock);
5377 if (conflock)
5378 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 return status;
5380}
5381
5382/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005383 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5384 * so we do a temporary open here just to get an open file to pass to
5385 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5386 * inode operation.)
5387 */
Al Viro04da6e92012-04-13 00:00:04 -04005388static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005389{
5390 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005391 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5392 if (!err) {
5393 err = nfserrno(vfs_test_lock(file, lock));
5394 nfsd_close(file);
5395 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005396 return err;
5397}
5398
5399/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 * LOCKT operation
5401 */
Al Virob37ad282006-10-19 23:28:59 -07005402__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005403nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5404 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405{
Jeff Layton21179d82012-08-21 08:03:32 -04005406 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005407 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005408 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005409 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005411 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 return nfserr_grace;
5413
5414 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5415 return nfserr_inval;
5416
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005417 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005418 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005419 if (status)
5420 goto out;
5421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005423 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425
Jeff Layton21179d82012-08-21 08:03:32 -04005426 file_lock = locks_alloc_lock();
5427 if (!file_lock) {
5428 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5429 status = nfserr_jukebox;
5430 goto out;
5431 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005432
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 switch (lockt->lt_type) {
5434 case NFS4_READ_LT:
5435 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005436 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 break;
5438 case NFS4_WRITE_LT:
5439 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005440 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 break;
5442 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005443 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 status = nfserr_inval;
5445 goto out;
5446 }
5447
Trond Myklebustd4f04892014-07-29 21:34:36 -04005448 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5449 cstate->clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005450 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005451 file_lock->fl_owner = (fl_owner_t)lo;
5452 file_lock->fl_pid = current->tgid;
5453 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454
Jeff Layton21179d82012-08-21 08:03:32 -04005455 file_lock->fl_start = lockt->lt_offset;
5456 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457
Jeff Layton21179d82012-08-21 08:03:32 -04005458 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459
Jeff Layton21179d82012-08-21 08:03:32 -04005460 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005461 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005462 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005463
Jeff Layton21179d82012-08-21 08:03:32 -04005464 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005466 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 }
5468out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005469 if (lo)
5470 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04005471 if (file_lock)
5472 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 return status;
5474}
5475
Al Virob37ad282006-10-19 23:28:59 -07005476__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005477nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005478 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005480 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005482 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005483 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005484 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005485 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5486
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5488 (long long) locku->lu_offset,
5489 (long long) locku->lu_length);
5490
5491 if (check_lock_length(locku->lu_offset, locku->lu_length))
5492 return nfserr_inval;
5493
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005494 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005495 &locku->lu_stateid, NFS4_LOCK_STID,
5496 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005497 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005499 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005500 if (!filp) {
5501 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005502 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005503 }
Jeff Layton21179d82012-08-21 08:03:32 -04005504 file_lock = locks_alloc_lock();
5505 if (!file_lock) {
5506 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5507 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005508 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005509 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005510
Jeff Layton21179d82012-08-21 08:03:32 -04005511 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04005512 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04005513 file_lock->fl_pid = current->tgid;
5514 file_lock->fl_file = filp;
5515 file_lock->fl_flags = FL_POSIX;
5516 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5517 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518
Jeff Layton21179d82012-08-21 08:03:32 -04005519 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5520 locku->lu_length);
5521 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522
Jeff Layton21179d82012-08-21 08:03:32 -04005523 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005524 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005525 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 goto out_nfserr;
5527 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005528 update_stateid(&stp->st_stid.sc_stateid);
5529 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005530fput:
5531 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005532put_stateid:
5533 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005535 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005536 if (file_lock)
5537 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 return status;
5539
5540out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005541 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005542 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543}
5544
5545/*
5546 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005547 * true: locks held by lockowner
5548 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005550static bool
5551check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552{
5553 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005554 int status = false;
5555 struct file *filp = find_any_file(fp);
5556 struct inode *inode;
5557
5558 if (!filp) {
5559 /* Any valid lock stateid should have some sort of access */
5560 WARN_ON_ONCE(1);
5561 return status;
5562 }
5563
5564 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565
Jeff Layton1c8c6012013-06-21 08:58:15 -04005566 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005568 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005569 status = true;
5570 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005573 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005574 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 return status;
5576}
5577
Al Virob37ad282006-10-19 23:28:59 -07005578__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005579nfsd4_release_lockowner(struct svc_rqst *rqstp,
5580 struct nfsd4_compound_state *cstate,
5581 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582{
5583 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005584 struct nfs4_stateowner *sop;
5585 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005586 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005588 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005589 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005590 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005591 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592
5593 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5594 clid->cl_boot, clid->cl_id);
5595
Jeff Layton4b24ca72014-06-30 11:48:44 -04005596 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005597 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04005598 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005599
Trond Myklebustd4f04892014-07-29 21:34:36 -04005600 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005601 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005602 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005603 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005604 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005605
5606 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005607 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005608
Jeff Layton882e9d22014-07-29 21:34:37 -04005609 /* see if there are still any locks associated with it */
5610 lo = lockowner(sop);
5611 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5612 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5613 status = nfserr_locks_held;
5614 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04005615 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04005616 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005617 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005618
Kinglong Meeb5971af2014-08-22 10:18:43 -04005619 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04005620 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005621 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005622 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005623 if (lo)
5624 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 return status;
5626}
5627
5628static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005629alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630{
NeilBrowna55370a2005-06-23 22:03:52 -07005631 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632}
5633
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005634bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005635nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005636{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005637 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005638
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005639 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005640 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005641}
5642
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643/*
5644 * failure => all reset bets are off, nfserr_no_grace...
5645 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005646struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005647nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648{
5649 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005650 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651
NeilBrowna55370a2005-06-23 22:03:52 -07005652 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5653 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005654 if (crp) {
5655 strhashval = clientstr_hashval(name);
5656 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005657 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005658 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005659 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005660 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005661 }
5662 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663}
5664
Jeff Layton2a4317c2012-03-21 16:42:43 -04005665void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005666nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005667{
5668 list_del(&crp->cr_strhash);
5669 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005670 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005671}
5672
5673void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005674nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675{
5676 struct nfs4_client_reclaim *crp = NULL;
5677 int i;
5678
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005680 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5681 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005683 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 }
5685 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005686 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687}
5688
5689/*
5690 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005691struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005692nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693{
5694 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 struct nfs4_client_reclaim *crp = NULL;
5696
Jeff Layton278c9312012-11-12 15:00:52 -05005697 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698
Jeff Layton278c9312012-11-12 15:00:52 -05005699 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005700 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005701 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 return crp;
5703 }
5704 }
5705 return NULL;
5706}
5707
5708/*
5709* Called from OPEN. Look for clientid in reclaim list.
5710*/
Al Virob37ad282006-10-19 23:28:59 -07005711__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005712nfs4_check_open_reclaim(clientid_t *clid,
5713 struct nfsd4_compound_state *cstate,
5714 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005716 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005717
5718 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005719 status = lookup_clientid(clid, cstate, nn);
5720 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005721 return nfserr_reclaim_bad;
5722
Jeff Layton3b3e7b72014-09-12 16:40:22 -04005723 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
5724 return nfserr_no_grace;
5725
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005726 if (nfsd4_client_record_check(cstate->clp))
5727 return nfserr_reclaim_bad;
5728
5729 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730}
5731
Bryan Schumaker65178db2011-11-01 13:35:21 -04005732#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04005733static inline void
5734put_client(struct nfs4_client *clp)
5735{
5736 atomic_dec(&clp->cl_refcount);
5737}
5738
Jeff Layton285abde2014-07-30 08:27:24 -04005739static struct nfs4_client *
5740nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5741{
5742 struct nfs4_client *clp;
5743 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5744 nfsd_net_id);
5745
5746 if (!nfsd_netns_ready(nn))
5747 return NULL;
5748
5749 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5750 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5751 return clp;
5752 }
5753 return NULL;
5754}
5755
Jeff Layton7ec0e362014-07-30 08:27:17 -04005756u64
Jeff Layton285abde2014-07-30 08:27:24 -04005757nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04005758{
5759 struct nfs4_client *clp;
5760 u64 count = 0;
5761 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5762 nfsd_net_id);
5763 char buf[INET6_ADDRSTRLEN];
5764
5765 if (!nfsd_netns_ready(nn))
5766 return 0;
5767
5768 spin_lock(&nn->client_lock);
5769 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5770 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5771 pr_info("NFS Client: %s\n", buf);
5772 ++count;
5773 }
5774 spin_unlock(&nn->client_lock);
5775
5776 return count;
5777}
Bryan Schumaker65178db2011-11-01 13:35:21 -04005778
Jeff Laytona0926d12014-07-30 08:27:18 -04005779u64
Jeff Layton285abde2014-07-30 08:27:24 -04005780nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04005781{
5782 u64 count = 0;
5783 struct nfs4_client *clp;
5784 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5785 nfsd_net_id);
5786
5787 if (!nfsd_netns_ready(nn))
5788 return count;
5789
5790 spin_lock(&nn->client_lock);
5791 clp = nfsd_find_client(addr, addr_size);
5792 if (clp) {
5793 if (mark_client_expired_locked(clp) == nfs_ok)
5794 ++count;
5795 else
5796 clp = NULL;
5797 }
5798 spin_unlock(&nn->client_lock);
5799
5800 if (clp)
5801 expire_client(clp);
5802
5803 return count;
5804}
5805
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005806u64
Jeff Layton285abde2014-07-30 08:27:24 -04005807nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005808{
5809 u64 count = 0;
5810 struct nfs4_client *clp, *next;
5811 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5812 nfsd_net_id);
5813 LIST_HEAD(reaplist);
5814
5815 if (!nfsd_netns_ready(nn))
5816 return count;
5817
5818 spin_lock(&nn->client_lock);
5819 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5820 if (mark_client_expired_locked(clp) == nfs_ok) {
5821 list_add(&clp->cl_lru, &reaplist);
5822 if (max != 0 && ++count >= max)
5823 break;
5824 }
5825 }
5826 spin_unlock(&nn->client_lock);
5827
5828 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
5829 expire_client(clp);
5830
5831 return count;
5832}
5833
Bryan Schumaker184c1842012-11-29 11:40:44 -05005834static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5835 const char *type)
5836{
5837 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005838 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005839 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5840}
5841
Jeff Layton016200c2014-07-30 08:27:21 -04005842static void
5843nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
5844 struct list_head *collect)
5845{
5846 struct nfs4_client *clp = lst->st_stid.sc_client;
5847 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5848 nfsd_net_id);
5849
5850 if (!collect)
5851 return;
5852
5853 lockdep_assert_held(&nn->client_lock);
5854 atomic_inc(&clp->cl_refcount);
5855 list_add(&lst->st_locks, collect);
5856}
5857
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005858static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04005859 struct list_head *collect,
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005860 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005861{
5862 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005863 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005864 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005865 u64 count = 0;
5866
Jeff Layton016200c2014-07-30 08:27:21 -04005867 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005868 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005869 list_for_each_entry_safe(stp, st_next,
5870 &oop->oo_owner.so_stateids, st_perstateowner) {
5871 list_for_each_entry_safe(lst, lst_next,
5872 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04005873 if (func) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005874 func(lst);
Jeff Layton016200c2014-07-30 08:27:21 -04005875 nfsd_inject_add_lock_to_list(lst,
5876 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04005877 }
Jeff Layton016200c2014-07-30 08:27:21 -04005878 ++count;
5879 /*
5880 * Despite the fact that these functions deal
5881 * with 64-bit integers for "count", we must
5882 * ensure that it doesn't blow up the
5883 * clp->cl_refcount. Throw a warning if we
5884 * start to approach INT_MAX here.
5885 */
5886 WARN_ON_ONCE(count == (INT_MAX / 2));
5887 if (count == max)
5888 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005889 }
5890 }
5891 }
Jeff Layton016200c2014-07-30 08:27:21 -04005892out:
5893 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005894
5895 return count;
5896}
5897
Jeff Layton016200c2014-07-30 08:27:21 -04005898static u64
5899nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
5900 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05005901{
Jeff Layton016200c2014-07-30 08:27:21 -04005902 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005903}
5904
Jeff Layton016200c2014-07-30 08:27:21 -04005905static u64
5906nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05005907{
Jeff Layton016200c2014-07-30 08:27:21 -04005908 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005909 nfsd_print_count(clp, count, "locked files");
5910 return count;
5911}
5912
Jeff Layton016200c2014-07-30 08:27:21 -04005913u64
Jeff Layton285abde2014-07-30 08:27:24 -04005914nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04005915{
5916 struct nfs4_client *clp;
5917 u64 count = 0;
5918 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5919 nfsd_net_id);
5920
5921 if (!nfsd_netns_ready(nn))
5922 return 0;
5923
5924 spin_lock(&nn->client_lock);
5925 list_for_each_entry(clp, &nn->client_lru, cl_lru)
5926 count += nfsd_print_client_locks(clp);
5927 spin_unlock(&nn->client_lock);
5928
5929 return count;
5930}
5931
5932static void
5933nfsd_reap_locks(struct list_head *reaplist)
5934{
5935 struct nfs4_client *clp;
5936 struct nfs4_ol_stateid *stp, *next;
5937
5938 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
5939 list_del_init(&stp->st_locks);
5940 clp = stp->st_stid.sc_client;
5941 nfs4_put_stid(&stp->st_stid);
5942 put_client(clp);
5943 }
5944}
5945
5946u64
Jeff Layton285abde2014-07-30 08:27:24 -04005947nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04005948{
5949 unsigned int count = 0;
5950 struct nfs4_client *clp;
5951 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5952 nfsd_net_id);
5953 LIST_HEAD(reaplist);
5954
5955 if (!nfsd_netns_ready(nn))
5956 return count;
5957
5958 spin_lock(&nn->client_lock);
5959 clp = nfsd_find_client(addr, addr_size);
5960 if (clp)
5961 count = nfsd_collect_client_locks(clp, &reaplist, 0);
5962 spin_unlock(&nn->client_lock);
5963 nfsd_reap_locks(&reaplist);
5964 return count;
5965}
5966
5967u64
Jeff Layton285abde2014-07-30 08:27:24 -04005968nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04005969{
5970 u64 count = 0;
5971 struct nfs4_client *clp;
5972 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5973 nfsd_net_id);
5974 LIST_HEAD(reaplist);
5975
5976 if (!nfsd_netns_ready(nn))
5977 return count;
5978
5979 spin_lock(&nn->client_lock);
5980 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5981 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
5982 if (max != 0 && count >= max)
5983 break;
5984 }
5985 spin_unlock(&nn->client_lock);
5986 nfsd_reap_locks(&reaplist);
5987 return count;
5988}
5989
Jeff Layton82e05ef2014-07-30 08:27:22 -04005990static u64
5991nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
5992 struct list_head *collect,
5993 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005994{
5995 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04005996 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5997 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005998 u64 count = 0;
5999
Jeff Layton82e05ef2014-07-30 08:27:22 -04006000 lockdep_assert_held(&nn->client_lock);
6001
6002 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006003 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04006004 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006005 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04006006 if (collect) {
6007 atomic_inc(&clp->cl_refcount);
6008 list_add(&oop->oo_perclient, collect);
6009 }
6010 }
6011 ++count;
6012 /*
6013 * Despite the fact that these functions deal with
6014 * 64-bit integers for "count", we must ensure that
6015 * it doesn't blow up the clp->cl_refcount. Throw a
6016 * warning if we start to approach INT_MAX here.
6017 */
6018 WARN_ON_ONCE(count == (INT_MAX / 2));
6019 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006020 break;
6021 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04006022 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006023
6024 return count;
6025}
6026
Jeff Layton82e05ef2014-07-30 08:27:22 -04006027static u64
6028nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006029{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006030 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6031
6032 nfsd_print_count(clp, count, "openowners");
6033 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006034}
6035
Jeff Layton82e05ef2014-07-30 08:27:22 -04006036static u64
6037nfsd_collect_client_openowners(struct nfs4_client *clp,
6038 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006039{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006040 return nfsd_foreach_client_openowner(clp, max, collect,
6041 unhash_openowner_locked);
6042}
6043
6044u64
Jeff Layton285abde2014-07-30 08:27:24 -04006045nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006046{
6047 struct nfs4_client *clp;
6048 u64 count = 0;
6049 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6050 nfsd_net_id);
6051
6052 if (!nfsd_netns_ready(nn))
6053 return 0;
6054
6055 spin_lock(&nn->client_lock);
6056 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6057 count += nfsd_print_client_openowners(clp);
6058 spin_unlock(&nn->client_lock);
6059
6060 return count;
6061}
6062
6063static void
6064nfsd_reap_openowners(struct list_head *reaplist)
6065{
6066 struct nfs4_client *clp;
6067 struct nfs4_openowner *oop, *next;
6068
6069 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6070 list_del_init(&oop->oo_perclient);
6071 clp = oop->oo_owner.so_client;
6072 release_openowner(oop);
6073 put_client(clp);
6074 }
6075}
6076
6077u64
Jeff Layton285abde2014-07-30 08:27:24 -04006078nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6079 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006080{
6081 unsigned int count = 0;
6082 struct nfs4_client *clp;
6083 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6084 nfsd_net_id);
6085 LIST_HEAD(reaplist);
6086
6087 if (!nfsd_netns_ready(nn))
6088 return count;
6089
6090 spin_lock(&nn->client_lock);
6091 clp = nfsd_find_client(addr, addr_size);
6092 if (clp)
6093 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6094 spin_unlock(&nn->client_lock);
6095 nfsd_reap_openowners(&reaplist);
6096 return count;
6097}
6098
6099u64
Jeff Layton285abde2014-07-30 08:27:24 -04006100nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006101{
6102 u64 count = 0;
6103 struct nfs4_client *clp;
6104 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6105 nfsd_net_id);
6106 LIST_HEAD(reaplist);
6107
6108 if (!nfsd_netns_ready(nn))
6109 return count;
6110
6111 spin_lock(&nn->client_lock);
6112 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6113 count += nfsd_collect_client_openowners(clp, &reaplist,
6114 max - count);
6115 if (max != 0 && count >= max)
6116 break;
6117 }
6118 spin_unlock(&nn->client_lock);
6119 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006120 return count;
6121}
6122
Bryan Schumaker269de302012-11-29 11:40:42 -05006123static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6124 struct list_head *victims)
6125{
6126 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006127 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6128 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006129 u64 count = 0;
6130
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006131 lockdep_assert_held(&nn->client_lock);
6132
6133 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006134 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006135 if (victims) {
6136 /*
6137 * It's not safe to mess with delegations that have a
6138 * non-zero dl_time. They might have already been broken
6139 * and could be processed by the laundromat outside of
6140 * the state_lock. Just leave them be.
6141 */
6142 if (dp->dl_time != 0)
6143 continue;
6144
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006145 atomic_inc(&clp->cl_refcount);
Jeff Layton42690672014-07-25 07:34:20 -04006146 unhash_delegation_locked(dp);
6147 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006148 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006149 ++count;
6150 /*
6151 * Despite the fact that these functions deal with
6152 * 64-bit integers for "count", we must ensure that
6153 * it doesn't blow up the clp->cl_refcount. Throw a
6154 * warning if we start to approach INT_MAX here.
6155 */
6156 WARN_ON_ONCE(count == (INT_MAX / 2));
6157 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006158 break;
6159 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006160 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006161 return count;
6162}
6163
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006164static u64
6165nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006166{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006167 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006168
6169 nfsd_print_count(clp, count, "delegations");
6170 return count;
6171}
6172
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006173u64
Jeff Layton285abde2014-07-30 08:27:24 -04006174nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006175{
6176 struct nfs4_client *clp;
6177 u64 count = 0;
6178 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6179 nfsd_net_id);
6180
6181 if (!nfsd_netns_ready(nn))
6182 return 0;
6183
6184 spin_lock(&nn->client_lock);
6185 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6186 count += nfsd_print_client_delegations(clp);
6187 spin_unlock(&nn->client_lock);
6188
6189 return count;
6190}
6191
6192static void
6193nfsd_forget_delegations(struct list_head *reaplist)
6194{
6195 struct nfs4_client *clp;
6196 struct nfs4_delegation *dp, *next;
6197
6198 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6199 list_del_init(&dp->dl_recall_lru);
6200 clp = dp->dl_stid.sc_client;
6201 revoke_delegation(dp);
6202 put_client(clp);
6203 }
6204}
6205
6206u64
Jeff Layton285abde2014-07-30 08:27:24 -04006207nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6208 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006209{
6210 u64 count = 0;
6211 struct nfs4_client *clp;
6212 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6213 nfsd_net_id);
6214 LIST_HEAD(reaplist);
6215
6216 if (!nfsd_netns_ready(nn))
6217 return count;
6218
6219 spin_lock(&nn->client_lock);
6220 clp = nfsd_find_client(addr, addr_size);
6221 if (clp)
6222 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6223 spin_unlock(&nn->client_lock);
6224
6225 nfsd_forget_delegations(&reaplist);
6226 return count;
6227}
6228
6229u64
Jeff Layton285abde2014-07-30 08:27:24 -04006230nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006231{
6232 u64 count = 0;
6233 struct nfs4_client *clp;
6234 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6235 nfsd_net_id);
6236 LIST_HEAD(reaplist);
6237
6238 if (!nfsd_netns_ready(nn))
6239 return count;
6240
6241 spin_lock(&nn->client_lock);
6242 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6243 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6244 if (max != 0 && count >= max)
6245 break;
6246 }
6247 spin_unlock(&nn->client_lock);
6248 nfsd_forget_delegations(&reaplist);
6249 return count;
6250}
6251
6252static void
6253nfsd_recall_delegations(struct list_head *reaplist)
6254{
6255 struct nfs4_client *clp;
6256 struct nfs4_delegation *dp, *next;
6257
6258 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6259 list_del_init(&dp->dl_recall_lru);
6260 clp = dp->dl_stid.sc_client;
6261 /*
6262 * We skipped all entries that had a zero dl_time before,
6263 * so we can now reset the dl_time back to 0. If a delegation
6264 * break comes in now, then it won't make any difference since
6265 * we're recalling it either way.
6266 */
6267 spin_lock(&state_lock);
6268 dp->dl_time = 0;
6269 spin_unlock(&state_lock);
6270 nfsd_break_one_deleg(dp);
6271 put_client(clp);
6272 }
6273}
6274
6275u64
Jeff Layton285abde2014-07-30 08:27:24 -04006276nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006277 size_t addr_size)
6278{
6279 u64 count = 0;
6280 struct nfs4_client *clp;
6281 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6282 nfsd_net_id);
6283 LIST_HEAD(reaplist);
6284
6285 if (!nfsd_netns_ready(nn))
6286 return count;
6287
6288 spin_lock(&nn->client_lock);
6289 clp = nfsd_find_client(addr, addr_size);
6290 if (clp)
6291 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6292 spin_unlock(&nn->client_lock);
6293
6294 nfsd_recall_delegations(&reaplist);
6295 return count;
6296}
6297
6298u64
Jeff Layton285abde2014-07-30 08:27:24 -04006299nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006300{
6301 u64 count = 0;
6302 struct nfs4_client *clp, *next;
6303 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6304 nfsd_net_id);
6305 LIST_HEAD(reaplist);
6306
6307 if (!nfsd_netns_ready(nn))
6308 return count;
6309
6310 spin_lock(&nn->client_lock);
6311 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6312 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6313 if (max != 0 && ++count >= max)
6314 break;
6315 }
6316 spin_unlock(&nn->client_lock);
6317 nfsd_recall_delegations(&reaplist);
6318 return count;
6319}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006320#endif /* CONFIG_NFSD_FAULT_INJECTION */
6321
Meelap Shahc2f1a552007-07-17 04:04:39 -07006322/*
6323 * Since the lifetime of a delegation isn't limited to that of an open, a
6324 * client may quite reasonably hang on to a delegation as long as it has
6325 * the inode cached. This becomes an obvious problem the first time a
6326 * client's inode cache approaches the size of the server's total memory.
6327 *
6328 * For now we avoid this problem by imposing a hard limit on the number
6329 * of delegations, which varies according to the server's memory size.
6330 */
6331static void
6332set_max_delegations(void)
6333{
6334 /*
6335 * Allow at most 4 delegations per megabyte of RAM. Quick
6336 * estimates suggest that in the worst case (where every delegation
6337 * is for a different inode), a delegation could take about 1.5K,
6338 * giving a worst case usage of about 6% of memory.
6339 */
6340 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6341}
6342
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006343static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006344{
6345 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6346 int i;
6347
6348 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6349 CLIENT_HASH_SIZE, GFP_KERNEL);
6350 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006351 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006352 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6353 CLIENT_HASH_SIZE, GFP_KERNEL);
6354 if (!nn->unconf_id_hashtbl)
6355 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006356 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6357 SESSION_HASH_SIZE, GFP_KERNEL);
6358 if (!nn->sessionid_hashtbl)
6359 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006360
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006361 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006362 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006363 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006364 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006365 for (i = 0; i < SESSION_HASH_SIZE; i++)
6366 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006367 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006368 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03006369 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03006370 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006371 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03006372 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006373
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006374 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006375 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006376
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006377 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006378
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006379err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03006380 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006381err_unconf_id:
6382 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006383err:
6384 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006385}
6386
6387static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006388nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006389{
6390 int i;
6391 struct nfs4_client *clp = NULL;
6392 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6393
6394 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6395 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6396 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6397 destroy_client(clp);
6398 }
6399 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006400
Kinglong Mee2b905632014-03-26 22:09:30 +08006401 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6402 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6403 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6404 destroy_client(clp);
6405 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006406 }
6407
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006408 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006409 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006410 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006411 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006412}
6413
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006414int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006415nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07006416{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006417 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006418 int ret;
6419
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006420 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006421 if (ret)
6422 return ret;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006423 nn->boot_time = get_seconds();
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04006424 nn->grace_ended = false;
Jeff Laytond4318ac2014-09-12 16:40:21 -04006425 locks_start_grace(net, &nn->nfsd4_manager);
6426 nfsd4_client_tracking_init(net);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006427 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03006428 nn->nfsd4_grace, net);
6429 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006430 return 0;
6431}
6432
6433/* initialization to perform when the nfsd service is started: */
6434
6435int
6436nfs4_state_start(void)
6437{
6438 int ret;
6439
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006440 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006441 if (ret)
6442 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07006443 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05006444 if (laundry_wq == NULL) {
6445 ret = -ENOMEM;
6446 goto out_recovery;
6447 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006448 ret = nfsd4_create_callback_queue();
6449 if (ret)
6450 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006451
Meelap Shahc2f1a552007-07-17 04:04:39 -07006452 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006453
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006454 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006455
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006456out_free_laundry:
6457 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05006458out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006459 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460}
6461
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006462void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006463nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006467 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006469 cancel_delayed_work_sync(&nn->laundromat_work);
6470 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05006471
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04006473 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006474 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006476 unhash_delegation_locked(dp);
6477 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478 }
Benny Halevycdc97502014-05-30 09:09:30 -04006479 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480 list_for_each_safe(pos, next, &reaplist) {
6481 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006482 list_del_init(&dp->dl_recall_lru);
Jeff Laytonafbda402014-08-09 10:22:41 -04006483 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04006484 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485 }
6486
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006487 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006488 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489}
6490
6491void
6492nfs4_state_shutdown(void)
6493{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006494 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006495 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006497
6498static void
6499get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6500{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006501 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6502 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006503}
6504
6505static void
6506put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6507{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006508 if (cstate->minorversion) {
6509 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6510 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6511 }
6512}
6513
6514void
6515clear_current_stateid(struct nfsd4_compound_state *cstate)
6516{
6517 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006518}
6519
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006520/*
6521 * functions to set current state id
6522 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006523void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006524nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6525{
6526 put_stateid(cstate, &odp->od_stateid);
6527}
6528
6529void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006530nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6531{
6532 put_stateid(cstate, &open->op_stateid);
6533}
6534
6535void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006536nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6537{
6538 put_stateid(cstate, &close->cl_stateid);
6539}
6540
6541void
6542nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6543{
6544 put_stateid(cstate, &lock->lk_resp_stateid);
6545}
6546
6547/*
6548 * functions to consume current state id
6549 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006550
6551void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006552nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6553{
6554 get_stateid(cstate, &odp->od_stateid);
6555}
6556
6557void
6558nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6559{
6560 get_stateid(cstate, &drp->dr_stateid);
6561}
6562
6563void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006564nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6565{
6566 get_stateid(cstate, &fsp->fr_stateid);
6567}
6568
6569void
6570nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6571{
6572 get_stateid(cstate, &setattr->sa_stateid);
6573}
6574
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006575void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006576nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6577{
6578 get_stateid(cstate, &close->cl_stateid);
6579}
6580
6581void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006582nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006583{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006584 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006585}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006586
6587void
6588nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6589{
6590 get_stateid(cstate, &read->rd_stateid);
6591}
6592
6593void
6594nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6595{
6596 get_stateid(cstate, &write->wr_stateid);
6597}