blob: 551f32d7f5c7bdc8aa4408897aa89228cdbe2ac1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
Dave Hansenaceaf782008-02-15 14:37:31 -080035#include <linux/file.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020036#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070038#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070039#include <linux/swap.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
Chuck Lever7df302f2012-05-29 13:56:37 -040041#include <linux/ratelimit.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050042#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton59766872013-02-04 12:50:00 -050043#include <linux/sunrpc/addr.h>
NeilBrown6282cd52014-06-04 17:39:26 +100044#include <linux/hash.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020045#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040046#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050047#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040048#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040050#include "netns.h"
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define NFSDDBG_FACILITY NFSDDBG_PROC
53
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050054#define all_ones {{~0,~0},~0}
55static const stateid_t one_stateid = {
56 .si_generation = ~0,
57 .si_opaque = all_ones,
58};
59static const stateid_t zero_stateid = {
60 /* all fields zero */
61};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010062static const stateid_t currentstateid = {
63 .si_generation = 1,
64};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050065
Andy Adamsonec6b5d72009-04-03 08:28:28 +030066static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070067
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050068#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
69#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010070#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040073static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040074static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
J. Bruce Fields8b671b82009-02-22 14:51:34 -080076/* Locking: */
77
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 Halevycdc975052014-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
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400221static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400222same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400223{
224 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400225 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400226}
227
228static struct nfs4_openowner *
229find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400230 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400231{
232 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400233
Trond Myklebustd4f04892014-07-29 21:34:36 -0400234 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400235
Trond Myklebustd4f04892014-07-29 21:34:36 -0400236 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
237 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400238 if (!so->so_is_open_owner)
239 continue;
Trond Myklebustd4f04892014-07-29 21:34:36 -0400240 if (same_owner_str(so, &open->op_owner)) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400241 atomic_inc(&so->so_count);
Trond Myklebustd4f04892014-07-29 21:34:36 -0400242 return openowner(so);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400243 }
244 }
245 return NULL;
246}
247
248static struct nfs4_openowner *
249find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400250 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400251{
252 struct nfs4_openowner *oo;
253
Trond Myklebustd4f04892014-07-29 21:34:36 -0400254 spin_lock(&clp->cl_lock);
255 oo = find_openstateowner_str_locked(hashval, open, clp);
256 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400257 return oo;
258}
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260static inline u32
261opaque_hashval(const void *ptr, int nbytes)
262{
263 unsigned char *cptr = (unsigned char *) ptr;
264
265 u32 x = 0;
266 while (nbytes--) {
267 x *= 37;
268 x += *cptr++;
269 }
270 return x;
271}
272
J. Bruce Fields32513b42011-10-13 16:00:16 -0400273static void nfsd4_free_file(struct nfs4_file *f)
274{
275 kmem_cache_free(file_slab, f);
276}
277
NeilBrown13cd2182005-06-23 22:03:10 -0700278static inline void
279put_nfs4_file(struct nfs4_file *fi)
280{
Jeff Layton02e12152014-07-16 10:31:57 -0400281 might_lock(&state_lock);
282
Benny Halevycdc975052014-05-30 09:09:30 -0400283 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton89876f82013-04-02 09:01:59 -0400284 hlist_del(&fi->fi_hash);
Benny Halevycdc975052014-05-30 09:09:30 -0400285 spin_unlock(&state_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400286 nfsd4_free_file(fi);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800287 }
NeilBrown13cd2182005-06-23 22:03:10 -0700288}
289
290static inline void
291get_nfs4_file(struct nfs4_file *fi)
292{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800293 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700294}
295
Trond Myklebustde186432014-07-10 14:07:26 -0400296static struct file *
297__nfs4_get_fd(struct nfs4_file *f, int oflag)
298{
299 if (f->fi_fds[oflag])
300 return get_file(f->fi_fds[oflag]);
301 return NULL;
302}
303
304static struct file *
305find_writeable_file_locked(struct nfs4_file *f)
306{
307 struct file *ret;
308
309 lockdep_assert_held(&f->fi_lock);
310
311 ret = __nfs4_get_fd(f, O_WRONLY);
312 if (!ret)
313 ret = __nfs4_get_fd(f, O_RDWR);
314 return ret;
315}
316
317static struct file *
318find_writeable_file(struct nfs4_file *f)
319{
320 struct file *ret;
321
322 spin_lock(&f->fi_lock);
323 ret = find_writeable_file_locked(f);
324 spin_unlock(&f->fi_lock);
325
326 return ret;
327}
328
329static struct file *find_readable_file_locked(struct nfs4_file *f)
330{
331 struct file *ret;
332
333 lockdep_assert_held(&f->fi_lock);
334
335 ret = __nfs4_get_fd(f, O_RDONLY);
336 if (!ret)
337 ret = __nfs4_get_fd(f, O_RDWR);
338 return ret;
339}
340
341static struct file *
342find_readable_file(struct nfs4_file *f)
343{
344 struct file *ret;
345
346 spin_lock(&f->fi_lock);
347 ret = find_readable_file_locked(f);
348 spin_unlock(&f->fi_lock);
349
350 return ret;
351}
352
353static struct file *
354find_any_file(struct nfs4_file *f)
355{
356 struct file *ret;
357
358 spin_lock(&f->fi_lock);
359 ret = __nfs4_get_fd(f, O_RDWR);
360 if (!ret) {
361 ret = __nfs4_get_fd(f, O_WRONLY);
362 if (!ret)
363 ret = __nfs4_get_fd(f, O_RDONLY);
364 }
365 spin_unlock(&f->fi_lock);
366 return ret;
367}
368
Trond Myklebust02a35082014-07-25 07:34:22 -0400369static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800370unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700371
372/*
373 * Open owner state (share locks)
374 */
375
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500376/* hash tables for lock and open owners */
377#define OWNER_HASH_BITS 8
378#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
379#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700380
Trond Myklebustd4f04892014-07-29 21:34:36 -0400381static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400382{
383 unsigned int ret;
384
385 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500386 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400387}
NeilBrownef0f3392006-04-10 22:55:41 -0700388
NeilBrownef0f3392006-04-10 22:55:41 -0700389/* hash table for nfs4_file */
390#define FILE_HASH_BITS 8
391#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800392
Trond Myklebustca943212014-07-23 16:17:39 -0400393static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400394{
Trond Myklebustca943212014-07-23 16:17:39 -0400395 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
396}
397
398static unsigned int file_hashval(struct knfsd_fh *fh)
399{
400 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
401}
402
403static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
404{
405 return fh1->fh_size == fh2->fh_size &&
406 !memcmp(fh1->fh_base.fh_pad,
407 fh2->fh_base.fh_pad,
408 fh1->fh_size);
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400409}
410
Jeff Layton89876f82013-04-02 09:01:59 -0400411static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700412
Jeff Layton12659652014-07-10 14:07:28 -0400413static void
414__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400415{
Jeff Layton7214e862014-07-10 14:07:33 -0400416 lockdep_assert_held(&fp->fi_lock);
417
Jeff Layton12659652014-07-10 14:07:28 -0400418 if (access & NFS4_SHARE_ACCESS_WRITE)
419 atomic_inc(&fp->fi_access[O_WRONLY]);
420 if (access & NFS4_SHARE_ACCESS_READ)
421 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400422}
423
Jeff Layton12659652014-07-10 14:07:28 -0400424static __be32
425nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400426{
Jeff Layton7214e862014-07-10 14:07:33 -0400427 lockdep_assert_held(&fp->fi_lock);
428
Jeff Layton12659652014-07-10 14:07:28 -0400429 /* Does this access mode make sense? */
430 if (access & ~NFS4_SHARE_ACCESS_BOTH)
431 return nfserr_inval;
432
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400433 /* Does it conflict with a deny mode already set? */
434 if ((access & fp->fi_share_deny) != 0)
435 return nfserr_share_denied;
436
Jeff Layton12659652014-07-10 14:07:28 -0400437 __nfs4_file_get_access(fp, access);
438 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400439}
440
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400441static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
442{
443 /* Common case is that there is no deny mode. */
444 if (deny) {
445 /* Does this deny mode make sense? */
446 if (deny & ~NFS4_SHARE_DENY_BOTH)
447 return nfserr_inval;
448
449 if ((deny & NFS4_SHARE_DENY_READ) &&
450 atomic_read(&fp->fi_access[O_RDONLY]))
451 return nfserr_share_denied;
452
453 if ((deny & NFS4_SHARE_DENY_WRITE) &&
454 atomic_read(&fp->fi_access[O_WRONLY]))
455 return nfserr_share_denied;
456 }
457 return nfs_ok;
458}
459
J. Bruce Fields998db522010-08-07 09:21:41 -0400460static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400461{
Trond Myklebustde186432014-07-10 14:07:26 -0400462 might_lock(&fp->fi_lock);
463
464 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
465 struct file *f1 = NULL;
466 struct file *f2 = NULL;
467
Jeff Layton6d338b52014-07-10 14:07:29 -0400468 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400469 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400470 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400471 spin_unlock(&fp->fi_lock);
472 if (f1)
473 fput(f1);
474 if (f2)
475 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400476 }
477}
478
Jeff Layton12659652014-07-10 14:07:28 -0400479static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400480{
Jeff Layton12659652014-07-10 14:07:28 -0400481 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
482
483 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400484 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400485 if (access & NFS4_SHARE_ACCESS_READ)
486 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400487}
488
Trond Myklebust60116952014-07-29 21:34:06 -0400489static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
490 struct kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400491{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500492 struct nfs4_stid *stid;
493 int new_id;
494
Trond Myklebustf8338832014-07-25 07:34:19 -0400495 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500496 if (!stid)
497 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400498
Jeff Layton4770d722014-07-29 21:34:10 -0400499 idr_preload(GFP_KERNEL);
500 spin_lock(&cl->cl_lock);
501 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
502 spin_unlock(&cl->cl_lock);
503 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700504 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500505 goto out_free;
506 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500507 stid->sc_stateid.si_opaque.so_id = new_id;
508 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
509 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400510 atomic_set(&stid->sc_count, 1);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500511
J. Bruce Fields996e0932011-10-17 11:14:48 -0400512 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500513 * It shouldn't be a problem to reuse an opaque stateid value.
514 * I don't think it is for 4.1. But with 4.0 I worry that, for
515 * example, a stray write retransmission could be accepted by
516 * the server when it should have been rejected. Therefore,
517 * adopt a trick from the sctp code to attempt to maximize the
518 * amount of time until an id is reused, by ensuring they always
519 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400520 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500521 return stid;
522out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800523 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500524 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400525}
526
Jeff Laytonb49e0842014-07-29 21:34:11 -0400527static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400528{
Trond Myklebust60116952014-07-29 21:34:06 -0400529 struct nfs4_stid *stid;
530 struct nfs4_ol_stateid *stp;
531
532 stid = nfs4_alloc_stid(clp, stateid_slab);
533 if (!stid)
534 return NULL;
535
536 stp = openlockstateid(stid);
537 stp->st_stid.sc_free = nfs4_free_ol_stateid;
538 return stp;
539}
540
541static void nfs4_free_deleg(struct nfs4_stid *stid)
542{
Trond Myklebust60116952014-07-29 21:34:06 -0400543 kmem_cache_free(deleg_slab, stid);
544 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400545}
546
NeilBrown6282cd52014-06-04 17:39:26 +1000547/*
548 * When we recall a delegation, we should be careful not to hand it
549 * out again straight away.
550 * To ensure this we keep a pair of bloom filters ('new' and 'old')
551 * in which the filehandles of recalled delegations are "stored".
552 * If a filehandle appear in either filter, a delegation is blocked.
553 * When a delegation is recalled, the filehandle is stored in the "new"
554 * filter.
555 * Every 30 seconds we swap the filters and clear the "new" one,
556 * unless both are empty of course.
557 *
558 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
559 * low 3 bytes as hash-table indices.
560 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400561 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000562 * is used to manage concurrent access. Testing does not need the lock
563 * except when swapping the two filters.
564 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400565static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000566static struct bloom_pair {
567 int entries, old_entries;
568 time_t swap_time;
569 int new; /* index into 'set' */
570 DECLARE_BITMAP(set[2], 256);
571} blocked_delegations;
572
573static int delegation_blocked(struct knfsd_fh *fh)
574{
575 u32 hash;
576 struct bloom_pair *bd = &blocked_delegations;
577
578 if (bd->entries == 0)
579 return 0;
580 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400581 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000582 if (seconds_since_boot() - bd->swap_time > 30) {
583 bd->entries -= bd->old_entries;
584 bd->old_entries = bd->entries;
585 memset(bd->set[bd->new], 0,
586 sizeof(bd->set[0]));
587 bd->new = 1-bd->new;
588 bd->swap_time = seconds_since_boot();
589 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400590 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000591 }
592 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
593 if (test_bit(hash&255, bd->set[0]) &&
594 test_bit((hash>>8)&255, bd->set[0]) &&
595 test_bit((hash>>16)&255, bd->set[0]))
596 return 1;
597
598 if (test_bit(hash&255, bd->set[1]) &&
599 test_bit((hash>>8)&255, bd->set[1]) &&
600 test_bit((hash>>16)&255, bd->set[1]))
601 return 1;
602
603 return 0;
604}
605
606static void block_delegations(struct knfsd_fh *fh)
607{
608 u32 hash;
609 struct bloom_pair *bd = &blocked_delegations;
610
611 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
612
Jeff Laytonf54fe962014-07-25 07:34:26 -0400613 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000614 __set_bit(hash&255, bd->set[bd->new]);
615 __set_bit((hash>>8)&255, bd->set[bd->new]);
616 __set_bit((hash>>16)&255, bd->set[bd->new]);
617 if (bd->entries == 0)
618 bd->swap_time = seconds_since_boot();
619 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400620 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000621}
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623static struct nfs4_delegation *
Jeff Laytonf9416e22014-07-25 07:34:23 -0400624alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400627 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400630 n = atomic_long_inc_return(&num_delegations);
631 if (n < 0 || n > max_delegations)
632 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000633 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400634 goto out_dec;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400635 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700636 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400637 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400638
639 dp->dl_stid.sc_free = nfs4_free_deleg;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400640 /*
641 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400642 * meaning of seqid 0 isn't meaningful, really, but let's avoid
643 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400644 */
645 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700646 INIT_LIST_HEAD(&dp->dl_perfile);
647 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 INIT_LIST_HEAD(&dp->dl_recall_lru);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400649 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200650 dp->dl_retries = 1;
651 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200652 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400654out_dec:
655 atomic_long_dec(&num_delegations);
656 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
659void
Trond Myklebust60116952014-07-29 21:34:06 -0400660nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Trond Myklebust11b91642014-07-29 21:34:08 -0400662 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400663 struct nfs4_client *clp = s->sc_client;
664
Jeff Layton4770d722014-07-29 21:34:10 -0400665 might_lock(&clp->cl_lock);
666
Jeff Laytonb401be222014-07-29 21:34:33 -0400667 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
668 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400669 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400670 }
Trond Myklebust60116952014-07-29 21:34:06 -0400671 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400672 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400673 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400674 if (fp)
675 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500678static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Jeff Layton6bcc0342014-08-09 10:22:40 -0400680 struct file *filp = NULL;
681 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -0400682
Jeff Layton6bcc0342014-08-09 10:22:40 -0400683 spin_lock(&fp->fi_lock);
684 if (fp->fi_lease && atomic_dec_and_test(&fp->fi_delegees)) {
685 swap(filp, fp->fi_deleg_file);
686 fl = fp->fi_lease;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500687 fp->fi_lease = NULL;
Jeff Layton6bcc0342014-08-09 10:22:40 -0400688 }
689 spin_unlock(&fp->fi_lock);
690
691 if (filp) {
692 vfs_setlease(filp, F_UNLCK, &fl);
693 fput(filp);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400697static void unhash_stid(struct nfs4_stid *s)
698{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500699 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400700}
701
Benny Halevy931ee562014-05-30 09:09:27 -0400702static void
703hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
704{
Benny Halevycdc975052014-05-30 09:09:30 -0400705 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400706 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400707
Trond Myklebust67cb1272014-07-29 21:34:17 -0400708 atomic_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400709 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400710 list_add(&dp->dl_perfile, &fp->fi_delegations);
711 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
712}
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714static void
Jeff Layton42690672014-07-25 07:34:20 -0400715unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Trond Myklebust11b91642014-07-29 21:34:08 -0400717 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400718
Jeff Layton42690672014-07-25 07:34:20 -0400719 lockdep_assert_held(&state_lock);
720
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400721 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400722 /* Ensure that deleg break won't try to requeue it */
723 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400724 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400725 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400727 list_del_init(&dp->dl_perfile);
728 spin_unlock(&fp->fi_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400729}
730
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400731static void destroy_delegation(struct nfs4_delegation *dp)
732{
Jeff Layton42690672014-07-25 07:34:20 -0400733 spin_lock(&state_lock);
734 unhash_delegation_locked(dp);
735 spin_unlock(&state_lock);
Jeff Laytonafbda402014-08-09 10:22:41 -0400736 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -0400737 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400738}
739
740static void revoke_delegation(struct nfs4_delegation *dp)
741{
742 struct nfs4_client *clp = dp->dl_stid.sc_client;
743
Jeff Layton2d4a5322014-07-25 07:34:21 -0400744 WARN_ON(!list_empty(&dp->dl_recall_lru));
745
Jeff Laytonafbda402014-08-09 10:22:41 -0400746 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
747
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400748 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400749 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400750 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400751 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400752 spin_lock(&clp->cl_lock);
753 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
754 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400755 }
756}
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758/*
759 * SETCLIENTID state
760 */
761
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400762static unsigned int clientid_hashval(u32 id)
763{
764 return id & CLIENT_HASH_MASK;
765}
766
767static unsigned int clientstr_hashval(const char *name)
768{
769 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
770}
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400773 * We store the NONE, READ, WRITE, and BOTH bits separately in the
774 * st_{access,deny}_bmap field of the stateid, in order to track not
775 * only what share bits are currently in force, but also what
776 * combinations of share bits previous opens have used. This allows us
777 * to enforce the recommendation of rfc 3530 14.2.19 that the server
778 * return an error if the client attempt to downgrade to a combination
779 * of share bits not explicable by closing some of its previous opens.
780 *
781 * XXX: This enforcement is actually incomplete, since we don't keep
782 * track of access/deny bit combinations; so, e.g., we allow:
783 *
784 * OPEN allow read, deny write
785 * OPEN allow both, deny none
786 * DOWNGRADE allow read, deny none
787 *
788 * which we should reject.
789 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400790static unsigned int
791bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400792 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400793 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400794
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400795 for (i = 1; i < 4; i++) {
796 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400797 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400798 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400799 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400800}
801
Jeff Layton82c5ff12012-05-11 09:45:13 -0400802/* set share access for a given stateid */
803static inline void
804set_access(u32 access, struct nfs4_ol_stateid *stp)
805{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400806 unsigned char mask = 1 << access;
807
808 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
809 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400810}
811
812/* clear share access for a given stateid */
813static inline void
814clear_access(u32 access, struct nfs4_ol_stateid *stp)
815{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400816 unsigned char mask = 1 << access;
817
818 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
819 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400820}
821
822/* test whether a given stateid has access */
823static inline bool
824test_access(u32 access, struct nfs4_ol_stateid *stp)
825{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400826 unsigned char mask = 1 << access;
827
828 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400829}
830
Jeff Laytonce0fc432012-05-11 09:45:14 -0400831/* set share deny for a given stateid */
832static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400833set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400834{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400835 unsigned char mask = 1 << deny;
836
837 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
838 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400839}
840
841/* clear share deny for a given stateid */
842static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400843clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400844{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400845 unsigned char mask = 1 << deny;
846
847 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
848 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400849}
850
851/* test whether a given stateid is denying specific access */
852static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400853test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400854{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400855 unsigned char mask = 1 << deny;
856
857 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400858}
859
860static int nfs4_access_to_omode(u32 access)
861{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400862 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400863 case NFS4_SHARE_ACCESS_READ:
864 return O_RDONLY;
865 case NFS4_SHARE_ACCESS_WRITE:
866 return O_WRONLY;
867 case NFS4_SHARE_ACCESS_BOTH:
868 return O_RDWR;
869 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500870 WARN_ON_ONCE(1);
871 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400872}
873
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400874/*
875 * A stateid that had a deny mode associated with it is being released
876 * or downgraded. Recalculate the deny mode on the file.
877 */
878static void
879recalculate_deny_mode(struct nfs4_file *fp)
880{
881 struct nfs4_ol_stateid *stp;
882
883 spin_lock(&fp->fi_lock);
884 fp->fi_share_deny = 0;
885 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
886 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
887 spin_unlock(&fp->fi_lock);
888}
889
890static void
891reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
892{
893 int i;
894 bool change = false;
895
896 for (i = 1; i < 4; i++) {
897 if ((i & deny) != i) {
898 change = true;
899 clear_deny(i, stp);
900 }
901 }
902
903 /* Recalculate per-file deny mode if there was a change */
904 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -0400905 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400906}
907
Jeff Layton82c5ff12012-05-11 09:45:13 -0400908/* release all access and file references for a given stateid */
909static void
910release_all_access(struct nfs4_ol_stateid *stp)
911{
912 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -0400913 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400914
915 if (fp && stp->st_deny_bmap != 0)
916 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400917
918 for (i = 1; i < 4; i++) {
919 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -0400920 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400921 clear_access(i, stp);
922 }
923}
924
Jeff Layton6b180f02014-07-29 21:34:26 -0400925static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
926{
Jeff Laytona819ecc2014-07-29 21:34:38 -0400927 struct nfs4_client *clp = sop->so_client;
928
929 might_lock(&clp->cl_lock);
930
931 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -0400932 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -0400933 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -0400934 spin_unlock(&clp->cl_lock);
Jeff Layton6b180f02014-07-29 21:34:26 -0400935 kfree(sop->so_owner.data);
936 sop->so_ops->so_free(sop);
937}
938
Jeff Layton4ae098d2014-07-29 21:34:43 -0400939static void unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500940{
Trond Myklebust11b91642014-07-29 21:34:08 -0400941 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -0400942
Jeff Layton1c755dc2014-07-29 21:34:12 -0400943 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
944
Trond Myklebust1d31a252014-07-10 14:07:25 -0400945 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500946 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400947 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500948 list_del(&stp->st_perstateowner);
949}
950
Trond Myklebust60116952014-07-29 21:34:06 -0400951static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500952{
Trond Myklebust60116952014-07-29 21:34:06 -0400953 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400954
Trond Myklebust60116952014-07-29 21:34:06 -0400955 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -0400956 if (stp->st_stateowner)
957 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -0400958 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500959}
960
Jeff Laytonb49e0842014-07-29 21:34:11 -0400961static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500962{
Jeff Laytonb49e0842014-07-29 21:34:11 -0400963 struct nfs4_ol_stateid *stp = openlockstateid(stid);
964 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500965 struct file *file;
966
Jeff Laytonb49e0842014-07-29 21:34:11 -0400967 file = find_any_file(stp->st_stid.sc_file);
968 if (file)
969 filp_close(file, (fl_owner_t)lo);
970 nfs4_free_ol_stateid(stid);
971}
972
Jeff Layton2c41beb2014-07-29 21:34:41 -0400973/*
974 * Put the persistent reference to an already unhashed generic stateid, while
975 * holding the cl_lock. If it's the last reference, then put it onto the
976 * reaplist for later destruction.
977 */
978static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
979 struct list_head *reaplist)
980{
981 struct nfs4_stid *s = &stp->st_stid;
982 struct nfs4_client *clp = s->sc_client;
983
984 lockdep_assert_held(&clp->cl_lock);
985
986 WARN_ON_ONCE(!list_empty(&stp->st_locks));
987
988 if (!atomic_dec_and_test(&s->sc_count)) {
989 wake_up_all(&close_wq);
990 return;
991 }
992
993 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
994 list_add(&stp->st_locks, reaplist);
995}
996
Jeff Layton3c1c9952014-07-29 21:34:39 -0400997static void unhash_lock_stateid(struct nfs4_ol_stateid *stp)
998{
999 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1000
1001 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
1002
1003 list_del_init(&stp->st_locks);
Jeff Layton4ae098d2014-07-29 21:34:43 -04001004 unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001005 unhash_stid(&stp->st_stid);
1006}
1007
Jeff Layton5adfd882014-07-29 21:34:31 -04001008static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001009{
Jeff Layton1c755dc2014-07-29 21:34:12 -04001010 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1011
1012 spin_lock(&oo->oo_owner.so_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001013 unhash_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001014 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04001015 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001016}
1017
Trond Myklebustc58c6612014-07-29 21:34:35 -04001018static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001019{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001020 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001021
Trond Myklebustd4f04892014-07-29 21:34:36 -04001022 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001023
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001024 list_del_init(&lo->lo_owner.so_strhash);
1025}
1026
Jeff Layton2c41beb2014-07-29 21:34:41 -04001027/*
1028 * Free a list of generic stateids that were collected earlier after being
1029 * fully unhashed.
1030 */
1031static void
1032free_ol_stateid_reaplist(struct list_head *reaplist)
1033{
1034 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001035 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001036
1037 might_sleep();
1038
1039 while (!list_empty(reaplist)) {
1040 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1041 st_locks);
1042 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001043 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001044 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001045 if (fp)
1046 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001047 }
1048}
1049
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001050static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001051{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001052 struct nfs4_client *clp = lo->lo_owner.so_client;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001053 struct nfs4_ol_stateid *stp;
1054 struct list_head reaplist;
1055
1056 INIT_LIST_HEAD(&reaplist);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001057
Trond Myklebustd4f04892014-07-29 21:34:36 -04001058 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001059 unhash_lockowner_locked(lo);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001060 while (!list_empty(&lo->lo_owner.so_stateids)) {
1061 stp = list_first_entry(&lo->lo_owner.so_stateids,
1062 struct nfs4_ol_stateid, st_perstateowner);
1063 unhash_lock_stateid(stp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001064 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001065 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001066 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001067 free_ol_stateid_reaplist(&reaplist);
Jeff Layton6b180f02014-07-29 21:34:26 -04001068 nfs4_put_stateowner(&lo->lo_owner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001069}
1070
Jeff Laytond83017f2014-07-29 21:34:42 -04001071static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1072 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001073{
1074 struct nfs4_ol_stateid *stp;
1075
1076 while (!list_empty(&open_stp->st_locks)) {
1077 stp = list_entry(open_stp->st_locks.next,
1078 struct nfs4_ol_stateid, st_locks);
Jeff Laytond83017f2014-07-29 21:34:42 -04001079 unhash_lock_stateid(stp);
1080 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001081 }
1082}
1083
Jeff Laytond83017f2014-07-29 21:34:42 -04001084static void unhash_open_stateid(struct nfs4_ol_stateid *stp,
1085 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001086{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001087 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1088
Jeff Layton4ae098d2014-07-29 21:34:43 -04001089 unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001090 release_open_stateid_locks(stp, reaplist);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001091}
1092
1093static void release_open_stateid(struct nfs4_ol_stateid *stp)
1094{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001095 LIST_HEAD(reaplist);
1096
1097 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04001098 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001099 put_ol_stateid_locked(stp, &reaplist);
1100 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1101 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001102}
1103
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001104static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001105{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001106 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001107
Trond Myklebustd4f04892014-07-29 21:34:36 -04001108 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001109
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001110 list_del_init(&oo->oo_owner.so_strhash);
1111 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001112}
1113
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001114static void release_last_closed_stateid(struct nfs4_openowner *oo)
1115{
Jeff Layton217526e2014-07-30 08:27:11 -04001116 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1117 nfsd_net_id);
1118 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001119
Jeff Layton217526e2014-07-30 08:27:11 -04001120 spin_lock(&nn->client_lock);
1121 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001122 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001123 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001124 oo->oo_last_closed_stid = NULL;
1125 }
Jeff Layton217526e2014-07-30 08:27:11 -04001126 spin_unlock(&nn->client_lock);
1127 if (s)
1128 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001129}
1130
Jeff Layton2c41beb2014-07-29 21:34:41 -04001131static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001132{
1133 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001134 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001135 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001136
Jeff Layton2c41beb2014-07-29 21:34:41 -04001137 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001138
Trond Myklebustd4f04892014-07-29 21:34:36 -04001139 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001140 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001141 while (!list_empty(&oo->oo_owner.so_stateids)) {
1142 stp = list_first_entry(&oo->oo_owner.so_stateids,
1143 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytond83017f2014-07-29 21:34:42 -04001144 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001145 put_ol_stateid_locked(stp, &reaplist);
1146 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001147 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001148 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001149 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001150 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001151}
1152
Marc Eshel5282fd72009-04-03 08:27:52 +03001153static inline int
1154hash_sessionid(struct nfs4_sessionid *sessionid)
1155{
1156 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1157
1158 return sid->sequence % SESSION_HASH_SIZE;
1159}
1160
Trond Myklebust8f199b82012-03-20 15:11:17 -04001161#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001162static inline void
1163dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1164{
1165 u32 *ptr = (u32 *)(&sessionid->data[0]);
1166 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1167}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001168#else
1169static inline void
1170dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1171{
1172}
1173#endif
1174
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001175/*
1176 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1177 * won't be used for replay.
1178 */
1179void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1180{
1181 struct nfs4_stateowner *so = cstate->replay_owner;
1182
1183 if (nfserr == nfserr_replay_me)
1184 return;
1185
1186 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001187 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001188 return;
1189 }
1190 if (!so)
1191 return;
1192 if (so->so_is_open_owner)
1193 release_last_closed_stateid(openowner(so));
1194 so->so_seqid++;
1195 return;
1196}
Marc Eshel5282fd72009-04-03 08:27:52 +03001197
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001198static void
1199gen_sessionid(struct nfsd4_session *ses)
1200{
1201 struct nfs4_client *clp = ses->se_client;
1202 struct nfsd4_sessionid *sid;
1203
1204 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1205 sid->clientid = clp->cl_clientid;
1206 sid->sequence = current_sessionid++;
1207 sid->reserved = 0;
1208}
1209
1210/*
Andy Adamsona649637c72009-08-28 08:45:01 -04001211 * The protocol defines ca_maxresponssize_cached to include the size of
1212 * the rpc header, but all we need to cache is the data starting after
1213 * the end of the initial SEQUENCE operation--the rest we regenerate
1214 * each time. Therefore we can advertise a ca_maxresponssize_cached
1215 * value that is the number of bytes in our cache plus a few additional
1216 * bytes. In order to stay on the safe side, and not promise more than
1217 * we can cache, those additional bytes must be the minimum possible: 24
1218 * bytes of rpc header (xid through accept state, with AUTH_NULL
1219 * verifier), 12 for the compound header (with zero-length tag), and 44
1220 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001221 */
Andy Adamsona649637c72009-08-28 08:45:01 -04001222#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1223
Andy Adamson557ce262009-08-28 08:45:04 -04001224static void
1225free_session_slots(struct nfsd4_session *ses)
1226{
1227 int i;
1228
1229 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1230 kfree(ses->se_slots[i]);
1231}
1232
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001233/*
1234 * We don't actually need to cache the rpc and session headers, so we
1235 * can allocate a little less for each slot:
1236 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001237static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001238{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001239 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001240
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001241 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1242 size = 0;
1243 else
1244 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1245 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001246}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001247
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001248/*
1249 * XXX: If we run out of reserved DRC memory we could (up to a point)
1250 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001251 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001252 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001253static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001254{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001255 u32 slotsize = slot_bytes(ca);
1256 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001257 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001258
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001259 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001260 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1261 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001262 num = min_t(int, num, avail / slotsize);
1263 nfsd_drc_mem_used += num * slotsize;
1264 spin_unlock(&nfsd_drc_lock);
1265
1266 return num;
1267}
1268
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001269static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001270{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001271 int slotsize = slot_bytes(ca);
1272
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001273 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001274 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001275 spin_unlock(&nfsd_drc_lock);
1276}
1277
Kinglong Mee60810e52014-01-01 00:35:47 +08001278static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1279 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001280{
Kinglong Mee60810e52014-01-01 00:35:47 +08001281 int numslots = fattrs->maxreqs;
1282 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001283 struct nfsd4_session *new;
1284 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001285
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001286 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001287 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1288 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001289
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001290 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001291 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001292 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001293 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001294 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001295 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001296 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001297 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001298 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001299
1300 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1301 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1302
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001303 return new;
1304out_free:
1305 while (i--)
1306 kfree(new->se_slots[i]);
1307 kfree(new);
1308 return NULL;
1309}
1310
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001311static void free_conn(struct nfsd4_conn *c)
1312{
1313 svc_xprt_put(c->cn_xprt);
1314 kfree(c);
1315}
1316
1317static void nfsd4_conn_lost(struct svc_xpt_user *u)
1318{
1319 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1320 struct nfs4_client *clp = c->cn_session->se_client;
1321
1322 spin_lock(&clp->cl_lock);
1323 if (!list_empty(&c->cn_persession)) {
1324 list_del(&c->cn_persession);
1325 free_conn(c);
1326 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001327 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001328 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001329}
1330
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001331static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001332{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001333 struct nfsd4_conn *conn;
1334
1335 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1336 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001337 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001338 svc_xprt_get(rqstp->rq_xprt);
1339 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001340 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001341 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1342 return conn;
1343}
1344
J. Bruce Fields328ead22010-09-29 16:11:06 -04001345static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1346{
1347 conn->cn_session = ses;
1348 list_add(&conn->cn_persession, &ses->se_conns);
1349}
1350
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001351static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1352{
1353 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001354
1355 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001356 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001357 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001358}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001359
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001360static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001361{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001362 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001363 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001364}
1365
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001366static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001367{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001368 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001369
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001370 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001371 ret = nfsd4_register_conn(conn);
1372 if (ret)
1373 /* oops; xprt is already down: */
1374 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001375 /* We may have gained or lost a callback channel: */
1376 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001377}
1378
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001379static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001380{
1381 u32 dir = NFS4_CDFC4_FORE;
1382
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001383 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001384 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001385 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001386}
1387
1388/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001389static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001390{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001391 struct nfs4_client *clp = s->se_client;
1392 struct nfsd4_conn *c;
1393
1394 spin_lock(&clp->cl_lock);
1395 while (!list_empty(&s->se_conns)) {
1396 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1397 list_del_init(&c->cn_persession);
1398 spin_unlock(&clp->cl_lock);
1399
1400 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1401 free_conn(c);
1402
1403 spin_lock(&clp->cl_lock);
1404 }
1405 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001406}
1407
J. Bruce Fields1377b692012-09-11 21:42:40 -04001408static void __free_session(struct nfsd4_session *ses)
1409{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001410 free_session_slots(ses);
1411 kfree(ses);
1412}
1413
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001414static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001415{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001416 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001417 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001418 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001419}
Andy Adamson557ce262009-08-28 08:45:04 -04001420
Fengguang Wu135ae822012-11-10 07:20:25 -05001421static 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 -04001422{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001423 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001424 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001425
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001426 new->se_client = clp;
1427 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001428
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001429 INIT_LIST_HEAD(&new->se_conns);
1430
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001431 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001432 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001433 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001434 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001435 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001436 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001437 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001438 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001439 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001440 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001441
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001442 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001443 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001444 /*
1445 * This is a little silly; with sessions there's no real
1446 * use for the callback address. Use the peer address
1447 * as a reasonable default for now, but consider fixing
1448 * the rpc client not to require an address in the
1449 * future:
1450 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001451 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1452 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001453 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001454}
1455
Benny Halevy9089f1b2010-05-12 00:12:26 +03001456/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001457static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001458__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001459{
1460 struct nfsd4_session *elem;
1461 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001462 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001463
Trond Myklebust0a880a22014-07-30 08:27:10 -04001464 lockdep_assert_held(&nn->client_lock);
1465
Marc Eshel5282fd72009-04-03 08:27:52 +03001466 dump_sessionid(__func__, sessionid);
1467 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001468 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001469 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001470 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1471 NFS4_MAX_SESSIONID_LEN)) {
1472 return elem;
1473 }
1474 }
1475
1476 dprintk("%s: session not found\n", __func__);
1477 return NULL;
1478}
1479
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001480static struct nfsd4_session *
1481find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1482 __be32 *ret)
1483{
1484 struct nfsd4_session *session;
1485 __be32 status = nfserr_badsession;
1486
1487 session = __find_in_sessionid_hashtbl(sessionid, net);
1488 if (!session)
1489 goto out;
1490 status = nfsd4_get_session_locked(session);
1491 if (status)
1492 session = NULL;
1493out:
1494 *ret = status;
1495 return session;
1496}
1497
Benny Halevy9089f1b2010-05-12 00:12:26 +03001498/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001499static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001500unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001501{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001502 struct nfs4_client *clp = ses->se_client;
1503 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1504
1505 lockdep_assert_held(&nn->client_lock);
1506
Andy Adamson7116ed62009-04-03 08:27:43 +03001507 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001508 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001509 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001510 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001511}
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1514static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001515STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001517 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001519 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001520 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return 1;
1522}
1523
1524/*
1525 * XXX Should we use a slab cache ?
1526 * This type of memory management is somewhat inefficient, but we use it
1527 * anyway since SETCLIENTID is not a common operation.
1528 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001529static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
1531 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001532 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001534 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1535 if (clp == NULL)
1536 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001537 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001538 if (clp->cl_name.data == NULL)
1539 goto err_no_name;
1540 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1541 OWNER_HASH_SIZE, GFP_KERNEL);
1542 if (!clp->cl_ownerstr_hashtbl)
1543 goto err_no_hashtbl;
1544 for (i = 0; i < OWNER_HASH_SIZE; i++)
1545 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001546 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001547 INIT_LIST_HEAD(&clp->cl_sessions);
1548 idr_init(&clp->cl_stateids);
1549 atomic_set(&clp->cl_refcount, 0);
1550 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1551 INIT_LIST_HEAD(&clp->cl_idhash);
1552 INIT_LIST_HEAD(&clp->cl_openowners);
1553 INIT_LIST_HEAD(&clp->cl_delegations);
1554 INIT_LIST_HEAD(&clp->cl_lru);
1555 INIT_LIST_HEAD(&clp->cl_callbacks);
1556 INIT_LIST_HEAD(&clp->cl_revoked);
1557 spin_lock_init(&clp->cl_lock);
1558 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001560err_no_hashtbl:
1561 kfree(clp->cl_name.data);
1562err_no_name:
1563 kfree(clp);
1564 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001567static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568free_client(struct nfs4_client *clp)
1569{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001570 while (!list_empty(&clp->cl_sessions)) {
1571 struct nfsd4_session *ses;
1572 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1573 se_perclnt);
1574 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001575 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1576 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001577 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001578 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001579 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001580 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001582 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 kfree(clp);
1584}
1585
Benny Halevy84d38ac2010-05-12 00:13:16 +03001586/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001587static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001588unhash_client_locked(struct nfs4_client *clp)
1589{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001590 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001591 struct nfsd4_session *ses;
1592
Trond Myklebust0a880a22014-07-30 08:27:10 -04001593 lockdep_assert_held(&nn->client_lock);
1594
Trond Myklebust4beb3452014-07-30 08:27:02 -04001595 /* Mark the client as expired! */
1596 clp->cl_time = 0;
1597 /* Make it invisible */
1598 if (!list_empty(&clp->cl_idhash)) {
1599 list_del_init(&clp->cl_idhash);
1600 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1601 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1602 else
1603 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1604 }
1605 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001606 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001607 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1608 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001609 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001610}
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001613unhash_client(struct nfs4_client *clp)
1614{
1615 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1616
1617 spin_lock(&nn->client_lock);
1618 unhash_client_locked(clp);
1619 spin_unlock(&nn->client_lock);
1620}
1621
Jeff Layton97403d92014-07-30 08:27:12 -04001622static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1623{
1624 if (atomic_read(&clp->cl_refcount))
1625 return nfserr_jukebox;
1626 unhash_client_locked(clp);
1627 return nfs_ok;
1628}
1629
Trond Myklebust4beb3452014-07-30 08:27:02 -04001630static void
1631__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001633 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 struct list_head reaplist;
1636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04001638 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001639 while (!list_empty(&clp->cl_delegations)) {
1640 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001641 unhash_delegation_locked(dp);
1642 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
Benny Halevycdc975052014-05-30 09:09:30 -04001644 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 while (!list_empty(&reaplist)) {
1646 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001647 list_del_init(&dp->dl_recall_lru);
Jeff Laytonafbda402014-08-09 10:22:41 -04001648 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04001649 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001651 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001652 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001653 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001654 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001655 }
NeilBrownea1da632005-06-23 22:04:17 -07001656 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001657 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Jeff Laytond3134b12014-07-29 21:34:32 -04001658 atomic_inc(&oo->oo_owner.so_count);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001659 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001661 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001662 if (clp->cl_cb_conn.cb_xprt)
1663 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001664 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
1666
Trond Myklebust4beb3452014-07-30 08:27:02 -04001667static void
1668destroy_client(struct nfs4_client *clp)
1669{
1670 unhash_client(clp);
1671 __destroy_client(clp);
1672}
1673
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001674static void expire_client(struct nfs4_client *clp)
1675{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001676 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001677 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001678 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001679}
1680
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001681static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1682{
1683 memcpy(target->cl_verifier.data, source->data,
1684 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001687static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1688{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1690 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1691}
1692
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001693static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001694{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001695 if (source->cr_principal) {
1696 target->cr_principal =
1697 kstrdup(source->cr_principal, GFP_KERNEL);
1698 if (target->cr_principal == NULL)
1699 return -ENOMEM;
1700 } else
1701 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001702 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 target->cr_uid = source->cr_uid;
1704 target->cr_gid = source->cr_gid;
1705 target->cr_group_info = source->cr_group_info;
1706 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001707 target->cr_gss_mech = source->cr_gss_mech;
1708 if (source->cr_gss_mech)
1709 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001710 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711}
1712
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001713static long long
1714compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1715{
1716 long long res;
1717
1718 res = o1->len - o2->len;
1719 if (res)
1720 return res;
1721 return (long long)memcmp(o1->data, o2->data, o1->len);
1722}
1723
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001724static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001725{
NeilBrowna55370a2005-06-23 22:03:52 -07001726 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728
1729static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001730same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1731{
1732 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
1734
1735static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001736same_clid(clientid_t *cl1, clientid_t *cl2)
1737{
1738 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739}
1740
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001741static bool groups_equal(struct group_info *g1, struct group_info *g2)
1742{
1743 int i;
1744
1745 if (g1->ngroups != g2->ngroups)
1746 return false;
1747 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001748 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001749 return false;
1750 return true;
1751}
1752
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001753/*
1754 * RFC 3530 language requires clid_inuse be returned when the
1755 * "principal" associated with a requests differs from that previously
1756 * used. We use uid, gid's, and gss principal string as our best
1757 * approximation. We also don't want to allow non-gss use of a client
1758 * established using gss: in theory cr_principal should catch that
1759 * change, but in practice cr_principal can be null even in the gss case
1760 * since gssd doesn't always pass down a principal string.
1761 */
1762static bool is_gss_cred(struct svc_cred *cr)
1763{
1764 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1765 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1766}
1767
1768
Vivek Trivedi5559b502012-07-24 21:18:20 +05301769static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001770same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1771{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001772 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001773 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1774 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001775 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1776 return false;
1777 if (cr1->cr_principal == cr2->cr_principal)
1778 return true;
1779 if (!cr1->cr_principal || !cr2->cr_principal)
1780 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301781 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782}
1783
J. Bruce Fields57266a62013-04-13 14:27:29 -04001784static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1785{
1786 struct svc_cred *cr = &rqstp->rq_cred;
1787 u32 service;
1788
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001789 if (!cr->cr_gss_mech)
1790 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001791 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1792 return service == RPC_GSS_SVC_INTEGRITY ||
1793 service == RPC_GSS_SVC_PRIVACY;
1794}
1795
1796static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1797{
1798 struct svc_cred *cr = &rqstp->rq_cred;
1799
1800 if (!cl->cl_mach_cred)
1801 return true;
1802 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1803 return false;
1804 if (!svc_rqst_integrity_protected(rqstp))
1805 return false;
1806 if (!cr->cr_principal)
1807 return false;
1808 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1809}
1810
Jeff Layton294ac322014-07-30 08:27:15 -04001811static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001812{
Chuck Leverab4684d2012-03-02 17:13:50 -05001813 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Jeff Laytonf4199922014-06-17 07:44:11 -04001815 /*
1816 * This is opaque to client, so no need to byte-swap. Use
1817 * __force to keep sparse happy
1818 */
1819 verf[0] = (__force __be32)get_seconds();
Jeff Layton294ac322014-07-30 08:27:15 -04001820 verf[1] = (__force __be32)nn->clientid_counter;
Chuck Leverab4684d2012-03-02 17:13:50 -05001821 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
1823
Jeff Layton294ac322014-07-30 08:27:15 -04001824static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1825{
1826 clp->cl_clientid.cl_boot = nn->boot_time;
1827 clp->cl_clientid.cl_id = nn->clientid_counter++;
1828 gen_confirm(clp, nn);
1829}
1830
Jeff Layton4770d722014-07-29 21:34:10 -04001831static struct nfs4_stid *
1832find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001833{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001834 struct nfs4_stid *ret;
1835
1836 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1837 if (!ret || !ret->sc_type)
1838 return NULL;
1839 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001840}
1841
Jeff Layton4770d722014-07-29 21:34:10 -04001842static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001843find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001844{
1845 struct nfs4_stid *s;
1846
Jeff Layton4770d722014-07-29 21:34:10 -04001847 spin_lock(&cl->cl_lock);
1848 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001849 if (s != NULL) {
1850 if (typemask & s->sc_type)
1851 atomic_inc(&s->sc_count);
1852 else
1853 s = NULL;
1854 }
Jeff Layton4770d722014-07-29 21:34:10 -04001855 spin_unlock(&cl->cl_lock);
1856 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001857}
1858
Jeff Layton2216d442012-11-12 15:00:57 -05001859static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001860 struct svc_rqst *rqstp, nfs4_verifier *verf)
1861{
1862 struct nfs4_client *clp;
1863 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001864 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001865 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001866
1867 clp = alloc_client(name);
1868 if (clp == NULL)
1869 return NULL;
1870
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001871 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1872 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001873 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001874 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001875 }
Christoph Hellwig0162ac22014-09-24 12:19:19 +02001876 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Benny Halevy07cd4902010-05-12 00:13:41 +03001877 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001878 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001879 copy_verf(clp, verf);
1880 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001881 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001882 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001883 return clp;
1884}
1885
NeilBrownfd39ca92005-06-23 22:04:03 -07001886static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001887add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1888{
1889 struct rb_node **new = &(root->rb_node), *parent = NULL;
1890 struct nfs4_client *clp;
1891
1892 while (*new) {
1893 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1894 parent = *new;
1895
1896 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1897 new = &((*new)->rb_left);
1898 else
1899 new = &((*new)->rb_right);
1900 }
1901
1902 rb_link_node(&new_clp->cl_namenode, parent, new);
1903 rb_insert_color(&new_clp->cl_namenode, root);
1904}
1905
1906static struct nfs4_client *
1907find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1908{
1909 long long cmp;
1910 struct rb_node *node = root->rb_node;
1911 struct nfs4_client *clp;
1912
1913 while (node) {
1914 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1915 cmp = compare_blob(&clp->cl_name, name);
1916 if (cmp > 0)
1917 node = node->rb_left;
1918 else if (cmp < 0)
1919 node = node->rb_right;
1920 else
1921 return clp;
1922 }
1923 return NULL;
1924}
1925
1926static void
1927add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
1929 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001930 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Trond Myklebust0a880a22014-07-30 08:27:10 -04001932 lockdep_assert_held(&nn->client_lock);
1933
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001934 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001935 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001937 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001938 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939}
1940
NeilBrownfd39ca92005-06-23 22:04:03 -07001941static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942move_to_confirmed(struct nfs4_client *clp)
1943{
1944 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001945 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Trond Myklebust0a880a22014-07-30 08:27:10 -04001947 lockdep_assert_held(&nn->client_lock);
1948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001950 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001951 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001952 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001953 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001954 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
1956
1957static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001958find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
1960 struct nfs4_client *clp;
1961 unsigned int idhashval = clientid_hashval(clid->cl_id);
1962
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001963 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001964 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001965 if ((bool)clp->cl_minorversion != sessions)
1966 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001967 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
1971 return NULL;
1972}
1973
1974static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001975find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1976{
1977 struct list_head *tbl = nn->conf_id_hashtbl;
1978
Trond Myklebust0a880a22014-07-30 08:27:10 -04001979 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001980 return find_client_in_id_table(tbl, clid, sessions);
1981}
1982
1983static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001984find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001986 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Trond Myklebust0a880a22014-07-30 08:27:10 -04001988 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001989 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990}
1991
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001992static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001993{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001994 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001995}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001996
NeilBrown28ce6052005-06-23 22:03:56 -07001997static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001998find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001999{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002000 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002001 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002002}
2003
2004static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002005find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002006{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002007 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002008 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002009}
2010
NeilBrownfd39ca92005-06-23 22:04:03 -07002011static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002012gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002014 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002015 struct sockaddr *sa = svc_addr(rqstp);
2016 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002017 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Jeff Layton7077ecb2009-08-14 12:57:58 -04002019 /* Currently, we only support tcp and tcp6 for the callback channel */
2020 if (se->se_callback_netid_len == 3 &&
2021 !memcmp(se->se_callback_netid_val, "tcp", 3))
2022 expected_family = AF_INET;
2023 else if (se->se_callback_netid_len == 4 &&
2024 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2025 expected_family = AF_INET6;
2026 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 goto out_err;
2028
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002029 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002030 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002031 (struct sockaddr *)&conn->cb_addr,
2032 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002033
J. Bruce Fields07263f12010-05-31 19:09:40 -04002034 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002036
J. Bruce Fields07263f12010-05-31 19:09:40 -04002037 if (conn->cb_addr.ss_family == AF_INET6)
2038 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002039
J. Bruce Fields07263f12010-05-31 19:09:40 -04002040 conn->cb_prog = se->se_callback_prog;
2041 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002042 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 return;
2044out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002045 conn->cb_addr.ss_family = AF_UNSPEC;
2046 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002047 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 "will not receive delegations\n",
2049 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return;
2052}
2053
Andy Adamson074fe892009-04-03 08:28:15 +03002054/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002055 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002056 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002057static void
Andy Adamson074fe892009-04-03 08:28:15 +03002058nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2059{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002060 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002061 struct nfsd4_slot *slot = resp->cstate.slot;
2062 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002063
Andy Adamson557ce262009-08-28 08:45:04 -04002064 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002065
Andy Adamson557ce262009-08-28 08:45:04 -04002066 slot->sl_opcnt = resp->opcnt;
2067 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002068
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002069 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002070 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002071 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002072 return;
2073 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002074 base = resp->cstate.data_offset;
2075 slot->sl_datalen = buf->len - base;
2076 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04002077 WARN("%s: sessions DRC could not cache compound\n", __func__);
2078 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002079}
2080
2081/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002082 * Encode the replay sequence operation from the slot values.
2083 * If cachethis is FALSE encode the uncached rep error on the next
2084 * operation which sets resp->p and increments resp->opcnt for
2085 * nfs4svc_encode_compoundres.
2086 *
Andy Adamson074fe892009-04-03 08:28:15 +03002087 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002088static __be32
2089nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2090 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002091{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002092 struct nfsd4_op *op;
2093 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002094
Andy Adamsonabfabf82009-07-23 19:02:18 -04002095 /* Encode the replayed sequence operation */
2096 op = &args->ops[resp->opcnt - 1];
2097 nfsd4_encode_operation(resp, op);
2098
2099 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002100 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002101 op = &args->ops[resp->opcnt++];
2102 op->status = nfserr_retry_uncached_rep;
2103 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002104 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002105 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002106}
2107
2108/*
Andy Adamson557ce262009-08-28 08:45:04 -04002109 * The sequence operation is not cached because we can use the slot and
2110 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002111 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002112static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002113nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2114 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002115{
Andy Adamson557ce262009-08-28 08:45:04 -04002116 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002117 struct xdr_stream *xdr = &resp->xdr;
2118 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002119 __be32 status;
2120
Andy Adamson557ce262009-08-28 08:45:04 -04002121 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002122
Andy Adamsonabfabf82009-07-23 19:02:18 -04002123 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002124 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002125 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002126
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002127 p = xdr_reserve_space(xdr, slot->sl_datalen);
2128 if (!p) {
2129 WARN_ON_ONCE(1);
2130 return nfserr_serverfault;
2131 }
2132 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2133 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002134
Andy Adamson557ce262009-08-28 08:45:04 -04002135 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002136 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002137}
2138
Andy Adamson0733d212009-04-03 08:28:01 +03002139/*
2140 * Set the exchange_id flags returned by the server.
2141 */
2142static void
2143nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2144{
2145 /* pNFS is not supported */
2146 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2147
2148 /* Referrals are supported, Migration is not. */
2149 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2150
2151 /* set the wire flags to return to client. */
2152 clid->flags = new->cl_exchange_flags;
2153}
2154
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002155static bool client_has_state(struct nfs4_client *clp)
2156{
2157 /*
2158 * Note clp->cl_openowners check isn't quite right: there's no
2159 * need to count owners without stateid's.
2160 *
2161 * Also note we should probably be using this in 4.0 case too.
2162 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002163 return !list_empty(&clp->cl_openowners)
2164 || !list_empty(&clp->cl_delegations)
2165 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002166}
2167
Al Virob37ad282006-10-19 23:28:59 -07002168__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002169nfsd4_exchange_id(struct svc_rqst *rqstp,
2170 struct nfsd4_compound_state *cstate,
2171 struct nfsd4_exchange_id *exid)
2172{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002173 struct nfs4_client *conf, *new;
2174 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002175 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002176 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002177 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002178 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002179 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002180 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002181
Jeff Layton363168b2009-08-14 12:57:56 -04002182 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002183 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002184 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002185 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002186 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002187
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002188 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002189 return nfserr_inval;
2190
Andy Adamson0733d212009-04-03 08:28:01 +03002191 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002192 case SP4_MACH_CRED:
2193 if (!svc_rqst_integrity_protected(rqstp))
2194 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002195 case SP4_NONE:
2196 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002197 default: /* checked by xdr code */
2198 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002199 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002200 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002201 }
2202
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002203 new = create_client(exid->clname, rqstp, &verf);
2204 if (new == NULL)
2205 return nfserr_jukebox;
2206
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002207 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002208 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002209 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002210 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002211 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2212 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2213
J. Bruce Fields136e6582012-05-12 20:37:23 -04002214 if (update) {
2215 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002216 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002217 goto out;
2218 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002219 if (!mach_creds_match(conf, rqstp)) {
2220 status = nfserr_wrong_cred;
2221 goto out;
2222 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002223 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002224 status = nfserr_perm;
2225 goto out;
2226 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002227 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002228 status = nfserr_not_same;
2229 goto out;
2230 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002231 /* case 6 */
2232 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002233 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002234 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002235 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002236 if (client_has_state(conf)) {
2237 status = nfserr_clid_inuse;
2238 goto out;
2239 }
Andy Adamson0733d212009-04-03 08:28:01 +03002240 goto out_new;
2241 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002242 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002243 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002244 goto out_copy;
2245 }
2246 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002247 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002248 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002249 }
2250
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002251 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002252 status = nfserr_noent;
2253 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002254 }
2255
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002256 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002257 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002258 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002259
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002260 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002261out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002262 if (conf) {
2263 status = mark_client_expired_locked(conf);
2264 if (status)
2265 goto out;
2266 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002267 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002268 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002269
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002270 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002271 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002272 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002273out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002274 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2275 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002276
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002277 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2278 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002279
2280 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002281 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002282 status = nfs_ok;
2283
2284out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002285 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002286 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002287 expire_client(new);
2288 if (unconf)
2289 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002290 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002291}
2292
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002293static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002294check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002295{
Andy Adamson88e588d2009-07-23 19:02:15 -04002296 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2297 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002298
2299 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002300 if (slot_inuse) {
2301 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002302 return nfserr_jukebox;
2303 else
2304 return nfserr_seq_misordered;
2305 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002306 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002307 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002308 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002309 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002310 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002311 return nfserr_seq_misordered;
2312}
2313
Andy Adamson49557cc2009-07-23 19:02:16 -04002314/*
2315 * Cache the create session result into the create session single DRC
2316 * slot cache by saving the xdr structure. sl_seqid has been set.
2317 * Do this for solo or embedded create session operations.
2318 */
2319static void
2320nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002321 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002322{
2323 slot->sl_status = nfserr;
2324 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2325}
2326
2327static __be32
2328nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2329 struct nfsd4_clid_slot *slot)
2330{
2331 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2332 return slot->sl_status;
2333}
2334
Mi Jinlong1b74c252011-07-14 14:50:17 +08002335#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2336 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2337 1 + /* MIN tag is length with zero, only length */ \
2338 3 + /* version, opcount, opcode */ \
2339 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2340 /* seqid, slotID, slotID, cache */ \
2341 4 ) * sizeof(__be32))
2342
2343#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2344 2 + /* verifier: AUTH_NULL, length 0 */\
2345 1 + /* status */ \
2346 1 + /* MIN tag is length with zero, only length */ \
2347 3 + /* opcount, opcode, opstatus*/ \
2348 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2349 /* seqid, slotID, slotID, slotID, status */ \
2350 5 ) * sizeof(__be32))
2351
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002352static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002353{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002354 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2355
J. Bruce Fields373cd402013-04-08 15:42:12 -04002356 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2357 return nfserr_toosmall;
2358 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2359 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002360 ca->headerpadsz = 0;
2361 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2362 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2363 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2364 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2365 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2366 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2367 /*
2368 * Note decreasing slot size below client's request may make it
2369 * difficult for client to function correctly, whereas
2370 * decreasing the number of slots will (just?) affect
2371 * performance. When short on memory we therefore prefer to
2372 * decrease number of slots instead of their size. Clients that
2373 * request larger slots than they need will get poor results:
2374 */
2375 ca->maxreqs = nfsd4_get_drc_mem(ca);
2376 if (!ca->maxreqs)
2377 return nfserr_jukebox;
2378
J. Bruce Fields373cd402013-04-08 15:42:12 -04002379 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002380}
2381
Kinglong Mee8a891632013-12-23 18:11:02 +08002382#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2383 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2384#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2385 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2386
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002387static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2388{
2389 ca->headerpadsz = 0;
2390
2391 /*
2392 * These RPC_MAX_HEADER macros are overkill, especially since we
2393 * don't even do gss on the backchannel yet. But this is still
2394 * less than 1k. Tighten up this estimate in the unlikely event
2395 * it turns out to be a problem for some client:
2396 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002397 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002398 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002399 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002400 return nfserr_toosmall;
2401 ca->maxresp_cached = 0;
2402 if (ca->maxops < 2)
2403 return nfserr_toosmall;
2404
2405 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002406}
2407
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002408static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2409{
2410 switch (cbs->flavor) {
2411 case RPC_AUTH_NULL:
2412 case RPC_AUTH_UNIX:
2413 return nfs_ok;
2414 default:
2415 /*
2416 * GSS case: the spec doesn't allow us to return this
2417 * error. But it also doesn't allow us not to support
2418 * GSS.
2419 * I'd rather this fail hard than return some error the
2420 * client might think it can already handle:
2421 */
2422 return nfserr_encr_alg_unsupp;
2423 }
2424}
2425
Andy Adamson069b6ad2009-04-03 08:27:58 +03002426__be32
2427nfsd4_create_session(struct svc_rqst *rqstp,
2428 struct nfsd4_compound_state *cstate,
2429 struct nfsd4_create_session *cr_ses)
2430{
Jeff Layton363168b2009-08-14 12:57:56 -04002431 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002432 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002433 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04002434 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002435 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002436 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002437 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002438 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002439
Mi Jinlonga62573d2011-03-23 17:57:07 +08002440 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2441 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002442 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2443 if (status)
2444 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002445 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002446 if (status)
2447 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002448 status = check_backchannel_attrs(&cr_ses->back_channel);
2449 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002450 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002451 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002452 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002453 if (!new)
2454 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002455 conn = alloc_conn_from_crses(rqstp, cr_ses);
2456 if (!conn)
2457 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002458
Jeff Laytond20c11d2014-07-30 08:27:07 -04002459 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002460 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002461 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002462 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002463
2464 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002465 status = nfserr_wrong_cred;
2466 if (!mach_creds_match(conf, rqstp))
2467 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002468 cs_slot = &conf->cl_cs_slot;
2469 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002470 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002471 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002472 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002473 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002474 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002475 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002476 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002477 } else if (unconf) {
2478 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002479 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002480 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002481 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002482 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002483 status = nfserr_wrong_cred;
2484 if (!mach_creds_match(unconf, rqstp))
2485 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002486 cs_slot = &unconf->cl_cs_slot;
2487 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002488 if (status) {
2489 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002490 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002491 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002492 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002493 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002494 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002495 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002496 if (status) {
2497 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002498 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002499 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002500 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002501 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002502 conf = unconf;
2503 } else {
2504 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002505 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002506 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002507 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002508 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002509 * We do not support RDMA or persistent sessions
2510 */
2511 cr_ses->flags &= ~SESSION4_PERSIST;
2512 cr_ses->flags &= ~SESSION4_RDMA;
2513
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002514 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002515 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002516
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04002517 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002518 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002519 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002520 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002521
Jeff Laytond20c11d2014-07-30 08:27:07 -04002522 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002523 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002524 spin_unlock(&nn->client_lock);
2525 /* init connection and backchannel */
2526 nfsd4_init_conn(rqstp, conn, new);
2527 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002528 if (old)
2529 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002530 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002531out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002532 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002533 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002534 if (old)
2535 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002536out_free_session:
2537 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002538out_release_drc_mem:
2539 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002540 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002541}
2542
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002543static __be32 nfsd4_map_bcts_dir(u32 *dir)
2544{
2545 switch (*dir) {
2546 case NFS4_CDFC4_FORE:
2547 case NFS4_CDFC4_BACK:
2548 return nfs_ok;
2549 case NFS4_CDFC4_FORE_OR_BOTH:
2550 case NFS4_CDFC4_BACK_OR_BOTH:
2551 *dir = NFS4_CDFC4_BOTH;
2552 return nfs_ok;
2553 };
2554 return nfserr_inval;
2555}
2556
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002557__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2558{
2559 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002560 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002561 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002562
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002563 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2564 if (status)
2565 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002566 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002567 session->se_cb_prog = bc->bc_cb_program;
2568 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002569 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002570
2571 nfsd4_probe_callback(session->se_client);
2572
2573 return nfs_ok;
2574}
2575
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002576__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2577 struct nfsd4_compound_state *cstate,
2578 struct nfsd4_bind_conn_to_session *bcts)
2579{
2580 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002581 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002582 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002583 struct net *net = SVC_NET(rqstp);
2584 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002585
2586 if (!nfsd4_last_compound_op(rqstp))
2587 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002588 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002589 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002590 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002591 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002592 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002593 status = nfserr_wrong_cred;
2594 if (!mach_creds_match(session->se_client, rqstp))
2595 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002596 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002597 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002598 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002599 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002600 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002601 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002602 goto out;
2603 nfsd4_init_conn(rqstp, conn, session);
2604 status = nfs_ok;
2605out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002606 nfsd4_put_session(session);
2607out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002608 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002609}
2610
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002611static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2612{
2613 if (!session)
2614 return 0;
2615 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2616}
2617
Andy Adamson069b6ad2009-04-03 08:27:58 +03002618__be32
2619nfsd4_destroy_session(struct svc_rqst *r,
2620 struct nfsd4_compound_state *cstate,
2621 struct nfsd4_destroy_session *sessionid)
2622{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002623 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002624 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002625 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002626 struct net *net = SVC_NET(r);
2627 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002628
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002629 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002630 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002631 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002632 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002633 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002634 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002635 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002636 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002637 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002638 if (!ses)
2639 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002640 status = nfserr_wrong_cred;
2641 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002642 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002643 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002644 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002645 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002646 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002647 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002648
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002649 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002650
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002651 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002652 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002653out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002654 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002655out_client_lock:
2656 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002657out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03002658 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002659}
2660
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002661static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002662{
2663 struct nfsd4_conn *c;
2664
2665 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002666 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002667 return c;
2668 }
2669 }
2670 return NULL;
2671}
2672
J. Bruce Fields57266a62013-04-13 14:27:29 -04002673static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002674{
2675 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002676 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002677 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002678 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002679
2680 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002681 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002682 if (c)
2683 goto out_free;
2684 status = nfserr_conn_not_bound_to_session;
2685 if (clp->cl_mach_cred)
2686 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002687 __nfsd4_hash_conn(new, ses);
2688 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002689 ret = nfsd4_register_conn(new);
2690 if (ret)
2691 /* oops; xprt is already down: */
2692 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002693 return nfs_ok;
2694out_free:
2695 spin_unlock(&clp->cl_lock);
2696 free_conn(new);
2697 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002698}
2699
Mi Jinlong868b89c2011-04-27 09:09:58 +08002700static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2701{
2702 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2703
2704 return args->opcnt > session->se_fchannel.maxops;
2705}
2706
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002707static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2708 struct nfsd4_session *session)
2709{
2710 struct xdr_buf *xb = &rqstp->rq_arg;
2711
2712 return xb->len > session->se_fchannel.maxreq_sz;
2713}
2714
Andy Adamson069b6ad2009-04-03 08:27:58 +03002715__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002716nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002717 struct nfsd4_compound_state *cstate,
2718 struct nfsd4_sequence *seq)
2719{
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002720 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002721 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002722 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002723 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002724 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002725 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002726 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002727 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002728 struct net *net = SVC_NET(rqstp);
2729 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002730
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002731 if (resp->opcnt != 1)
2732 return nfserr_sequence_pos;
2733
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002734 /*
2735 * Will be either used or freed by nfsd4_sequence_check_conn
2736 * below.
2737 */
2738 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2739 if (!conn)
2740 return nfserr_jukebox;
2741
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002742 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002743 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002744 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002745 goto out_no_session;
2746 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002747
Mi Jinlong868b89c2011-04-27 09:09:58 +08002748 status = nfserr_too_many_ops;
2749 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002750 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002751
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002752 status = nfserr_req_too_big;
2753 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002754 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002755
Benny Halevyb85d4c02009-04-03 08:28:08 +03002756 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002757 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002758 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002759
Andy Adamson557ce262009-08-28 08:45:04 -04002760 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002761 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2762
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002763 /* We do not negotiate the number of slots yet, so set the
2764 * maxslots to the session maxreqs which is used to encode
2765 * sr_highest_slotid and the sr_target_slot id to maxslots */
2766 seq->maxslots = session->se_fchannel.maxreqs;
2767
J. Bruce Fields73e79482012-02-13 16:39:00 -05002768 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2769 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002770 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002771 status = nfserr_seq_misordered;
2772 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002773 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002774 cstate->slot = slot;
2775 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002776 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002777 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002778 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002779 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002780 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002781 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002782 }
2783 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002784 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002785
J. Bruce Fields57266a62013-04-13 14:27:29 -04002786 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002787 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002788 if (status)
2789 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002790
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002791 buflen = (seq->cachethis) ?
2792 session->se_fchannel.maxresp_cached :
2793 session->se_fchannel.maxresp_sz;
2794 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2795 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002796 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002797 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002798 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002799
2800 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002801 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002802 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002803 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002804 if (seq->cachethis)
2805 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002806 else
2807 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002808
2809 cstate->slot = slot;
2810 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002811 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002812
Benny Halevyb85d4c02009-04-03 08:28:08 +03002813out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002814 switch (clp->cl_cb_state) {
2815 case NFSD4_CB_DOWN:
2816 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2817 break;
2818 case NFSD4_CB_FAULT:
2819 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2820 break;
2821 default:
2822 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002823 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002824 if (!list_empty(&clp->cl_revoked))
2825 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002826out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002827 if (conn)
2828 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002829 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002830 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002831out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002832 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002833 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002834}
2835
Trond Myklebustb6076642014-06-30 11:48:35 -04002836void
2837nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2838{
2839 struct nfsd4_compound_state *cs = &resp->cstate;
2840
2841 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002842 if (cs->status != nfserr_replay_cache) {
2843 nfsd4_store_cache_entry(resp);
2844 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2845 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002846 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002847 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002848 } else if (cs->clp)
2849 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002850}
2851
Mi Jinlong345c2842011-10-20 17:51:39 +08002852__be32
2853nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2854{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002855 struct nfs4_client *conf, *unconf;
2856 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002857 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002858 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002859
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002860 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002861 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002862 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002863 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002864
2865 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002866 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002867 status = nfserr_clientid_busy;
2868 goto out;
2869 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04002870 status = mark_client_expired_locked(conf);
2871 if (status)
2872 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002873 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08002874 } else if (unconf)
2875 clp = unconf;
2876 else {
2877 status = nfserr_stale_clientid;
2878 goto out;
2879 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002880 if (!mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002881 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002882 status = nfserr_wrong_cred;
2883 goto out;
2884 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002885 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002886out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002887 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002888 if (clp)
2889 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002890 return status;
2891}
2892
Andy Adamson069b6ad2009-04-03 08:27:58 +03002893__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002894nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2895{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002896 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002897
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002898 if (rc->rca_one_fs) {
2899 if (!cstate->current_fh.fh_dentry)
2900 return nfserr_nofilehandle;
2901 /*
2902 * We don't take advantage of the rca_one_fs case.
2903 * That's OK, it's optional, we can safely ignore it.
2904 */
2905 return nfs_ok;
2906 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002907
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002908 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002909 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2910 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002911 goto out;
2912
2913 status = nfserr_stale_clientid;
2914 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002915 /*
2916 * The following error isn't really legal.
2917 * But we only get here if the client just explicitly
2918 * destroyed the client. Surely it no longer cares what
2919 * error it gets back on an operation for the dead
2920 * client.
2921 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002922 goto out;
2923
2924 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002925 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002926out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002927 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002928}
2929
2930__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002931nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2932 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002934 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002936 struct nfs4_client *conf, *new;
2937 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002938 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002939 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2940
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002941 new = create_client(clname, rqstp, &clverifier);
2942 if (new == NULL)
2943 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002944 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002945 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002946 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002947 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002948 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002950 if (clp_used_exchangeid(conf))
2951 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002952 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002953 char addr_str[INET6_ADDRSTRLEN];
2954 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2955 sizeof(addr_str));
2956 dprintk("NFSD: setclientid: string in use by client "
2957 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 goto out;
2959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002961 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002962 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002963 unhash_client_locked(unconf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002964 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002965 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002967 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002968 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002969 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002970 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002971 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2973 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2974 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002975 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 status = nfs_ok;
2977out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002978 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002979 if (new)
2980 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002981 if (unconf)
2982 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 return status;
2984}
2985
2986
Al Virob37ad282006-10-19 23:28:59 -07002987__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002988nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2989 struct nfsd4_compound_state *cstate,
2990 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991{
NeilBrown21ab45a2005-06-23 22:04:14 -07002992 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002993 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2995 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002996 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002997 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002999 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003001
Jeff Laytond20c11d2014-07-30 08:27:07 -04003002 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003003 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003004 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003005 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003006 * We try hard to give out unique clientid's, so if we get an
3007 * attempt to confirm the same clientid with a different cred,
3008 * there's a bug somewhere. Let's charitably assume it's our
3009 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003010 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04003011 status = nfserr_serverfault;
3012 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3013 goto out;
3014 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3015 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003016 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003017 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3018 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003020 else /* case 4: client hasn't noticed we rebooted yet? */
3021 status = nfserr_stale_clientid;
3022 goto out;
3023 }
3024 status = nfs_ok;
3025 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003026 old = unconf;
3027 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003028 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003029 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003030 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3031 if (old) {
3032 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003033 if (status) {
3034 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003035 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003036 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003037 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003038 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003039 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003040 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003041 get_client_locked(conf);
3042 spin_unlock(&nn->client_lock);
3043 nfsd4_probe_callback(conf);
3044 spin_lock(&nn->client_lock);
3045 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003047 spin_unlock(&nn->client_lock);
3048 if (old)
3049 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 return status;
3051}
3052
J. Bruce Fields32513b42011-10-13 16:00:16 -04003053static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003055 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3056}
3057
3058/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003059static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003060{
Trond Myklebustca943212014-07-23 16:17:39 -04003061 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Trond Myklebust950e0112014-06-30 11:48:31 -04003063 lockdep_assert_held(&state_lock);
3064
J. Bruce Fields32513b42011-10-13 16:00:16 -04003065 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003066 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003067 INIT_LIST_HEAD(&fp->fi_stateids);
3068 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003069 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003070 fp->fi_had_conflict = false;
3071 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003072 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003073 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3074 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04003075 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076}
3077
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003078void
NeilBrowne60d4392005-06-23 22:03:01 -07003079nfsd4_free_slabs(void)
3080{
Christoph Hellwigabf11352014-05-21 07:43:03 -07003081 kmem_cache_destroy(openowner_slab);
3082 kmem_cache_destroy(lockowner_slab);
3083 kmem_cache_destroy(file_slab);
3084 kmem_cache_destroy(stateid_slab);
3085 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003086}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
Bryan Schumaker72083392011-11-01 15:24:59 -04003088int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089nfsd4_init_slabs(void)
3090{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003091 openowner_slab = kmem_cache_create("nfsd4_openowners",
3092 sizeof(struct nfs4_openowner), 0, 0, NULL);
3093 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003094 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003095 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003096 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003097 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003098 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003099 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003100 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003101 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003102 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003103 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003104 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003105 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003106 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003107 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003108 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003109 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003110 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003112
3113out_free_stateid_slab:
3114 kmem_cache_destroy(stateid_slab);
3115out_free_file_slab:
3116 kmem_cache_destroy(file_slab);
3117out_free_lockowner_slab:
3118 kmem_cache_destroy(lockowner_slab);
3119out_free_openowner_slab:
3120 kmem_cache_destroy(openowner_slab);
3121out:
NeilBrowne60d4392005-06-23 22:03:01 -07003122 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3123 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124}
3125
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003126static void init_nfs4_replay(struct nfs4_replay *rp)
3127{
3128 rp->rp_status = nfserr_serverfault;
3129 rp->rp_buflen = 0;
3130 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003131 mutex_init(&rp->rp_mutex);
3132}
3133
3134static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3135 struct nfs4_stateowner *so)
3136{
3137 if (!nfsd4_has_session(cstate)) {
3138 mutex_lock(&so->so_replay.rp_mutex);
3139 cstate->replay_owner = so;
3140 atomic_inc(&so->so_count);
3141 }
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);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003239 stp->st_stateowner = &oo->oo_owner;
Jeff Laytond3134b12014-07-29 21:34:32 -04003240 atomic_inc(&stp->st_stateowner->so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07003241 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003242 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 stp->st_access_bmap = 0;
3244 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003245 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003246 spin_lock(&oo->oo_owner.so_client->cl_lock);
3247 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003248 spin_lock(&fp->fi_lock);
3249 list_add(&stp->st_perfile, &fp->fi_stateids);
3250 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003251 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252}
3253
Jeff Laytond3134b12014-07-29 21:34:32 -04003254/*
3255 * In the 4.0 case we need to keep the owners around a little while to handle
3256 * CLOSE replay. We still do need to release any file access that is held by
3257 * them before returning however.
3258 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003260move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261{
Jeff Layton217526e2014-07-30 08:27:11 -04003262 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003263 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3264 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3265 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003266
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003267 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
Jeff Laytonb401be222014-07-29 21:34:33 -04003269 /*
3270 * We know that we hold one reference via nfsd4_close, and another
3271 * "persistent" reference for the client. If the refcount is higher
3272 * than 2, then there are still calls in progress that are using this
3273 * stateid. We can't put the sc_file reference until they are finished.
3274 * Wait for the refcount to drop to 2. Since it has been unhashed,
3275 * there should be no danger of the refcount going back up again at
3276 * this point.
3277 */
3278 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3279
Jeff Laytond3134b12014-07-29 21:34:32 -04003280 release_all_access(s);
3281 if (s->st_stid.sc_file) {
3282 put_nfs4_file(s->st_stid.sc_file);
3283 s->st_stid.sc_file = NULL;
3284 }
Jeff Layton217526e2014-07-30 08:27:11 -04003285
3286 spin_lock(&nn->client_lock);
3287 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003288 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003289 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003290 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003291 spin_unlock(&nn->client_lock);
3292 if (last)
3293 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294}
3295
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296/* search file_hashtbl[] for file */
3297static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003298find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299{
Trond Myklebustca943212014-07-23 16:17:39 -04003300 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 struct nfs4_file *fp;
3302
Trond Myklebust950e0112014-06-30 11:48:31 -04003303 lockdep_assert_held(&state_lock);
3304
Jeff Layton89876f82013-04-02 09:01:59 -04003305 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003306 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003307 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 }
3311 return NULL;
3312}
3313
Trond Myklebust950e0112014-06-30 11:48:31 -04003314static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003315find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003316{
3317 struct nfs4_file *fp;
3318
3319 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003320 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003321 spin_unlock(&state_lock);
3322 return fp;
3323}
3324
3325static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003326find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003327{
3328 struct nfs4_file *fp;
3329
3330 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003331 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003332 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003333 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003334 fp = new;
3335 }
3336 spin_unlock(&state_lock);
3337
3338 return fp;
3339}
3340
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003341/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 * Called to check deny when READ with all zero stateid or
3343 * WRITE with all zero or all one stateid
3344 */
Al Virob37ad282006-10-19 23:28:59 -07003345static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3347{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003349 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350
Trond Myklebustca943212014-07-23 16:17:39 -04003351 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003352 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003353 return ret;
3354 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003355 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003356 if (fp->fi_share_deny & deny_type)
3357 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003358 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003359 put_nfs4_file(fp);
3360 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361}
3362
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003363#define cb_to_delegation(cb) \
3364 container_of(cb, struct nfs4_delegation, dl_recall)
3365
3366static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003368 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04003369 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3370 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003371
Trond Myklebust11b91642014-07-29 21:34:08 -04003372 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003373
Jeff Layton02e12152014-07-16 10:31:57 -04003374 /*
3375 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003376 * already holding inode->i_lock.
3377 *
Jeff Laytondff13992014-07-08 14:02:49 -04003378 * If the dl_time != 0, then we know that it has already been
3379 * queued for a lease break. Don't queue it again.
3380 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003381 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003382 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003383 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003384 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003385 }
Jeff Layton02e12152014-07-16 10:31:57 -04003386 spin_unlock(&state_lock);
3387}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003389static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3390 struct rpc_task *task)
3391{
3392 struct nfs4_delegation *dp = cb_to_delegation(cb);
3393
3394 switch (task->tk_status) {
3395 case 0:
3396 return 1;
3397 case -EBADHANDLE:
3398 case -NFS4ERR_BAD_STATEID:
3399 /*
3400 * Race: client probably got cb_recall before open reply
3401 * granting delegation.
3402 */
3403 if (dp->dl_retries--) {
3404 rpc_delay(task, 2 * HZ);
3405 return 0;
3406 }
3407 /*FALLTHRU*/
3408 default:
3409 return -1;
3410 }
3411}
3412
3413static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3414{
3415 struct nfs4_delegation *dp = cb_to_delegation(cb);
3416
3417 nfs4_put_stid(&dp->dl_stid);
3418}
3419
3420static struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3421 .prepare = nfsd4_cb_recall_prepare,
3422 .done = nfsd4_cb_recall_done,
3423 .release = nfsd4_cb_recall_release,
3424};
3425
Jeff Layton02e12152014-07-16 10:31:57 -04003426static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3427{
3428 /*
3429 * We're assuming the state code never drops its reference
3430 * without first removing the lease. Since we're in this lease
3431 * callback (and since the lease code is serialized by the kernel
3432 * lock) we know the server hasn't removed the lease yet, we know
3433 * it's safe to take a reference.
3434 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003435 atomic_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02003436 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003437}
3438
Jeff Layton1c8c6012013-06-21 08:58:15 -04003439/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003440static void nfsd_break_deleg_cb(struct file_lock *fl)
3441{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003442 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3443 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003444
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003445 if (!fp) {
3446 WARN(1, "(%p)->fl_owner NULL\n", fl);
3447 return;
3448 }
3449 if (fp->fi_had_conflict) {
3450 WARN(1, "duplicate break on %p\n", fp);
3451 return;
3452 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003453 /*
3454 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003455 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003456 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003457 */
3458 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
Jeff Layton02e12152014-07-16 10:31:57 -04003460 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003461 fp->fi_had_conflict = true;
3462 /*
3463 * If there are no delegations on the list, then we can't count on this
3464 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3465 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3466 * true should keep any new delegations from being hashed.
3467 */
3468 if (list_empty(&fp->fi_delegations))
3469 fl->fl_break_time = jiffies;
3470 else
3471 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3472 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003473 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474}
3475
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476static
3477int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3478{
3479 if (arg & F_UNLCK)
3480 return lease_modify(onlist, arg);
3481 else
3482 return -EAGAIN;
3483}
3484
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003485static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003486 .lm_break = nfsd_break_deleg_cb,
3487 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488};
3489
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003490static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3491{
3492 if (nfsd4_has_session(cstate))
3493 return nfs_ok;
3494 if (seqid == so->so_seqid - 1)
3495 return nfserr_replay_me;
3496 if (seqid == so->so_seqid)
3497 return nfs_ok;
3498 return nfserr_bad_seqid;
3499}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
Jeff Layton4b24ca72014-06-30 11:48:44 -04003501static __be32 lookup_clientid(clientid_t *clid,
3502 struct nfsd4_compound_state *cstate,
3503 struct nfsd_net *nn)
3504{
3505 struct nfs4_client *found;
3506
3507 if (cstate->clp) {
3508 found = cstate->clp;
3509 if (!same_clid(&found->cl_clientid, clid))
3510 return nfserr_stale_clientid;
3511 return nfs_ok;
3512 }
3513
3514 if (STALE_CLIENTID(clid, nn))
3515 return nfserr_stale_clientid;
3516
3517 /*
3518 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3519 * cached already then we know this is for is for v4.0 and "sessions"
3520 * will be false.
3521 */
3522 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003523 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003524 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003525 if (!found) {
3526 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003527 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003528 }
3529 atomic_inc(&found->cl_refcount);
3530 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003531
3532 /* Cache the nfs4_client in cstate! */
3533 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003534 return nfs_ok;
3535}
3536
Al Virob37ad282006-10-19 23:28:59 -07003537__be32
Andy Adamson66689582009-04-03 08:28:45 +03003538nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003539 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 clientid_t *clientid = &open->op_clientid;
3542 struct nfs4_client *clp = NULL;
3543 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003544 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003545 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003547 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003549 /*
3550 * In case we need it later, after we've already created the
3551 * file and don't want to risk a further failure:
3552 */
3553 open->op_file = nfsd4_alloc_file();
3554 if (open->op_file == NULL)
3555 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
Trond Myklebust2d91e892014-06-30 11:48:46 -04003557 status = lookup_clientid(clientid, cstate, nn);
3558 if (status)
3559 return status;
3560 clp = cstate->clp;
3561
Trond Myklebustd4f04892014-07-29 21:34:36 -04003562 strhashval = ownerstr_hashval(&open->op_owner);
3563 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003564 open->op_openowner = oo;
3565 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003566 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003568 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003569 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003570 release_openowner(oo);
3571 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003572 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003573 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003574 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3575 if (status)
3576 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003577 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003578new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003579 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003580 if (oo == NULL)
3581 return nfserr_jukebox;
3582 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003583alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003584 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003585 if (!open->op_stp)
3586 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003587 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588}
3589
Al Virob37ad282006-10-19 23:28:59 -07003590static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003591nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3592{
3593 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3594 return nfserr_openmode;
3595 else
3596 return nfs_ok;
3597}
3598
Daniel Mackc47d8322011-05-16 16:38:14 +02003599static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003600{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003601 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3602}
3603
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003604static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003605{
3606 struct nfs4_stid *ret;
3607
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003608 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003609 if (!ret)
3610 return NULL;
3611 return delegstateid(ret);
3612}
3613
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003614static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3615{
3616 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3617 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3618}
3619
Al Virob37ad282006-10-19 23:28:59 -07003620static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003621nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003622 struct nfs4_delegation **dp)
3623{
3624 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003625 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003626 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003627
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003628 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3629 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003630 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003631 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003632 status = nfs4_check_delegmode(deleg, flags);
3633 if (status) {
3634 nfs4_put_stid(&deleg->dl_stid);
3635 goto out;
3636 }
3637 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003638out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003639 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003640 return nfs_ok;
3641 if (status)
3642 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003643 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003644 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003645}
3646
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003647static struct nfs4_ol_stateid *
3648nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003650 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003651 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652
Trond Myklebust1d31a252014-07-10 14:07:25 -04003653 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003654 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 /* ignore lock owners */
3656 if (local->st_stateowner->so_is_open_owner == 0)
3657 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003658 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003659 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003660 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003661 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003664 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003665 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666}
3667
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003668static inline int nfs4_access_to_access(u32 nfs4_access)
3669{
3670 int flags = 0;
3671
3672 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3673 flags |= NFSD_MAY_READ;
3674 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3675 flags |= NFSD_MAY_WRITE;
3676 return flags;
3677}
3678
Al Virob37ad282006-10-19 23:28:59 -07003679static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3681 struct nfsd4_open *open)
3682{
3683 struct iattr iattr = {
3684 .ia_valid = ATTR_SIZE,
3685 .ia_size = 0,
3686 };
3687 if (!open->op_truncate)
3688 return 0;
3689 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003690 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3692}
3693
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003694static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003695 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3696 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003697{
Trond Myklebustde186432014-07-10 14:07:26 -04003698 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003699 __be32 status;
3700 int oflag = nfs4_access_to_omode(open->op_share_access);
3701 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003702 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003703
Trond Myklebustde186432014-07-10 14:07:26 -04003704 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003705
3706 /*
3707 * Are we trying to set a deny mode that would conflict with
3708 * current access?
3709 */
3710 status = nfs4_file_check_deny(fp, open->op_share_deny);
3711 if (status != nfs_ok) {
3712 spin_unlock(&fp->fi_lock);
3713 goto out;
3714 }
3715
3716 /* set access to the file */
3717 status = nfs4_file_get_access(fp, open->op_share_access);
3718 if (status != nfs_ok) {
3719 spin_unlock(&fp->fi_lock);
3720 goto out;
3721 }
3722
3723 /* Set access bits in stateid */
3724 old_access_bmap = stp->st_access_bmap;
3725 set_access(open->op_share_access, stp);
3726
3727 /* Set new deny mask */
3728 old_deny_bmap = stp->st_deny_bmap;
3729 set_deny(open->op_share_deny, stp);
3730 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3731
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003732 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003733 spin_unlock(&fp->fi_lock);
3734 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003735 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003736 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003737 spin_lock(&fp->fi_lock);
3738 if (!fp->fi_fds[oflag]) {
3739 fp->fi_fds[oflag] = filp;
3740 filp = NULL;
3741 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003742 }
Trond Myklebustde186432014-07-10 14:07:26 -04003743 spin_unlock(&fp->fi_lock);
3744 if (filp)
3745 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003746
3747 status = nfsd4_truncate(rqstp, cur_fh, open);
3748 if (status)
3749 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003750out:
3751 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003752out_put_access:
3753 stp->st_access_bmap = old_access_bmap;
3754 nfs4_file_put_access(fp, open->op_share_access);
3755 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3756 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003757}
3758
Al Virob37ad282006-10-19 23:28:59 -07003759static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003760nfs4_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 -07003761{
Al Virob37ad282006-10-19 23:28:59 -07003762 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003763 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003765 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003766 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003767
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003768 /* test and set deny mode */
3769 spin_lock(&fp->fi_lock);
3770 status = nfs4_file_check_deny(fp, open->op_share_deny);
3771 if (status == nfs_ok) {
3772 old_deny_bmap = stp->st_deny_bmap;
3773 set_deny(open->op_share_deny, stp);
3774 fp->fi_share_deny |=
3775 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3776 }
3777 spin_unlock(&fp->fi_lock);
3778
3779 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003782 status = nfsd4_truncate(rqstp, cur_fh, open);
3783 if (status != nfs_ok)
3784 reset_union_bmap_deny(old_deny_bmap, stp);
3785 return status;
3786}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003789nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003791 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792}
3793
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003794/* Should we give out recallable state?: */
3795static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3796{
3797 if (clp->cl_cb_state == NFSD4_CB_UP)
3798 return true;
3799 /*
3800 * In the sessions case, since we don't have to establish a
3801 * separate connection for callbacks, we assume it's OK
3802 * until we hear otherwise:
3803 */
3804 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3805}
3806
Jeff Laytond564fbe2014-07-16 10:31:58 -04003807static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003808{
3809 struct file_lock *fl;
3810
3811 fl = locks_alloc_lock();
3812 if (!fl)
3813 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003814 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003815 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003816 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3817 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003818 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003819 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003820 return fl;
3821}
3822
J. Bruce Fields99c41512013-05-21 16:21:25 -04003823static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003824{
Trond Myklebust11b91642014-07-29 21:34:08 -04003825 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003826 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003827 struct file *filp;
3828 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003829
Jeff Laytond564fbe2014-07-16 10:31:58 -04003830 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003831 if (!fl)
3832 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003833 filp = find_readable_file(fp);
3834 if (!filp) {
3835 /* We should always have a readable file here */
3836 WARN_ON_ONCE(1);
3837 return -EBADF;
3838 }
3839 fl->fl_file = filp;
3840 status = vfs_setlease(filp, fl->fl_type, &fl);
3841 if (status) {
3842 locks_free_lock(fl);
3843 goto out_fput;
3844 }
Benny Halevycdc975052014-05-30 09:09:30 -04003845 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003846 spin_lock(&fp->fi_lock);
3847 /* Did the lease get broken before we took the lock? */
3848 status = -EAGAIN;
3849 if (fp->fi_had_conflict)
3850 goto out_unlock;
3851 /* Race breaker */
3852 if (fp->fi_lease) {
3853 status = 0;
3854 atomic_inc(&fp->fi_delegees);
3855 hash_delegation_locked(dp, fp);
3856 goto out_unlock;
3857 }
3858 fp->fi_lease = fl;
3859 fp->fi_deleg_file = filp;
3860 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003861 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003862 spin_unlock(&fp->fi_lock);
Benny Halevycdc975052014-05-30 09:09:30 -04003863 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003864 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003865out_unlock:
3866 spin_unlock(&fp->fi_lock);
3867 spin_unlock(&state_lock);
3868out_fput:
3869 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003870 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003871}
3872
Jeff Layton0b266932014-07-25 07:34:25 -04003873static struct nfs4_delegation *
3874nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3875 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003876{
Jeff Layton0b266932014-07-25 07:34:25 -04003877 int status;
3878 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003879
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003880 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003881 return ERR_PTR(-EAGAIN);
3882
3883 dp = alloc_init_deleg(clp, fh);
3884 if (!dp)
3885 return ERR_PTR(-ENOMEM);
3886
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003887 get_nfs4_file(fp);
Benny Halevycdc975052014-05-30 09:09:30 -04003888 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003889 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003890 dp->dl_stid.sc_file = fp;
Jeff Layton417c6622014-07-21 09:34:57 -04003891 if (!fp->fi_lease) {
3892 spin_unlock(&fp->fi_lock);
3893 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003894 status = nfs4_setlease(dp);
3895 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003896 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003897 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003898 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003899 status = -EAGAIN;
3900 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003901 }
Benny Halevy931ee562014-05-30 09:09:27 -04003902 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003903 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003904out_unlock:
3905 spin_unlock(&fp->fi_lock);
Benny Halevycdc975052014-05-30 09:09:30 -04003906 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003907out:
3908 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003909 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003910 return ERR_PTR(status);
3911 }
3912 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003913}
3914
Benny Halevy4aa89132012-02-21 14:16:44 -08003915static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3916{
3917 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3918 if (status == -EAGAIN)
3919 open->op_why_no_deleg = WND4_CONTENTION;
3920 else {
3921 open->op_why_no_deleg = WND4_RESOURCE;
3922 switch (open->op_deleg_want) {
3923 case NFS4_SHARE_WANT_READ_DELEG:
3924 case NFS4_SHARE_WANT_WRITE_DELEG:
3925 case NFS4_SHARE_WANT_ANY_DELEG:
3926 break;
3927 case NFS4_SHARE_WANT_CANCEL:
3928 open->op_why_no_deleg = WND4_CANCELLED;
3929 break;
3930 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003931 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003932 }
3933 }
3934}
3935
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936/*
3937 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003938 *
3939 * Note we don't support write delegations, and won't until the vfs has
3940 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 */
3942static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003943nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3944 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945{
3946 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003947 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3948 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003949 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003950 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003952 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003953 open->op_recall = 0;
3954 switch (open->op_claim_type) {
3955 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003956 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003957 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003958 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3959 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003960 break;
3961 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003962 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003963 /*
3964 * Let's not give out any delegations till everyone's
3965 * had the chance to reclaim theirs....
3966 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003967 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003968 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003969 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003970 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003971 /*
3972 * Also, if the file was opened for write or
3973 * create, there's a good chance the client's
3974 * about to write to it, resulting in an
3975 * immediate recall (since we don't support
3976 * write delegations):
3977 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003978 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003979 goto out_no_deleg;
3980 if (open->op_create == NFS4_OPEN_CREATE)
3981 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003982 break;
3983 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003984 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003985 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003986 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003987 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003988 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003990 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003992 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003993 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003994 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003995 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003996 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003997out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003998 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3999 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004000 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04004001 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004002 open->op_recall = 1;
4003 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04004004
4005 /* 4.1 client asking for a delegation? */
4006 if (open->op_deleg_want)
4007 nfsd4_open_deleg_none_ext(open, status);
4008 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009}
4010
Benny Halevye27f49c2012-02-21 14:16:54 -08004011static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4012 struct nfs4_delegation *dp)
4013{
4014 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4015 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4016 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4017 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4018 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4019 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4020 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4021 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4022 }
4023 /* Otherwise the client must be confused wanting a delegation
4024 * it already has, therefore we don't return
4025 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4026 */
4027}
4028
Al Virob37ad282006-10-19 23:28:59 -07004029__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4031{
Andy Adamson66689582009-04-03 08:28:45 +03004032 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004033 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004035 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004036 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004037 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 /*
4040 * Lookup file; if found, lookup stateid and check open request,
4041 * and check for delegations in the process of being recalled.
4042 * If not found, create the nfs4_file struct
4043 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004044 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004045 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004046 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004047 if (status)
4048 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04004049 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004051 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004052 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004053 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004054 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004055 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 }
4057
4058 /*
4059 * OPEN the file, or upgrade an existing OPEN.
4060 * If truncate fails, the OPEN fails.
4061 */
4062 if (stp) {
4063 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004064 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 if (status)
4066 goto out;
4067 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004068 stp = open->op_stp;
4069 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04004070 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004071 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4072 if (status) {
4073 release_open_stateid(stp);
4074 goto out;
4075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004077 update_stateid(&stp->st_stid.sc_stateid);
4078 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
Benny Halevyd24433c2012-02-16 20:57:17 +02004080 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004081 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4082 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4083 open->op_why_no_deleg = WND4_NOT_WANTED;
4084 goto nodeleg;
4085 }
4086 }
4087
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 /*
4089 * Attempt to hand out a delegation. No error return, because the
4090 * OPEN succeeds even if we fail.
4091 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004092 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004093nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 status = nfs_ok;
4095
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004096 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004097 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004099 /* 4.1 client trying to upgrade/downgrade delegation? */
4100 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004101 open->op_deleg_want)
4102 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004103
NeilBrown13cd2182005-06-23 22:03:10 -07004104 if (fp)
4105 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004106 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05004107 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 /*
4109 * To finish the open response, we just need to set the rflags.
4110 */
4111 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004112 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004113 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004115 if (dp)
4116 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004117 if (stp)
4118 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
4120 return status;
4121}
4122
Jeff Layton58fb12e2014-07-29 21:34:27 -04004123void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4124 struct nfsd4_open *open, __be32 status)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004125{
4126 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004127 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004128
Jeff Laytond3134b12014-07-29 21:34:32 -04004129 nfsd4_cstate_assign_replay(cstate, so);
4130 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004131 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004132 if (open->op_file)
4133 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004134 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004135 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004136}
4137
Al Virob37ad282006-10-19 23:28:59 -07004138__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004139nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4140 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141{
4142 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004143 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004144 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 dprintk("process_renew(%08x/%08x): starting\n",
4147 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004148 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004149 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004151 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004153 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004154 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 goto out;
4156 status = nfs_ok;
4157out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 return status;
4159}
4160
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04004161void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004162nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004163{
Jeff Layton33dcc482012-04-10 11:08:48 -04004164 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004165 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004166 return;
4167
NeilBrowna76b4312005-06-23 22:04:01 -07004168 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004169 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04004170 /*
4171 * If the server goes down again right now, an NFSv4
4172 * client will still be allowed to reclaim after it comes back up,
4173 * even if it hasn't yet had a chance to reclaim state this time.
4174 *
4175 */
Jeff Layton919b8042014-09-12 16:40:20 -04004176 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004177 /*
4178 * At this point, NFSv4 clients can still reclaim. But if the
4179 * server crashes, any that have not yet reclaimed will be out
4180 * of luck on the next boot.
4181 *
4182 * (NFSv4.1+ clients are considered to have reclaimed once they
4183 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4184 * have reclaimed after their first OPEN.)
4185 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004186 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004187 /*
4188 * At this point, and once lockd and/or any other containers
4189 * exit their grace period, further reclaims will fail and
4190 * regular locking can resume.
4191 */
NeilBrowna76b4312005-06-23 22:04:01 -07004192}
4193
NeilBrownfd39ca92005-06-23 22:04:03 -07004194static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004195nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196{
4197 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004198 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004200 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004202 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004203 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004206 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004207 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004208 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004209 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 clp = list_entry(pos, struct nfs4_client, cl_lru);
4211 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4212 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004213 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 break;
4215 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004216 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004217 dprintk("NFSD: client in use (clientid %08x)\n",
4218 clp->cl_clientid.cl_id);
4219 continue;
4220 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004221 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004222 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004223 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004224 list_for_each_safe(pos, next, &reaplist) {
4225 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 dprintk("NFSD: purging unused client (clientid %08x)\n",
4227 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004228 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 expire_client(clp);
4230 }
Benny Halevycdc975052014-05-30 09:09:30 -04004231 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004232 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004234 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4235 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004237 t = dp->dl_time - cutoff;
4238 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 break;
4240 }
Jeff Layton42690672014-07-25 07:34:20 -04004241 unhash_delegation_locked(dp);
4242 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 }
Benny Halevycdc975052014-05-30 09:09:30 -04004244 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004245 while (!list_empty(&reaplist)) {
4246 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4247 dl_recall_lru);
4248 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004249 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 }
Jeff Layton217526e2014-07-30 08:27:11 -04004251
4252 spin_lock(&nn->client_lock);
4253 while (!list_empty(&nn->close_lru)) {
4254 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4255 oo_close_lru);
4256 if (time_after((unsigned long)oo->oo_time,
4257 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004258 t = oo->oo_time - cutoff;
4259 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 break;
4261 }
Jeff Layton217526e2014-07-30 08:27:11 -04004262 list_del_init(&oo->oo_close_lru);
4263 stp = oo->oo_last_closed_stid;
4264 oo->oo_last_closed_stid = NULL;
4265 spin_unlock(&nn->client_lock);
4266 nfs4_put_stid(&stp->st_stid);
4267 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 }
Jeff Layton217526e2014-07-30 08:27:11 -04004269 spin_unlock(&nn->client_lock);
4270
Jeff Laytona832e7a2014-05-30 09:09:26 -04004271 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004272 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273}
4274
Harvey Harrisona254b242008-02-20 12:49:00 -08004275static struct workqueue_struct *laundry_wq;
4276static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004277
4278static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004279laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280{
4281 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004282 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4283 work);
4284 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4285 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004287 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004289 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290}
4291
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004292static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004293{
Trond Myklebust11b91642014-07-29 21:34:08 -04004294 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004295 return nfserr_bad_stateid;
4296 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297}
4298
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004300access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004302 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4303 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4304 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305}
4306
4307static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004308access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004310 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4311 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312}
4313
4314static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004315__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316{
Al Virob37ad282006-10-19 23:28:59 -07004317 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
J. Bruce Fields02921912010-07-29 15:16:59 -04004319 /* For lock stateid's, we test the parent open, not the lock: */
4320 if (stp->st_openstp)
4321 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004322 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004324 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 goto out;
4326 status = nfs_ok;
4327out:
4328 return status;
4329}
4330
Al Virob37ad282006-10-19 23:28:59 -07004331static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004332check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004334 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004336 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004337 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 * conflicting state; so we must wait out the grace period. */
4339 return nfserr_grace;
4340 } else if (flags & WR_STATE)
4341 return nfs4_share_conflict(current_fh,
4342 NFS4_SHARE_DENY_WRITE);
4343 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4344 return nfs4_share_conflict(current_fh,
4345 NFS4_SHARE_DENY_READ);
4346}
4347
4348/*
4349 * Allow READ/WRITE during grace period on recovered state only for files
4350 * that are not able to provide mandatory locking.
4351 */
4352static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004353grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004355 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356}
4357
J. Bruce Fields81b82962011-08-23 11:03:29 -04004358/* Returns true iff a is later than b: */
4359static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4360{
Jim Rees1a9357f2013-05-17 17:33:00 -04004361 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004362}
4363
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004364static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004365{
Andy Adamson66689582009-04-03 08:28:45 +03004366 /*
4367 * When sessions are used the stateid generation number is ignored
4368 * when it is zero.
4369 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004370 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004371 return nfs_ok;
4372
4373 if (in->si_generation == ref->si_generation)
4374 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004375
J. Bruce Fields0836f582008-01-26 19:08:12 -05004376 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004377 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004378 return nfserr_bad_stateid;
4379 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004380 * However, we could see a stateid from the past, even from a
4381 * non-buggy client. For example, if the client sends a lock
4382 * while some IO is outstanding, the lock may bump si_generation
4383 * while the IO is still in flight. The client could avoid that
4384 * situation by waiting for responses on all the IO requests,
4385 * but better performance may result in retrying IO that
4386 * receives an old_stateid error if requests are rarely
4387 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004388 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004389 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004390}
4391
Chuck Lever7df302f2012-05-29 13:56:37 -04004392static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004393{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004394 struct nfs4_stid *s;
4395 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004396 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004397
Chuck Lever7df302f2012-05-29 13:56:37 -04004398 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004399 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004400 /* Client debugging aid. */
4401 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4402 char addr_str[INET6_ADDRSTRLEN];
4403 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4404 sizeof(addr_str));
4405 pr_warn_ratelimited("NFSD: client %s testing state ID "
4406 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004407 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004408 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004409 spin_lock(&cl->cl_lock);
4410 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004411 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004412 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004413 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004414 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004415 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004416 switch (s->sc_type) {
4417 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004418 status = nfs_ok;
4419 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004420 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004421 status = nfserr_deleg_revoked;
4422 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004423 case NFS4_OPEN_STID:
4424 case NFS4_LOCK_STID:
4425 ols = openlockstateid(s);
4426 if (ols->st_stateowner->so_is_open_owner
4427 && !(openowner(ols->st_stateowner)->oo_flags
4428 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004429 status = nfserr_bad_stateid;
4430 else
4431 status = nfs_ok;
4432 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004433 default:
4434 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004435 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004436 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004437 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004438 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004439 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004440out_unlock:
4441 spin_unlock(&cl->cl_lock);
4442 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004443}
4444
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004445static __be32
4446nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4447 stateid_t *stateid, unsigned char typemask,
4448 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004449{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004450 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004451
4452 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4453 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004454 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004455 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004456 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004457 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004458 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004459 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004460 if (status)
4461 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004462 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004463 if (!*s)
4464 return nfserr_bad_stateid;
4465 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004466}
4467
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468/*
4469* Checks for stateid operations
4470*/
Al Virob37ad282006-10-19 23:28:59 -07004471__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004472nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004473 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004475 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004476 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004478 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004480 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004481 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004482 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 if (filpp)
4485 *filpp = NULL;
4486
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004487 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 return nfserr_grace;
4489
4490 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004491 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004493 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004494 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004495 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004496 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04004497 return status;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004498 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4499 if (status)
4500 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004501 switch (s->sc_type) {
4502 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004503 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004504 status = nfs4_check_delegmode(dp, flags);
4505 if (status)
4506 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004507 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004508 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004509 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004510 WARN_ON_ONCE(1);
4511 status = nfserr_serverfault;
4512 goto out;
4513 }
Trond Myklebustde186432014-07-10 14:07:26 -04004514 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004515 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004516 break;
4517 case NFS4_OPEN_STID:
4518 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004519 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004520 status = nfs4_check_fh(current_fh, stp);
4521 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004523 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004524 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004526 status = nfs4_check_openmode(stp, flags);
4527 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004529 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004530 struct nfs4_file *fp = stp->st_stid.sc_file;
4531
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004532 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004533 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004534 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004535 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004536 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004537 break;
4538 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004539 status = nfserr_bad_stateid;
4540 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 }
4542 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004543 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004544 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004546 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 return status;
4548}
4549
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004550/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004551 * Test if the stateid is valid
4552 */
4553__be32
4554nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4555 struct nfsd4_test_stateid *test_stateid)
4556{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004557 struct nfsd4_test_stateid_id *stateid;
4558 struct nfs4_client *cl = cstate->session->se_client;
4559
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004560 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004561 stateid->ts_id_status =
4562 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004563
Bryan Schumaker17456802011-07-13 10:50:48 -04004564 return nfs_ok;
4565}
4566
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004567__be32
4568nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4569 struct nfsd4_free_stateid *free_stateid)
4570{
4571 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004572 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004573 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004574 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004575 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004576 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004577
Jeff Layton1af71cc2014-07-29 21:34:14 -04004578 spin_lock(&cl->cl_lock);
4579 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004580 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004581 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004582 switch (s->sc_type) {
4583 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004584 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004585 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004586 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004587 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4588 if (ret)
4589 break;
4590 ret = nfserr_locks_held;
4591 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004592 case NFS4_LOCK_STID:
4593 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4594 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004595 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004596 stp = openlockstateid(s);
4597 ret = nfserr_locks_held;
4598 if (check_for_locks(stp->st_stid.sc_file,
4599 lockowner(stp->st_stateowner)))
4600 break;
4601 unhash_lock_stateid(stp);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004602 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004603 nfs4_put_stid(s);
4604 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004605 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004606 case NFS4_REVOKED_DELEG_STID:
4607 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004608 list_del_init(&dp->dl_recall_lru);
4609 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004610 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004611 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004612 goto out;
4613 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004614 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004615out_unlock:
4616 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004617out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004618 return ret;
4619}
4620
NeilBrown4c4cd222005-07-07 17:59:27 -07004621static inline int
4622setlkflg (int type)
4623{
4624 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4625 RD_STATE : WR_STATE;
4626}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004628static __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 -04004629{
4630 struct svc_fh *current_fh = &cstate->current_fh;
4631 struct nfs4_stateowner *sop = stp->st_stateowner;
4632 __be32 status;
4633
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004634 status = nfsd4_check_seqid(cstate, sop, seqid);
4635 if (status)
4636 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004637 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4638 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004639 /*
4640 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004641 * nfserr_replay_me from the previous step, and
4642 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004643 */
4644 return nfserr_bad_stateid;
4645 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4646 if (status)
4647 return status;
4648 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004649}
4650
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651/*
4652 * Checks for sequence id mutating operations.
4653 */
Al Virob37ad282006-10-19 23:28:59 -07004654static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004655nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004656 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004657 struct nfs4_ol_stateid **stpp,
4658 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004660 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004661 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004662 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004664 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4665 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004666
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004668 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004669 if (status)
4670 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004671 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004672 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004674 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004675 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004676 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004677 else
4678 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004679 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004680}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004681
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004682static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4683 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004684{
4685 __be32 status;
4686 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004687 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004689 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004690 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004691 if (status)
4692 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004693 oo = openowner(stp->st_stateowner);
4694 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4695 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004696 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004697 }
4698 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004699 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700}
4701
Al Virob37ad282006-10-19 23:28:59 -07004702__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004703nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004704 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705{
Al Virob37ad282006-10-19 23:28:59 -07004706 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004707 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004708 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004709 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710
Al Viroa6a9f182013-09-16 10:57:01 -04004711 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4712 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004714 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004715 if (status)
4716 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004718 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004719 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004720 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004721 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004722 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004723 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004724 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004725 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004726 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004727 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004728 update_stateid(&stp->st_stid.sc_stateid);
4729 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004730 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004731 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004732
Jeff Layton2a4317c2012-03-21 16:42:43 -04004733 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004734 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004735put_stateid:
4736 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004738 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 return status;
4740}
4741
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004742static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004744 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004745 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004746 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004747 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004748}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004749
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004750static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4751{
4752 switch (to_access) {
4753 case NFS4_SHARE_ACCESS_READ:
4754 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4755 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4756 break;
4757 case NFS4_SHARE_ACCESS_WRITE:
4758 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4759 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4760 break;
4761 case NFS4_SHARE_ACCESS_BOTH:
4762 break;
4763 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004764 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 }
4766}
4767
Al Virob37ad282006-10-19 23:28:59 -07004768__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004769nfsd4_open_downgrade(struct svc_rqst *rqstp,
4770 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004771 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772{
Al Virob37ad282006-10-19 23:28:59 -07004773 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004774 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004775 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776
Al Viroa6a9f182013-09-16 10:57:01 -04004777 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4778 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004780 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004781 if (od->od_deleg_want)
4782 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4783 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004785 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004786 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004787 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004790 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004791 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004793 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004795 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004796 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004798 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004800 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801
Jeff Laytonce0fc432012-05-11 09:45:14 -04004802 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004804 update_stateid(&stp->st_stid.sc_stateid);
4805 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004807put_stateid:
4808 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004810 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 return status;
4812}
4813
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004814static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4815{
Trond Myklebustacf92952014-06-30 11:48:37 -04004816 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytond83017f2014-07-29 21:34:42 -04004817 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004818
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004819 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04004820 spin_lock(&clp->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04004821 unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004822
Jeff Laytond83017f2014-07-29 21:34:42 -04004823 if (clp->cl_minorversion) {
4824 put_ol_stateid_locked(s, &reaplist);
4825 spin_unlock(&clp->cl_lock);
4826 free_ol_stateid_reaplist(&reaplist);
4827 } else {
4828 spin_unlock(&clp->cl_lock);
4829 free_ol_stateid_reaplist(&reaplist);
Jeff Laytond3134b12014-07-29 21:34:32 -04004830 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04004831 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004832}
4833
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834/*
4835 * nfs4_unlock_state() called after encode
4836 */
Al Virob37ad282006-10-19 23:28:59 -07004837__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004838nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004839 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840{
Al Virob37ad282006-10-19 23:28:59 -07004841 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004842 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004843 struct net *net = SVC_NET(rqstp);
4844 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845
Al Viroa6a9f182013-09-16 10:57:01 -04004846 dprintk("NFSD: nfsd4_close on file %pd\n",
4847 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004849 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4850 &close->cl_stateid,
4851 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004852 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004853 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004854 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004856 update_stateid(&stp->st_stid.sc_stateid);
4857 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004859 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004860
4861 /* put reference from nfs4_preprocess_seqid_op */
4862 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 return status;
4865}
4866
Al Virob37ad282006-10-19 23:28:59 -07004867__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004868nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4869 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004871 struct nfs4_delegation *dp;
4872 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004873 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004874 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004875 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004877 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004878 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004880 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004881 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004882 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004883 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004884 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004885 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004886 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004887
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004888 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004889put_stateid:
4890 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891out:
4892 return status;
4893}
4894
4895
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897
Benny Halevy87df4de2008-12-15 19:42:03 +02004898static inline u64
4899end_offset(u64 start, u64 len)
4900{
4901 u64 end;
4902
4903 end = start + len;
4904 return end >= start ? end: NFS4_MAX_UINT64;
4905}
4906
4907/* last octet in a range */
4908static inline u64
4909last_byte_offset(u64 start, u64 len)
4910{
4911 u64 end;
4912
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004913 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004914 end = start + len;
4915 return end > start ? end - 1: NFS4_MAX_UINT64;
4916}
4917
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918/*
4919 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4920 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4921 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4922 * locking, this prevents us from being completely protocol-compliant. The
4923 * real solution to this problem is to start using unsigned file offsets in
4924 * the VFS, but this is a very deep change!
4925 */
4926static inline void
4927nfs4_transform_lock_offset(struct file_lock *lock)
4928{
4929 if (lock->fl_start < 0)
4930 lock->fl_start = OFFSET_MAX;
4931 if (lock->fl_end < 0)
4932 lock->fl_end = OFFSET_MAX;
4933}
4934
NeilBrownd5b90262006-04-10 22:55:22 -07004935/* Hack!: For now, we're defining this just so we can use a pointer to it
4936 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004937static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004938};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939
4940static inline void
4941nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4942{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004943 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944
NeilBrownd5b90262006-04-10 22:55:22 -07004945 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004946 lo = (struct nfs4_lockowner *) fl->fl_owner;
4947 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4948 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004949 if (!deny->ld_owner.data)
4950 /* We just don't care that much */
4951 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004952 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4953 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004954 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004955nevermind:
4956 deny->ld_owner.len = 0;
4957 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004958 deny->ld_clientid.cl_boot = 0;
4959 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 }
4961 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004962 deny->ld_length = NFS4_MAX_UINT64;
4963 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4965 deny->ld_type = NFS4_READ_LT;
4966 if (fl->fl_type != F_RDLCK)
4967 deny->ld_type = NFS4_WRITE_LT;
4968}
4969
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004970static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004971find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004972 struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004974 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004975 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976
Trond Myklebust0a880a22014-07-30 08:27:10 -04004977 lockdep_assert_held(&clp->cl_lock);
4978
Trond Myklebustd4f04892014-07-29 21:34:36 -04004979 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4980 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004981 if (so->so_is_open_owner)
4982 continue;
Trond Myklebustd4f04892014-07-29 21:34:36 -04004983 if (!same_owner_str(so, owner))
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004984 continue;
Jeff Layton5db1c032014-07-29 21:34:28 -04004985 atomic_inc(&so->so_count);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004986 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 }
4988 return NULL;
4989}
4990
Trond Myklebustc58c6612014-07-29 21:34:35 -04004991static struct nfs4_lockowner *
4992find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004993 struct nfs4_client *clp)
Trond Myklebustc58c6612014-07-29 21:34:35 -04004994{
4995 struct nfs4_lockowner *lo;
4996
Trond Myklebustd4f04892014-07-29 21:34:36 -04004997 spin_lock(&clp->cl_lock);
4998 lo = find_lockowner_str_locked(clid, owner, clp);
4999 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005000 return lo;
5001}
5002
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005003static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5004{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005005 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005006}
5007
Jeff Layton6b180f02014-07-29 21:34:26 -04005008static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5009{
5010 struct nfs4_lockowner *lo = lockowner(sop);
5011
5012 kmem_cache_free(lockowner_slab, lo);
5013}
5014
5015static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005016 .so_unhash = nfs4_unhash_lockowner,
5017 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005018};
5019
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020/*
5021 * Alloc a lock owner structure.
5022 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005023 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005025 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005027static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005028alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5029 struct nfs4_ol_stateid *open_stp,
5030 struct nfsd4_lock *lock)
5031{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005032 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005034 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5035 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005037 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5038 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005039 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005040 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005041 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005042 ret = find_lockowner_str_locked(&clp->cl_clientid,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005043 &lock->lk_new_owner, clp);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005044 if (ret == NULL) {
5045 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005046 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005047 ret = lo;
5048 } else
5049 nfs4_free_lockowner(&lo->lo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005050 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005051 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052}
5053
Jeff Layton356a95e2014-07-29 21:34:13 -04005054static void
5055init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5056 struct nfs4_file *fp, struct inode *inode,
5057 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005059 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060
Jeff Layton356a95e2014-07-29 21:34:13 -04005061 lockdep_assert_held(&clp->cl_lock);
5062
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005063 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005064 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005065 stp->st_stateowner = &lo->lo_owner;
Trond Myklebuste4f1dd72014-07-29 21:34:30 -04005066 atomic_inc(&lo->lo_owner.so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07005067 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005068 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04005069 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005070 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005072 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005073 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005074 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005075 spin_lock(&fp->fi_lock);
5076 list_add(&stp->st_perfile, &fp->fi_stateids);
5077 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078}
5079
Jeff Laytonc53530d2014-06-30 11:48:39 -04005080static struct nfs4_ol_stateid *
5081find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5082{
5083 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005084 struct nfs4_client *clp = lo->lo_owner.so_client;
5085
5086 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005087
5088 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005089 if (lst->st_stid.sc_file == fp) {
5090 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005091 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005092 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005093 }
5094 return NULL;
5095}
5096
Jeff Layton356a95e2014-07-29 21:34:13 -04005097static struct nfs4_ol_stateid *
5098find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5099 struct inode *inode, struct nfs4_ol_stateid *ost,
5100 bool *new)
5101{
5102 struct nfs4_stid *ns = NULL;
5103 struct nfs4_ol_stateid *lst;
5104 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5105 struct nfs4_client *clp = oo->oo_owner.so_client;
5106
5107 spin_lock(&clp->cl_lock);
5108 lst = find_lock_stateid(lo, fi);
5109 if (lst == NULL) {
5110 spin_unlock(&clp->cl_lock);
5111 ns = nfs4_alloc_stid(clp, stateid_slab);
5112 if (ns == NULL)
5113 return NULL;
5114
5115 spin_lock(&clp->cl_lock);
5116 lst = find_lock_stateid(lo, fi);
5117 if (likely(!lst)) {
5118 lst = openlockstateid(ns);
5119 init_lock_stateid(lst, lo, fi, inode, ost);
5120 ns = NULL;
5121 *new = true;
5122 }
5123 }
5124 spin_unlock(&clp->cl_lock);
5125 if (ns)
5126 nfs4_put_stid(ns);
5127 return lst;
5128}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005129
NeilBrownfd39ca92005-06-23 22:04:03 -07005130static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131check_lock_length(u64 offset, u64 length)
5132{
Benny Halevy87df4de2008-12-15 19:42:03 +02005133 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 LOFF_OVERFLOW(offset, length)));
5135}
5136
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005137static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005138{
Trond Myklebust11b91642014-07-29 21:34:08 -04005139 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005140
Jeff Layton7214e862014-07-10 14:07:33 -04005141 lockdep_assert_held(&fp->fi_lock);
5142
Jeff Layton82c5ff12012-05-11 09:45:13 -04005143 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005144 return;
Jeff Layton12659652014-07-10 14:07:28 -04005145 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005146 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005147}
5148
Jeff Layton356a95e2014-07-29 21:34:13 -04005149static __be32
5150lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5151 struct nfs4_ol_stateid *ost,
5152 struct nfsd4_lock *lock,
5153 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005154{
Jeff Layton5db1c032014-07-29 21:34:28 -04005155 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005156 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005157 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5158 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005159 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005160 struct nfs4_lockowner *lo;
5161 unsigned int strhashval;
5162
Trond Myklebustd4f04892014-07-29 21:34:36 -04005163 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005164 if (!lo) {
Trond Myklebustd4f04892014-07-29 21:34:36 -04005165 strhashval = ownerstr_hashval(&lock->v.new.owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005166 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5167 if (lo == NULL)
5168 return nfserr_jukebox;
5169 } else {
5170 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005171 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005172 if (!cstate->minorversion &&
5173 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005174 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005175 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005176
Jeff Layton356a95e2014-07-29 21:34:13 -04005177 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005178 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005179 status = nfserr_jukebox;
5180 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005181 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005182 status = nfs_ok;
5183out:
5184 nfs4_put_stateowner(&lo->lo_owner);
5185 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005186}
5187
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188/*
5189 * LOCK operation
5190 */
Al Virob37ad282006-10-19 23:28:59 -07005191__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005192nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005193 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005195 struct nfs4_openowner *open_sop = NULL;
5196 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005197 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005198 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005199 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005200 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005201 struct file_lock *file_lock = NULL;
5202 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005203 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005204 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005205 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005206 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005207 struct net *net = SVC_NET(rqstp);
5208 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209
5210 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5211 (long long) lock->lk_offset,
5212 (long long) lock->lk_length);
5213
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 if (check_lock_length(lock->lk_offset, lock->lk_length))
5215 return nfserr_inval;
5216
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005217 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005218 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005219 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5220 return status;
5221 }
5222
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005224 if (nfsd4_has_session(cstate))
5225 /* See rfc 5661 18.10.3: given clientid is ignored: */
5226 memcpy(&lock->v.new.clientid,
5227 &cstate->session->se_client->cl_clientid,
5228 sizeof(clientid_t));
5229
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005231 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005235 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 lock->lk_new_open_seqid,
5237 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005238 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005239 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005241 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005242 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005243 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005244 &lock->v.new.clientid))
5245 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005246 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005247 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005248 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005249 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005250 lock->lk_old_lock_seqid,
5251 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005252 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005253 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005254 if (status)
5255 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005256 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005258 lkflg = setlkflg(lock->lk_type);
5259 status = nfs4_check_openmode(lock_stp, lkflg);
5260 if (status)
5261 goto out;
5262
NeilBrown0dd395d2005-07-07 17:59:15 -07005263 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005264 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005265 goto out;
5266 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005267 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005268 goto out;
5269
Jeff Layton21179d82012-08-21 08:03:32 -04005270 file_lock = locks_alloc_lock();
5271 if (!file_lock) {
5272 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5273 status = nfserr_jukebox;
5274 goto out;
5275 }
5276
Trond Myklebust11b91642014-07-29 21:34:08 -04005277 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 switch (lock->lk_type) {
5279 case NFS4_READ_LT:
5280 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005281 spin_lock(&fp->fi_lock);
5282 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005283 if (filp)
5284 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005285 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005286 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005287 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288 case NFS4_WRITE_LT:
5289 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005290 spin_lock(&fp->fi_lock);
5291 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005292 if (filp)
5293 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005294 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005295 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005296 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 default:
5298 status = nfserr_inval;
5299 goto out;
5300 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005301 if (!filp) {
5302 status = nfserr_openmode;
5303 goto out;
5304 }
Jeff Layton21179d82012-08-21 08:03:32 -04005305 file_lock->fl_owner = (fl_owner_t)lock_sop;
5306 file_lock->fl_pid = current->tgid;
5307 file_lock->fl_file = filp;
5308 file_lock->fl_flags = FL_POSIX;
5309 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5310 file_lock->fl_start = lock->lk_offset;
5311 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5312 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313
Jeff Layton21179d82012-08-21 08:03:32 -04005314 conflock = locks_alloc_lock();
5315 if (!conflock) {
5316 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5317 status = nfserr_jukebox;
5318 goto out;
5319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320
Jeff Layton21179d82012-08-21 08:03:32 -04005321 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005322 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005324 update_stateid(&lock_stp->st_stid.sc_stateid);
5325 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005327 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005328 break;
5329 case (EAGAIN): /* conflock holds conflicting lock */
5330 status = nfserr_denied;
5331 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005332 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005333 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 case (EDEADLK):
5335 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005336 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005337 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005338 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005339 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005340 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342out:
Trond Myklebustde186432014-07-10 14:07:26 -04005343 if (filp)
5344 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005345 if (lock_stp) {
5346 /* Bump seqid manually if the 4.0 replay owner is openowner */
5347 if (cstate->replay_owner &&
5348 cstate->replay_owner != &lock_sop->lo_owner &&
5349 seqid_mutating_err(ntohl(status)))
5350 lock_sop->lo_owner.so_seqid++;
5351
5352 /*
5353 * If this is a new, never-before-used stateid, and we are
5354 * returning an error, then just go ahead and release it.
5355 */
5356 if (status && new)
5357 release_lock_stateid(lock_stp);
5358
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005359 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005360 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005361 if (open_stp)
5362 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005363 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005364 if (file_lock)
5365 locks_free_lock(file_lock);
5366 if (conflock)
5367 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 return status;
5369}
5370
5371/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005372 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5373 * so we do a temporary open here just to get an open file to pass to
5374 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5375 * inode operation.)
5376 */
Al Viro04da6e92012-04-13 00:00:04 -04005377static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005378{
5379 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005380 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5381 if (!err) {
5382 err = nfserrno(vfs_test_lock(file, lock));
5383 nfsd_close(file);
5384 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005385 return err;
5386}
5387
5388/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 * LOCKT operation
5390 */
Al Virob37ad282006-10-19 23:28:59 -07005391__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005392nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5393 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394{
Jeff Layton21179d82012-08-21 08:03:32 -04005395 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005396 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005397 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005398 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005400 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 return nfserr_grace;
5402
5403 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5404 return nfserr_inval;
5405
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005406 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005407 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005408 if (status)
5409 goto out;
5410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005412 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414
Jeff Layton21179d82012-08-21 08:03:32 -04005415 file_lock = locks_alloc_lock();
5416 if (!file_lock) {
5417 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5418 status = nfserr_jukebox;
5419 goto out;
5420 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005421
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 switch (lockt->lt_type) {
5423 case NFS4_READ_LT:
5424 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005425 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 break;
5427 case NFS4_WRITE_LT:
5428 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005429 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 break;
5431 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005432 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 status = nfserr_inval;
5434 goto out;
5435 }
5436
Trond Myklebustd4f04892014-07-29 21:34:36 -04005437 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5438 cstate->clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005439 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005440 file_lock->fl_owner = (fl_owner_t)lo;
5441 file_lock->fl_pid = current->tgid;
5442 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443
Jeff Layton21179d82012-08-21 08:03:32 -04005444 file_lock->fl_start = lockt->lt_offset;
5445 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446
Jeff Layton21179d82012-08-21 08:03:32 -04005447 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448
Jeff Layton21179d82012-08-21 08:03:32 -04005449 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005450 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005451 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005452
Jeff Layton21179d82012-08-21 08:03:32 -04005453 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005455 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 }
5457out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005458 if (lo)
5459 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04005460 if (file_lock)
5461 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 return status;
5463}
5464
Al Virob37ad282006-10-19 23:28:59 -07005465__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005466nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005467 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005469 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005471 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005472 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005473 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005474 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5475
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5477 (long long) locku->lu_offset,
5478 (long long) locku->lu_length);
5479
5480 if (check_lock_length(locku->lu_offset, locku->lu_length))
5481 return nfserr_inval;
5482
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005483 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005484 &locku->lu_stateid, NFS4_LOCK_STID,
5485 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005486 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005488 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005489 if (!filp) {
5490 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005491 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005492 }
Jeff Layton21179d82012-08-21 08:03:32 -04005493 file_lock = locks_alloc_lock();
5494 if (!file_lock) {
5495 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5496 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005497 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005498 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005499
Jeff Layton21179d82012-08-21 08:03:32 -04005500 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005501 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005502 file_lock->fl_pid = current->tgid;
5503 file_lock->fl_file = filp;
5504 file_lock->fl_flags = FL_POSIX;
5505 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5506 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507
Jeff Layton21179d82012-08-21 08:03:32 -04005508 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5509 locku->lu_length);
5510 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511
Jeff Layton21179d82012-08-21 08:03:32 -04005512 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005513 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005514 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 goto out_nfserr;
5516 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005517 update_stateid(&stp->st_stid.sc_stateid);
5518 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005519fput:
5520 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005521put_stateid:
5522 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005524 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005525 if (file_lock)
5526 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 return status;
5528
5529out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005530 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005531 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532}
5533
5534/*
5535 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005536 * true: locks held by lockowner
5537 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005539static bool
5540check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541{
5542 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005543 int status = false;
5544 struct file *filp = find_any_file(fp);
5545 struct inode *inode;
5546
5547 if (!filp) {
5548 /* Any valid lock stateid should have some sort of access */
5549 WARN_ON_ONCE(1);
5550 return status;
5551 }
5552
5553 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554
Jeff Layton1c8c6012013-06-21 08:58:15 -04005555 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005557 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005558 status = true;
5559 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005562 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005563 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 return status;
5565}
5566
Al Virob37ad282006-10-19 23:28:59 -07005567__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005568nfsd4_release_lockowner(struct svc_rqst *rqstp,
5569 struct nfsd4_compound_state *cstate,
5570 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571{
5572 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005573 struct nfs4_stateowner *sop;
5574 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005575 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005577 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005578 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005579 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005580 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581
5582 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5583 clid->cl_boot, clid->cl_id);
5584
Jeff Layton4b24ca72014-06-30 11:48:44 -04005585 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005586 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04005587 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005588
Trond Myklebustd4f04892014-07-29 21:34:36 -04005589 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005590 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005591 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005592 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005593 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005594
5595 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005596 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005597
Jeff Layton882e9d22014-07-29 21:34:37 -04005598 /* see if there are still any locks associated with it */
5599 lo = lockowner(sop);
5600 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5601 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5602 status = nfserr_locks_held;
5603 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04005604 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04005605 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005606 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005607
5608 atomic_inc(&sop->so_count);
5609 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005610 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005611 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005612 if (lo)
5613 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 return status;
5615}
5616
5617static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005618alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619{
NeilBrowna55370a2005-06-23 22:03:52 -07005620 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621}
5622
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005623bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005624nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005625{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005626 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005627
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005628 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005629 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005630}
5631
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632/*
5633 * failure => all reset bets are off, nfserr_no_grace...
5634 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005635struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005636nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637{
5638 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005639 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640
NeilBrowna55370a2005-06-23 22:03:52 -07005641 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5642 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005643 if (crp) {
5644 strhashval = clientstr_hashval(name);
5645 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005646 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005647 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005648 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005649 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005650 }
5651 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652}
5653
Jeff Layton2a4317c2012-03-21 16:42:43 -04005654void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005655nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005656{
5657 list_del(&crp->cr_strhash);
5658 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005659 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005660}
5661
5662void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005663nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664{
5665 struct nfs4_client_reclaim *crp = NULL;
5666 int i;
5667
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005669 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5670 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005672 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 }
5674 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005675 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676}
5677
5678/*
5679 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005680struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005681nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682{
5683 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 struct nfs4_client_reclaim *crp = NULL;
5685
Jeff Layton278c9312012-11-12 15:00:52 -05005686 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687
Jeff Layton278c9312012-11-12 15:00:52 -05005688 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005689 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005690 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 return crp;
5692 }
5693 }
5694 return NULL;
5695}
5696
5697/*
5698* Called from OPEN. Look for clientid in reclaim list.
5699*/
Al Virob37ad282006-10-19 23:28:59 -07005700__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005701nfs4_check_open_reclaim(clientid_t *clid,
5702 struct nfsd4_compound_state *cstate,
5703 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005705 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005706
5707 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005708 status = lookup_clientid(clid, cstate, nn);
5709 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005710 return nfserr_reclaim_bad;
5711
Jeff Layton3b3e7b72014-09-12 16:40:22 -04005712 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
5713 return nfserr_no_grace;
5714
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005715 if (nfsd4_client_record_check(cstate->clp))
5716 return nfserr_reclaim_bad;
5717
5718 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719}
5720
Bryan Schumaker65178db2011-11-01 13:35:21 -04005721#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04005722static inline void
5723put_client(struct nfs4_client *clp)
5724{
5725 atomic_dec(&clp->cl_refcount);
5726}
5727
Jeff Layton285abde2014-07-30 08:27:24 -04005728static struct nfs4_client *
5729nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5730{
5731 struct nfs4_client *clp;
5732 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5733 nfsd_net_id);
5734
5735 if (!nfsd_netns_ready(nn))
5736 return NULL;
5737
5738 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5739 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5740 return clp;
5741 }
5742 return NULL;
5743}
5744
Jeff Layton7ec0e362014-07-30 08:27:17 -04005745u64
Jeff Layton285abde2014-07-30 08:27:24 -04005746nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04005747{
5748 struct nfs4_client *clp;
5749 u64 count = 0;
5750 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5751 nfsd_net_id);
5752 char buf[INET6_ADDRSTRLEN];
5753
5754 if (!nfsd_netns_ready(nn))
5755 return 0;
5756
5757 spin_lock(&nn->client_lock);
5758 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5759 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5760 pr_info("NFS Client: %s\n", buf);
5761 ++count;
5762 }
5763 spin_unlock(&nn->client_lock);
5764
5765 return count;
5766}
Bryan Schumaker65178db2011-11-01 13:35:21 -04005767
Jeff Laytona0926d12014-07-30 08:27:18 -04005768u64
Jeff Layton285abde2014-07-30 08:27:24 -04005769nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04005770{
5771 u64 count = 0;
5772 struct nfs4_client *clp;
5773 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5774 nfsd_net_id);
5775
5776 if (!nfsd_netns_ready(nn))
5777 return count;
5778
5779 spin_lock(&nn->client_lock);
5780 clp = nfsd_find_client(addr, addr_size);
5781 if (clp) {
5782 if (mark_client_expired_locked(clp) == nfs_ok)
5783 ++count;
5784 else
5785 clp = NULL;
5786 }
5787 spin_unlock(&nn->client_lock);
5788
5789 if (clp)
5790 expire_client(clp);
5791
5792 return count;
5793}
5794
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005795u64
Jeff Layton285abde2014-07-30 08:27:24 -04005796nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005797{
5798 u64 count = 0;
5799 struct nfs4_client *clp, *next;
5800 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5801 nfsd_net_id);
5802 LIST_HEAD(reaplist);
5803
5804 if (!nfsd_netns_ready(nn))
5805 return count;
5806
5807 spin_lock(&nn->client_lock);
5808 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5809 if (mark_client_expired_locked(clp) == nfs_ok) {
5810 list_add(&clp->cl_lru, &reaplist);
5811 if (max != 0 && ++count >= max)
5812 break;
5813 }
5814 }
5815 spin_unlock(&nn->client_lock);
5816
5817 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
5818 expire_client(clp);
5819
5820 return count;
5821}
5822
Bryan Schumaker184c1842012-11-29 11:40:44 -05005823static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5824 const char *type)
5825{
5826 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005827 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005828 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5829}
5830
Jeff Layton016200c2014-07-30 08:27:21 -04005831static void
5832nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
5833 struct list_head *collect)
5834{
5835 struct nfs4_client *clp = lst->st_stid.sc_client;
5836 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5837 nfsd_net_id);
5838
5839 if (!collect)
5840 return;
5841
5842 lockdep_assert_held(&nn->client_lock);
5843 atomic_inc(&clp->cl_refcount);
5844 list_add(&lst->st_locks, collect);
5845}
5846
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005847static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04005848 struct list_head *collect,
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005849 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005850{
5851 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005852 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005853 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005854 u64 count = 0;
5855
Jeff Layton016200c2014-07-30 08:27:21 -04005856 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005857 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005858 list_for_each_entry_safe(stp, st_next,
5859 &oop->oo_owner.so_stateids, st_perstateowner) {
5860 list_for_each_entry_safe(lst, lst_next,
5861 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04005862 if (func) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005863 func(lst);
Jeff Layton016200c2014-07-30 08:27:21 -04005864 nfsd_inject_add_lock_to_list(lst,
5865 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04005866 }
Jeff Layton016200c2014-07-30 08:27:21 -04005867 ++count;
5868 /*
5869 * Despite the fact that these functions deal
5870 * with 64-bit integers for "count", we must
5871 * ensure that it doesn't blow up the
5872 * clp->cl_refcount. Throw a warning if we
5873 * start to approach INT_MAX here.
5874 */
5875 WARN_ON_ONCE(count == (INT_MAX / 2));
5876 if (count == max)
5877 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005878 }
5879 }
5880 }
Jeff Layton016200c2014-07-30 08:27:21 -04005881out:
5882 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005883
5884 return count;
5885}
5886
Jeff Layton016200c2014-07-30 08:27:21 -04005887static u64
5888nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
5889 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05005890{
Jeff Layton016200c2014-07-30 08:27:21 -04005891 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005892}
5893
Jeff Layton016200c2014-07-30 08:27:21 -04005894static u64
5895nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05005896{
Jeff Layton016200c2014-07-30 08:27:21 -04005897 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005898 nfsd_print_count(clp, count, "locked files");
5899 return count;
5900}
5901
Jeff Layton016200c2014-07-30 08:27:21 -04005902u64
Jeff Layton285abde2014-07-30 08:27:24 -04005903nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04005904{
5905 struct nfs4_client *clp;
5906 u64 count = 0;
5907 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5908 nfsd_net_id);
5909
5910 if (!nfsd_netns_ready(nn))
5911 return 0;
5912
5913 spin_lock(&nn->client_lock);
5914 list_for_each_entry(clp, &nn->client_lru, cl_lru)
5915 count += nfsd_print_client_locks(clp);
5916 spin_unlock(&nn->client_lock);
5917
5918 return count;
5919}
5920
5921static void
5922nfsd_reap_locks(struct list_head *reaplist)
5923{
5924 struct nfs4_client *clp;
5925 struct nfs4_ol_stateid *stp, *next;
5926
5927 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
5928 list_del_init(&stp->st_locks);
5929 clp = stp->st_stid.sc_client;
5930 nfs4_put_stid(&stp->st_stid);
5931 put_client(clp);
5932 }
5933}
5934
5935u64
Jeff Layton285abde2014-07-30 08:27:24 -04005936nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04005937{
5938 unsigned int count = 0;
5939 struct nfs4_client *clp;
5940 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5941 nfsd_net_id);
5942 LIST_HEAD(reaplist);
5943
5944 if (!nfsd_netns_ready(nn))
5945 return count;
5946
5947 spin_lock(&nn->client_lock);
5948 clp = nfsd_find_client(addr, addr_size);
5949 if (clp)
5950 count = nfsd_collect_client_locks(clp, &reaplist, 0);
5951 spin_unlock(&nn->client_lock);
5952 nfsd_reap_locks(&reaplist);
5953 return count;
5954}
5955
5956u64
Jeff Layton285abde2014-07-30 08:27:24 -04005957nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04005958{
5959 u64 count = 0;
5960 struct nfs4_client *clp;
5961 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5962 nfsd_net_id);
5963 LIST_HEAD(reaplist);
5964
5965 if (!nfsd_netns_ready(nn))
5966 return count;
5967
5968 spin_lock(&nn->client_lock);
5969 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5970 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
5971 if (max != 0 && count >= max)
5972 break;
5973 }
5974 spin_unlock(&nn->client_lock);
5975 nfsd_reap_locks(&reaplist);
5976 return count;
5977}
5978
Jeff Layton82e05ef2014-07-30 08:27:22 -04005979static u64
5980nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
5981 struct list_head *collect,
5982 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005983{
5984 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04005985 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5986 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005987 u64 count = 0;
5988
Jeff Layton82e05ef2014-07-30 08:27:22 -04005989 lockdep_assert_held(&nn->client_lock);
5990
5991 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005992 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04005993 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005994 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04005995 if (collect) {
5996 atomic_inc(&clp->cl_refcount);
5997 list_add(&oop->oo_perclient, collect);
5998 }
5999 }
6000 ++count;
6001 /*
6002 * Despite the fact that these functions deal with
6003 * 64-bit integers for "count", we must ensure that
6004 * it doesn't blow up the clp->cl_refcount. Throw a
6005 * warning if we start to approach INT_MAX here.
6006 */
6007 WARN_ON_ONCE(count == (INT_MAX / 2));
6008 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006009 break;
6010 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04006011 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006012
6013 return count;
6014}
6015
Jeff Layton82e05ef2014-07-30 08:27:22 -04006016static u64
6017nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006018{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006019 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6020
6021 nfsd_print_count(clp, count, "openowners");
6022 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006023}
6024
Jeff Layton82e05ef2014-07-30 08:27:22 -04006025static u64
6026nfsd_collect_client_openowners(struct nfs4_client *clp,
6027 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006028{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006029 return nfsd_foreach_client_openowner(clp, max, collect,
6030 unhash_openowner_locked);
6031}
6032
6033u64
Jeff Layton285abde2014-07-30 08:27:24 -04006034nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006035{
6036 struct nfs4_client *clp;
6037 u64 count = 0;
6038 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6039 nfsd_net_id);
6040
6041 if (!nfsd_netns_ready(nn))
6042 return 0;
6043
6044 spin_lock(&nn->client_lock);
6045 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6046 count += nfsd_print_client_openowners(clp);
6047 spin_unlock(&nn->client_lock);
6048
6049 return count;
6050}
6051
6052static void
6053nfsd_reap_openowners(struct list_head *reaplist)
6054{
6055 struct nfs4_client *clp;
6056 struct nfs4_openowner *oop, *next;
6057
6058 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6059 list_del_init(&oop->oo_perclient);
6060 clp = oop->oo_owner.so_client;
6061 release_openowner(oop);
6062 put_client(clp);
6063 }
6064}
6065
6066u64
Jeff Layton285abde2014-07-30 08:27:24 -04006067nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6068 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006069{
6070 unsigned int count = 0;
6071 struct nfs4_client *clp;
6072 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6073 nfsd_net_id);
6074 LIST_HEAD(reaplist);
6075
6076 if (!nfsd_netns_ready(nn))
6077 return count;
6078
6079 spin_lock(&nn->client_lock);
6080 clp = nfsd_find_client(addr, addr_size);
6081 if (clp)
6082 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6083 spin_unlock(&nn->client_lock);
6084 nfsd_reap_openowners(&reaplist);
6085 return count;
6086}
6087
6088u64
Jeff Layton285abde2014-07-30 08:27:24 -04006089nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006090{
6091 u64 count = 0;
6092 struct nfs4_client *clp;
6093 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6094 nfsd_net_id);
6095 LIST_HEAD(reaplist);
6096
6097 if (!nfsd_netns_ready(nn))
6098 return count;
6099
6100 spin_lock(&nn->client_lock);
6101 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6102 count += nfsd_collect_client_openowners(clp, &reaplist,
6103 max - count);
6104 if (max != 0 && count >= max)
6105 break;
6106 }
6107 spin_unlock(&nn->client_lock);
6108 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006109 return count;
6110}
6111
Bryan Schumaker269de302012-11-29 11:40:42 -05006112static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6113 struct list_head *victims)
6114{
6115 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006116 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6117 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006118 u64 count = 0;
6119
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006120 lockdep_assert_held(&nn->client_lock);
6121
6122 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006123 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006124 if (victims) {
6125 /*
6126 * It's not safe to mess with delegations that have a
6127 * non-zero dl_time. They might have already been broken
6128 * and could be processed by the laundromat outside of
6129 * the state_lock. Just leave them be.
6130 */
6131 if (dp->dl_time != 0)
6132 continue;
6133
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006134 atomic_inc(&clp->cl_refcount);
Jeff Layton42690672014-07-25 07:34:20 -04006135 unhash_delegation_locked(dp);
6136 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006137 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006138 ++count;
6139 /*
6140 * Despite the fact that these functions deal with
6141 * 64-bit integers for "count", we must ensure that
6142 * it doesn't blow up the clp->cl_refcount. Throw a
6143 * warning if we start to approach INT_MAX here.
6144 */
6145 WARN_ON_ONCE(count == (INT_MAX / 2));
6146 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006147 break;
6148 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006149 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006150 return count;
6151}
6152
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006153static u64
6154nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006155{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006156 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006157
6158 nfsd_print_count(clp, count, "delegations");
6159 return count;
6160}
6161
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006162u64
Jeff Layton285abde2014-07-30 08:27:24 -04006163nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006164{
6165 struct nfs4_client *clp;
6166 u64 count = 0;
6167 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6168 nfsd_net_id);
6169
6170 if (!nfsd_netns_ready(nn))
6171 return 0;
6172
6173 spin_lock(&nn->client_lock);
6174 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6175 count += nfsd_print_client_delegations(clp);
6176 spin_unlock(&nn->client_lock);
6177
6178 return count;
6179}
6180
6181static void
6182nfsd_forget_delegations(struct list_head *reaplist)
6183{
6184 struct nfs4_client *clp;
6185 struct nfs4_delegation *dp, *next;
6186
6187 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6188 list_del_init(&dp->dl_recall_lru);
6189 clp = dp->dl_stid.sc_client;
6190 revoke_delegation(dp);
6191 put_client(clp);
6192 }
6193}
6194
6195u64
Jeff Layton285abde2014-07-30 08:27:24 -04006196nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6197 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006198{
6199 u64 count = 0;
6200 struct nfs4_client *clp;
6201 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6202 nfsd_net_id);
6203 LIST_HEAD(reaplist);
6204
6205 if (!nfsd_netns_ready(nn))
6206 return count;
6207
6208 spin_lock(&nn->client_lock);
6209 clp = nfsd_find_client(addr, addr_size);
6210 if (clp)
6211 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6212 spin_unlock(&nn->client_lock);
6213
6214 nfsd_forget_delegations(&reaplist);
6215 return count;
6216}
6217
6218u64
Jeff Layton285abde2014-07-30 08:27:24 -04006219nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006220{
6221 u64 count = 0;
6222 struct nfs4_client *clp;
6223 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6224 nfsd_net_id);
6225 LIST_HEAD(reaplist);
6226
6227 if (!nfsd_netns_ready(nn))
6228 return count;
6229
6230 spin_lock(&nn->client_lock);
6231 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6232 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6233 if (max != 0 && count >= max)
6234 break;
6235 }
6236 spin_unlock(&nn->client_lock);
6237 nfsd_forget_delegations(&reaplist);
6238 return count;
6239}
6240
6241static void
6242nfsd_recall_delegations(struct list_head *reaplist)
6243{
6244 struct nfs4_client *clp;
6245 struct nfs4_delegation *dp, *next;
6246
6247 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6248 list_del_init(&dp->dl_recall_lru);
6249 clp = dp->dl_stid.sc_client;
6250 /*
6251 * We skipped all entries that had a zero dl_time before,
6252 * so we can now reset the dl_time back to 0. If a delegation
6253 * break comes in now, then it won't make any difference since
6254 * we're recalling it either way.
6255 */
6256 spin_lock(&state_lock);
6257 dp->dl_time = 0;
6258 spin_unlock(&state_lock);
6259 nfsd_break_one_deleg(dp);
6260 put_client(clp);
6261 }
6262}
6263
6264u64
Jeff Layton285abde2014-07-30 08:27:24 -04006265nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006266 size_t addr_size)
6267{
6268 u64 count = 0;
6269 struct nfs4_client *clp;
6270 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6271 nfsd_net_id);
6272 LIST_HEAD(reaplist);
6273
6274 if (!nfsd_netns_ready(nn))
6275 return count;
6276
6277 spin_lock(&nn->client_lock);
6278 clp = nfsd_find_client(addr, addr_size);
6279 if (clp)
6280 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6281 spin_unlock(&nn->client_lock);
6282
6283 nfsd_recall_delegations(&reaplist);
6284 return count;
6285}
6286
6287u64
Jeff Layton285abde2014-07-30 08:27:24 -04006288nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006289{
6290 u64 count = 0;
6291 struct nfs4_client *clp, *next;
6292 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6293 nfsd_net_id);
6294 LIST_HEAD(reaplist);
6295
6296 if (!nfsd_netns_ready(nn))
6297 return count;
6298
6299 spin_lock(&nn->client_lock);
6300 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6301 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6302 if (max != 0 && ++count >= max)
6303 break;
6304 }
6305 spin_unlock(&nn->client_lock);
6306 nfsd_recall_delegations(&reaplist);
6307 return count;
6308}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006309#endif /* CONFIG_NFSD_FAULT_INJECTION */
6310
Meelap Shahc2f1a552007-07-17 04:04:39 -07006311/*
6312 * Since the lifetime of a delegation isn't limited to that of an open, a
6313 * client may quite reasonably hang on to a delegation as long as it has
6314 * the inode cached. This becomes an obvious problem the first time a
6315 * client's inode cache approaches the size of the server's total memory.
6316 *
6317 * For now we avoid this problem by imposing a hard limit on the number
6318 * of delegations, which varies according to the server's memory size.
6319 */
6320static void
6321set_max_delegations(void)
6322{
6323 /*
6324 * Allow at most 4 delegations per megabyte of RAM. Quick
6325 * estimates suggest that in the worst case (where every delegation
6326 * is for a different inode), a delegation could take about 1.5K,
6327 * giving a worst case usage of about 6% of memory.
6328 */
6329 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6330}
6331
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006332static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006333{
6334 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6335 int i;
6336
6337 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6338 CLIENT_HASH_SIZE, GFP_KERNEL);
6339 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006340 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006341 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6342 CLIENT_HASH_SIZE, GFP_KERNEL);
6343 if (!nn->unconf_id_hashtbl)
6344 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006345 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6346 SESSION_HASH_SIZE, GFP_KERNEL);
6347 if (!nn->sessionid_hashtbl)
6348 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006349
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006350 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006351 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006352 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006353 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006354 for (i = 0; i < SESSION_HASH_SIZE; i++)
6355 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006356 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006357 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03006358 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03006359 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006360 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03006361 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006362
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006363 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006364 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006365
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006366 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006367
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006368err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03006369 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006370err_unconf_id:
6371 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006372err:
6373 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006374}
6375
6376static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006377nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006378{
6379 int i;
6380 struct nfs4_client *clp = NULL;
6381 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6382
6383 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6384 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6385 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6386 destroy_client(clp);
6387 }
6388 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006389
Kinglong Mee2b905632014-03-26 22:09:30 +08006390 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6391 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6392 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6393 destroy_client(clp);
6394 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006395 }
6396
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006397 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006398 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006399 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006400 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006401}
6402
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006403int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006404nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07006405{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006406 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006407 int ret;
6408
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006409 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006410 if (ret)
6411 return ret;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006412 nn->boot_time = get_seconds();
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04006413 nn->grace_ended = false;
Jeff Laytond4318ac2014-09-12 16:40:21 -04006414 locks_start_grace(net, &nn->nfsd4_manager);
6415 nfsd4_client_tracking_init(net);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006416 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03006417 nn->nfsd4_grace, net);
6418 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006419 return 0;
6420}
6421
6422/* initialization to perform when the nfsd service is started: */
6423
6424int
6425nfs4_state_start(void)
6426{
6427 int ret;
6428
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006429 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006430 if (ret)
6431 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07006432 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05006433 if (laundry_wq == NULL) {
6434 ret = -ENOMEM;
6435 goto out_recovery;
6436 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006437 ret = nfsd4_create_callback_queue();
6438 if (ret)
6439 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006440
Meelap Shahc2f1a552007-07-17 04:04:39 -07006441 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006442
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006443 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006444
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006445out_free_laundry:
6446 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05006447out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006448 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449}
6450
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006451void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006452nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006456 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006458 cancel_delayed_work_sync(&nn->laundromat_work);
6459 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05006460
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04006462 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006463 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006465 unhash_delegation_locked(dp);
6466 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467 }
Benny Halevycdc975052014-05-30 09:09:30 -04006468 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469 list_for_each_safe(pos, next, &reaplist) {
6470 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006471 list_del_init(&dp->dl_recall_lru);
Jeff Laytonafbda402014-08-09 10:22:41 -04006472 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04006473 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474 }
6475
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006476 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006477 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478}
6479
6480void
6481nfs4_state_shutdown(void)
6482{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006483 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006484 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006486
6487static void
6488get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6489{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006490 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6491 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006492}
6493
6494static void
6495put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6496{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006497 if (cstate->minorversion) {
6498 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6499 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6500 }
6501}
6502
6503void
6504clear_current_stateid(struct nfsd4_compound_state *cstate)
6505{
6506 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006507}
6508
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006509/*
6510 * functions to set current state id
6511 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006512void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006513nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6514{
6515 put_stateid(cstate, &odp->od_stateid);
6516}
6517
6518void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006519nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6520{
6521 put_stateid(cstate, &open->op_stateid);
6522}
6523
6524void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006525nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6526{
6527 put_stateid(cstate, &close->cl_stateid);
6528}
6529
6530void
6531nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6532{
6533 put_stateid(cstate, &lock->lk_resp_stateid);
6534}
6535
6536/*
6537 * functions to consume current state id
6538 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006539
6540void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006541nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6542{
6543 get_stateid(cstate, &odp->od_stateid);
6544}
6545
6546void
6547nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6548{
6549 get_stateid(cstate, &drp->dr_stateid);
6550}
6551
6552void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006553nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6554{
6555 get_stateid(cstate, &fsp->fr_stateid);
6556}
6557
6558void
6559nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6560{
6561 get_stateid(cstate, &setattr->sa_stateid);
6562}
6563
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006564void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006565nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6566{
6567 get_stateid(cstate, &close->cl_stateid);
6568}
6569
6570void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006571nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006572{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006573 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006574}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006575
6576void
6577nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6578{
6579 get_stateid(cstate, &read->rd_stateid);
6580}
6581
6582void
6583nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6584{
6585 get_stateid(cstate, &write->wr_stateid);
6586}