blob: 52a4677f6f35a05a3c1fe1c2aa92e611b8302b85 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
Dave Hansenaceaf782008-02-15 14:37:31 -080035#include <linux/file.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020036#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070038#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070039#include <linux/swap.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
Chuck Lever7df302f2012-05-29 13:56:37 -040041#include <linux/ratelimit.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050042#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton59766872013-02-04 12:50:00 -050043#include <linux/sunrpc/addr.h>
NeilBrown6282cd52014-06-04 17:39:26 +100044#include <linux/hash.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020045#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040046#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050047#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040048#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040050#include "netns.h"
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define NFSDDBG_FACILITY NFSDDBG_PROC
53
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050054#define all_ones {{~0,~0},~0}
55static const stateid_t one_stateid = {
56 .si_generation = ~0,
57 .si_opaque = all_ones,
58};
59static const stateid_t zero_stateid = {
60 /* all fields zero */
61};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010062static const stateid_t currentstateid = {
63 .si_generation = 1,
64};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050065
Andy Adamsonec6b5d72009-04-03 08:28:28 +030066static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070067
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050068#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
69#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010070#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040073static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040074static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
J. Bruce Fields8b671b82009-02-22 14:51:34 -080076/* Locking: */
77
78/* Currently used for almost all code touching nfsv4 state: */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080079static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
J. Bruce Fields8b671b82009-02-22 14:51:34 -080081/*
82 * Currently used for the del_recall_lru and file hash table. In an
83 * effort to decrease the scope of the client_mutex, this spinlock may
84 * eventually cover more:
85 */
Benny Halevycdc97502014-05-30 09:09:30 -040086static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080087
Jeff Laytonb401be222014-07-29 21:34:33 -040088/*
89 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
90 * the refcount on the open stateid to drop.
91 */
92static DECLARE_WAIT_QUEUE_HEAD(close_wq);
93
Christoph Hellwigabf11352014-05-21 07:43:03 -070094static struct kmem_cache *openowner_slab;
95static struct kmem_cache *lockowner_slab;
96static struct kmem_cache *file_slab;
97static struct kmem_cache *stateid_slab;
98static struct kmem_cache *deleg_slab;
NeilBrowne60d4392005-06-23 22:03:01 -070099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100void
101nfs4_lock_state(void)
102{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800103 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400106static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800107
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400108static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800109{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400110 return ses->se_flags & NFS4_SESSION_DEAD;
111}
112
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400113static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
114{
115 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400116 return nfserr_jukebox;
117 ses->se_flags |= NFS4_SESSION_DEAD;
118 return nfs_ok;
119}
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121void
122nfs4_unlock_state(void)
123{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800124 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
J. Bruce Fields221a6872013-04-01 22:23:49 -0400127static bool is_client_expired(struct nfs4_client *clp)
128{
129 return clp->cl_time == 0;
130}
131
132static __be32 mark_client_expired_locked(struct nfs4_client *clp)
133{
134 if (atomic_read(&clp->cl_refcount))
135 return nfserr_jukebox;
136 clp->cl_time = 0;
137 return nfs_ok;
138}
139
J. Bruce Fields221a6872013-04-01 22:23:49 -0400140static __be32 get_client_locked(struct nfs4_client *clp)
141{
142 if (is_client_expired(clp))
143 return nfserr_expired;
144 atomic_inc(&clp->cl_refcount);
145 return nfs_ok;
146}
147
148/* must be called under the client_lock */
149static inline void
150renew_client_locked(struct nfs4_client *clp)
151{
152 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
153
154 if (is_client_expired(clp)) {
155 WARN_ON(1);
156 printk("%s: client (clientid %08x/%08x) already expired\n",
157 __func__,
158 clp->cl_clientid.cl_boot,
159 clp->cl_clientid.cl_id);
160 return;
161 }
162
163 dprintk("renewing client (clientid %08x/%08x)\n",
164 clp->cl_clientid.cl_boot,
165 clp->cl_clientid.cl_id);
166 list_move_tail(&clp->cl_lru, &nn->client_lru);
167 clp->cl_time = get_seconds();
168}
169
170static inline void
171renew_client(struct nfs4_client *clp)
172{
173 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
174
175 spin_lock(&nn->client_lock);
176 renew_client_locked(clp);
177 spin_unlock(&nn->client_lock);
178}
179
Fengguang Wuba138432013-04-16 22:14:15 -0400180static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400181{
182 if (!atomic_dec_and_test(&clp->cl_refcount))
183 return;
184 if (!is_client_expired(clp))
185 renew_client_locked(clp);
186}
187
Jeff Layton4b24ca72014-06-30 11:48:44 -0400188static void put_client_renew(struct nfs4_client *clp)
189{
190 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
191
Jeff Laytond6c249b2014-07-08 14:02:50 -0400192 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
193 return;
194 if (!is_client_expired(clp))
195 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400196 spin_unlock(&nn->client_lock);
197}
198
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400199static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
200{
201 __be32 status;
202
203 if (is_session_dead(ses))
204 return nfserr_badsession;
205 status = get_client_locked(ses->se_client);
206 if (status)
207 return status;
208 atomic_inc(&ses->se_ref);
209 return nfs_ok;
210}
211
212static void nfsd4_put_session_locked(struct nfsd4_session *ses)
213{
214 struct nfs4_client *clp = ses->se_client;
215
216 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
217 free_session(ses);
218 put_client_renew_locked(clp);
219}
220
221static void nfsd4_put_session(struct nfsd4_session *ses)
222{
223 struct nfs4_client *clp = ses->se_client;
224 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
225
226 spin_lock(&nn->client_lock);
227 nfsd4_put_session_locked(ses);
228 spin_unlock(&nn->client_lock);
229}
230
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400231static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400232same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400233{
234 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400235 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400236}
237
238static struct nfs4_openowner *
239find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400240 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400241{
242 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400243
Trond Myklebustd4f04892014-07-29 21:34:36 -0400244 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400245
Trond Myklebustd4f04892014-07-29 21:34:36 -0400246 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
247 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400248 if (!so->so_is_open_owner)
249 continue;
Trond Myklebustd4f04892014-07-29 21:34:36 -0400250 if (same_owner_str(so, &open->op_owner)) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400251 atomic_inc(&so->so_count);
Trond Myklebustd4f04892014-07-29 21:34:36 -0400252 return openowner(so);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400253 }
254 }
255 return NULL;
256}
257
258static struct nfs4_openowner *
259find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400260 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400261{
262 struct nfs4_openowner *oo;
263
Trond Myklebustd4f04892014-07-29 21:34:36 -0400264 spin_lock(&clp->cl_lock);
265 oo = find_openstateowner_str_locked(hashval, open, clp);
266 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400267 return oo;
268}
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270static inline u32
271opaque_hashval(const void *ptr, int nbytes)
272{
273 unsigned char *cptr = (unsigned char *) ptr;
274
275 u32 x = 0;
276 while (nbytes--) {
277 x *= 37;
278 x += *cptr++;
279 }
280 return x;
281}
282
J. Bruce Fields32513b42011-10-13 16:00:16 -0400283static void nfsd4_free_file(struct nfs4_file *f)
284{
285 kmem_cache_free(file_slab, f);
286}
287
NeilBrown13cd2182005-06-23 22:03:10 -0700288static inline void
289put_nfs4_file(struct nfs4_file *fi)
290{
Jeff Layton02e12152014-07-16 10:31:57 -0400291 might_lock(&state_lock);
292
Benny Halevycdc97502014-05-30 09:09:30 -0400293 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton89876f82013-04-02 09:01:59 -0400294 hlist_del(&fi->fi_hash);
Benny Halevycdc97502014-05-30 09:09:30 -0400295 spin_unlock(&state_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400296 nfsd4_free_file(fi);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800297 }
NeilBrown13cd2182005-06-23 22:03:10 -0700298}
299
300static inline void
301get_nfs4_file(struct nfs4_file *fi)
302{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800303 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700304}
305
Trond Myklebustde186432014-07-10 14:07:26 -0400306static struct file *
307__nfs4_get_fd(struct nfs4_file *f, int oflag)
308{
309 if (f->fi_fds[oflag])
310 return get_file(f->fi_fds[oflag]);
311 return NULL;
312}
313
314static struct file *
315find_writeable_file_locked(struct nfs4_file *f)
316{
317 struct file *ret;
318
319 lockdep_assert_held(&f->fi_lock);
320
321 ret = __nfs4_get_fd(f, O_WRONLY);
322 if (!ret)
323 ret = __nfs4_get_fd(f, O_RDWR);
324 return ret;
325}
326
327static struct file *
328find_writeable_file(struct nfs4_file *f)
329{
330 struct file *ret;
331
332 spin_lock(&f->fi_lock);
333 ret = find_writeable_file_locked(f);
334 spin_unlock(&f->fi_lock);
335
336 return ret;
337}
338
339static struct file *find_readable_file_locked(struct nfs4_file *f)
340{
341 struct file *ret;
342
343 lockdep_assert_held(&f->fi_lock);
344
345 ret = __nfs4_get_fd(f, O_RDONLY);
346 if (!ret)
347 ret = __nfs4_get_fd(f, O_RDWR);
348 return ret;
349}
350
351static struct file *
352find_readable_file(struct nfs4_file *f)
353{
354 struct file *ret;
355
356 spin_lock(&f->fi_lock);
357 ret = find_readable_file_locked(f);
358 spin_unlock(&f->fi_lock);
359
360 return ret;
361}
362
363static struct file *
364find_any_file(struct nfs4_file *f)
365{
366 struct file *ret;
367
368 spin_lock(&f->fi_lock);
369 ret = __nfs4_get_fd(f, O_RDWR);
370 if (!ret) {
371 ret = __nfs4_get_fd(f, O_WRONLY);
372 if (!ret)
373 ret = __nfs4_get_fd(f, O_RDONLY);
374 }
375 spin_unlock(&f->fi_lock);
376 return ret;
377}
378
Trond Myklebust02a35082014-07-25 07:34:22 -0400379static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800380unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700381
382/*
383 * Open owner state (share locks)
384 */
385
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500386/* hash tables for lock and open owners */
387#define OWNER_HASH_BITS 8
388#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
389#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700390
Trond Myklebustd4f04892014-07-29 21:34:36 -0400391static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400392{
393 unsigned int ret;
394
395 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500396 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400397}
NeilBrownef0f3392006-04-10 22:55:41 -0700398
NeilBrownef0f3392006-04-10 22:55:41 -0700399/* hash table for nfs4_file */
400#define FILE_HASH_BITS 8
401#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800402
Trond Myklebustca943212014-07-23 16:17:39 -0400403static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400404{
Trond Myklebustca943212014-07-23 16:17:39 -0400405 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
406}
407
408static unsigned int file_hashval(struct knfsd_fh *fh)
409{
410 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
411}
412
413static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
414{
415 return fh1->fh_size == fh2->fh_size &&
416 !memcmp(fh1->fh_base.fh_pad,
417 fh2->fh_base.fh_pad,
418 fh1->fh_size);
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400419}
420
Jeff Layton89876f82013-04-02 09:01:59 -0400421static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700422
Jeff Layton12659652014-07-10 14:07:28 -0400423static void
424__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400425{
Jeff Layton7214e862014-07-10 14:07:33 -0400426 lockdep_assert_held(&fp->fi_lock);
427
Jeff Layton12659652014-07-10 14:07:28 -0400428 if (access & NFS4_SHARE_ACCESS_WRITE)
429 atomic_inc(&fp->fi_access[O_WRONLY]);
430 if (access & NFS4_SHARE_ACCESS_READ)
431 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400432}
433
Jeff Layton12659652014-07-10 14:07:28 -0400434static __be32
435nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400436{
Jeff Layton7214e862014-07-10 14:07:33 -0400437 lockdep_assert_held(&fp->fi_lock);
438
Jeff Layton12659652014-07-10 14:07:28 -0400439 /* Does this access mode make sense? */
440 if (access & ~NFS4_SHARE_ACCESS_BOTH)
441 return nfserr_inval;
442
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400443 /* Does it conflict with a deny mode already set? */
444 if ((access & fp->fi_share_deny) != 0)
445 return nfserr_share_denied;
446
Jeff Layton12659652014-07-10 14:07:28 -0400447 __nfs4_file_get_access(fp, access);
448 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400449}
450
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400451static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
452{
453 /* Common case is that there is no deny mode. */
454 if (deny) {
455 /* Does this deny mode make sense? */
456 if (deny & ~NFS4_SHARE_DENY_BOTH)
457 return nfserr_inval;
458
459 if ((deny & NFS4_SHARE_DENY_READ) &&
460 atomic_read(&fp->fi_access[O_RDONLY]))
461 return nfserr_share_denied;
462
463 if ((deny & NFS4_SHARE_DENY_WRITE) &&
464 atomic_read(&fp->fi_access[O_WRONLY]))
465 return nfserr_share_denied;
466 }
467 return nfs_ok;
468}
469
J. Bruce Fields998db522010-08-07 09:21:41 -0400470static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400471{
Trond Myklebustde186432014-07-10 14:07:26 -0400472 might_lock(&fp->fi_lock);
473
474 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
475 struct file *f1 = NULL;
476 struct file *f2 = NULL;
477
Jeff Layton6d338b52014-07-10 14:07:29 -0400478 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400479 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400480 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400481 spin_unlock(&fp->fi_lock);
482 if (f1)
483 fput(f1);
484 if (f2)
485 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400486 }
487}
488
Jeff Layton12659652014-07-10 14:07:28 -0400489static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400490{
Jeff Layton12659652014-07-10 14:07:28 -0400491 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
492
493 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400494 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400495 if (access & NFS4_SHARE_ACCESS_READ)
496 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400497}
498
Trond Myklebust60116952014-07-29 21:34:06 -0400499static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
500 struct kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400501{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500502 struct nfs4_stid *stid;
503 int new_id;
504
Trond Myklebustf8338832014-07-25 07:34:19 -0400505 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500506 if (!stid)
507 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400508
Jeff Layton4770d722014-07-29 21:34:10 -0400509 idr_preload(GFP_KERNEL);
510 spin_lock(&cl->cl_lock);
511 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
512 spin_unlock(&cl->cl_lock);
513 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700514 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500515 goto out_free;
516 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500517 stid->sc_stateid.si_opaque.so_id = new_id;
518 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
519 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400520 atomic_set(&stid->sc_count, 1);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500521
J. Bruce Fields996e0932011-10-17 11:14:48 -0400522 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500523 * It shouldn't be a problem to reuse an opaque stateid value.
524 * I don't think it is for 4.1. But with 4.0 I worry that, for
525 * example, a stray write retransmission could be accepted by
526 * the server when it should have been rejected. Therefore,
527 * adopt a trick from the sctp code to attempt to maximize the
528 * amount of time until an id is reused, by ensuring they always
529 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400530 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500531 return stid;
532out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800533 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500534 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400535}
536
Jeff Laytonb49e0842014-07-29 21:34:11 -0400537static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400538{
Trond Myklebust60116952014-07-29 21:34:06 -0400539 struct nfs4_stid *stid;
540 struct nfs4_ol_stateid *stp;
541
542 stid = nfs4_alloc_stid(clp, stateid_slab);
543 if (!stid)
544 return NULL;
545
546 stp = openlockstateid(stid);
547 stp->st_stid.sc_free = nfs4_free_ol_stateid;
548 return stp;
549}
550
551static void nfs4_free_deleg(struct nfs4_stid *stid)
552{
Trond Myklebust60116952014-07-29 21:34:06 -0400553 kmem_cache_free(deleg_slab, stid);
554 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400555}
556
NeilBrown6282cd52014-06-04 17:39:26 +1000557/*
558 * When we recall a delegation, we should be careful not to hand it
559 * out again straight away.
560 * To ensure this we keep a pair of bloom filters ('new' and 'old')
561 * in which the filehandles of recalled delegations are "stored".
562 * If a filehandle appear in either filter, a delegation is blocked.
563 * When a delegation is recalled, the filehandle is stored in the "new"
564 * filter.
565 * Every 30 seconds we swap the filters and clear the "new" one,
566 * unless both are empty of course.
567 *
568 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
569 * low 3 bytes as hash-table indices.
570 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400571 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000572 * is used to manage concurrent access. Testing does not need the lock
573 * except when swapping the two filters.
574 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400575static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000576static struct bloom_pair {
577 int entries, old_entries;
578 time_t swap_time;
579 int new; /* index into 'set' */
580 DECLARE_BITMAP(set[2], 256);
581} blocked_delegations;
582
583static int delegation_blocked(struct knfsd_fh *fh)
584{
585 u32 hash;
586 struct bloom_pair *bd = &blocked_delegations;
587
588 if (bd->entries == 0)
589 return 0;
590 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400591 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000592 if (seconds_since_boot() - bd->swap_time > 30) {
593 bd->entries -= bd->old_entries;
594 bd->old_entries = bd->entries;
595 memset(bd->set[bd->new], 0,
596 sizeof(bd->set[0]));
597 bd->new = 1-bd->new;
598 bd->swap_time = seconds_since_boot();
599 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400600 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000601 }
602 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
603 if (test_bit(hash&255, bd->set[0]) &&
604 test_bit((hash>>8)&255, bd->set[0]) &&
605 test_bit((hash>>16)&255, bd->set[0]))
606 return 1;
607
608 if (test_bit(hash&255, bd->set[1]) &&
609 test_bit((hash>>8)&255, bd->set[1]) &&
610 test_bit((hash>>16)&255, bd->set[1]))
611 return 1;
612
613 return 0;
614}
615
616static void block_delegations(struct knfsd_fh *fh)
617{
618 u32 hash;
619 struct bloom_pair *bd = &blocked_delegations;
620
621 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
622
Jeff Laytonf54fe962014-07-25 07:34:26 -0400623 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000624 __set_bit(hash&255, bd->set[bd->new]);
625 __set_bit((hash>>8)&255, bd->set[bd->new]);
626 __set_bit((hash>>16)&255, bd->set[bd->new]);
627 if (bd->entries == 0)
628 bd->swap_time = seconds_since_boot();
629 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400630 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000631}
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633static struct nfs4_delegation *
Jeff Laytonf9416e22014-07-25 07:34:23 -0400634alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400637 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400640 n = atomic_long_inc_return(&num_delegations);
641 if (n < 0 || n > max_delegations)
642 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000643 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400644 goto out_dec;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400645 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700646 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400647 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400648
649 dp->dl_stid.sc_free = nfs4_free_deleg;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400650 /*
651 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400652 * meaning of seqid 0 isn't meaningful, really, but let's avoid
653 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400654 */
655 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700656 INIT_LIST_HEAD(&dp->dl_perfile);
657 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 INIT_LIST_HEAD(&dp->dl_recall_lru);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400659 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Jeff Layton02e12152014-07-16 10:31:57 -0400660 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400662out_dec:
663 atomic_long_dec(&num_delegations);
664 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
667void
Trond Myklebust60116952014-07-29 21:34:06 -0400668nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Trond Myklebust11b91642014-07-29 21:34:08 -0400670 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400671 struct nfs4_client *clp = s->sc_client;
672
Jeff Layton4770d722014-07-29 21:34:10 -0400673 might_lock(&clp->cl_lock);
674
Jeff Laytonb401be222014-07-29 21:34:33 -0400675 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
676 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400677 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400678 }
Trond Myklebust60116952014-07-29 21:34:06 -0400679 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400680 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400681 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400682 if (fp)
683 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500686static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Jeff Layton417c6622014-07-21 09:34:57 -0400688 lockdep_assert_held(&state_lock);
689
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -0500690 if (!fp->fi_lease)
691 return;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500692 if (atomic_dec_and_test(&fp->fi_delegees)) {
693 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
694 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400695 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500696 fp->fi_deleg_file = NULL;
697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400700static void unhash_stid(struct nfs4_stid *s)
701{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500702 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400703}
704
Benny Halevy931ee562014-05-30 09:09:27 -0400705static void
706hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
707{
Benny Halevycdc97502014-05-30 09:09:30 -0400708 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400709 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400710
Trond Myklebust67cb1272014-07-29 21:34:17 -0400711 atomic_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400712 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400713 list_add(&dp->dl_perfile, &fp->fi_delegations);
714 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
715}
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717static void
Jeff Layton42690672014-07-25 07:34:20 -0400718unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Trond Myklebust11b91642014-07-29 21:34:08 -0400720 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400721
Jeff Layton42690672014-07-25 07:34:20 -0400722 lockdep_assert_held(&state_lock);
723
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400724 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400725 /* Ensure that deleg break won't try to requeue it */
726 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400727 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400728 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400730 list_del_init(&dp->dl_perfile);
731 spin_unlock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400732 if (fp)
Trond Myklebustf8338832014-07-25 07:34:19 -0400733 nfs4_put_deleg_lease(fp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400734}
735
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400736static void destroy_delegation(struct nfs4_delegation *dp)
737{
Jeff Layton42690672014-07-25 07:34:20 -0400738 spin_lock(&state_lock);
739 unhash_delegation_locked(dp);
740 spin_unlock(&state_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400741 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400742}
743
744static void revoke_delegation(struct nfs4_delegation *dp)
745{
746 struct nfs4_client *clp = dp->dl_stid.sc_client;
747
Jeff Layton2d4a5322014-07-25 07:34:21 -0400748 WARN_ON(!list_empty(&dp->dl_recall_lru));
749
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400750 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400751 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400752 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400753 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400754 spin_lock(&clp->cl_lock);
755 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
756 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400757 }
758}
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760/*
761 * SETCLIENTID state
762 */
763
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400764static unsigned int clientid_hashval(u32 id)
765{
766 return id & CLIENT_HASH_MASK;
767}
768
769static unsigned int clientstr_hashval(const char *name)
770{
771 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
772}
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400775 * We store the NONE, READ, WRITE, and BOTH bits separately in the
776 * st_{access,deny}_bmap field of the stateid, in order to track not
777 * only what share bits are currently in force, but also what
778 * combinations of share bits previous opens have used. This allows us
779 * to enforce the recommendation of rfc 3530 14.2.19 that the server
780 * return an error if the client attempt to downgrade to a combination
781 * of share bits not explicable by closing some of its previous opens.
782 *
783 * XXX: This enforcement is actually incomplete, since we don't keep
784 * track of access/deny bit combinations; so, e.g., we allow:
785 *
786 * OPEN allow read, deny write
787 * OPEN allow both, deny none
788 * DOWNGRADE allow read, deny none
789 *
790 * which we should reject.
791 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400792static unsigned int
793bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400794 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400795 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400796
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400797 for (i = 1; i < 4; i++) {
798 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400799 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400800 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400801 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400802}
803
Jeff Layton82c5ff12012-05-11 09:45:13 -0400804/* set share access for a given stateid */
805static inline void
806set_access(u32 access, struct nfs4_ol_stateid *stp)
807{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400808 unsigned char mask = 1 << access;
809
810 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
811 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400812}
813
814/* clear share access for a given stateid */
815static inline void
816clear_access(u32 access, struct nfs4_ol_stateid *stp)
817{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400818 unsigned char mask = 1 << access;
819
820 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
821 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400822}
823
824/* test whether a given stateid has access */
825static inline bool
826test_access(u32 access, struct nfs4_ol_stateid *stp)
827{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400828 unsigned char mask = 1 << access;
829
830 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400831}
832
Jeff Laytonce0fc432012-05-11 09:45:14 -0400833/* set share deny for a given stateid */
834static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400835set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400836{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400837 unsigned char mask = 1 << deny;
838
839 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
840 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400841}
842
843/* clear share deny for a given stateid */
844static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400845clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400846{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400847 unsigned char mask = 1 << deny;
848
849 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
850 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400851}
852
853/* test whether a given stateid is denying specific access */
854static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400855test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400856{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400857 unsigned char mask = 1 << deny;
858
859 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400860}
861
862static int nfs4_access_to_omode(u32 access)
863{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400864 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400865 case NFS4_SHARE_ACCESS_READ:
866 return O_RDONLY;
867 case NFS4_SHARE_ACCESS_WRITE:
868 return O_WRONLY;
869 case NFS4_SHARE_ACCESS_BOTH:
870 return O_RDWR;
871 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500872 WARN_ON_ONCE(1);
873 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400874}
875
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400876/*
877 * A stateid that had a deny mode associated with it is being released
878 * or downgraded. Recalculate the deny mode on the file.
879 */
880static void
881recalculate_deny_mode(struct nfs4_file *fp)
882{
883 struct nfs4_ol_stateid *stp;
884
885 spin_lock(&fp->fi_lock);
886 fp->fi_share_deny = 0;
887 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
888 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
889 spin_unlock(&fp->fi_lock);
890}
891
892static void
893reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
894{
895 int i;
896 bool change = false;
897
898 for (i = 1; i < 4; i++) {
899 if ((i & deny) != i) {
900 change = true;
901 clear_deny(i, stp);
902 }
903 }
904
905 /* Recalculate per-file deny mode if there was a change */
906 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -0400907 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400908}
909
Jeff Layton82c5ff12012-05-11 09:45:13 -0400910/* release all access and file references for a given stateid */
911static void
912release_all_access(struct nfs4_ol_stateid *stp)
913{
914 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -0400915 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400916
917 if (fp && stp->st_deny_bmap != 0)
918 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400919
920 for (i = 1; i < 4; i++) {
921 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -0400922 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400923 clear_access(i, stp);
924 }
925}
926
Jeff Layton6b180f02014-07-29 21:34:26 -0400927static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
928{
Jeff Laytona819ecc2014-07-29 21:34:38 -0400929 struct nfs4_client *clp = sop->so_client;
930
931 might_lock(&clp->cl_lock);
932
933 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -0400934 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -0400935 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -0400936 spin_unlock(&clp->cl_lock);
Jeff Layton6b180f02014-07-29 21:34:26 -0400937 kfree(sop->so_owner.data);
938 sop->so_ops->so_free(sop);
939}
940
Jeff Layton4ae098d2014-07-29 21:34:43 -0400941static void unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500942{
Trond Myklebust11b91642014-07-29 21:34:08 -0400943 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -0400944
Jeff Layton1c755dc2014-07-29 21:34:12 -0400945 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
946
Trond Myklebust1d31a252014-07-10 14:07:25 -0400947 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500948 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400949 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500950 list_del(&stp->st_perstateowner);
951}
952
Trond Myklebust60116952014-07-29 21:34:06 -0400953static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500954{
Trond Myklebust60116952014-07-29 21:34:06 -0400955 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400956
Trond Myklebust60116952014-07-29 21:34:06 -0400957 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -0400958 if (stp->st_stateowner)
959 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -0400960 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500961}
962
Jeff Laytonb49e0842014-07-29 21:34:11 -0400963static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500964{
Jeff Laytonb49e0842014-07-29 21:34:11 -0400965 struct nfs4_ol_stateid *stp = openlockstateid(stid);
966 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500967 struct file *file;
968
Jeff Laytonb49e0842014-07-29 21:34:11 -0400969 file = find_any_file(stp->st_stid.sc_file);
970 if (file)
971 filp_close(file, (fl_owner_t)lo);
972 nfs4_free_ol_stateid(stid);
973}
974
Jeff Layton2c41beb2014-07-29 21:34:41 -0400975/*
976 * Put the persistent reference to an already unhashed generic stateid, while
977 * holding the cl_lock. If it's the last reference, then put it onto the
978 * reaplist for later destruction.
979 */
980static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
981 struct list_head *reaplist)
982{
983 struct nfs4_stid *s = &stp->st_stid;
984 struct nfs4_client *clp = s->sc_client;
985
986 lockdep_assert_held(&clp->cl_lock);
987
988 WARN_ON_ONCE(!list_empty(&stp->st_locks));
989
990 if (!atomic_dec_and_test(&s->sc_count)) {
991 wake_up_all(&close_wq);
992 return;
993 }
994
995 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
996 list_add(&stp->st_locks, reaplist);
997}
998
Jeff Layton3c1c9952014-07-29 21:34:39 -0400999static void unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1000{
1001 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1002
1003 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
1004
1005 list_del_init(&stp->st_locks);
Jeff Layton4ae098d2014-07-29 21:34:43 -04001006 unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001007 unhash_stid(&stp->st_stid);
1008}
1009
Jeff Layton5adfd882014-07-29 21:34:31 -04001010static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001011{
Jeff Layton1c755dc2014-07-29 21:34:12 -04001012 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1013
1014 spin_lock(&oo->oo_owner.so_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001015 unhash_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001016 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04001017 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001018}
1019
Trond Myklebustc58c6612014-07-29 21:34:35 -04001020static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001021{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001022 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001023
Trond Myklebustd4f04892014-07-29 21:34:36 -04001024 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001025
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001026 list_del_init(&lo->lo_owner.so_strhash);
1027}
1028
Jeff Layton2c41beb2014-07-29 21:34:41 -04001029/*
1030 * Free a list of generic stateids that were collected earlier after being
1031 * fully unhashed.
1032 */
1033static void
1034free_ol_stateid_reaplist(struct list_head *reaplist)
1035{
1036 struct nfs4_ol_stateid *stp;
1037
1038 might_sleep();
1039
1040 while (!list_empty(reaplist)) {
1041 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1042 st_locks);
1043 list_del(&stp->st_locks);
1044 stp->st_stid.sc_free(&stp->st_stid);
1045 }
1046}
1047
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001048static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001049{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001050 struct nfs4_client *clp = lo->lo_owner.so_client;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001051 struct nfs4_ol_stateid *stp;
1052 struct list_head reaplist;
1053
1054 INIT_LIST_HEAD(&reaplist);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001055
Trond Myklebustd4f04892014-07-29 21:34:36 -04001056 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001057 unhash_lockowner_locked(lo);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001058 while (!list_empty(&lo->lo_owner.so_stateids)) {
1059 stp = list_first_entry(&lo->lo_owner.so_stateids,
1060 struct nfs4_ol_stateid, st_perstateowner);
1061 unhash_lock_stateid(stp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001062 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001063 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001064 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001065 free_ol_stateid_reaplist(&reaplist);
Jeff Layton6b180f02014-07-29 21:34:26 -04001066 nfs4_put_stateowner(&lo->lo_owner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001067}
1068
Jeff Laytond83017f2014-07-29 21:34:42 -04001069static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1070 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001071{
1072 struct nfs4_ol_stateid *stp;
1073
1074 while (!list_empty(&open_stp->st_locks)) {
1075 stp = list_entry(open_stp->st_locks.next,
1076 struct nfs4_ol_stateid, st_locks);
Jeff Laytond83017f2014-07-29 21:34:42 -04001077 unhash_lock_stateid(stp);
1078 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001079 }
1080}
1081
Jeff Laytond83017f2014-07-29 21:34:42 -04001082static void unhash_open_stateid(struct nfs4_ol_stateid *stp,
1083 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001084{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001085 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1086
Jeff Layton4ae098d2014-07-29 21:34:43 -04001087 unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001088 release_open_stateid_locks(stp, reaplist);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001089}
1090
1091static void release_open_stateid(struct nfs4_ol_stateid *stp)
1092{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001093 LIST_HEAD(reaplist);
1094
1095 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04001096 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001097 put_ol_stateid_locked(stp, &reaplist);
1098 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1099 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001100}
1101
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001102static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001103{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001104 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001105
Trond Myklebustd4f04892014-07-29 21:34:36 -04001106 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001107
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001108 list_del_init(&oo->oo_owner.so_strhash);
1109 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001110}
1111
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001112static void release_last_closed_stateid(struct nfs4_openowner *oo)
1113{
1114 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
1115
1116 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001117 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001118 oo->oo_last_closed_stid = NULL;
Jeff Laytond3134b12014-07-29 21:34:32 -04001119 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001120 }
1121}
1122
Jeff Layton2c41beb2014-07-29 21:34:41 -04001123static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001124{
1125 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001126 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001127 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001128
Jeff Layton2c41beb2014-07-29 21:34:41 -04001129 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001130
Trond Myklebustd4f04892014-07-29 21:34:36 -04001131 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001132 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001133 while (!list_empty(&oo->oo_owner.so_stateids)) {
1134 stp = list_first_entry(&oo->oo_owner.so_stateids,
1135 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytond83017f2014-07-29 21:34:42 -04001136 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001137 put_ol_stateid_locked(stp, &reaplist);
1138 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001139 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001140 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001141 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001142 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001143}
1144
Marc Eshel5282fd72009-04-03 08:27:52 +03001145static inline int
1146hash_sessionid(struct nfs4_sessionid *sessionid)
1147{
1148 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1149
1150 return sid->sequence % SESSION_HASH_SIZE;
1151}
1152
Trond Myklebust8f199b82012-03-20 15:11:17 -04001153#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001154static inline void
1155dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1156{
1157 u32 *ptr = (u32 *)(&sessionid->data[0]);
1158 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1159}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001160#else
1161static inline void
1162dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1163{
1164}
1165#endif
1166
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001167/*
1168 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1169 * won't be used for replay.
1170 */
1171void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1172{
1173 struct nfs4_stateowner *so = cstate->replay_owner;
1174
1175 if (nfserr == nfserr_replay_me)
1176 return;
1177
1178 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001179 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001180 return;
1181 }
1182 if (!so)
1183 return;
1184 if (so->so_is_open_owner)
1185 release_last_closed_stateid(openowner(so));
1186 so->so_seqid++;
1187 return;
1188}
Marc Eshel5282fd72009-04-03 08:27:52 +03001189
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001190static void
1191gen_sessionid(struct nfsd4_session *ses)
1192{
1193 struct nfs4_client *clp = ses->se_client;
1194 struct nfsd4_sessionid *sid;
1195
1196 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1197 sid->clientid = clp->cl_clientid;
1198 sid->sequence = current_sessionid++;
1199 sid->reserved = 0;
1200}
1201
1202/*
Andy Adamsona6496372009-08-28 08:45:01 -04001203 * The protocol defines ca_maxresponssize_cached to include the size of
1204 * the rpc header, but all we need to cache is the data starting after
1205 * the end of the initial SEQUENCE operation--the rest we regenerate
1206 * each time. Therefore we can advertise a ca_maxresponssize_cached
1207 * value that is the number of bytes in our cache plus a few additional
1208 * bytes. In order to stay on the safe side, and not promise more than
1209 * we can cache, those additional bytes must be the minimum possible: 24
1210 * bytes of rpc header (xid through accept state, with AUTH_NULL
1211 * verifier), 12 for the compound header (with zero-length tag), and 44
1212 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001213 */
Andy Adamsona6496372009-08-28 08:45:01 -04001214#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1215
Andy Adamson557ce262009-08-28 08:45:04 -04001216static void
1217free_session_slots(struct nfsd4_session *ses)
1218{
1219 int i;
1220
1221 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1222 kfree(ses->se_slots[i]);
1223}
1224
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001225/*
1226 * We don't actually need to cache the rpc and session headers, so we
1227 * can allocate a little less for each slot:
1228 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001229static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001230{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001231 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001232
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001233 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1234 size = 0;
1235 else
1236 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1237 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001238}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001239
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001240/*
1241 * XXX: If we run out of reserved DRC memory we could (up to a point)
1242 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001243 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001244 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001245static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001246{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001247 u32 slotsize = slot_bytes(ca);
1248 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001249 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001250
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001251 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001252 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1253 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001254 num = min_t(int, num, avail / slotsize);
1255 nfsd_drc_mem_used += num * slotsize;
1256 spin_unlock(&nfsd_drc_lock);
1257
1258 return num;
1259}
1260
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001261static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001262{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001263 int slotsize = slot_bytes(ca);
1264
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001265 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001266 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001267 spin_unlock(&nfsd_drc_lock);
1268}
1269
Kinglong Mee60810e52014-01-01 00:35:47 +08001270static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1271 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001272{
Kinglong Mee60810e52014-01-01 00:35:47 +08001273 int numslots = fattrs->maxreqs;
1274 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001275 struct nfsd4_session *new;
1276 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001277
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001278 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001279 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1280 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001281
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001282 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001283 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001284 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001285 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001286 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001287 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001288 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001289 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001290 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001291
1292 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1293 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1294
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001295 return new;
1296out_free:
1297 while (i--)
1298 kfree(new->se_slots[i]);
1299 kfree(new);
1300 return NULL;
1301}
1302
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001303static void free_conn(struct nfsd4_conn *c)
1304{
1305 svc_xprt_put(c->cn_xprt);
1306 kfree(c);
1307}
1308
1309static void nfsd4_conn_lost(struct svc_xpt_user *u)
1310{
1311 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1312 struct nfs4_client *clp = c->cn_session->se_client;
1313
1314 spin_lock(&clp->cl_lock);
1315 if (!list_empty(&c->cn_persession)) {
1316 list_del(&c->cn_persession);
1317 free_conn(c);
1318 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001319 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001320 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001321}
1322
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001323static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001324{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001325 struct nfsd4_conn *conn;
1326
1327 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1328 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001329 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001330 svc_xprt_get(rqstp->rq_xprt);
1331 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001332 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001333 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1334 return conn;
1335}
1336
J. Bruce Fields328ead22010-09-29 16:11:06 -04001337static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1338{
1339 conn->cn_session = ses;
1340 list_add(&conn->cn_persession, &ses->se_conns);
1341}
1342
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001343static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1344{
1345 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001346
1347 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001348 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001349 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001350}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001351
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001352static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001353{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001354 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001355 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001356}
1357
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001358static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001359{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001360 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001361
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001362 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001363 ret = nfsd4_register_conn(conn);
1364 if (ret)
1365 /* oops; xprt is already down: */
1366 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001367 /* We may have gained or lost a callback channel: */
1368 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001369}
1370
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001371static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001372{
1373 u32 dir = NFS4_CDFC4_FORE;
1374
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001375 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001376 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001377 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001378}
1379
1380/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001381static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001382{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001383 struct nfs4_client *clp = s->se_client;
1384 struct nfsd4_conn *c;
1385
1386 spin_lock(&clp->cl_lock);
1387 while (!list_empty(&s->se_conns)) {
1388 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1389 list_del_init(&c->cn_persession);
1390 spin_unlock(&clp->cl_lock);
1391
1392 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1393 free_conn(c);
1394
1395 spin_lock(&clp->cl_lock);
1396 }
1397 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001398}
1399
J. Bruce Fields1377b692012-09-11 21:42:40 -04001400static void __free_session(struct nfsd4_session *ses)
1401{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001402 free_session_slots(ses);
1403 kfree(ses);
1404}
1405
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001406static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001407{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001408 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001409 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001410 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001411}
Andy Adamson557ce262009-08-28 08:45:04 -04001412
Fengguang Wu135ae822012-11-10 07:20:25 -05001413static 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 -04001414{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001415 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001416 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001417
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001418 new->se_client = clp;
1419 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001420
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001421 INIT_LIST_HEAD(&new->se_conns);
1422
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001423 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001424 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001425 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001426 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001427 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001428 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001429 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001430 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001431 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001432 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001433
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001434 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001435 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001436 /*
1437 * This is a little silly; with sessions there's no real
1438 * use for the callback address. Use the peer address
1439 * as a reasonable default for now, but consider fixing
1440 * the rpc client not to require an address in the
1441 * future:
1442 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001443 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1444 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001445 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001446}
1447
Benny Halevy9089f1b2010-05-12 00:12:26 +03001448/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001449static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001450__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001451{
1452 struct nfsd4_session *elem;
1453 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001454 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001455
1456 dump_sessionid(__func__, sessionid);
1457 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001458 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001459 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001460 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1461 NFS4_MAX_SESSIONID_LEN)) {
1462 return elem;
1463 }
1464 }
1465
1466 dprintk("%s: session not found\n", __func__);
1467 return NULL;
1468}
1469
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001470static struct nfsd4_session *
1471find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1472 __be32 *ret)
1473{
1474 struct nfsd4_session *session;
1475 __be32 status = nfserr_badsession;
1476
1477 session = __find_in_sessionid_hashtbl(sessionid, net);
1478 if (!session)
1479 goto out;
1480 status = nfsd4_get_session_locked(session);
1481 if (status)
1482 session = NULL;
1483out:
1484 *ret = status;
1485 return session;
1486}
1487
Benny Halevy9089f1b2010-05-12 00:12:26 +03001488/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001489static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001490unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001491{
1492 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001493 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001494 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001495 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001496}
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1499static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001500STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001502 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001504 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001505 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return 1;
1507}
1508
1509/*
1510 * XXX Should we use a slab cache ?
1511 * This type of memory management is somewhat inefficient, but we use it
1512 * anyway since SETCLIENTID is not a common operation.
1513 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001514static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
1516 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001517 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001519 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1520 if (clp == NULL)
1521 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001522 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001523 if (clp->cl_name.data == NULL)
1524 goto err_no_name;
1525 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1526 OWNER_HASH_SIZE, GFP_KERNEL);
1527 if (!clp->cl_ownerstr_hashtbl)
1528 goto err_no_hashtbl;
1529 for (i = 0; i < OWNER_HASH_SIZE; i++)
1530 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001531 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001532 INIT_LIST_HEAD(&clp->cl_sessions);
1533 idr_init(&clp->cl_stateids);
1534 atomic_set(&clp->cl_refcount, 0);
1535 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1536 INIT_LIST_HEAD(&clp->cl_idhash);
1537 INIT_LIST_HEAD(&clp->cl_openowners);
1538 INIT_LIST_HEAD(&clp->cl_delegations);
1539 INIT_LIST_HEAD(&clp->cl_lru);
1540 INIT_LIST_HEAD(&clp->cl_callbacks);
1541 INIT_LIST_HEAD(&clp->cl_revoked);
1542 spin_lock_init(&clp->cl_lock);
1543 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001545err_no_hashtbl:
1546 kfree(clp->cl_name.data);
1547err_no_name:
1548 kfree(clp);
1549 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
1551
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001552static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553free_client(struct nfs4_client *clp)
1554{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001555 while (!list_empty(&clp->cl_sessions)) {
1556 struct nfsd4_session *ses;
1557 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1558 se_perclnt);
1559 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001560 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1561 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001562 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001563 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001564 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001565 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001567 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 kfree(clp);
1569}
1570
Benny Halevy84d38ac2010-05-12 00:13:16 +03001571/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001572static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001573unhash_client_locked(struct nfs4_client *clp)
1574{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001575 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001576 struct nfsd4_session *ses;
1577
Trond Myklebust4beb3452014-07-30 08:27:02 -04001578 /* Mark the client as expired! */
1579 clp->cl_time = 0;
1580 /* Make it invisible */
1581 if (!list_empty(&clp->cl_idhash)) {
1582 list_del_init(&clp->cl_idhash);
1583 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1584 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1585 else
1586 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1587 }
1588 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001589 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001590 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1591 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001592 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001593}
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001596unhash_client(struct nfs4_client *clp)
1597{
1598 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1599
1600 spin_lock(&nn->client_lock);
1601 unhash_client_locked(clp);
1602 spin_unlock(&nn->client_lock);
1603}
1604
1605static void
1606__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001608 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 struct list_head reaplist;
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001613 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001614 while (!list_empty(&clp->cl_delegations)) {
1615 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001616 unhash_delegation_locked(dp);
1617 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 }
Benny Halevycdc97502014-05-30 09:09:30 -04001619 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 while (!list_empty(&reaplist)) {
1621 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001622 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001623 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001625 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001626 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001627 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001628 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001629 }
NeilBrownea1da632005-06-23 22:04:17 -07001630 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001631 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Jeff Laytond3134b12014-07-29 21:34:32 -04001632 atomic_inc(&oo->oo_owner.so_count);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001633 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001635 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001636 if (clp->cl_cb_conn.cb_xprt)
1637 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001638 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
Trond Myklebust4beb3452014-07-30 08:27:02 -04001641static void
1642destroy_client(struct nfs4_client *clp)
1643{
1644 unhash_client(clp);
1645 __destroy_client(clp);
1646}
1647
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001648static void expire_client(struct nfs4_client *clp)
1649{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001650 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001651 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001652 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001653}
1654
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001655static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1656{
1657 memcpy(target->cl_verifier.data, source->data,
1658 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
1660
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001661static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1662{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1664 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1665}
1666
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001667static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001668{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001669 if (source->cr_principal) {
1670 target->cr_principal =
1671 kstrdup(source->cr_principal, GFP_KERNEL);
1672 if (target->cr_principal == NULL)
1673 return -ENOMEM;
1674 } else
1675 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001676 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 target->cr_uid = source->cr_uid;
1678 target->cr_gid = source->cr_gid;
1679 target->cr_group_info = source->cr_group_info;
1680 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001681 target->cr_gss_mech = source->cr_gss_mech;
1682 if (source->cr_gss_mech)
1683 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001684 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001687static long long
1688compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1689{
1690 long long res;
1691
1692 res = o1->len - o2->len;
1693 if (res)
1694 return res;
1695 return (long long)memcmp(o1->data, o2->data, o1->len);
1696}
1697
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001698static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001699{
NeilBrowna55370a2005-06-23 22:03:52 -07001700 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
1702
1703static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001704same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1705{
1706 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
1708
1709static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001710same_clid(clientid_t *cl1, clientid_t *cl2)
1711{
1712 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713}
1714
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001715static bool groups_equal(struct group_info *g1, struct group_info *g2)
1716{
1717 int i;
1718
1719 if (g1->ngroups != g2->ngroups)
1720 return false;
1721 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001722 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001723 return false;
1724 return true;
1725}
1726
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001727/*
1728 * RFC 3530 language requires clid_inuse be returned when the
1729 * "principal" associated with a requests differs from that previously
1730 * used. We use uid, gid's, and gss principal string as our best
1731 * approximation. We also don't want to allow non-gss use of a client
1732 * established using gss: in theory cr_principal should catch that
1733 * change, but in practice cr_principal can be null even in the gss case
1734 * since gssd doesn't always pass down a principal string.
1735 */
1736static bool is_gss_cred(struct svc_cred *cr)
1737{
1738 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1739 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1740}
1741
1742
Vivek Trivedi5559b502012-07-24 21:18:20 +05301743static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001744same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1745{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001746 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001747 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1748 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001749 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1750 return false;
1751 if (cr1->cr_principal == cr2->cr_principal)
1752 return true;
1753 if (!cr1->cr_principal || !cr2->cr_principal)
1754 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301755 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
J. Bruce Fields57266a62013-04-13 14:27:29 -04001758static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1759{
1760 struct svc_cred *cr = &rqstp->rq_cred;
1761 u32 service;
1762
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001763 if (!cr->cr_gss_mech)
1764 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001765 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1766 return service == RPC_GSS_SVC_INTEGRITY ||
1767 service == RPC_GSS_SVC_PRIVACY;
1768}
1769
1770static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1771{
1772 struct svc_cred *cr = &rqstp->rq_cred;
1773
1774 if (!cl->cl_mach_cred)
1775 return true;
1776 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1777 return false;
1778 if (!svc_rqst_integrity_protected(rqstp))
1779 return false;
1780 if (!cr->cr_principal)
1781 return false;
1782 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1783}
1784
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001785static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001786{
1787 static u32 current_clientid = 1;
1788
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001789 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 clp->cl_clientid.cl_id = current_clientid++;
1791}
1792
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001793static void gen_confirm(struct nfs4_client *clp)
1794{
Chuck Leverab4684d2012-03-02 17:13:50 -05001795 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001796 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Jeff Laytonf4199922014-06-17 07:44:11 -04001798 /*
1799 * This is opaque to client, so no need to byte-swap. Use
1800 * __force to keep sparse happy
1801 */
1802 verf[0] = (__force __be32)get_seconds();
1803 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001804 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
Jeff Layton4770d722014-07-29 21:34:10 -04001807static struct nfs4_stid *
1808find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001809{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001810 struct nfs4_stid *ret;
1811
1812 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1813 if (!ret || !ret->sc_type)
1814 return NULL;
1815 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001816}
1817
Jeff Layton4770d722014-07-29 21:34:10 -04001818static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001819find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001820{
1821 struct nfs4_stid *s;
1822
Jeff Layton4770d722014-07-29 21:34:10 -04001823 spin_lock(&cl->cl_lock);
1824 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001825 if (s != NULL) {
1826 if (typemask & s->sc_type)
1827 atomic_inc(&s->sc_count);
1828 else
1829 s = NULL;
1830 }
Jeff Layton4770d722014-07-29 21:34:10 -04001831 spin_unlock(&cl->cl_lock);
1832 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001833}
1834
Jeff Layton2216d442012-11-12 15:00:57 -05001835static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001836 struct svc_rqst *rqstp, nfs4_verifier *verf)
1837{
1838 struct nfs4_client *clp;
1839 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001840 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001841 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001842
1843 clp = alloc_client(name);
1844 if (clp == NULL)
1845 return NULL;
1846
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001847 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1848 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001849 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001850 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001851 }
Jeff Layton02e12152014-07-16 10:31:57 -04001852 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001853 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001854 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001855 copy_verf(clp, verf);
1856 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001857 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001858 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001859 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001860 return clp;
1861}
1862
NeilBrownfd39ca92005-06-23 22:04:03 -07001863static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001864add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1865{
1866 struct rb_node **new = &(root->rb_node), *parent = NULL;
1867 struct nfs4_client *clp;
1868
1869 while (*new) {
1870 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1871 parent = *new;
1872
1873 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1874 new = &((*new)->rb_left);
1875 else
1876 new = &((*new)->rb_right);
1877 }
1878
1879 rb_link_node(&new_clp->cl_namenode, parent, new);
1880 rb_insert_color(&new_clp->cl_namenode, root);
1881}
1882
1883static struct nfs4_client *
1884find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1885{
1886 long long cmp;
1887 struct rb_node *node = root->rb_node;
1888 struct nfs4_client *clp;
1889
1890 while (node) {
1891 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1892 cmp = compare_blob(&clp->cl_name, name);
1893 if (cmp > 0)
1894 node = node->rb_left;
1895 else if (cmp < 0)
1896 node = node->rb_right;
1897 else
1898 return clp;
1899 }
1900 return NULL;
1901}
1902
1903static void
1904add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
1906 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001907 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001909 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001910 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001912 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001913 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914}
1915
NeilBrownfd39ca92005-06-23 22:04:03 -07001916static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917move_to_confirmed(struct nfs4_client *clp)
1918{
1919 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001920 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001923 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001924 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001925 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001926 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001927 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928}
1929
1930static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001931find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
1933 struct nfs4_client *clp;
1934 unsigned int idhashval = clientid_hashval(clid->cl_id);
1935
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001936 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001937 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001938 if ((bool)clp->cl_minorversion != sessions)
1939 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001940 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
1944 return NULL;
1945}
1946
1947static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001948find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1949{
1950 struct list_head *tbl = nn->conf_id_hashtbl;
1951
1952 return find_client_in_id_table(tbl, clid, sessions);
1953}
1954
1955static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001956find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001958 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001960 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
1962
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001963static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001964{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001965 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001966}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001967
NeilBrown28ce6052005-06-23 22:03:56 -07001968static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001969find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001970{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001971 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001972}
1973
1974static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001975find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001976{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001977 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001978}
1979
NeilBrownfd39ca92005-06-23 22:04:03 -07001980static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001981gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001983 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001984 struct sockaddr *sa = svc_addr(rqstp);
1985 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001986 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Jeff Layton7077ecb2009-08-14 12:57:58 -04001988 /* Currently, we only support tcp and tcp6 for the callback channel */
1989 if (se->se_callback_netid_len == 3 &&
1990 !memcmp(se->se_callback_netid_val, "tcp", 3))
1991 expected_family = AF_INET;
1992 else if (se->se_callback_netid_len == 4 &&
1993 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1994 expected_family = AF_INET6;
1995 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 goto out_err;
1997
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001998 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001999 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002000 (struct sockaddr *)&conn->cb_addr,
2001 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002002
J. Bruce Fields07263f12010-05-31 19:09:40 -04002003 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002005
J. Bruce Fields07263f12010-05-31 19:09:40 -04002006 if (conn->cb_addr.ss_family == AF_INET6)
2007 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002008
J. Bruce Fields07263f12010-05-31 19:09:40 -04002009 conn->cb_prog = se->se_callback_prog;
2010 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002011 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 return;
2013out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002014 conn->cb_addr.ss_family = AF_UNSPEC;
2015 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002016 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 "will not receive delegations\n",
2018 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2019
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 return;
2021}
2022
Andy Adamson074fe892009-04-03 08:28:15 +03002023/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002024 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002025 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002026static void
Andy Adamson074fe892009-04-03 08:28:15 +03002027nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2028{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002029 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002030 struct nfsd4_slot *slot = resp->cstate.slot;
2031 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002032
Andy Adamson557ce262009-08-28 08:45:04 -04002033 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002034
Andy Adamson557ce262009-08-28 08:45:04 -04002035 slot->sl_opcnt = resp->opcnt;
2036 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002037
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002038 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002039 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002040 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002041 return;
2042 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002043 base = resp->cstate.data_offset;
2044 slot->sl_datalen = buf->len - base;
2045 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04002046 WARN("%s: sessions DRC could not cache compound\n", __func__);
2047 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002048}
2049
2050/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002051 * Encode the replay sequence operation from the slot values.
2052 * If cachethis is FALSE encode the uncached rep error on the next
2053 * operation which sets resp->p and increments resp->opcnt for
2054 * nfs4svc_encode_compoundres.
2055 *
Andy Adamson074fe892009-04-03 08:28:15 +03002056 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002057static __be32
2058nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2059 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002060{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002061 struct nfsd4_op *op;
2062 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002063
Andy Adamsonabfabf82009-07-23 19:02:18 -04002064 /* Encode the replayed sequence operation */
2065 op = &args->ops[resp->opcnt - 1];
2066 nfsd4_encode_operation(resp, op);
2067
2068 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002069 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002070 op = &args->ops[resp->opcnt++];
2071 op->status = nfserr_retry_uncached_rep;
2072 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002073 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002074 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002075}
2076
2077/*
Andy Adamson557ce262009-08-28 08:45:04 -04002078 * The sequence operation is not cached because we can use the slot and
2079 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002080 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002081static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002082nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2083 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002084{
Andy Adamson557ce262009-08-28 08:45:04 -04002085 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002086 struct xdr_stream *xdr = &resp->xdr;
2087 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002088 __be32 status;
2089
Andy Adamson557ce262009-08-28 08:45:04 -04002090 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002091
Andy Adamsonabfabf82009-07-23 19:02:18 -04002092 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002093 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002094 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002095
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002096 p = xdr_reserve_space(xdr, slot->sl_datalen);
2097 if (!p) {
2098 WARN_ON_ONCE(1);
2099 return nfserr_serverfault;
2100 }
2101 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2102 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002103
Andy Adamson557ce262009-08-28 08:45:04 -04002104 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002105 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002106}
2107
Andy Adamson0733d212009-04-03 08:28:01 +03002108/*
2109 * Set the exchange_id flags returned by the server.
2110 */
2111static void
2112nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2113{
2114 /* pNFS is not supported */
2115 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2116
2117 /* Referrals are supported, Migration is not. */
2118 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2119
2120 /* set the wire flags to return to client. */
2121 clid->flags = new->cl_exchange_flags;
2122}
2123
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002124static bool client_has_state(struct nfs4_client *clp)
2125{
2126 /*
2127 * Note clp->cl_openowners check isn't quite right: there's no
2128 * need to count owners without stateid's.
2129 *
2130 * Also note we should probably be using this in 4.0 case too.
2131 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002132 return !list_empty(&clp->cl_openowners)
2133 || !list_empty(&clp->cl_delegations)
2134 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002135}
2136
Al Virob37ad282006-10-19 23:28:59 -07002137__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002138nfsd4_exchange_id(struct svc_rqst *rqstp,
2139 struct nfsd4_compound_state *cstate,
2140 struct nfsd4_exchange_id *exid)
2141{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002142 struct nfs4_client *conf, *new;
2143 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002144 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002145 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002146 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002147 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002148 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002149 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002150
Jeff Layton363168b2009-08-14 12:57:56 -04002151 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002152 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002153 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002154 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002155 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002156
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002157 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002158 return nfserr_inval;
2159
Andy Adamson0733d212009-04-03 08:28:01 +03002160 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002161 case SP4_MACH_CRED:
2162 if (!svc_rqst_integrity_protected(rqstp))
2163 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002164 case SP4_NONE:
2165 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002166 default: /* checked by xdr code */
2167 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002168 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002169 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002170 }
2171
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002172 new = create_client(exid->clname, rqstp, &verf);
2173 if (new == NULL)
2174 return nfserr_jukebox;
2175
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002176 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03002177 nfs4_lock_state();
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002178 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002179 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002180 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002181 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2182 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2183
J. Bruce Fields136e6582012-05-12 20:37:23 -04002184 if (update) {
2185 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002186 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002187 goto out;
2188 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002189 if (!mach_creds_match(conf, rqstp)) {
2190 status = nfserr_wrong_cred;
2191 goto out;
2192 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002193 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002194 status = nfserr_perm;
2195 goto out;
2196 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002197 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002198 status = nfserr_not_same;
2199 goto out;
2200 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002201 /* case 6 */
2202 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002203 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002204 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002205 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002206 if (client_has_state(conf)) {
2207 status = nfserr_clid_inuse;
2208 goto out;
2209 }
Andy Adamson0733d212009-04-03 08:28:01 +03002210 goto out_new;
2211 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002212 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002213 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002214 goto out_copy;
2215 }
2216 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002217 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002218 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002219 }
2220
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002221 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002222 status = nfserr_noent;
2223 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002224 }
2225
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002226 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002227 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002228 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002229
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002230 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002231out_new:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002232 if (conf)
2233 unhash_client_locked(conf);
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002234 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002235 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002236
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002237 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002238 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002239 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002240out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002241 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2242 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002243
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002244 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2245 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002246
2247 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002248 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002249 status = nfs_ok;
2250
2251out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002252 spin_unlock(&nn->client_lock);
Andy Adamson0733d212009-04-03 08:28:01 +03002253 nfs4_unlock_state();
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002254 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002255 expire_client(new);
2256 if (unconf)
2257 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002258 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002259}
2260
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002261static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002262check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002263{
Andy Adamson88e588d2009-07-23 19:02:15 -04002264 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2265 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002266
2267 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002268 if (slot_inuse) {
2269 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002270 return nfserr_jukebox;
2271 else
2272 return nfserr_seq_misordered;
2273 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002274 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002275 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002276 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002277 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002278 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002279 return nfserr_seq_misordered;
2280}
2281
Andy Adamson49557cc2009-07-23 19:02:16 -04002282/*
2283 * Cache the create session result into the create session single DRC
2284 * slot cache by saving the xdr structure. sl_seqid has been set.
2285 * Do this for solo or embedded create session operations.
2286 */
2287static void
2288nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002289 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002290{
2291 slot->sl_status = nfserr;
2292 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2293}
2294
2295static __be32
2296nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2297 struct nfsd4_clid_slot *slot)
2298{
2299 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2300 return slot->sl_status;
2301}
2302
Mi Jinlong1b74c252011-07-14 14:50:17 +08002303#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2304 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2305 1 + /* MIN tag is length with zero, only length */ \
2306 3 + /* version, opcount, opcode */ \
2307 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2308 /* seqid, slotID, slotID, cache */ \
2309 4 ) * sizeof(__be32))
2310
2311#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2312 2 + /* verifier: AUTH_NULL, length 0 */\
2313 1 + /* status */ \
2314 1 + /* MIN tag is length with zero, only length */ \
2315 3 + /* opcount, opcode, opstatus*/ \
2316 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2317 /* seqid, slotID, slotID, slotID, status */ \
2318 5 ) * sizeof(__be32))
2319
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002320static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002321{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002322 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2323
J. Bruce Fields373cd402013-04-08 15:42:12 -04002324 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2325 return nfserr_toosmall;
2326 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2327 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002328 ca->headerpadsz = 0;
2329 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2330 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2331 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2332 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2333 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2334 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2335 /*
2336 * Note decreasing slot size below client's request may make it
2337 * difficult for client to function correctly, whereas
2338 * decreasing the number of slots will (just?) affect
2339 * performance. When short on memory we therefore prefer to
2340 * decrease number of slots instead of their size. Clients that
2341 * request larger slots than they need will get poor results:
2342 */
2343 ca->maxreqs = nfsd4_get_drc_mem(ca);
2344 if (!ca->maxreqs)
2345 return nfserr_jukebox;
2346
J. Bruce Fields373cd402013-04-08 15:42:12 -04002347 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002348}
2349
Kinglong Mee8a891632013-12-23 18:11:02 +08002350#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2351 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2352#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2353 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2354
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002355static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2356{
2357 ca->headerpadsz = 0;
2358
2359 /*
2360 * These RPC_MAX_HEADER macros are overkill, especially since we
2361 * don't even do gss on the backchannel yet. But this is still
2362 * less than 1k. Tighten up this estimate in the unlikely event
2363 * it turns out to be a problem for some client:
2364 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002365 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002366 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002367 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002368 return nfserr_toosmall;
2369 ca->maxresp_cached = 0;
2370 if (ca->maxops < 2)
2371 return nfserr_toosmall;
2372
2373 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002374}
2375
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002376static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2377{
2378 switch (cbs->flavor) {
2379 case RPC_AUTH_NULL:
2380 case RPC_AUTH_UNIX:
2381 return nfs_ok;
2382 default:
2383 /*
2384 * GSS case: the spec doesn't allow us to return this
2385 * error. But it also doesn't allow us not to support
2386 * GSS.
2387 * I'd rather this fail hard than return some error the
2388 * client might think it can already handle:
2389 */
2390 return nfserr_encr_alg_unsupp;
2391 }
2392}
2393
Andy Adamson069b6ad2009-04-03 08:27:58 +03002394__be32
2395nfsd4_create_session(struct svc_rqst *rqstp,
2396 struct nfsd4_compound_state *cstate,
2397 struct nfsd4_create_session *cr_ses)
2398{
Jeff Layton363168b2009-08-14 12:57:56 -04002399 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002400 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002401 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002402 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002403 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002404 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002405 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002406 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002407
Mi Jinlonga62573d2011-03-23 17:57:07 +08002408 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2409 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002410 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2411 if (status)
2412 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002413 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002414 if (status)
2415 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002416 status = check_backchannel_attrs(&cr_ses->back_channel);
2417 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002418 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002419 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002420 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002421 if (!new)
2422 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002423 conn = alloc_conn_from_crses(rqstp, cr_ses);
2424 if (!conn)
2425 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002426
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002427 nfs4_lock_state();
Jeff Laytond20c11d2014-07-30 08:27:07 -04002428 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002429 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002430 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002431 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002432
2433 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002434 status = nfserr_wrong_cred;
2435 if (!mach_creds_match(conf, rqstp))
2436 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002437 cs_slot = &conf->cl_cs_slot;
2438 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002439 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002440 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002441 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002442 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002443 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002444 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002445 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002446 } else if (unconf) {
2447 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002448 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002449 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002450 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002451 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002452 status = nfserr_wrong_cred;
2453 if (!mach_creds_match(unconf, rqstp))
2454 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002455 cs_slot = &unconf->cl_cs_slot;
2456 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002457 if (status) {
2458 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002459 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002460 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002461 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002462 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002463 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002464 status = mark_client_expired_locked(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002465 if (status)
2466 goto out_free_conn;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002467 unhash_client_locked(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002468 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002469 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002470 conf = unconf;
2471 } else {
2472 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002473 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002474 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002475 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002476 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002477 * We do not support RDMA or persistent sessions
2478 */
2479 cr_ses->flags &= ~SESSION4_PERSIST;
2480 cr_ses->flags &= ~SESSION4_RDMA;
2481
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002482 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002483 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002484
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002485 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002486 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002487 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002488 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002489
Jeff Laytond20c11d2014-07-30 08:27:07 -04002490 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002491 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002492 spin_unlock(&nn->client_lock);
2493 /* init connection and backchannel */
2494 nfsd4_init_conn(rqstp, conn, new);
2495 nfsd4_put_session(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002496 nfs4_unlock_state();
Jeff Laytond20c11d2014-07-30 08:27:07 -04002497 if (old)
2498 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002499 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002500out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002501 spin_unlock(&nn->client_lock);
Yanchuan Nian266533c2012-12-24 18:11:45 +08002502 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002503 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002504 if (old)
2505 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002506out_free_session:
2507 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002508out_release_drc_mem:
2509 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002510 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002511}
2512
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002513static __be32 nfsd4_map_bcts_dir(u32 *dir)
2514{
2515 switch (*dir) {
2516 case NFS4_CDFC4_FORE:
2517 case NFS4_CDFC4_BACK:
2518 return nfs_ok;
2519 case NFS4_CDFC4_FORE_OR_BOTH:
2520 case NFS4_CDFC4_BACK_OR_BOTH:
2521 *dir = NFS4_CDFC4_BOTH;
2522 return nfs_ok;
2523 };
2524 return nfserr_inval;
2525}
2526
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002527__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2528{
2529 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002530 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002531 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002532
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002533 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2534 if (status)
2535 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002536 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002537 session->se_cb_prog = bc->bc_cb_program;
2538 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002539 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002540
2541 nfsd4_probe_callback(session->se_client);
2542
2543 return nfs_ok;
2544}
2545
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002546__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2547 struct nfsd4_compound_state *cstate,
2548 struct nfsd4_bind_conn_to_session *bcts)
2549{
2550 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002551 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002552 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002553 struct net *net = SVC_NET(rqstp);
2554 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002555
2556 if (!nfsd4_last_compound_op(rqstp))
2557 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002558 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002559 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002560 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002561 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002562 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002563 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002564 status = nfserr_wrong_cred;
2565 if (!mach_creds_match(session->se_client, rqstp))
2566 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002567 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002568 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002569 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002570 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002571 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002572 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002573 goto out;
2574 nfsd4_init_conn(rqstp, conn, session);
2575 status = nfs_ok;
2576out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002577 nfsd4_put_session(session);
2578out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002579 nfs4_unlock_state();
2580 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002581}
2582
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002583static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2584{
2585 if (!session)
2586 return 0;
2587 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2588}
2589
Andy Adamson069b6ad2009-04-03 08:27:58 +03002590__be32
2591nfsd4_destroy_session(struct svc_rqst *r,
2592 struct nfsd4_compound_state *cstate,
2593 struct nfsd4_destroy_session *sessionid)
2594{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002595 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002596 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002597 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002598 struct net *net = SVC_NET(r);
2599 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002600
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002601 nfs4_lock_state();
2602 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002603 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002604 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002605 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002606 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002607 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002608 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002609 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002610 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002611 if (!ses)
2612 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002613 status = nfserr_wrong_cred;
2614 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002615 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002616 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002617 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002618 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002619 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002620 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002621
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002622 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002623
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002624 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002625 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002626out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002627 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002628out_client_lock:
2629 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002630out:
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002631 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002632 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002633}
2634
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002635static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002636{
2637 struct nfsd4_conn *c;
2638
2639 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002640 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002641 return c;
2642 }
2643 }
2644 return NULL;
2645}
2646
J. Bruce Fields57266a62013-04-13 14:27:29 -04002647static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002648{
2649 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002650 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002651 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002652 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002653
2654 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002655 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002656 if (c)
2657 goto out_free;
2658 status = nfserr_conn_not_bound_to_session;
2659 if (clp->cl_mach_cred)
2660 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002661 __nfsd4_hash_conn(new, ses);
2662 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002663 ret = nfsd4_register_conn(new);
2664 if (ret)
2665 /* oops; xprt is already down: */
2666 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002667 return nfs_ok;
2668out_free:
2669 spin_unlock(&clp->cl_lock);
2670 free_conn(new);
2671 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002672}
2673
Mi Jinlong868b89c2011-04-27 09:09:58 +08002674static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2675{
2676 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2677
2678 return args->opcnt > session->se_fchannel.maxops;
2679}
2680
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002681static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2682 struct nfsd4_session *session)
2683{
2684 struct xdr_buf *xb = &rqstp->rq_arg;
2685
2686 return xb->len > session->se_fchannel.maxreq_sz;
2687}
2688
Andy Adamson069b6ad2009-04-03 08:27:58 +03002689__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002690nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002691 struct nfsd4_compound_state *cstate,
2692 struct nfsd4_sequence *seq)
2693{
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002694 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002695 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002696 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002697 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002698 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002699 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002700 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002701 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002702 struct net *net = SVC_NET(rqstp);
2703 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002704
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002705 if (resp->opcnt != 1)
2706 return nfserr_sequence_pos;
2707
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002708 /*
2709 * Will be either used or freed by nfsd4_sequence_check_conn
2710 * below.
2711 */
2712 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2713 if (!conn)
2714 return nfserr_jukebox;
2715
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002716 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002717 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002718 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002719 goto out_no_session;
2720 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002721
Mi Jinlong868b89c2011-04-27 09:09:58 +08002722 status = nfserr_too_many_ops;
2723 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002724 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002725
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002726 status = nfserr_req_too_big;
2727 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002728 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002729
Benny Halevyb85d4c02009-04-03 08:28:08 +03002730 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002731 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002732 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002733
Andy Adamson557ce262009-08-28 08:45:04 -04002734 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002735 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2736
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002737 /* We do not negotiate the number of slots yet, so set the
2738 * maxslots to the session maxreqs which is used to encode
2739 * sr_highest_slotid and the sr_target_slot id to maxslots */
2740 seq->maxslots = session->se_fchannel.maxreqs;
2741
J. Bruce Fields73e79482012-02-13 16:39:00 -05002742 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2743 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002744 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002745 status = nfserr_seq_misordered;
2746 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002747 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002748 cstate->slot = slot;
2749 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002750 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002751 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002752 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002753 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002754 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002755 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002756 }
2757 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002758 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002759
J. Bruce Fields57266a62013-04-13 14:27:29 -04002760 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002761 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002762 if (status)
2763 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002764
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002765 buflen = (seq->cachethis) ?
2766 session->se_fchannel.maxresp_cached :
2767 session->se_fchannel.maxresp_sz;
2768 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2769 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002770 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002771 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002772 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002773
2774 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002775 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002776 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002777 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002778 if (seq->cachethis)
2779 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002780 else
2781 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002782
2783 cstate->slot = slot;
2784 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002785 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002786
Benny Halevyb85d4c02009-04-03 08:28:08 +03002787out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002788 switch (clp->cl_cb_state) {
2789 case NFSD4_CB_DOWN:
2790 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2791 break;
2792 case NFSD4_CB_FAULT:
2793 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2794 break;
2795 default:
2796 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002797 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002798 if (!list_empty(&clp->cl_revoked))
2799 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002800out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002801 if (conn)
2802 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002803 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002804 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002805out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002806 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002807 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002808}
2809
Trond Myklebustb6076642014-06-30 11:48:35 -04002810void
2811nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2812{
2813 struct nfsd4_compound_state *cs = &resp->cstate;
2814
2815 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002816 if (cs->status != nfserr_replay_cache) {
2817 nfsd4_store_cache_entry(resp);
2818 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2819 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002820 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002821 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002822 } else if (cs->clp)
2823 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002824}
2825
Mi Jinlong345c2842011-10-20 17:51:39 +08002826__be32
2827nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2828{
2829 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002830 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002831 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002832
2833 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002834 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002835 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002836 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002837
2838 if (conf) {
2839 clp = conf;
2840
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002841 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002842 status = nfserr_clientid_busy;
2843 goto out;
2844 }
2845 } else if (unconf)
2846 clp = unconf;
2847 else {
2848 status = nfserr_stale_clientid;
2849 goto out;
2850 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002851 if (!mach_creds_match(clp, rqstp)) {
2852 status = nfserr_wrong_cred;
2853 goto out;
2854 }
Mi Jinlong345c2842011-10-20 17:51:39 +08002855 expire_client(clp);
2856out:
2857 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002858 return status;
2859}
2860
Andy Adamson069b6ad2009-04-03 08:27:58 +03002861__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002862nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2863{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002864 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002865
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002866 if (rc->rca_one_fs) {
2867 if (!cstate->current_fh.fh_dentry)
2868 return nfserr_nofilehandle;
2869 /*
2870 * We don't take advantage of the rca_one_fs case.
2871 * That's OK, it's optional, we can safely ignore it.
2872 */
2873 return nfs_ok;
2874 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002875
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002876 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002877 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002878 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2879 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002880 goto out;
2881
2882 status = nfserr_stale_clientid;
2883 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002884 /*
2885 * The following error isn't really legal.
2886 * But we only get here if the client just explicitly
2887 * destroyed the client. Surely it no longer cares what
2888 * error it gets back on an operation for the dead
2889 * client.
2890 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002891 goto out;
2892
2893 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002894 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002895out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002896 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002897 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002898}
2899
2900__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002901nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2902 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002904 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002906 struct nfs4_client *conf, *new;
2907 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002908 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002909 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2910
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002911 new = create_client(clname, rqstp, &clverifier);
2912 if (new == NULL)
2913 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002914 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 nfs4_lock_state();
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002916 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002917 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002918 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002919 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002921 if (clp_used_exchangeid(conf))
2922 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002923 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002924 char addr_str[INET6_ADDRSTRLEN];
2925 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2926 sizeof(addr_str));
2927 dprintk("NFSD: setclientid: string in use by client "
2928 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 goto out;
2930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002932 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002933 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002934 unhash_client_locked(unconf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002935 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002936 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002938 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002939 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002940 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002941 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002942 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2944 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2945 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002946 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 status = nfs_ok;
2948out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002949 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 nfs4_unlock_state();
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002951 if (new)
2952 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002953 if (unconf)
2954 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 return status;
2956}
2957
2958
Al Virob37ad282006-10-19 23:28:59 -07002959__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002960nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2961 struct nfsd4_compound_state *cstate,
2962 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963{
NeilBrown21ab45a2005-06-23 22:04:14 -07002964 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002965 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2967 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002968 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002969 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002971 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002974
Jeff Laytond20c11d2014-07-30 08:27:07 -04002975 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002976 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002977 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002978 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002979 * We try hard to give out unique clientid's, so if we get an
2980 * attempt to confirm the same clientid with a different cred,
2981 * there's a bug somewhere. Let's charitably assume it's our
2982 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002983 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002984 status = nfserr_serverfault;
2985 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2986 goto out;
2987 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2988 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002989 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002990 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2991 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002993 else /* case 4: client hasn't noticed we rebooted yet? */
2994 status = nfserr_stale_clientid;
2995 goto out;
2996 }
2997 status = nfs_ok;
2998 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04002999 old = unconf;
3000 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003001 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003002 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003003 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3004 if (old) {
3005 status = mark_client_expired_locked(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003006 if (status)
3007 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003008 unhash_client_locked(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003009 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003010 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003011 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003012 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003013 get_client_locked(conf);
3014 spin_unlock(&nn->client_lock);
3015 nfsd4_probe_callback(conf);
3016 spin_lock(&nn->client_lock);
3017 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003019 spin_unlock(&nn->client_lock);
3020 if (old)
3021 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 nfs4_unlock_state();
3023 return status;
3024}
3025
J. Bruce Fields32513b42011-10-13 16:00:16 -04003026static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003028 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3029}
3030
3031/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003032static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003033{
Trond Myklebustca943212014-07-23 16:17:39 -04003034 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Trond Myklebust950e0112014-06-30 11:48:31 -04003036 lockdep_assert_held(&state_lock);
3037
J. Bruce Fields32513b42011-10-13 16:00:16 -04003038 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003039 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003040 INIT_LIST_HEAD(&fp->fi_stateids);
3041 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003042 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003043 fp->fi_had_conflict = false;
3044 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003045 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003046 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3047 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04003048 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049}
3050
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003051void
NeilBrowne60d4392005-06-23 22:03:01 -07003052nfsd4_free_slabs(void)
3053{
Christoph Hellwigabf11352014-05-21 07:43:03 -07003054 kmem_cache_destroy(openowner_slab);
3055 kmem_cache_destroy(lockowner_slab);
3056 kmem_cache_destroy(file_slab);
3057 kmem_cache_destroy(stateid_slab);
3058 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003059}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
Bryan Schumaker72083392011-11-01 15:24:59 -04003061int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062nfsd4_init_slabs(void)
3063{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003064 openowner_slab = kmem_cache_create("nfsd4_openowners",
3065 sizeof(struct nfs4_openowner), 0, 0, NULL);
3066 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003067 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003068 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003069 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003070 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003071 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003072 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003073 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003074 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003075 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003076 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003077 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003078 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003079 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003080 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003081 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003082 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003083 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003085
3086out_free_stateid_slab:
3087 kmem_cache_destroy(stateid_slab);
3088out_free_file_slab:
3089 kmem_cache_destroy(file_slab);
3090out_free_lockowner_slab:
3091 kmem_cache_destroy(lockowner_slab);
3092out_free_openowner_slab:
3093 kmem_cache_destroy(openowner_slab);
3094out:
NeilBrowne60d4392005-06-23 22:03:01 -07003095 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3096 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097}
3098
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003099static void init_nfs4_replay(struct nfs4_replay *rp)
3100{
3101 rp->rp_status = nfserr_serverfault;
3102 rp->rp_buflen = 0;
3103 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003104 mutex_init(&rp->rp_mutex);
3105}
3106
3107static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3108 struct nfs4_stateowner *so)
3109{
3110 if (!nfsd4_has_session(cstate)) {
3111 mutex_lock(&so->so_replay.rp_mutex);
3112 cstate->replay_owner = so;
3113 atomic_inc(&so->so_count);
3114 }
3115}
3116
3117void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3118{
3119 struct nfs4_stateowner *so = cstate->replay_owner;
3120
3121 if (so != NULL) {
3122 cstate->replay_owner = NULL;
3123 mutex_unlock(&so->so_replay.rp_mutex);
3124 nfs4_put_stateowner(so);
3125 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003126}
3127
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003128static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129{
3130 struct nfs4_stateowner *sop;
3131
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003132 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003133 if (!sop)
3134 return NULL;
3135
3136 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3137 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003138 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003139 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003141 sop->so_owner.len = owner->len;
3142
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003143 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003144 sop->so_client = clp;
3145 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003146 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003147 return sop;
3148}
3149
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003150static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003151{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003152 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003153
Trond Myklebustd4f04892014-07-29 21:34:36 -04003154 list_add(&oo->oo_owner.so_strhash,
3155 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003156 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157}
3158
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003159static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3160{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003161 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003162}
3163
Jeff Layton6b180f02014-07-29 21:34:26 -04003164static void nfs4_free_openowner(struct nfs4_stateowner *so)
3165{
3166 struct nfs4_openowner *oo = openowner(so);
3167
3168 kmem_cache_free(openowner_slab, oo);
3169}
3170
3171static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003172 .so_unhash = nfs4_unhash_openowner,
3173 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003174};
3175
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003176static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003177alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003178 struct nfsd4_compound_state *cstate)
3179{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003180 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003181 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003183 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3184 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003186 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003187 oo->oo_owner.so_is_open_owner = 1;
3188 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003189 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003190 if (nfsd4_has_session(cstate))
3191 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003192 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003193 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003194 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003195 spin_lock(&clp->cl_lock);
3196 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003197 if (ret == NULL) {
3198 hash_openowner(oo, clp, strhashval);
3199 ret = oo;
3200 } else
3201 nfs4_free_openowner(&oo->oo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003202 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003203 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204}
3205
J. Bruce Fields996e0932011-10-17 11:14:48 -04003206static 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 -04003207 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003209 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003210 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003211 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003212 stp->st_stateowner = &oo->oo_owner;
Jeff Laytond3134b12014-07-29 21:34:32 -04003213 atomic_inc(&stp->st_stateowner->so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07003214 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003215 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 stp->st_access_bmap = 0;
3217 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003218 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003219 spin_lock(&oo->oo_owner.so_client->cl_lock);
3220 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003221 spin_lock(&fp->fi_lock);
3222 list_add(&stp->st_perfile, &fp->fi_stateids);
3223 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003224 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225}
3226
Jeff Laytond3134b12014-07-29 21:34:32 -04003227/*
3228 * In the 4.0 case we need to keep the owners around a little while to handle
3229 * CLOSE replay. We still do need to release any file access that is held by
3230 * them before returning however.
3231 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003233move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234{
Jeff Laytond3134b12014-07-29 21:34:32 -04003235 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3236 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3237 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003238
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003239 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
Jeff Laytonb401be222014-07-29 21:34:33 -04003241 /*
3242 * We know that we hold one reference via nfsd4_close, and another
3243 * "persistent" reference for the client. If the refcount is higher
3244 * than 2, then there are still calls in progress that are using this
3245 * stateid. We can't put the sc_file reference until they are finished.
3246 * Wait for the refcount to drop to 2. Since it has been unhashed,
3247 * there should be no danger of the refcount going back up again at
3248 * this point.
3249 */
3250 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3251
Jeff Laytond3134b12014-07-29 21:34:32 -04003252 release_all_access(s);
3253 if (s->st_stid.sc_file) {
3254 put_nfs4_file(s->st_stid.sc_file);
3255 s->st_stid.sc_file = NULL;
3256 }
3257 release_last_closed_stateid(oo);
3258 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003259 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003260 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261}
3262
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263/* search file_hashtbl[] for file */
3264static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003265find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266{
Trond Myklebustca943212014-07-23 16:17:39 -04003267 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 struct nfs4_file *fp;
3269
Trond Myklebust950e0112014-06-30 11:48:31 -04003270 lockdep_assert_held(&state_lock);
3271
Jeff Layton89876f82013-04-02 09:01:59 -04003272 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003273 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003274 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 }
3278 return NULL;
3279}
3280
Trond Myklebust950e0112014-06-30 11:48:31 -04003281static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003282find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003283{
3284 struct nfs4_file *fp;
3285
3286 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003287 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003288 spin_unlock(&state_lock);
3289 return fp;
3290}
3291
3292static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003293find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003294{
3295 struct nfs4_file *fp;
3296
3297 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003298 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003299 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003300 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003301 fp = new;
3302 }
3303 spin_unlock(&state_lock);
3304
3305 return fp;
3306}
3307
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003308/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 * Called to check deny when READ with all zero stateid or
3310 * WRITE with all zero or all one stateid
3311 */
Al Virob37ad282006-10-19 23:28:59 -07003312static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3314{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003316 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
Trond Myklebustca943212014-07-23 16:17:39 -04003318 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003319 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003320 return ret;
3321 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003322 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003323 if (fp->fi_share_deny & deny_type)
3324 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003325 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003326 put_nfs4_file(fp);
3327 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328}
3329
Jeff Layton02e12152014-07-16 10:31:57 -04003330void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331{
Trond Myklebust11b91642014-07-29 21:34:08 -04003332 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3333 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003334
Trond Myklebust11b91642014-07-29 21:34:08 -04003335 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003336
Jeff Layton02e12152014-07-16 10:31:57 -04003337 /*
3338 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003339 * already holding inode->i_lock.
3340 *
Jeff Laytondff13992014-07-08 14:02:49 -04003341 * If the dl_time != 0, then we know that it has already been
3342 * queued for a lease break. Don't queue it again.
3343 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003344 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003345 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003346 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003347 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003348 }
Jeff Layton02e12152014-07-16 10:31:57 -04003349 spin_unlock(&state_lock);
3350}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351
Jeff Layton02e12152014-07-16 10:31:57 -04003352static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3353{
3354 /*
3355 * We're assuming the state code never drops its reference
3356 * without first removing the lease. Since we're in this lease
3357 * callback (and since the lease code is serialized by the kernel
3358 * lock) we know the server hasn't removed the lease yet, we know
3359 * it's safe to take a reference.
3360 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003361 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003362 nfsd4_cb_recall(dp);
3363}
3364
Jeff Layton1c8c6012013-06-21 08:58:15 -04003365/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003366static void nfsd_break_deleg_cb(struct file_lock *fl)
3367{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003368 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3369 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003370
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003371 if (!fp) {
3372 WARN(1, "(%p)->fl_owner NULL\n", fl);
3373 return;
3374 }
3375 if (fp->fi_had_conflict) {
3376 WARN(1, "duplicate break on %p\n", fp);
3377 return;
3378 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003379 /*
3380 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003381 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003382 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003383 */
3384 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
Jeff Layton02e12152014-07-16 10:31:57 -04003386 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003387 fp->fi_had_conflict = true;
3388 /*
3389 * If there are no delegations on the list, then we can't count on this
3390 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3391 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3392 * true should keep any new delegations from being hashed.
3393 */
3394 if (list_empty(&fp->fi_delegations))
3395 fl->fl_break_time = jiffies;
3396 else
3397 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3398 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003399 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400}
3401
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402static
3403int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3404{
3405 if (arg & F_UNLCK)
3406 return lease_modify(onlist, arg);
3407 else
3408 return -EAGAIN;
3409}
3410
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003411static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003412 .lm_break = nfsd_break_deleg_cb,
3413 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414};
3415
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003416static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3417{
3418 if (nfsd4_has_session(cstate))
3419 return nfs_ok;
3420 if (seqid == so->so_seqid - 1)
3421 return nfserr_replay_me;
3422 if (seqid == so->so_seqid)
3423 return nfs_ok;
3424 return nfserr_bad_seqid;
3425}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
Jeff Layton4b24ca72014-06-30 11:48:44 -04003427static __be32 lookup_clientid(clientid_t *clid,
3428 struct nfsd4_compound_state *cstate,
3429 struct nfsd_net *nn)
3430{
3431 struct nfs4_client *found;
3432
3433 if (cstate->clp) {
3434 found = cstate->clp;
3435 if (!same_clid(&found->cl_clientid, clid))
3436 return nfserr_stale_clientid;
3437 return nfs_ok;
3438 }
3439
3440 if (STALE_CLIENTID(clid, nn))
3441 return nfserr_stale_clientid;
3442
3443 /*
3444 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3445 * cached already then we know this is for is for v4.0 and "sessions"
3446 * will be false.
3447 */
3448 WARN_ON_ONCE(cstate->session);
3449 found = find_confirmed_client(clid, false, nn);
3450 if (!found)
3451 return nfserr_expired;
3452
3453 /* Cache the nfs4_client in cstate! */
3454 cstate->clp = found;
3455 atomic_inc(&found->cl_refcount);
3456 return nfs_ok;
3457}
3458
Al Virob37ad282006-10-19 23:28:59 -07003459__be32
Andy Adamson66689582009-04-03 08:28:45 +03003460nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003461 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 clientid_t *clientid = &open->op_clientid;
3464 struct nfs4_client *clp = NULL;
3465 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003466 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003467 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003469 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003471 /*
3472 * In case we need it later, after we've already created the
3473 * file and don't want to risk a further failure:
3474 */
3475 open->op_file = nfsd4_alloc_file();
3476 if (open->op_file == NULL)
3477 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
Trond Myklebust2d91e892014-06-30 11:48:46 -04003479 status = lookup_clientid(clientid, cstate, nn);
3480 if (status)
3481 return status;
3482 clp = cstate->clp;
3483
Trond Myklebustd4f04892014-07-29 21:34:36 -04003484 strhashval = ownerstr_hashval(&open->op_owner);
3485 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003486 open->op_openowner = oo;
3487 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003488 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003490 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003491 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003492 release_openowner(oo);
3493 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003494 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003495 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003496 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3497 if (status)
3498 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003499 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003500new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003501 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003502 if (oo == NULL)
3503 return nfserr_jukebox;
3504 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003505alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003506 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003507 if (!open->op_stp)
3508 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003509 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510}
3511
Al Virob37ad282006-10-19 23:28:59 -07003512static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003513nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3514{
3515 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3516 return nfserr_openmode;
3517 else
3518 return nfs_ok;
3519}
3520
Daniel Mackc47d8322011-05-16 16:38:14 +02003521static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003522{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003523 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3524}
3525
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003526static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003527{
3528 struct nfs4_stid *ret;
3529
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003530 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003531 if (!ret)
3532 return NULL;
3533 return delegstateid(ret);
3534}
3535
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003536static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3537{
3538 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3539 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3540}
3541
Al Virob37ad282006-10-19 23:28:59 -07003542static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003543nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003544 struct nfs4_delegation **dp)
3545{
3546 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003547 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003548 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003549
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003550 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3551 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003552 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003553 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003554 status = nfs4_check_delegmode(deleg, flags);
3555 if (status) {
3556 nfs4_put_stid(&deleg->dl_stid);
3557 goto out;
3558 }
3559 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003560out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003561 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003562 return nfs_ok;
3563 if (status)
3564 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003565 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003566 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003567}
3568
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003569static struct nfs4_ol_stateid *
3570nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003572 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003573 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574
Trond Myklebust1d31a252014-07-10 14:07:25 -04003575 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003576 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 /* ignore lock owners */
3578 if (local->st_stateowner->so_is_open_owner == 0)
3579 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003580 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003581 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003582 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003583 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003586 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003587 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588}
3589
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003590static inline int nfs4_access_to_access(u32 nfs4_access)
3591{
3592 int flags = 0;
3593
3594 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3595 flags |= NFSD_MAY_READ;
3596 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3597 flags |= NFSD_MAY_WRITE;
3598 return flags;
3599}
3600
Al Virob37ad282006-10-19 23:28:59 -07003601static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3603 struct nfsd4_open *open)
3604{
3605 struct iattr iattr = {
3606 .ia_valid = ATTR_SIZE,
3607 .ia_size = 0,
3608 };
3609 if (!open->op_truncate)
3610 return 0;
3611 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003612 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3614}
3615
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003616static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003617 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3618 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003619{
Trond Myklebustde186432014-07-10 14:07:26 -04003620 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003621 __be32 status;
3622 int oflag = nfs4_access_to_omode(open->op_share_access);
3623 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003624 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003625
Trond Myklebustde186432014-07-10 14:07:26 -04003626 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003627
3628 /*
3629 * Are we trying to set a deny mode that would conflict with
3630 * current access?
3631 */
3632 status = nfs4_file_check_deny(fp, open->op_share_deny);
3633 if (status != nfs_ok) {
3634 spin_unlock(&fp->fi_lock);
3635 goto out;
3636 }
3637
3638 /* set access to the file */
3639 status = nfs4_file_get_access(fp, open->op_share_access);
3640 if (status != nfs_ok) {
3641 spin_unlock(&fp->fi_lock);
3642 goto out;
3643 }
3644
3645 /* Set access bits in stateid */
3646 old_access_bmap = stp->st_access_bmap;
3647 set_access(open->op_share_access, stp);
3648
3649 /* Set new deny mask */
3650 old_deny_bmap = stp->st_deny_bmap;
3651 set_deny(open->op_share_deny, stp);
3652 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3653
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003654 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003655 spin_unlock(&fp->fi_lock);
3656 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003657 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003658 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003659 spin_lock(&fp->fi_lock);
3660 if (!fp->fi_fds[oflag]) {
3661 fp->fi_fds[oflag] = filp;
3662 filp = NULL;
3663 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003664 }
Trond Myklebustde186432014-07-10 14:07:26 -04003665 spin_unlock(&fp->fi_lock);
3666 if (filp)
3667 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003668
3669 status = nfsd4_truncate(rqstp, cur_fh, open);
3670 if (status)
3671 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003672out:
3673 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003674out_put_access:
3675 stp->st_access_bmap = old_access_bmap;
3676 nfs4_file_put_access(fp, open->op_share_access);
3677 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3678 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003679}
3680
Al Virob37ad282006-10-19 23:28:59 -07003681static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003682nfs4_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 -07003683{
Al Virob37ad282006-10-19 23:28:59 -07003684 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003685 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003687 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003688 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003689
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003690 /* test and set deny mode */
3691 spin_lock(&fp->fi_lock);
3692 status = nfs4_file_check_deny(fp, open->op_share_deny);
3693 if (status == nfs_ok) {
3694 old_deny_bmap = stp->st_deny_bmap;
3695 set_deny(open->op_share_deny, stp);
3696 fp->fi_share_deny |=
3697 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3698 }
3699 spin_unlock(&fp->fi_lock);
3700
3701 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003704 status = nfsd4_truncate(rqstp, cur_fh, open);
3705 if (status != nfs_ok)
3706 reset_union_bmap_deny(old_deny_bmap, stp);
3707 return status;
3708}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003711nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003713 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714}
3715
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003716/* Should we give out recallable state?: */
3717static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3718{
3719 if (clp->cl_cb_state == NFSD4_CB_UP)
3720 return true;
3721 /*
3722 * In the sessions case, since we don't have to establish a
3723 * separate connection for callbacks, we assume it's OK
3724 * until we hear otherwise:
3725 */
3726 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3727}
3728
Jeff Laytond564fbe2014-07-16 10:31:58 -04003729static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003730{
3731 struct file_lock *fl;
3732
3733 fl = locks_alloc_lock();
3734 if (!fl)
3735 return NULL;
3736 locks_init_lock(fl);
3737 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003738 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003739 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3740 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003741 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003742 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003743 return fl;
3744}
3745
J. Bruce Fields99c41512013-05-21 16:21:25 -04003746static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003747{
Trond Myklebust11b91642014-07-29 21:34:08 -04003748 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003749 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003750 struct file *filp;
3751 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003752
Jeff Laytond564fbe2014-07-16 10:31:58 -04003753 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003754 if (!fl)
3755 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003756 filp = find_readable_file(fp);
3757 if (!filp) {
3758 /* We should always have a readable file here */
3759 WARN_ON_ONCE(1);
3760 return -EBADF;
3761 }
3762 fl->fl_file = filp;
3763 status = vfs_setlease(filp, fl->fl_type, &fl);
3764 if (status) {
3765 locks_free_lock(fl);
3766 goto out_fput;
3767 }
Benny Halevycdc97502014-05-30 09:09:30 -04003768 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003769 spin_lock(&fp->fi_lock);
3770 /* Did the lease get broken before we took the lock? */
3771 status = -EAGAIN;
3772 if (fp->fi_had_conflict)
3773 goto out_unlock;
3774 /* Race breaker */
3775 if (fp->fi_lease) {
3776 status = 0;
3777 atomic_inc(&fp->fi_delegees);
3778 hash_delegation_locked(dp, fp);
3779 goto out_unlock;
3780 }
3781 fp->fi_lease = fl;
3782 fp->fi_deleg_file = filp;
3783 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003784 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003785 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003786 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003787 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003788out_unlock:
3789 spin_unlock(&fp->fi_lock);
3790 spin_unlock(&state_lock);
3791out_fput:
3792 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003793 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003794}
3795
Jeff Layton0b266932014-07-25 07:34:25 -04003796static struct nfs4_delegation *
3797nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3798 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003799{
Jeff Layton0b266932014-07-25 07:34:25 -04003800 int status;
3801 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003802
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003803 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003804 return ERR_PTR(-EAGAIN);
3805
3806 dp = alloc_init_deleg(clp, fh);
3807 if (!dp)
3808 return ERR_PTR(-ENOMEM);
3809
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003810 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003811 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003812 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003813 dp->dl_stid.sc_file = fp;
Jeff Layton417c6622014-07-21 09:34:57 -04003814 if (!fp->fi_lease) {
3815 spin_unlock(&fp->fi_lock);
3816 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003817 status = nfs4_setlease(dp);
3818 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003819 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003820 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003821 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003822 status = -EAGAIN;
3823 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003824 }
Benny Halevy931ee562014-05-30 09:09:27 -04003825 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003826 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003827out_unlock:
3828 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003829 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003830out:
3831 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003832 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003833 return ERR_PTR(status);
3834 }
3835 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003836}
3837
Benny Halevy4aa89132012-02-21 14:16:44 -08003838static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3839{
3840 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3841 if (status == -EAGAIN)
3842 open->op_why_no_deleg = WND4_CONTENTION;
3843 else {
3844 open->op_why_no_deleg = WND4_RESOURCE;
3845 switch (open->op_deleg_want) {
3846 case NFS4_SHARE_WANT_READ_DELEG:
3847 case NFS4_SHARE_WANT_WRITE_DELEG:
3848 case NFS4_SHARE_WANT_ANY_DELEG:
3849 break;
3850 case NFS4_SHARE_WANT_CANCEL:
3851 open->op_why_no_deleg = WND4_CANCELLED;
3852 break;
3853 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003854 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003855 }
3856 }
3857}
3858
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859/*
3860 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003861 *
3862 * Note we don't support write delegations, and won't until the vfs has
3863 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 */
3865static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003866nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3867 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868{
3869 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003870 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3871 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003872 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003873 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003875 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003876 open->op_recall = 0;
3877 switch (open->op_claim_type) {
3878 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003879 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003880 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003881 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3882 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003883 break;
3884 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003885 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003886 /*
3887 * Let's not give out any delegations till everyone's
3888 * had the chance to reclaim theirs....
3889 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003890 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003891 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003892 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003893 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003894 /*
3895 * Also, if the file was opened for write or
3896 * create, there's a good chance the client's
3897 * about to write to it, resulting in an
3898 * immediate recall (since we don't support
3899 * write delegations):
3900 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003901 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003902 goto out_no_deleg;
3903 if (open->op_create == NFS4_OPEN_CREATE)
3904 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003905 break;
3906 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003907 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003908 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003909 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003910 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003911 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003913 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003915 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003916 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003917 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003918 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003919 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003920out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003921 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3922 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003923 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003924 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003925 open->op_recall = 1;
3926 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003927
3928 /* 4.1 client asking for a delegation? */
3929 if (open->op_deleg_want)
3930 nfsd4_open_deleg_none_ext(open, status);
3931 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932}
3933
Benny Halevye27f49c2012-02-21 14:16:54 -08003934static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3935 struct nfs4_delegation *dp)
3936{
3937 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3938 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3939 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3940 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3941 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3942 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3943 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3944 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3945 }
3946 /* Otherwise the client must be confused wanting a delegation
3947 * it already has, therefore we don't return
3948 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3949 */
3950}
3951
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952/*
3953 * called with nfs4_lock_state() held.
3954 */
Al Virob37ad282006-10-19 23:28:59 -07003955__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3957{
Andy Adamson66689582009-04-03 08:28:45 +03003958 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003959 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003961 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003962 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003963 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 /*
3966 * Lookup file; if found, lookup stateid and check open request,
3967 * and check for delegations in the process of being recalled.
3968 * If not found, create the nfs4_file struct
3969 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003970 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04003971 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04003972 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003973 if (status)
3974 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003975 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04003977 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003978 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003979 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003980 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003981 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 }
3983
3984 /*
3985 * OPEN the file, or upgrade an existing OPEN.
3986 * If truncate fails, the OPEN fails.
3987 */
3988 if (stp) {
3989 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003990 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 if (status)
3992 goto out;
3993 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003994 stp = open->op_stp;
3995 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003996 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003997 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3998 if (status) {
3999 release_open_stateid(stp);
4000 goto out;
4001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004003 update_stateid(&stp->st_stid.sc_stateid);
4004 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
Benny Halevyd24433c2012-02-16 20:57:17 +02004006 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004007 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4008 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4009 open->op_why_no_deleg = WND4_NOT_WANTED;
4010 goto nodeleg;
4011 }
4012 }
4013
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 /*
4015 * Attempt to hand out a delegation. No error return, because the
4016 * OPEN succeeds even if we fail.
4017 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004018 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004019nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 status = nfs_ok;
4021
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004022 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004023 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004025 /* 4.1 client trying to upgrade/downgrade delegation? */
4026 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004027 open->op_deleg_want)
4028 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004029
NeilBrown13cd2182005-06-23 22:03:10 -07004030 if (fp)
4031 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004032 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05004033 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 /*
4035 * To finish the open response, we just need to set the rflags.
4036 */
4037 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004038 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004039 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004041 if (dp)
4042 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004043 if (stp)
4044 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045
4046 return status;
4047}
4048
Jeff Layton58fb12e2014-07-29 21:34:27 -04004049void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4050 struct nfsd4_open *open, __be32 status)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004051{
4052 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004053 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004054
Jeff Laytond3134b12014-07-29 21:34:32 -04004055 nfsd4_cstate_assign_replay(cstate, so);
4056 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004057 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004058 if (open->op_file)
4059 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004060 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004061 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004062}
4063
Al Virob37ad282006-10-19 23:28:59 -07004064__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004065nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4066 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067{
4068 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004069 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004070 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071
4072 nfs4_lock_state();
4073 dprintk("process_renew(%08x/%08x): starting\n",
4074 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004075 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004076 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004078 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004080 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004081 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 goto out;
4083 status = nfs_ok;
4084out:
4085 nfs4_unlock_state();
4086 return status;
4087}
4088
NeilBrowna76b4312005-06-23 22:04:01 -07004089static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004090nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004091{
Jeff Layton33dcc482012-04-10 11:08:48 -04004092 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004093 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004094 return;
4095
NeilBrowna76b4312005-06-23 22:04:01 -07004096 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004097 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004098 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004099 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004100 /*
4101 * Now that every NFSv4 client has had the chance to recover and
4102 * to see the (possibly new, possibly shorter) lease time, we
4103 * can safely set the next grace time to the current lease time:
4104 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03004105 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07004106}
4107
NeilBrownfd39ca92005-06-23 22:04:03 -07004108static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004109nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110{
4111 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004112 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 struct nfs4_delegation *dp;
4114 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004115 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004116 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
4118 nfs4_lock_state();
4119
4120 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004121 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004122 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004123 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004124 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 clp = list_entry(pos, struct nfs4_client, cl_lru);
4126 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4127 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004128 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 break;
4130 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004131 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004132 dprintk("NFSD: client in use (clientid %08x)\n",
4133 clp->cl_clientid.cl_id);
4134 continue;
4135 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004136 unhash_client_locked(clp);
4137 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004138 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004139 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004140 list_for_each_safe(pos, next, &reaplist) {
4141 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 dprintk("NFSD: purging unused client (clientid %08x)\n",
4143 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004144 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 expire_client(clp);
4146 }
Benny Halevycdc97502014-05-30 09:09:30 -04004147 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004148 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004150 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4151 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004153 t = dp->dl_time - cutoff;
4154 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 break;
4156 }
Jeff Layton42690672014-07-25 07:34:20 -04004157 unhash_delegation_locked(dp);
4158 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 }
Benny Halevycdc97502014-05-30 09:09:30 -04004160 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004161 while (!list_empty(&reaplist)) {
4162 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4163 dl_recall_lru);
4164 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004165 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004167 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004168 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
4169 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004170 t = oo->oo_time - cutoff;
4171 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 break;
4173 }
Jeff Laytond3134b12014-07-29 21:34:32 -04004174 release_last_closed_stateid(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04004176 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04004178 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179}
4180
Harvey Harrisona254b242008-02-20 12:49:00 -08004181static struct workqueue_struct *laundry_wq;
4182static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004183
4184static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004185laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186{
4187 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004188 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4189 work);
4190 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4191 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004193 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004195 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196}
4197
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004198static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004199{
Trond Myklebust11b91642014-07-29 21:34:08 -04004200 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004201 return nfserr_bad_stateid;
4202 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203}
4204
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004206access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004208 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4209 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4210 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211}
4212
4213static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004214access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004216 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4217 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218}
4219
4220static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004221__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222{
Al Virob37ad282006-10-19 23:28:59 -07004223 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224
J. Bruce Fields02921912010-07-29 15:16:59 -04004225 /* For lock stateid's, we test the parent open, not the lock: */
4226 if (stp->st_openstp)
4227 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004228 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004230 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 goto out;
4232 status = nfs_ok;
4233out:
4234 return status;
4235}
4236
Al Virob37ad282006-10-19 23:28:59 -07004237static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004238check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004240 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004242 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004243 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 * conflicting state; so we must wait out the grace period. */
4245 return nfserr_grace;
4246 } else if (flags & WR_STATE)
4247 return nfs4_share_conflict(current_fh,
4248 NFS4_SHARE_DENY_WRITE);
4249 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4250 return nfs4_share_conflict(current_fh,
4251 NFS4_SHARE_DENY_READ);
4252}
4253
4254/*
4255 * Allow READ/WRITE during grace period on recovered state only for files
4256 * that are not able to provide mandatory locking.
4257 */
4258static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004259grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004261 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262}
4263
J. Bruce Fields81b82962011-08-23 11:03:29 -04004264/* Returns true iff a is later than b: */
4265static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4266{
Jim Rees1a9357f2013-05-17 17:33:00 -04004267 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004268}
4269
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004270static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004271{
Andy Adamson66689582009-04-03 08:28:45 +03004272 /*
4273 * When sessions are used the stateid generation number is ignored
4274 * when it is zero.
4275 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004276 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004277 return nfs_ok;
4278
4279 if (in->si_generation == ref->si_generation)
4280 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004281
J. Bruce Fields0836f582008-01-26 19:08:12 -05004282 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004283 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004284 return nfserr_bad_stateid;
4285 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004286 * However, we could see a stateid from the past, even from a
4287 * non-buggy client. For example, if the client sends a lock
4288 * while some IO is outstanding, the lock may bump si_generation
4289 * while the IO is still in flight. The client could avoid that
4290 * situation by waiting for responses on all the IO requests,
4291 * but better performance may result in retrying IO that
4292 * receives an old_stateid error if requests are rarely
4293 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004294 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004295 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004296}
4297
Chuck Lever7df302f2012-05-29 13:56:37 -04004298static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004299{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004300 struct nfs4_stid *s;
4301 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004302 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004303
Chuck Lever7df302f2012-05-29 13:56:37 -04004304 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004305 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004306 /* Client debugging aid. */
4307 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4308 char addr_str[INET6_ADDRSTRLEN];
4309 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4310 sizeof(addr_str));
4311 pr_warn_ratelimited("NFSD: client %s testing state ID "
4312 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004313 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004314 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004315 spin_lock(&cl->cl_lock);
4316 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004317 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004318 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004319 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004320 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004321 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004322 switch (s->sc_type) {
4323 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004324 status = nfs_ok;
4325 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004326 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004327 status = nfserr_deleg_revoked;
4328 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004329 case NFS4_OPEN_STID:
4330 case NFS4_LOCK_STID:
4331 ols = openlockstateid(s);
4332 if (ols->st_stateowner->so_is_open_owner
4333 && !(openowner(ols->st_stateowner)->oo_flags
4334 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004335 status = nfserr_bad_stateid;
4336 else
4337 status = nfs_ok;
4338 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004339 default:
4340 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004341 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004342 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004343 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004344 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004345 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004346out_unlock:
4347 spin_unlock(&cl->cl_lock);
4348 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004349}
4350
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004351static __be32
4352nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4353 stateid_t *stateid, unsigned char typemask,
4354 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004355{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004356 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004357
4358 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4359 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004360 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004361 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004362 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004363 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004364 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004365 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004366 if (status)
4367 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004368 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004369 if (!*s)
4370 return nfserr_bad_stateid;
4371 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004372}
4373
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374/*
4375* Checks for stateid operations
4376*/
Al Virob37ad282006-10-19 23:28:59 -07004377__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004378nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004379 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004381 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004382 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004384 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004386 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004387 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004388 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 if (filpp)
4391 *filpp = NULL;
4392
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004393 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 return nfserr_grace;
4395
4396 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004397 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398
Trond Myklebust14bcab12014-04-18 14:44:07 -04004399 nfs4_lock_state();
4400
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004401 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004402 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004403 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004404 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004405 goto unlock_state;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004406 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4407 if (status)
4408 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004409 switch (s->sc_type) {
4410 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004411 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004412 status = nfs4_check_delegmode(dp, flags);
4413 if (status)
4414 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004415 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004416 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004417 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004418 WARN_ON_ONCE(1);
4419 status = nfserr_serverfault;
4420 goto out;
4421 }
Trond Myklebustde186432014-07-10 14:07:26 -04004422 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004423 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004424 break;
4425 case NFS4_OPEN_STID:
4426 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004427 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004428 status = nfs4_check_fh(current_fh, stp);
4429 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004431 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004432 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004434 status = nfs4_check_openmode(stp, flags);
4435 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004437 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004438 struct nfs4_file *fp = stp->st_stid.sc_file;
4439
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004440 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004441 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004442 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004443 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004444 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004445 break;
4446 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004447 status = nfserr_bad_stateid;
4448 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 }
4450 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004451 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004452 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004454 nfs4_put_stid(s);
4455unlock_state:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004456 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 return status;
4458}
4459
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004460/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004461 * Test if the stateid is valid
4462 */
4463__be32
4464nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4465 struct nfsd4_test_stateid *test_stateid)
4466{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004467 struct nfsd4_test_stateid_id *stateid;
4468 struct nfs4_client *cl = cstate->session->se_client;
4469
4470 nfs4_lock_state();
4471 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004472 stateid->ts_id_status =
4473 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004474 nfs4_unlock_state();
4475
Bryan Schumaker17456802011-07-13 10:50:48 -04004476 return nfs_ok;
4477}
4478
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004479__be32
4480nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4481 struct nfsd4_free_stateid *free_stateid)
4482{
4483 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004484 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004485 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004486 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004487 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004488 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004489
4490 nfs4_lock_state();
Jeff Layton1af71cc2014-07-29 21:34:14 -04004491 spin_lock(&cl->cl_lock);
4492 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004493 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004494 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004495 switch (s->sc_type) {
4496 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004497 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004498 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004499 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004500 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4501 if (ret)
4502 break;
4503 ret = nfserr_locks_held;
4504 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004505 case NFS4_LOCK_STID:
4506 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4507 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004508 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004509 stp = openlockstateid(s);
4510 ret = nfserr_locks_held;
4511 if (check_for_locks(stp->st_stid.sc_file,
4512 lockowner(stp->st_stateowner)))
4513 break;
4514 unhash_lock_stateid(stp);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004515 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004516 nfs4_put_stid(s);
4517 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004518 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004519 case NFS4_REVOKED_DELEG_STID:
4520 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004521 list_del_init(&dp->dl_recall_lru);
4522 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004523 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004524 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004525 goto out;
4526 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004527 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004528out_unlock:
4529 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004530out:
4531 nfs4_unlock_state();
4532 return ret;
4533}
4534
NeilBrown4c4cd222005-07-07 17:59:27 -07004535static inline int
4536setlkflg (int type)
4537{
4538 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4539 RD_STATE : WR_STATE;
4540}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004542static __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 -04004543{
4544 struct svc_fh *current_fh = &cstate->current_fh;
4545 struct nfs4_stateowner *sop = stp->st_stateowner;
4546 __be32 status;
4547
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004548 status = nfsd4_check_seqid(cstate, sop, seqid);
4549 if (status)
4550 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004551 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4552 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004553 /*
4554 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004555 * nfserr_replay_me from the previous step, and
4556 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004557 */
4558 return nfserr_bad_stateid;
4559 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4560 if (status)
4561 return status;
4562 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004563}
4564
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565/*
4566 * Checks for sequence id mutating operations.
4567 */
Al Virob37ad282006-10-19 23:28:59 -07004568static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004569nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004570 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004571 struct nfs4_ol_stateid **stpp,
4572 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004574 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004575 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004576 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004578 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4579 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004580
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004582 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004583 if (status)
4584 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004585 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004586 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004588 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004589 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004590 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004591 else
4592 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004593 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004594}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004595
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004596static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4597 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004598{
4599 __be32 status;
4600 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004601 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004603 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004604 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004605 if (status)
4606 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004607 oo = openowner(stp->st_stateowner);
4608 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4609 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004610 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004611 }
4612 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004613 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614}
4615
Al Virob37ad282006-10-19 23:28:59 -07004616__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004617nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004618 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619{
Al Virob37ad282006-10-19 23:28:59 -07004620 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004621 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004622 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004623 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624
Al Viroa6a9f182013-09-16 10:57:01 -04004625 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4626 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004628 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004629 if (status)
4630 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631
4632 nfs4_lock_state();
4633
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004634 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004635 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004636 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004637 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004638 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004639 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004640 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004641 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004642 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004643 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004644 update_stateid(&stp->st_stid.sc_stateid);
4645 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004646 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004647 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004648
Jeff Layton2a4317c2012-03-21 16:42:43 -04004649 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004650 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004651put_stateid:
4652 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004654 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004655 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 return status;
4657}
4658
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004659static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004661 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004662 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004663 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004664 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004665}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004666
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004667static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4668{
4669 switch (to_access) {
4670 case NFS4_SHARE_ACCESS_READ:
4671 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4672 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4673 break;
4674 case NFS4_SHARE_ACCESS_WRITE:
4675 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4676 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4677 break;
4678 case NFS4_SHARE_ACCESS_BOTH:
4679 break;
4680 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004681 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 }
4683}
4684
Al Virob37ad282006-10-19 23:28:59 -07004685__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004686nfsd4_open_downgrade(struct svc_rqst *rqstp,
4687 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004688 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689{
Al Virob37ad282006-10-19 23:28:59 -07004690 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004691 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004692 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693
Al Viroa6a9f182013-09-16 10:57:01 -04004694 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4695 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004697 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004698 if (od->od_deleg_want)
4699 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4700 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701
4702 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004703 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004704 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004705 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004708 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004709 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004711 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004713 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004714 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004716 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004718 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719
Jeff Laytonce0fc432012-05-11 09:45:14 -04004720 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004722 update_stateid(&stp->st_stid.sc_stateid);
4723 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004725put_stateid:
4726 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004728 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004729 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 return status;
4731}
4732
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004733static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4734{
Trond Myklebustacf92952014-06-30 11:48:37 -04004735 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytond83017f2014-07-29 21:34:42 -04004736 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004737
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004738 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04004739 spin_lock(&clp->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04004740 unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004741
Jeff Laytond83017f2014-07-29 21:34:42 -04004742 if (clp->cl_minorversion) {
4743 put_ol_stateid_locked(s, &reaplist);
4744 spin_unlock(&clp->cl_lock);
4745 free_ol_stateid_reaplist(&reaplist);
4746 } else {
4747 spin_unlock(&clp->cl_lock);
4748 free_ol_stateid_reaplist(&reaplist);
Jeff Laytond3134b12014-07-29 21:34:32 -04004749 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04004750 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004751}
4752
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753/*
4754 * nfs4_unlock_state() called after encode
4755 */
Al Virob37ad282006-10-19 23:28:59 -07004756__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004757nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004758 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759{
Al Virob37ad282006-10-19 23:28:59 -07004760 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004761 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004762 struct net *net = SVC_NET(rqstp);
4763 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764
Al Viroa6a9f182013-09-16 10:57:01 -04004765 dprintk("NFSD: nfsd4_close on file %pd\n",
4766 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767
4768 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004769 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4770 &close->cl_stateid,
4771 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004772 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004773 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004774 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004776 update_stateid(&stp->st_stid.sc_stateid);
4777 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004779 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004780
4781 /* put reference from nfs4_preprocess_seqid_op */
4782 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783out:
Jeff Layton58fb12e2014-07-29 21:34:27 -04004784 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 return status;
4786}
4787
Al Virob37ad282006-10-19 23:28:59 -07004788__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004789nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4790 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004792 struct nfs4_delegation *dp;
4793 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004794 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004795 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004796 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004798 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004799 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800
4801 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004802 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004803 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004804 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004805 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004806 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004807 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004808 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004809
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004810 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004811put_stateid:
4812 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004814 nfs4_unlock_state();
4815
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 return status;
4817}
4818
4819
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
Benny Halevy87df4de2008-12-15 19:42:03 +02004822static inline u64
4823end_offset(u64 start, u64 len)
4824{
4825 u64 end;
4826
4827 end = start + len;
4828 return end >= start ? end: NFS4_MAX_UINT64;
4829}
4830
4831/* last octet in a range */
4832static inline u64
4833last_byte_offset(u64 start, u64 len)
4834{
4835 u64 end;
4836
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004837 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004838 end = start + len;
4839 return end > start ? end - 1: NFS4_MAX_UINT64;
4840}
4841
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842/*
4843 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4844 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4845 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4846 * locking, this prevents us from being completely protocol-compliant. The
4847 * real solution to this problem is to start using unsigned file offsets in
4848 * the VFS, but this is a very deep change!
4849 */
4850static inline void
4851nfs4_transform_lock_offset(struct file_lock *lock)
4852{
4853 if (lock->fl_start < 0)
4854 lock->fl_start = OFFSET_MAX;
4855 if (lock->fl_end < 0)
4856 lock->fl_end = OFFSET_MAX;
4857}
4858
NeilBrownd5b90262006-04-10 22:55:22 -07004859/* Hack!: For now, we're defining this just so we can use a pointer to it
4860 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004861static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004862};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863
4864static inline void
4865nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4866{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004867 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868
NeilBrownd5b90262006-04-10 22:55:22 -07004869 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004870 lo = (struct nfs4_lockowner *) fl->fl_owner;
4871 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4872 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004873 if (!deny->ld_owner.data)
4874 /* We just don't care that much */
4875 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004876 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4877 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004878 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004879nevermind:
4880 deny->ld_owner.len = 0;
4881 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004882 deny->ld_clientid.cl_boot = 0;
4883 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 }
4885 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004886 deny->ld_length = NFS4_MAX_UINT64;
4887 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4889 deny->ld_type = NFS4_READ_LT;
4890 if (fl->fl_type != F_RDLCK)
4891 deny->ld_type = NFS4_WRITE_LT;
4892}
4893
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004894static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004895find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004896 struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004898 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004899 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900
Trond Myklebustd4f04892014-07-29 21:34:36 -04004901 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4902 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004903 if (so->so_is_open_owner)
4904 continue;
Trond Myklebustd4f04892014-07-29 21:34:36 -04004905 if (!same_owner_str(so, owner))
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004906 continue;
Jeff Layton5db1c032014-07-29 21:34:28 -04004907 atomic_inc(&so->so_count);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004908 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 }
4910 return NULL;
4911}
4912
Trond Myklebustc58c6612014-07-29 21:34:35 -04004913static struct nfs4_lockowner *
4914find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004915 struct nfs4_client *clp)
Trond Myklebustc58c6612014-07-29 21:34:35 -04004916{
4917 struct nfs4_lockowner *lo;
4918
Trond Myklebustd4f04892014-07-29 21:34:36 -04004919 spin_lock(&clp->cl_lock);
4920 lo = find_lockowner_str_locked(clid, owner, clp);
4921 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004922 return lo;
4923}
4924
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004925static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4926{
Trond Myklebustc58c6612014-07-29 21:34:35 -04004927 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004928}
4929
Jeff Layton6b180f02014-07-29 21:34:26 -04004930static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4931{
4932 struct nfs4_lockowner *lo = lockowner(sop);
4933
4934 kmem_cache_free(lockowner_slab, lo);
4935}
4936
4937static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004938 .so_unhash = nfs4_unhash_lockowner,
4939 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004940};
4941
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942/*
4943 * Alloc a lock owner structure.
4944 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004945 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004947 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004949static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004950alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
4951 struct nfs4_ol_stateid *open_stp,
4952 struct nfsd4_lock *lock)
4953{
Trond Myklebustc58c6612014-07-29 21:34:35 -04004954 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004956 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4957 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004959 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4960 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04004961 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04004962 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04004963 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004964 ret = find_lockowner_str_locked(&clp->cl_clientid,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004965 &lock->lk_new_owner, clp);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004966 if (ret == NULL) {
4967 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004968 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004969 ret = lo;
4970 } else
4971 nfs4_free_lockowner(&lo->lo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04004972 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004973 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974}
4975
Jeff Layton356a95e2014-07-29 21:34:13 -04004976static void
4977init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
4978 struct nfs4_file *fp, struct inode *inode,
4979 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004981 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982
Jeff Layton356a95e2014-07-29 21:34:13 -04004983 lockdep_assert_held(&clp->cl_lock);
4984
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004985 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004986 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004987 stp->st_stateowner = &lo->lo_owner;
Trond Myklebuste4f1dd72014-07-29 21:34:30 -04004988 atomic_inc(&lo->lo_owner.so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07004989 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004990 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04004991 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004992 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004994 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004995 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04004996 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004997 spin_lock(&fp->fi_lock);
4998 list_add(&stp->st_perfile, &fp->fi_stateids);
4999 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000}
5001
Jeff Laytonc53530d2014-06-30 11:48:39 -04005002static struct nfs4_ol_stateid *
5003find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5004{
5005 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005006 struct nfs4_client *clp = lo->lo_owner.so_client;
5007
5008 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005009
5010 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005011 if (lst->st_stid.sc_file == fp) {
5012 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005013 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005014 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005015 }
5016 return NULL;
5017}
5018
Jeff Layton356a95e2014-07-29 21:34:13 -04005019static struct nfs4_ol_stateid *
5020find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5021 struct inode *inode, struct nfs4_ol_stateid *ost,
5022 bool *new)
5023{
5024 struct nfs4_stid *ns = NULL;
5025 struct nfs4_ol_stateid *lst;
5026 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5027 struct nfs4_client *clp = oo->oo_owner.so_client;
5028
5029 spin_lock(&clp->cl_lock);
5030 lst = find_lock_stateid(lo, fi);
5031 if (lst == NULL) {
5032 spin_unlock(&clp->cl_lock);
5033 ns = nfs4_alloc_stid(clp, stateid_slab);
5034 if (ns == NULL)
5035 return NULL;
5036
5037 spin_lock(&clp->cl_lock);
5038 lst = find_lock_stateid(lo, fi);
5039 if (likely(!lst)) {
5040 lst = openlockstateid(ns);
5041 init_lock_stateid(lst, lo, fi, inode, ost);
5042 ns = NULL;
5043 *new = true;
5044 }
5045 }
5046 spin_unlock(&clp->cl_lock);
5047 if (ns)
5048 nfs4_put_stid(ns);
5049 return lst;
5050}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005051
NeilBrownfd39ca92005-06-23 22:04:03 -07005052static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053check_lock_length(u64 offset, u64 length)
5054{
Benny Halevy87df4de2008-12-15 19:42:03 +02005055 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 LOFF_OVERFLOW(offset, length)));
5057}
5058
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005059static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005060{
Trond Myklebust11b91642014-07-29 21:34:08 -04005061 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005062
Jeff Layton7214e862014-07-10 14:07:33 -04005063 lockdep_assert_held(&fp->fi_lock);
5064
Jeff Layton82c5ff12012-05-11 09:45:13 -04005065 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005066 return;
Jeff Layton12659652014-07-10 14:07:28 -04005067 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005068 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005069}
5070
Jeff Layton356a95e2014-07-29 21:34:13 -04005071static __be32
5072lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5073 struct nfs4_ol_stateid *ost,
5074 struct nfsd4_lock *lock,
5075 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005076{
Jeff Layton5db1c032014-07-29 21:34:28 -04005077 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005078 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005079 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5080 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005081 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005082 struct nfs4_lockowner *lo;
5083 unsigned int strhashval;
5084
Trond Myklebustd4f04892014-07-29 21:34:36 -04005085 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005086 if (!lo) {
Trond Myklebustd4f04892014-07-29 21:34:36 -04005087 strhashval = ownerstr_hashval(&lock->v.new.owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005088 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5089 if (lo == NULL)
5090 return nfserr_jukebox;
5091 } else {
5092 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005093 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005094 if (!cstate->minorversion &&
5095 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005096 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005097 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005098
Jeff Layton356a95e2014-07-29 21:34:13 -04005099 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005100 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005101 status = nfserr_jukebox;
5102 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005103 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005104 status = nfs_ok;
5105out:
5106 nfs4_put_stateowner(&lo->lo_owner);
5107 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005108}
5109
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110/*
5111 * LOCK operation
5112 */
Al Virob37ad282006-10-19 23:28:59 -07005113__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005114nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005115 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005117 struct nfs4_openowner *open_sop = NULL;
5118 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005119 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005120 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005121 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005122 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005123 struct file_lock *file_lock = NULL;
5124 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005125 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005126 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005127 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005128 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005129 struct net *net = SVC_NET(rqstp);
5130 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131
5132 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5133 (long long) lock->lk_offset,
5134 (long long) lock->lk_length);
5135
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 if (check_lock_length(lock->lk_offset, lock->lk_length))
5137 return nfserr_inval;
5138
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005139 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005140 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005141 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5142 return status;
5143 }
5144
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 nfs4_lock_state();
5146
5147 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005148 if (nfsd4_has_session(cstate))
5149 /* See rfc 5661 18.10.3: given clientid is ignored: */
5150 memcpy(&lock->v.new.clientid,
5151 &cstate->session->se_client->cl_clientid,
5152 sizeof(clientid_t));
5153
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005155 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005159 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 lock->lk_new_open_seqid,
5161 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005162 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005163 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005165 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005166 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005167 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005168 &lock->v.new.clientid))
5169 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005170 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005171 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005172 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005173 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005174 lock->lk_old_lock_seqid,
5175 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005176 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005177 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005178 if (status)
5179 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005180 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005182 lkflg = setlkflg(lock->lk_type);
5183 status = nfs4_check_openmode(lock_stp, lkflg);
5184 if (status)
5185 goto out;
5186
NeilBrown0dd395d2005-07-07 17:59:15 -07005187 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005188 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005189 goto out;
5190 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005191 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005192 goto out;
5193
Jeff Layton21179d82012-08-21 08:03:32 -04005194 file_lock = locks_alloc_lock();
5195 if (!file_lock) {
5196 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5197 status = nfserr_jukebox;
5198 goto out;
5199 }
5200
Trond Myklebust11b91642014-07-29 21:34:08 -04005201 fp = lock_stp->st_stid.sc_file;
Jeff Layton21179d82012-08-21 08:03:32 -04005202 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 switch (lock->lk_type) {
5204 case NFS4_READ_LT:
5205 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005206 spin_lock(&fp->fi_lock);
5207 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005208 if (filp)
5209 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005210 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005211 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005212 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 case NFS4_WRITE_LT:
5214 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005215 spin_lock(&fp->fi_lock);
5216 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005217 if (filp)
5218 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005219 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005220 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005221 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 default:
5223 status = nfserr_inval;
5224 goto out;
5225 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005226 if (!filp) {
5227 status = nfserr_openmode;
5228 goto out;
5229 }
Jeff Layton21179d82012-08-21 08:03:32 -04005230 file_lock->fl_owner = (fl_owner_t)lock_sop;
5231 file_lock->fl_pid = current->tgid;
5232 file_lock->fl_file = filp;
5233 file_lock->fl_flags = FL_POSIX;
5234 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5235 file_lock->fl_start = lock->lk_offset;
5236 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5237 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238
Jeff Layton21179d82012-08-21 08:03:32 -04005239 conflock = locks_alloc_lock();
5240 if (!conflock) {
5241 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5242 status = nfserr_jukebox;
5243 goto out;
5244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245
Jeff Layton21179d82012-08-21 08:03:32 -04005246 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005247 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005249 update_stateid(&lock_stp->st_stid.sc_stateid);
5250 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005252 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005253 break;
5254 case (EAGAIN): /* conflock holds conflicting lock */
5255 status = nfserr_denied;
5256 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005257 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005258 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 case (EDEADLK):
5260 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005261 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005262 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005263 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005264 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005265 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267out:
Trond Myklebustde186432014-07-10 14:07:26 -04005268 if (filp)
5269 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005270 if (lock_stp) {
5271 /* Bump seqid manually if the 4.0 replay owner is openowner */
5272 if (cstate->replay_owner &&
5273 cstate->replay_owner != &lock_sop->lo_owner &&
5274 seqid_mutating_err(ntohl(status)))
5275 lock_sop->lo_owner.so_seqid++;
5276
5277 /*
5278 * If this is a new, never-before-used stateid, and we are
5279 * returning an error, then just go ahead and release it.
5280 */
5281 if (status && new)
5282 release_lock_stateid(lock_stp);
5283
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005284 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005285 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005286 if (open_stp)
5287 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005288 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005289 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005290 if (file_lock)
5291 locks_free_lock(file_lock);
5292 if (conflock)
5293 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 return status;
5295}
5296
5297/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005298 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5299 * so we do a temporary open here just to get an open file to pass to
5300 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5301 * inode operation.)
5302 */
Al Viro04da6e92012-04-13 00:00:04 -04005303static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005304{
5305 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005306 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5307 if (!err) {
5308 err = nfserrno(vfs_test_lock(file, lock));
5309 nfsd_close(file);
5310 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005311 return err;
5312}
5313
5314/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 * LOCKT operation
5316 */
Al Virob37ad282006-10-19 23:28:59 -07005317__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005318nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5319 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320{
Jeff Layton21179d82012-08-21 08:03:32 -04005321 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005322 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005323 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005324 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005326 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 return nfserr_grace;
5328
5329 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5330 return nfserr_inval;
5331
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 nfs4_lock_state();
5333
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005334 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005335 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005336 if (status)
5337 goto out;
5338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005340 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342
Jeff Layton21179d82012-08-21 08:03:32 -04005343 file_lock = locks_alloc_lock();
5344 if (!file_lock) {
5345 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5346 status = nfserr_jukebox;
5347 goto out;
5348 }
5349 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 switch (lockt->lt_type) {
5351 case NFS4_READ_LT:
5352 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005353 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 break;
5355 case NFS4_WRITE_LT:
5356 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005357 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 break;
5359 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005360 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 status = nfserr_inval;
5362 goto out;
5363 }
5364
Trond Myklebustd4f04892014-07-29 21:34:36 -04005365 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5366 cstate->clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005367 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005368 file_lock->fl_owner = (fl_owner_t)lo;
5369 file_lock->fl_pid = current->tgid;
5370 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371
Jeff Layton21179d82012-08-21 08:03:32 -04005372 file_lock->fl_start = lockt->lt_offset;
5373 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374
Jeff Layton21179d82012-08-21 08:03:32 -04005375 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376
Jeff Layton21179d82012-08-21 08:03:32 -04005377 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005378 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005379 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005380
Jeff Layton21179d82012-08-21 08:03:32 -04005381 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005383 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 }
5385out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005386 if (lo)
5387 nfs4_put_stateowner(&lo->lo_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005389 if (file_lock)
5390 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 return status;
5392}
5393
Al Virob37ad282006-10-19 23:28:59 -07005394__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005395nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005396 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005398 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005400 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005401 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005402 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005403 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5404
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5406 (long long) locku->lu_offset,
5407 (long long) locku->lu_length);
5408
5409 if (check_lock_length(locku->lu_offset, locku->lu_length))
5410 return nfserr_inval;
5411
5412 nfs4_lock_state();
5413
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005414 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005415 &locku->lu_stateid, NFS4_LOCK_STID,
5416 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005417 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005419 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005420 if (!filp) {
5421 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005422 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005423 }
Jeff Layton21179d82012-08-21 08:03:32 -04005424 file_lock = locks_alloc_lock();
5425 if (!file_lock) {
5426 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5427 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005428 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005429 }
5430 locks_init_lock(file_lock);
5431 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005432 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005433 file_lock->fl_pid = current->tgid;
5434 file_lock->fl_file = filp;
5435 file_lock->fl_flags = FL_POSIX;
5436 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5437 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438
Jeff Layton21179d82012-08-21 08:03:32 -04005439 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5440 locku->lu_length);
5441 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442
Jeff Layton21179d82012-08-21 08:03:32 -04005443 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005444 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005445 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 goto out_nfserr;
5447 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005448 update_stateid(&stp->st_stid.sc_stateid);
5449 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005450fput:
5451 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005452put_stateid:
5453 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005455 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005456 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005457 if (file_lock)
5458 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 return status;
5460
5461out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005462 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005463 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464}
5465
5466/*
5467 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005468 * true: locks held by lockowner
5469 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005471static bool
5472check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473{
5474 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005475 int status = false;
5476 struct file *filp = find_any_file(fp);
5477 struct inode *inode;
5478
5479 if (!filp) {
5480 /* Any valid lock stateid should have some sort of access */
5481 WARN_ON_ONCE(1);
5482 return status;
5483 }
5484
5485 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486
Jeff Layton1c8c6012013-06-21 08:58:15 -04005487 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005489 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005490 status = true;
5491 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005494 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005495 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 return status;
5497}
5498
Al Virob37ad282006-10-19 23:28:59 -07005499__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005500nfsd4_release_lockowner(struct svc_rqst *rqstp,
5501 struct nfsd4_compound_state *cstate,
5502 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503{
5504 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005505 struct nfs4_stateowner *sop;
5506 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005507 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005509 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005510 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005511 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005512 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513
5514 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5515 clid->cl_boot, clid->cl_id);
5516
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 nfs4_lock_state();
5518
Jeff Layton4b24ca72014-06-30 11:48:44 -04005519 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005520 if (status)
5521 goto out;
5522
Trond Myklebustd4f04892014-07-29 21:34:36 -04005523 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005524 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005525 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005526 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005527 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005528
5529 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005530 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005531
Jeff Layton882e9d22014-07-29 21:34:37 -04005532 /* see if there are still any locks associated with it */
5533 lo = lockowner(sop);
5534 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5535 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5536 status = nfserr_locks_held;
5537 spin_unlock(&clp->cl_lock);
5538 goto out;
5539 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005540 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005541
5542 atomic_inc(&sop->so_count);
5543 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005544 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005545 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005546 if (lo)
5547 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548out:
5549 nfs4_unlock_state();
5550 return status;
5551}
5552
5553static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005554alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555{
NeilBrowna55370a2005-06-23 22:03:52 -07005556 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557}
5558
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005559bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005560nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005561{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005562 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005563
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005564 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005565 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005566}
5567
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568/*
5569 * failure => all reset bets are off, nfserr_no_grace...
5570 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005571struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005572nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573{
5574 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005575 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576
NeilBrowna55370a2005-06-23 22:03:52 -07005577 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5578 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005579 if (crp) {
5580 strhashval = clientstr_hashval(name);
5581 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005582 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005583 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005584 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005585 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005586 }
5587 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588}
5589
Jeff Layton2a4317c2012-03-21 16:42:43 -04005590void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005591nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005592{
5593 list_del(&crp->cr_strhash);
5594 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005595 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005596}
5597
5598void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005599nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600{
5601 struct nfs4_client_reclaim *crp = NULL;
5602 int i;
5603
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005605 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5606 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005608 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 }
5610 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005611 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612}
5613
5614/*
5615 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005616struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005617nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618{
5619 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 struct nfs4_client_reclaim *crp = NULL;
5621
Jeff Layton278c9312012-11-12 15:00:52 -05005622 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623
Jeff Layton278c9312012-11-12 15:00:52 -05005624 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005625 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005626 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 return crp;
5628 }
5629 }
5630 return NULL;
5631}
5632
5633/*
5634* Called from OPEN. Look for clientid in reclaim list.
5635*/
Al Virob37ad282006-10-19 23:28:59 -07005636__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005637nfs4_check_open_reclaim(clientid_t *clid,
5638 struct nfsd4_compound_state *cstate,
5639 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005641 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005642
5643 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005644 status = lookup_clientid(clid, cstate, nn);
5645 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005646 return nfserr_reclaim_bad;
5647
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005648 if (nfsd4_client_record_check(cstate->clp))
5649 return nfserr_reclaim_bad;
5650
5651 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652}
5653
Bryan Schumaker65178db2011-11-01 13:35:21 -04005654#ifdef CONFIG_NFSD_FAULT_INJECTION
5655
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005656u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5657{
Jeff Laytond20c11d2014-07-30 08:27:07 -04005658 __be32 ret;
5659 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
5660
5661 spin_lock(&nn->client_lock);
5662 ret = mark_client_expired_locked(clp);
5663 spin_unlock(&nn->client_lock);
5664 if (ret != nfs_ok)
J. Bruce Fields221a6872013-04-01 22:23:49 -04005665 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005666 expire_client(clp);
5667 return 1;
5668}
5669
Bryan Schumaker184c1842012-11-29 11:40:44 -05005670u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5671{
5672 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005673 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005674 printk(KERN_INFO "NFS Client: %s\n", buf);
5675 return 1;
5676}
5677
5678static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5679 const char *type)
5680{
5681 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005682 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005683 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5684}
5685
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005686static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5687 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005688{
5689 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005690 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005691 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005692 u64 count = 0;
5693
5694 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005695 list_for_each_entry_safe(stp, st_next,
5696 &oop->oo_owner.so_stateids, st_perstateowner) {
5697 list_for_each_entry_safe(lst, lst_next,
5698 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005699 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005700 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005701 if (++count == max)
5702 return count;
5703 }
5704 }
5705 }
5706
5707 return count;
5708}
5709
5710u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5711{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005712 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005713}
5714
Bryan Schumaker184c1842012-11-29 11:40:44 -05005715u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5716{
5717 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5718 nfsd_print_count(clp, count, "locked files");
5719 return count;
5720}
5721
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005722static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5723{
5724 struct nfs4_openowner *oop, *next;
5725 u64 count = 0;
5726
5727 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5728 if (func)
5729 func(oop);
5730 if (++count == max)
5731 break;
5732 }
5733
5734 return count;
5735}
5736
5737u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5738{
5739 return nfsd_foreach_client_open(clp, max, release_openowner);
5740}
5741
Bryan Schumaker184c1842012-11-29 11:40:44 -05005742u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5743{
5744 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5745 nfsd_print_count(clp, count, "open files");
5746 return count;
5747}
5748
Bryan Schumaker269de302012-11-29 11:40:42 -05005749static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5750 struct list_head *victims)
5751{
5752 struct nfs4_delegation *dp, *next;
5753 u64 count = 0;
5754
Benny Halevycdc97502014-05-30 09:09:30 -04005755 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005756 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005757 if (victims) {
5758 /*
5759 * It's not safe to mess with delegations that have a
5760 * non-zero dl_time. They might have already been broken
5761 * and could be processed by the laundromat outside of
5762 * the state_lock. Just leave them be.
5763 */
5764 if (dp->dl_time != 0)
5765 continue;
5766
Jeff Layton42690672014-07-25 07:34:20 -04005767 unhash_delegation_locked(dp);
5768 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005769 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005770 if (++count == max)
5771 break;
5772 }
5773 return count;
5774}
5775
5776u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5777{
5778 struct nfs4_delegation *dp, *next;
5779 LIST_HEAD(victims);
5780 u64 count;
5781
Benny Halevycdc97502014-05-30 09:09:30 -04005782 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005783 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005784 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005785
Jeff Layton2d4a5322014-07-25 07:34:21 -04005786 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5787 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005788 revoke_delegation(dp);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005789 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005790
5791 return count;
5792}
5793
5794u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5795{
Jeff Laytondff13992014-07-08 14:02:49 -04005796 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005797 LIST_HEAD(victims);
5798 u64 count;
5799
Benny Halevycdc97502014-05-30 09:09:30 -04005800 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005801 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005802 while (!list_empty(&victims)) {
5803 dp = list_first_entry(&victims, struct nfs4_delegation,
5804 dl_recall_lru);
5805 list_del_init(&dp->dl_recall_lru);
5806 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005807 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005808 }
Benny Halevycdc97502014-05-30 09:09:30 -04005809 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005810
5811 return count;
5812}
5813
Bryan Schumaker184c1842012-11-29 11:40:44 -05005814u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5815{
5816 u64 count = 0;
5817
Benny Halevycdc97502014-05-30 09:09:30 -04005818 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005819 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005820 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005821
5822 nfsd_print_count(clp, count, "delegations");
5823 return count;
5824}
5825
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005826u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005827{
5828 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005829 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005830 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005831
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005832 if (!nfsd_netns_ready(nn))
5833 return 0;
5834
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005835 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005836 count += func(clp, max - count);
5837 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005838 break;
5839 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005840
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005841 return count;
5842}
5843
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005844struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5845{
5846 struct nfs4_client *clp;
5847 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5848
5849 if (!nfsd_netns_ready(nn))
5850 return NULL;
5851
5852 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5853 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5854 return clp;
5855 }
5856 return NULL;
5857}
5858
Bryan Schumaker65178db2011-11-01 13:35:21 -04005859#endif /* CONFIG_NFSD_FAULT_INJECTION */
5860
Meelap Shahc2f1a552007-07-17 04:04:39 -07005861/*
5862 * Since the lifetime of a delegation isn't limited to that of an open, a
5863 * client may quite reasonably hang on to a delegation as long as it has
5864 * the inode cached. This becomes an obvious problem the first time a
5865 * client's inode cache approaches the size of the server's total memory.
5866 *
5867 * For now we avoid this problem by imposing a hard limit on the number
5868 * of delegations, which varies according to the server's memory size.
5869 */
5870static void
5871set_max_delegations(void)
5872{
5873 /*
5874 * Allow at most 4 delegations per megabyte of RAM. Quick
5875 * estimates suggest that in the worst case (where every delegation
5876 * is for a different inode), a delegation could take about 1.5K,
5877 * giving a worst case usage of about 6% of memory.
5878 */
5879 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5880}
5881
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005882static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005883{
5884 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5885 int i;
5886
5887 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5888 CLIENT_HASH_SIZE, GFP_KERNEL);
5889 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005890 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005891 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5892 CLIENT_HASH_SIZE, GFP_KERNEL);
5893 if (!nn->unconf_id_hashtbl)
5894 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005895 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5896 SESSION_HASH_SIZE, GFP_KERNEL);
5897 if (!nn->sessionid_hashtbl)
5898 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005899
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005900 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005901 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005902 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005903 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005904 for (i = 0; i < SESSION_HASH_SIZE; i++)
5905 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005906 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005907 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005908 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005909 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005910 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005911 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005912
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005913 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005914 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005915
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005916 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005917
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005918err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005919 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005920err_unconf_id:
5921 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005922err:
5923 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005924}
5925
5926static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005927nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005928{
5929 int i;
5930 struct nfs4_client *clp = NULL;
5931 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5932
5933 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5934 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5935 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5936 destroy_client(clp);
5937 }
5938 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005939
Kinglong Mee2b905632014-03-26 22:09:30 +08005940 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5941 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5942 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5943 destroy_client(clp);
5944 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005945 }
5946
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005947 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005948 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005949 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005950 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005951}
5952
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005953int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005954nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005955{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005956 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005957 int ret;
5958
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005959 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005960 if (ret)
5961 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005962 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005963 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005964 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005965 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005966 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005967 nn->nfsd4_grace, net);
5968 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005969 return 0;
5970}
5971
5972/* initialization to perform when the nfsd service is started: */
5973
5974int
5975nfs4_state_start(void)
5976{
5977 int ret;
5978
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005979 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005980 if (ret)
5981 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005982 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005983 if (laundry_wq == NULL) {
5984 ret = -ENOMEM;
5985 goto out_recovery;
5986 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005987 ret = nfsd4_create_callback_queue();
5988 if (ret)
5989 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005990
Meelap Shahc2f1a552007-07-17 04:04:39 -07005991 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005992
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005993 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005994
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005995out_free_laundry:
5996 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005997out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005998 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999}
6000
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006001void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006002nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006006 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006008 cancel_delayed_work_sync(&nn->laundromat_work);
6009 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05006010
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04006011 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04006013 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006014 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006016 unhash_delegation_locked(dp);
6017 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 }
Benny Halevycdc97502014-05-30 09:09:30 -04006019 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020 list_for_each_safe(pos, next, &reaplist) {
6021 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006022 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04006023 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 }
6025
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006026 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006027 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04006028 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029}
6030
6031void
6032nfs4_state_shutdown(void)
6033{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006034 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006035 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006037
6038static void
6039get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6040{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006041 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6042 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006043}
6044
6045static void
6046put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6047{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006048 if (cstate->minorversion) {
6049 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6050 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6051 }
6052}
6053
6054void
6055clear_current_stateid(struct nfsd4_compound_state *cstate)
6056{
6057 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006058}
6059
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006060/*
6061 * functions to set current state id
6062 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006063void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006064nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6065{
6066 put_stateid(cstate, &odp->od_stateid);
6067}
6068
6069void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006070nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6071{
6072 put_stateid(cstate, &open->op_stateid);
6073}
6074
6075void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006076nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6077{
6078 put_stateid(cstate, &close->cl_stateid);
6079}
6080
6081void
6082nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6083{
6084 put_stateid(cstate, &lock->lk_resp_stateid);
6085}
6086
6087/*
6088 * functions to consume current state id
6089 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006090
6091void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006092nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6093{
6094 get_stateid(cstate, &odp->od_stateid);
6095}
6096
6097void
6098nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6099{
6100 get_stateid(cstate, &drp->dr_stateid);
6101}
6102
6103void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006104nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6105{
6106 get_stateid(cstate, &fsp->fr_stateid);
6107}
6108
6109void
6110nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6111{
6112 get_stateid(cstate, &setattr->sa_stateid);
6113}
6114
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006115void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006116nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6117{
6118 get_stateid(cstate, &close->cl_stateid);
6119}
6120
6121void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006122nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006123{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006124 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006125}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006126
6127void
6128nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6129{
6130 get_stateid(cstate, &read->rd_stateid);
6131}
6132
6133void
6134nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6135{
6136 get_stateid(cstate, &write->wr_stateid);
6137}