blob: f9d077d800ee7d1214f2ad1334f4fc6f8bc52dfb [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 Fieldsabcdff02013-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 Fieldsabcdff02013-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 Fieldsabcdff02013-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 Fieldsabcdff02013-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 Fieldsabcdff02013-03-14 19:55:33 -04002628out_client_lock:
2629 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002630out:
J. Bruce Fieldsabcdff02013-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 Adamsonf9bb94c2009-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 Adamsonf9bb94c2009-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{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002829 struct nfs4_client *conf, *unconf;
2830 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002831 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002832 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002833
2834 nfs4_lock_state();
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002835 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002836 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002837 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002838 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002839
2840 if (conf) {
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 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002845 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08002846 } else if (unconf)
2847 clp = unconf;
2848 else {
2849 status = nfserr_stale_clientid;
2850 goto out;
2851 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002852 if (!mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002853 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002854 status = nfserr_wrong_cred;
2855 goto out;
2856 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002857 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002858out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002859 spin_unlock(&nn->client_lock);
Mi Jinlong345c2842011-10-20 17:51:39 +08002860 nfs4_unlock_state();
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002861 if (clp)
2862 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002863 return status;
2864}
2865
Andy Adamson069b6ad2009-04-03 08:27:58 +03002866__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002867nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2868{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002869 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002870
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002871 if (rc->rca_one_fs) {
2872 if (!cstate->current_fh.fh_dentry)
2873 return nfserr_nofilehandle;
2874 /*
2875 * We don't take advantage of the rca_one_fs case.
2876 * That's OK, it's optional, we can safely ignore it.
2877 */
2878 return nfs_ok;
2879 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002880
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002881 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002882 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002883 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2884 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002885 goto out;
2886
2887 status = nfserr_stale_clientid;
2888 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002889 /*
2890 * The following error isn't really legal.
2891 * But we only get here if the client just explicitly
2892 * destroyed the client. Surely it no longer cares what
2893 * error it gets back on an operation for the dead
2894 * client.
2895 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002896 goto out;
2897
2898 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002899 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002900out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002901 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002902 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002903}
2904
2905__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002906nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2907 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002909 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002911 struct nfs4_client *conf, *new;
2912 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002913 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002914 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2915
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002916 new = create_client(clname, rqstp, &clverifier);
2917 if (new == NULL)
2918 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002919 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 nfs4_lock_state();
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002921 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002922 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002923 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002924 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002926 if (clp_used_exchangeid(conf))
2927 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002928 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002929 char addr_str[INET6_ADDRSTRLEN];
2930 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2931 sizeof(addr_str));
2932 dprintk("NFSD: setclientid: string in use by client "
2933 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 goto out;
2935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002937 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002938 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002939 unhash_client_locked(unconf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002940 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002941 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002943 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002944 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002945 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002946 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002947 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2949 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2950 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002951 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 status = nfs_ok;
2953out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002954 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 nfs4_unlock_state();
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002956 if (new)
2957 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002958 if (unconf)
2959 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 return status;
2961}
2962
2963
Al Virob37ad282006-10-19 23:28:59 -07002964__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002965nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2966 struct nfsd4_compound_state *cstate,
2967 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968{
NeilBrown21ab45a2005-06-23 22:04:14 -07002969 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002970 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2972 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002973 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002974 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002976 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002979
Jeff Laytond20c11d2014-07-30 08:27:07 -04002980 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002981 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002982 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002983 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002984 * We try hard to give out unique clientid's, so if we get an
2985 * attempt to confirm the same clientid with a different cred,
2986 * there's a bug somewhere. Let's charitably assume it's our
2987 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002988 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002989 status = nfserr_serverfault;
2990 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2991 goto out;
2992 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2993 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002994 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002995 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2996 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002998 else /* case 4: client hasn't noticed we rebooted yet? */
2999 status = nfserr_stale_clientid;
3000 goto out;
3001 }
3002 status = nfs_ok;
3003 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003004 old = unconf;
3005 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003006 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003007 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003008 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3009 if (old) {
3010 status = mark_client_expired_locked(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003011 if (status)
3012 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003013 unhash_client_locked(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003014 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003015 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003016 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003017 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003018 get_client_locked(conf);
3019 spin_unlock(&nn->client_lock);
3020 nfsd4_probe_callback(conf);
3021 spin_lock(&nn->client_lock);
3022 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003024 spin_unlock(&nn->client_lock);
3025 if (old)
3026 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 nfs4_unlock_state();
3028 return status;
3029}
3030
J. Bruce Fields32513b42011-10-13 16:00:16 -04003031static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003033 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3034}
3035
3036/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003037static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003038{
Trond Myklebustca943212014-07-23 16:17:39 -04003039 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
Trond Myklebust950e0112014-06-30 11:48:31 -04003041 lockdep_assert_held(&state_lock);
3042
J. Bruce Fields32513b42011-10-13 16:00:16 -04003043 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003044 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003045 INIT_LIST_HEAD(&fp->fi_stateids);
3046 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003047 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003048 fp->fi_had_conflict = false;
3049 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003050 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003051 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3052 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04003053 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054}
3055
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003056void
NeilBrowne60d4392005-06-23 22:03:01 -07003057nfsd4_free_slabs(void)
3058{
Christoph Hellwigabf11352014-05-21 07:43:03 -07003059 kmem_cache_destroy(openowner_slab);
3060 kmem_cache_destroy(lockowner_slab);
3061 kmem_cache_destroy(file_slab);
3062 kmem_cache_destroy(stateid_slab);
3063 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003064}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
Bryan Schumaker72083392011-11-01 15:24:59 -04003066int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067nfsd4_init_slabs(void)
3068{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003069 openowner_slab = kmem_cache_create("nfsd4_openowners",
3070 sizeof(struct nfs4_openowner), 0, 0, NULL);
3071 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003072 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003073 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003074 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003075 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003076 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003077 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003078 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003079 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003080 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003081 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003082 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003083 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003084 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003085 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003086 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003087 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003088 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003090
3091out_free_stateid_slab:
3092 kmem_cache_destroy(stateid_slab);
3093out_free_file_slab:
3094 kmem_cache_destroy(file_slab);
3095out_free_lockowner_slab:
3096 kmem_cache_destroy(lockowner_slab);
3097out_free_openowner_slab:
3098 kmem_cache_destroy(openowner_slab);
3099out:
NeilBrowne60d4392005-06-23 22:03:01 -07003100 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3101 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102}
3103
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003104static void init_nfs4_replay(struct nfs4_replay *rp)
3105{
3106 rp->rp_status = nfserr_serverfault;
3107 rp->rp_buflen = 0;
3108 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003109 mutex_init(&rp->rp_mutex);
3110}
3111
3112static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3113 struct nfs4_stateowner *so)
3114{
3115 if (!nfsd4_has_session(cstate)) {
3116 mutex_lock(&so->so_replay.rp_mutex);
3117 cstate->replay_owner = so;
3118 atomic_inc(&so->so_count);
3119 }
3120}
3121
3122void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3123{
3124 struct nfs4_stateowner *so = cstate->replay_owner;
3125
3126 if (so != NULL) {
3127 cstate->replay_owner = NULL;
3128 mutex_unlock(&so->so_replay.rp_mutex);
3129 nfs4_put_stateowner(so);
3130 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003131}
3132
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003133static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134{
3135 struct nfs4_stateowner *sop;
3136
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003137 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003138 if (!sop)
3139 return NULL;
3140
3141 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3142 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003143 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003144 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003146 sop->so_owner.len = owner->len;
3147
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003148 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003149 sop->so_client = clp;
3150 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003151 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003152 return sop;
3153}
3154
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003155static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003156{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003157 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003158
Trond Myklebustd4f04892014-07-29 21:34:36 -04003159 list_add(&oo->oo_owner.so_strhash,
3160 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003161 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162}
3163
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003164static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3165{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003166 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003167}
3168
Jeff Layton6b180f02014-07-29 21:34:26 -04003169static void nfs4_free_openowner(struct nfs4_stateowner *so)
3170{
3171 struct nfs4_openowner *oo = openowner(so);
3172
3173 kmem_cache_free(openowner_slab, oo);
3174}
3175
3176static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003177 .so_unhash = nfs4_unhash_openowner,
3178 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003179};
3180
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003181static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003182alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003183 struct nfsd4_compound_state *cstate)
3184{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003185 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003186 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003188 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3189 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003191 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003192 oo->oo_owner.so_is_open_owner = 1;
3193 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003194 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003195 if (nfsd4_has_session(cstate))
3196 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003197 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003198 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003199 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003200 spin_lock(&clp->cl_lock);
3201 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003202 if (ret == NULL) {
3203 hash_openowner(oo, clp, strhashval);
3204 ret = oo;
3205 } else
3206 nfs4_free_openowner(&oo->oo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003207 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003208 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209}
3210
J. Bruce Fields996e0932011-10-17 11:14:48 -04003211static 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 -04003212 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003214 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003215 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003216 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003217 stp->st_stateowner = &oo->oo_owner;
Jeff Laytond3134b12014-07-29 21:34:32 -04003218 atomic_inc(&stp->st_stateowner->so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07003219 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003220 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 stp->st_access_bmap = 0;
3222 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003223 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003224 spin_lock(&oo->oo_owner.so_client->cl_lock);
3225 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003226 spin_lock(&fp->fi_lock);
3227 list_add(&stp->st_perfile, &fp->fi_stateids);
3228 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003229 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230}
3231
Jeff Laytond3134b12014-07-29 21:34:32 -04003232/*
3233 * In the 4.0 case we need to keep the owners around a little while to handle
3234 * CLOSE replay. We still do need to release any file access that is held by
3235 * them before returning however.
3236 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003238move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239{
Jeff Laytond3134b12014-07-29 21:34:32 -04003240 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3241 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3242 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003243
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003244 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
Jeff Laytonb401be222014-07-29 21:34:33 -04003246 /*
3247 * We know that we hold one reference via nfsd4_close, and another
3248 * "persistent" reference for the client. If the refcount is higher
3249 * than 2, then there are still calls in progress that are using this
3250 * stateid. We can't put the sc_file reference until they are finished.
3251 * Wait for the refcount to drop to 2. Since it has been unhashed,
3252 * there should be no danger of the refcount going back up again at
3253 * this point.
3254 */
3255 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3256
Jeff Laytond3134b12014-07-29 21:34:32 -04003257 release_all_access(s);
3258 if (s->st_stid.sc_file) {
3259 put_nfs4_file(s->st_stid.sc_file);
3260 s->st_stid.sc_file = NULL;
3261 }
3262 release_last_closed_stateid(oo);
3263 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003264 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003265 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266}
3267
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268/* search file_hashtbl[] for file */
3269static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003270find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271{
Trond Myklebustca943212014-07-23 16:17:39 -04003272 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 struct nfs4_file *fp;
3274
Trond Myklebust950e0112014-06-30 11:48:31 -04003275 lockdep_assert_held(&state_lock);
3276
Jeff Layton89876f82013-04-02 09:01:59 -04003277 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003278 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003279 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 }
3283 return NULL;
3284}
3285
Trond Myklebust950e0112014-06-30 11:48:31 -04003286static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003287find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003288{
3289 struct nfs4_file *fp;
3290
3291 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003292 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003293 spin_unlock(&state_lock);
3294 return fp;
3295}
3296
3297static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003298find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003299{
3300 struct nfs4_file *fp;
3301
3302 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003303 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003304 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003305 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003306 fp = new;
3307 }
3308 spin_unlock(&state_lock);
3309
3310 return fp;
3311}
3312
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003313/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 * Called to check deny when READ with all zero stateid or
3315 * WRITE with all zero or all one stateid
3316 */
Al Virob37ad282006-10-19 23:28:59 -07003317static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3319{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003321 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
Trond Myklebustca943212014-07-23 16:17:39 -04003323 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003324 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003325 return ret;
3326 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003327 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003328 if (fp->fi_share_deny & deny_type)
3329 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003330 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003331 put_nfs4_file(fp);
3332 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333}
3334
Jeff Layton02e12152014-07-16 10:31:57 -04003335void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336{
Trond Myklebust11b91642014-07-29 21:34:08 -04003337 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3338 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003339
Trond Myklebust11b91642014-07-29 21:34:08 -04003340 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003341
Jeff Layton02e12152014-07-16 10:31:57 -04003342 /*
3343 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003344 * already holding inode->i_lock.
3345 *
Jeff Laytondff13992014-07-08 14:02:49 -04003346 * If the dl_time != 0, then we know that it has already been
3347 * queued for a lease break. Don't queue it again.
3348 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003349 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003350 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003351 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003352 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003353 }
Jeff Layton02e12152014-07-16 10:31:57 -04003354 spin_unlock(&state_lock);
3355}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
Jeff Layton02e12152014-07-16 10:31:57 -04003357static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3358{
3359 /*
3360 * We're assuming the state code never drops its reference
3361 * without first removing the lease. Since we're in this lease
3362 * callback (and since the lease code is serialized by the kernel
3363 * lock) we know the server hasn't removed the lease yet, we know
3364 * it's safe to take a reference.
3365 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003366 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003367 nfsd4_cb_recall(dp);
3368}
3369
Jeff Layton1c8c6012013-06-21 08:58:15 -04003370/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003371static void nfsd_break_deleg_cb(struct file_lock *fl)
3372{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003373 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3374 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003375
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003376 if (!fp) {
3377 WARN(1, "(%p)->fl_owner NULL\n", fl);
3378 return;
3379 }
3380 if (fp->fi_had_conflict) {
3381 WARN(1, "duplicate break on %p\n", fp);
3382 return;
3383 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003384 /*
3385 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003386 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003387 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003388 */
3389 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390
Jeff Layton02e12152014-07-16 10:31:57 -04003391 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003392 fp->fi_had_conflict = true;
3393 /*
3394 * If there are no delegations on the list, then we can't count on this
3395 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3396 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3397 * true should keep any new delegations from being hashed.
3398 */
3399 if (list_empty(&fp->fi_delegations))
3400 fl->fl_break_time = jiffies;
3401 else
3402 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3403 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003404 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405}
3406
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407static
3408int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3409{
3410 if (arg & F_UNLCK)
3411 return lease_modify(onlist, arg);
3412 else
3413 return -EAGAIN;
3414}
3415
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003416static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003417 .lm_break = nfsd_break_deleg_cb,
3418 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419};
3420
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003421static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3422{
3423 if (nfsd4_has_session(cstate))
3424 return nfs_ok;
3425 if (seqid == so->so_seqid - 1)
3426 return nfserr_replay_me;
3427 if (seqid == so->so_seqid)
3428 return nfs_ok;
3429 return nfserr_bad_seqid;
3430}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431
Jeff Layton4b24ca72014-06-30 11:48:44 -04003432static __be32 lookup_clientid(clientid_t *clid,
3433 struct nfsd4_compound_state *cstate,
3434 struct nfsd_net *nn)
3435{
3436 struct nfs4_client *found;
3437
3438 if (cstate->clp) {
3439 found = cstate->clp;
3440 if (!same_clid(&found->cl_clientid, clid))
3441 return nfserr_stale_clientid;
3442 return nfs_ok;
3443 }
3444
3445 if (STALE_CLIENTID(clid, nn))
3446 return nfserr_stale_clientid;
3447
3448 /*
3449 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3450 * cached already then we know this is for is for v4.0 and "sessions"
3451 * will be false.
3452 */
3453 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003454 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003455 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003456 if (!found) {
3457 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003458 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003459 }
3460 atomic_inc(&found->cl_refcount);
3461 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003462
3463 /* Cache the nfs4_client in cstate! */
3464 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003465 return nfs_ok;
3466}
3467
Al Virob37ad282006-10-19 23:28:59 -07003468__be32
Andy Adamson66689582009-04-03 08:28:45 +03003469nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003470 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 clientid_t *clientid = &open->op_clientid;
3473 struct nfs4_client *clp = NULL;
3474 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003475 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003476 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003478 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003480 /*
3481 * In case we need it later, after we've already created the
3482 * file and don't want to risk a further failure:
3483 */
3484 open->op_file = nfsd4_alloc_file();
3485 if (open->op_file == NULL)
3486 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487
Trond Myklebust2d91e892014-06-30 11:48:46 -04003488 status = lookup_clientid(clientid, cstate, nn);
3489 if (status)
3490 return status;
3491 clp = cstate->clp;
3492
Trond Myklebustd4f04892014-07-29 21:34:36 -04003493 strhashval = ownerstr_hashval(&open->op_owner);
3494 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003495 open->op_openowner = oo;
3496 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003497 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003499 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003500 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003501 release_openowner(oo);
3502 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003503 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003504 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003505 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3506 if (status)
3507 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003508 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003509new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003510 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003511 if (oo == NULL)
3512 return nfserr_jukebox;
3513 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003514alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003515 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003516 if (!open->op_stp)
3517 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003518 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519}
3520
Al Virob37ad282006-10-19 23:28:59 -07003521static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003522nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3523{
3524 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3525 return nfserr_openmode;
3526 else
3527 return nfs_ok;
3528}
3529
Daniel Mackc47d8322011-05-16 16:38:14 +02003530static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003531{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003532 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3533}
3534
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003535static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003536{
3537 struct nfs4_stid *ret;
3538
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003539 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003540 if (!ret)
3541 return NULL;
3542 return delegstateid(ret);
3543}
3544
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003545static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3546{
3547 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3548 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3549}
3550
Al Virob37ad282006-10-19 23:28:59 -07003551static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003552nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003553 struct nfs4_delegation **dp)
3554{
3555 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003556 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003557 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003558
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003559 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3560 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003561 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003562 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003563 status = nfs4_check_delegmode(deleg, flags);
3564 if (status) {
3565 nfs4_put_stid(&deleg->dl_stid);
3566 goto out;
3567 }
3568 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003569out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003570 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003571 return nfs_ok;
3572 if (status)
3573 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003574 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003575 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003576}
3577
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003578static struct nfs4_ol_stateid *
3579nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003581 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003582 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
Trond Myklebust1d31a252014-07-10 14:07:25 -04003584 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003585 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 /* ignore lock owners */
3587 if (local->st_stateowner->so_is_open_owner == 0)
3588 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003589 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003590 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003591 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003592 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003595 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003596 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597}
3598
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003599static inline int nfs4_access_to_access(u32 nfs4_access)
3600{
3601 int flags = 0;
3602
3603 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3604 flags |= NFSD_MAY_READ;
3605 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3606 flags |= NFSD_MAY_WRITE;
3607 return flags;
3608}
3609
Al Virob37ad282006-10-19 23:28:59 -07003610static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3612 struct nfsd4_open *open)
3613{
3614 struct iattr iattr = {
3615 .ia_valid = ATTR_SIZE,
3616 .ia_size = 0,
3617 };
3618 if (!open->op_truncate)
3619 return 0;
3620 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003621 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3623}
3624
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003625static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003626 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3627 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003628{
Trond Myklebustde186432014-07-10 14:07:26 -04003629 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003630 __be32 status;
3631 int oflag = nfs4_access_to_omode(open->op_share_access);
3632 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003633 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003634
Trond Myklebustde186432014-07-10 14:07:26 -04003635 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003636
3637 /*
3638 * Are we trying to set a deny mode that would conflict with
3639 * current access?
3640 */
3641 status = nfs4_file_check_deny(fp, open->op_share_deny);
3642 if (status != nfs_ok) {
3643 spin_unlock(&fp->fi_lock);
3644 goto out;
3645 }
3646
3647 /* set access to the file */
3648 status = nfs4_file_get_access(fp, open->op_share_access);
3649 if (status != nfs_ok) {
3650 spin_unlock(&fp->fi_lock);
3651 goto out;
3652 }
3653
3654 /* Set access bits in stateid */
3655 old_access_bmap = stp->st_access_bmap;
3656 set_access(open->op_share_access, stp);
3657
3658 /* Set new deny mask */
3659 old_deny_bmap = stp->st_deny_bmap;
3660 set_deny(open->op_share_deny, stp);
3661 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3662
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003663 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003664 spin_unlock(&fp->fi_lock);
3665 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003666 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003667 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003668 spin_lock(&fp->fi_lock);
3669 if (!fp->fi_fds[oflag]) {
3670 fp->fi_fds[oflag] = filp;
3671 filp = NULL;
3672 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003673 }
Trond Myklebustde186432014-07-10 14:07:26 -04003674 spin_unlock(&fp->fi_lock);
3675 if (filp)
3676 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003677
3678 status = nfsd4_truncate(rqstp, cur_fh, open);
3679 if (status)
3680 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003681out:
3682 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003683out_put_access:
3684 stp->st_access_bmap = old_access_bmap;
3685 nfs4_file_put_access(fp, open->op_share_access);
3686 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3687 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003688}
3689
Al Virob37ad282006-10-19 23:28:59 -07003690static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003691nfs4_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 -07003692{
Al Virob37ad282006-10-19 23:28:59 -07003693 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003694 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003696 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003697 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003698
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003699 /* test and set deny mode */
3700 spin_lock(&fp->fi_lock);
3701 status = nfs4_file_check_deny(fp, open->op_share_deny);
3702 if (status == nfs_ok) {
3703 old_deny_bmap = stp->st_deny_bmap;
3704 set_deny(open->op_share_deny, stp);
3705 fp->fi_share_deny |=
3706 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3707 }
3708 spin_unlock(&fp->fi_lock);
3709
3710 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003713 status = nfsd4_truncate(rqstp, cur_fh, open);
3714 if (status != nfs_ok)
3715 reset_union_bmap_deny(old_deny_bmap, stp);
3716 return status;
3717}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003720nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003722 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723}
3724
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003725/* Should we give out recallable state?: */
3726static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3727{
3728 if (clp->cl_cb_state == NFSD4_CB_UP)
3729 return true;
3730 /*
3731 * In the sessions case, since we don't have to establish a
3732 * separate connection for callbacks, we assume it's OK
3733 * until we hear otherwise:
3734 */
3735 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3736}
3737
Jeff Laytond564fbe2014-07-16 10:31:58 -04003738static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003739{
3740 struct file_lock *fl;
3741
3742 fl = locks_alloc_lock();
3743 if (!fl)
3744 return NULL;
3745 locks_init_lock(fl);
3746 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003747 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003748 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3749 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003750 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003751 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003752 return fl;
3753}
3754
J. Bruce Fields99c41512013-05-21 16:21:25 -04003755static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003756{
Trond Myklebust11b91642014-07-29 21:34:08 -04003757 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003758 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003759 struct file *filp;
3760 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003761
Jeff Laytond564fbe2014-07-16 10:31:58 -04003762 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003763 if (!fl)
3764 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003765 filp = find_readable_file(fp);
3766 if (!filp) {
3767 /* We should always have a readable file here */
3768 WARN_ON_ONCE(1);
3769 return -EBADF;
3770 }
3771 fl->fl_file = filp;
3772 status = vfs_setlease(filp, fl->fl_type, &fl);
3773 if (status) {
3774 locks_free_lock(fl);
3775 goto out_fput;
3776 }
Benny Halevycdc97502014-05-30 09:09:30 -04003777 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003778 spin_lock(&fp->fi_lock);
3779 /* Did the lease get broken before we took the lock? */
3780 status = -EAGAIN;
3781 if (fp->fi_had_conflict)
3782 goto out_unlock;
3783 /* Race breaker */
3784 if (fp->fi_lease) {
3785 status = 0;
3786 atomic_inc(&fp->fi_delegees);
3787 hash_delegation_locked(dp, fp);
3788 goto out_unlock;
3789 }
3790 fp->fi_lease = fl;
3791 fp->fi_deleg_file = filp;
3792 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003793 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003794 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003795 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003796 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003797out_unlock:
3798 spin_unlock(&fp->fi_lock);
3799 spin_unlock(&state_lock);
3800out_fput:
3801 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003802 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003803}
3804
Jeff Layton0b266932014-07-25 07:34:25 -04003805static struct nfs4_delegation *
3806nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3807 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003808{
Jeff Layton0b266932014-07-25 07:34:25 -04003809 int status;
3810 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003811
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003812 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003813 return ERR_PTR(-EAGAIN);
3814
3815 dp = alloc_init_deleg(clp, fh);
3816 if (!dp)
3817 return ERR_PTR(-ENOMEM);
3818
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003819 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003820 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003821 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003822 dp->dl_stid.sc_file = fp;
Jeff Layton417c6622014-07-21 09:34:57 -04003823 if (!fp->fi_lease) {
3824 spin_unlock(&fp->fi_lock);
3825 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003826 status = nfs4_setlease(dp);
3827 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003828 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003829 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003830 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003831 status = -EAGAIN;
3832 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003833 }
Benny Halevy931ee562014-05-30 09:09:27 -04003834 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003835 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003836out_unlock:
3837 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003838 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003839out:
3840 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003841 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003842 return ERR_PTR(status);
3843 }
3844 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003845}
3846
Benny Halevy4aa89132012-02-21 14:16:44 -08003847static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3848{
3849 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3850 if (status == -EAGAIN)
3851 open->op_why_no_deleg = WND4_CONTENTION;
3852 else {
3853 open->op_why_no_deleg = WND4_RESOURCE;
3854 switch (open->op_deleg_want) {
3855 case NFS4_SHARE_WANT_READ_DELEG:
3856 case NFS4_SHARE_WANT_WRITE_DELEG:
3857 case NFS4_SHARE_WANT_ANY_DELEG:
3858 break;
3859 case NFS4_SHARE_WANT_CANCEL:
3860 open->op_why_no_deleg = WND4_CANCELLED;
3861 break;
3862 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003863 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003864 }
3865 }
3866}
3867
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868/*
3869 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003870 *
3871 * Note we don't support write delegations, and won't until the vfs has
3872 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 */
3874static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003875nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3876 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877{
3878 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003879 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3880 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003881 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003882 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003884 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003885 open->op_recall = 0;
3886 switch (open->op_claim_type) {
3887 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003888 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003889 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003890 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3891 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003892 break;
3893 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003894 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003895 /*
3896 * Let's not give out any delegations till everyone's
3897 * had the chance to reclaim theirs....
3898 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003899 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003900 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003901 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003902 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003903 /*
3904 * Also, if the file was opened for write or
3905 * create, there's a good chance the client's
3906 * about to write to it, resulting in an
3907 * immediate recall (since we don't support
3908 * write delegations):
3909 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003910 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003911 goto out_no_deleg;
3912 if (open->op_create == NFS4_OPEN_CREATE)
3913 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003914 break;
3915 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003916 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003917 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003918 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003919 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003920 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003922 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003924 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003925 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003926 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003927 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003928 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003929out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003930 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3931 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003932 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003933 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003934 open->op_recall = 1;
3935 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003936
3937 /* 4.1 client asking for a delegation? */
3938 if (open->op_deleg_want)
3939 nfsd4_open_deleg_none_ext(open, status);
3940 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941}
3942
Benny Halevye27f49c2012-02-21 14:16:54 -08003943static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3944 struct nfs4_delegation *dp)
3945{
3946 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3947 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3948 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3949 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3950 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3951 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3952 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3953 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3954 }
3955 /* Otherwise the client must be confused wanting a delegation
3956 * it already has, therefore we don't return
3957 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3958 */
3959}
3960
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961/*
3962 * called with nfs4_lock_state() held.
3963 */
Al Virob37ad282006-10-19 23:28:59 -07003964__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3966{
Andy Adamson66689582009-04-03 08:28:45 +03003967 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003968 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003970 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003971 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003972 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 /*
3975 * Lookup file; if found, lookup stateid and check open request,
3976 * and check for delegations in the process of being recalled.
3977 * If not found, create the nfs4_file struct
3978 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003979 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04003980 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04003981 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003982 if (status)
3983 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003984 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04003986 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003987 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003988 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003989 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003990 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 }
3992
3993 /*
3994 * OPEN the file, or upgrade an existing OPEN.
3995 * If truncate fails, the OPEN fails.
3996 */
3997 if (stp) {
3998 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003999 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 if (status)
4001 goto out;
4002 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004003 stp = open->op_stp;
4004 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04004005 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004006 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4007 if (status) {
4008 release_open_stateid(stp);
4009 goto out;
4010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004012 update_stateid(&stp->st_stid.sc_stateid);
4013 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014
Benny Halevyd24433c2012-02-16 20:57:17 +02004015 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004016 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4017 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4018 open->op_why_no_deleg = WND4_NOT_WANTED;
4019 goto nodeleg;
4020 }
4021 }
4022
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 /*
4024 * Attempt to hand out a delegation. No error return, because the
4025 * OPEN succeeds even if we fail.
4026 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004027 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004028nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 status = nfs_ok;
4030
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004031 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004032 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004034 /* 4.1 client trying to upgrade/downgrade delegation? */
4035 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004036 open->op_deleg_want)
4037 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004038
NeilBrown13cd2182005-06-23 22:03:10 -07004039 if (fp)
4040 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004041 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05004042 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 /*
4044 * To finish the open response, we just need to set the rflags.
4045 */
4046 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004047 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004048 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004050 if (dp)
4051 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004052 if (stp)
4053 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054
4055 return status;
4056}
4057
Jeff Layton58fb12e2014-07-29 21:34:27 -04004058void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4059 struct nfsd4_open *open, __be32 status)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004060{
4061 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004062 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004063
Jeff Laytond3134b12014-07-29 21:34:32 -04004064 nfsd4_cstate_assign_replay(cstate, so);
4065 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004066 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004067 if (open->op_file)
4068 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004069 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004070 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004071}
4072
Al Virob37ad282006-10-19 23:28:59 -07004073__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004074nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4075 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076{
4077 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004078 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004079 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080
4081 nfs4_lock_state();
4082 dprintk("process_renew(%08x/%08x): starting\n",
4083 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004084 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004085 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004087 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004089 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004090 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 goto out;
4092 status = nfs_ok;
4093out:
4094 nfs4_unlock_state();
4095 return status;
4096}
4097
NeilBrowna76b4312005-06-23 22:04:01 -07004098static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004099nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004100{
Jeff Layton33dcc482012-04-10 11:08:48 -04004101 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004102 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004103 return;
4104
NeilBrowna76b4312005-06-23 22:04:01 -07004105 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004106 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004107 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004108 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004109 /*
4110 * Now that every NFSv4 client has had the chance to recover and
4111 * to see the (possibly new, possibly shorter) lease time, we
4112 * can safely set the next grace time to the current lease time:
4113 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03004114 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07004115}
4116
NeilBrownfd39ca92005-06-23 22:04:03 -07004117static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004118nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119{
4120 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004121 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 struct nfs4_delegation *dp;
4123 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004124 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004125 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126
4127 nfs4_lock_state();
4128
4129 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004130 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004131 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004132 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004133 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 clp = list_entry(pos, struct nfs4_client, cl_lru);
4135 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4136 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004137 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 break;
4139 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004140 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004141 dprintk("NFSD: client in use (clientid %08x)\n",
4142 clp->cl_clientid.cl_id);
4143 continue;
4144 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004145 unhash_client_locked(clp);
4146 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004147 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004148 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004149 list_for_each_safe(pos, next, &reaplist) {
4150 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 dprintk("NFSD: purging unused client (clientid %08x)\n",
4152 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004153 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 expire_client(clp);
4155 }
Benny Halevycdc97502014-05-30 09:09:30 -04004156 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004157 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004159 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4160 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004162 t = dp->dl_time - cutoff;
4163 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 break;
4165 }
Jeff Layton42690672014-07-25 07:34:20 -04004166 unhash_delegation_locked(dp);
4167 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 }
Benny Halevycdc97502014-05-30 09:09:30 -04004169 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004170 while (!list_empty(&reaplist)) {
4171 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4172 dl_recall_lru);
4173 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004174 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004176 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004177 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
4178 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004179 t = oo->oo_time - cutoff;
4180 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 break;
4182 }
Jeff Laytond3134b12014-07-29 21:34:32 -04004183 release_last_closed_stateid(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04004185 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04004187 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188}
4189
Harvey Harrisona254b242008-02-20 12:49:00 -08004190static struct workqueue_struct *laundry_wq;
4191static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004192
4193static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004194laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195{
4196 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004197 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4198 work);
4199 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4200 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004202 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004204 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205}
4206
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004207static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004208{
Trond Myklebust11b91642014-07-29 21:34:08 -04004209 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004210 return nfserr_bad_stateid;
4211 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212}
4213
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004215access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004217 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4218 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4219 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220}
4221
4222static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004223access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004225 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4226 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227}
4228
4229static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004230__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231{
Al Virob37ad282006-10-19 23:28:59 -07004232 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233
J. Bruce Fields02921912010-07-29 15:16:59 -04004234 /* For lock stateid's, we test the parent open, not the lock: */
4235 if (stp->st_openstp)
4236 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004237 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004239 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 goto out;
4241 status = nfs_ok;
4242out:
4243 return status;
4244}
4245
Al Virob37ad282006-10-19 23:28:59 -07004246static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004247check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004249 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004251 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004252 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 * conflicting state; so we must wait out the grace period. */
4254 return nfserr_grace;
4255 } else if (flags & WR_STATE)
4256 return nfs4_share_conflict(current_fh,
4257 NFS4_SHARE_DENY_WRITE);
4258 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4259 return nfs4_share_conflict(current_fh,
4260 NFS4_SHARE_DENY_READ);
4261}
4262
4263/*
4264 * Allow READ/WRITE during grace period on recovered state only for files
4265 * that are not able to provide mandatory locking.
4266 */
4267static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004268grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004270 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271}
4272
J. Bruce Fields81b82962011-08-23 11:03:29 -04004273/* Returns true iff a is later than b: */
4274static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4275{
Jim Rees1a9357f2013-05-17 17:33:00 -04004276 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004277}
4278
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004279static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004280{
Andy Adamson66689582009-04-03 08:28:45 +03004281 /*
4282 * When sessions are used the stateid generation number is ignored
4283 * when it is zero.
4284 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004285 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004286 return nfs_ok;
4287
4288 if (in->si_generation == ref->si_generation)
4289 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004290
J. Bruce Fields0836f582008-01-26 19:08:12 -05004291 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004292 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004293 return nfserr_bad_stateid;
4294 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004295 * However, we could see a stateid from the past, even from a
4296 * non-buggy client. For example, if the client sends a lock
4297 * while some IO is outstanding, the lock may bump si_generation
4298 * while the IO is still in flight. The client could avoid that
4299 * situation by waiting for responses on all the IO requests,
4300 * but better performance may result in retrying IO that
4301 * receives an old_stateid error if requests are rarely
4302 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004303 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004304 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004305}
4306
Chuck Lever7df302f2012-05-29 13:56:37 -04004307static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004308{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004309 struct nfs4_stid *s;
4310 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004311 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004312
Chuck Lever7df302f2012-05-29 13:56:37 -04004313 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004314 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004315 /* Client debugging aid. */
4316 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4317 char addr_str[INET6_ADDRSTRLEN];
4318 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4319 sizeof(addr_str));
4320 pr_warn_ratelimited("NFSD: client %s testing state ID "
4321 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004322 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004323 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004324 spin_lock(&cl->cl_lock);
4325 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004326 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004327 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004328 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004329 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004330 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004331 switch (s->sc_type) {
4332 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004333 status = nfs_ok;
4334 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004335 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004336 status = nfserr_deleg_revoked;
4337 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004338 case NFS4_OPEN_STID:
4339 case NFS4_LOCK_STID:
4340 ols = openlockstateid(s);
4341 if (ols->st_stateowner->so_is_open_owner
4342 && !(openowner(ols->st_stateowner)->oo_flags
4343 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004344 status = nfserr_bad_stateid;
4345 else
4346 status = nfs_ok;
4347 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004348 default:
4349 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004350 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004351 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004352 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004353 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004354 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004355out_unlock:
4356 spin_unlock(&cl->cl_lock);
4357 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004358}
4359
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004360static __be32
4361nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4362 stateid_t *stateid, unsigned char typemask,
4363 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004364{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004365 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004366
4367 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4368 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004369 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004370 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004371 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004372 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004373 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004374 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004375 if (status)
4376 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004377 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004378 if (!*s)
4379 return nfserr_bad_stateid;
4380 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004381}
4382
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383/*
4384* Checks for stateid operations
4385*/
Al Virob37ad282006-10-19 23:28:59 -07004386__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004387nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004388 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004390 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004391 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004393 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004395 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004396 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004397 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 if (filpp)
4400 *filpp = NULL;
4401
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004402 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 return nfserr_grace;
4404
4405 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004406 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407
Trond Myklebust14bcab12014-04-18 14:44:07 -04004408 nfs4_lock_state();
4409
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004410 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004411 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004412 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004413 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004414 goto unlock_state;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004415 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4416 if (status)
4417 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004418 switch (s->sc_type) {
4419 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004420 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004421 status = nfs4_check_delegmode(dp, flags);
4422 if (status)
4423 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004424 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004425 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004426 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004427 WARN_ON_ONCE(1);
4428 status = nfserr_serverfault;
4429 goto out;
4430 }
Trond Myklebustde186432014-07-10 14:07:26 -04004431 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004432 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004433 break;
4434 case NFS4_OPEN_STID:
4435 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004436 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004437 status = nfs4_check_fh(current_fh, stp);
4438 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004440 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004441 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004443 status = nfs4_check_openmode(stp, flags);
4444 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004446 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004447 struct nfs4_file *fp = stp->st_stid.sc_file;
4448
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004449 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004450 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004451 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004452 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004453 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004454 break;
4455 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004456 status = nfserr_bad_stateid;
4457 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 }
4459 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004460 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004461 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004463 nfs4_put_stid(s);
4464unlock_state:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004465 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 return status;
4467}
4468
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004469/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004470 * Test if the stateid is valid
4471 */
4472__be32
4473nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4474 struct nfsd4_test_stateid *test_stateid)
4475{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004476 struct nfsd4_test_stateid_id *stateid;
4477 struct nfs4_client *cl = cstate->session->se_client;
4478
4479 nfs4_lock_state();
4480 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004481 stateid->ts_id_status =
4482 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004483 nfs4_unlock_state();
4484
Bryan Schumaker17456802011-07-13 10:50:48 -04004485 return nfs_ok;
4486}
4487
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004488__be32
4489nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4490 struct nfsd4_free_stateid *free_stateid)
4491{
4492 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004493 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004494 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004495 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004496 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004497 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004498
4499 nfs4_lock_state();
Jeff Layton1af71cc2014-07-29 21:34:14 -04004500 spin_lock(&cl->cl_lock);
4501 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004502 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004503 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004504 switch (s->sc_type) {
4505 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004506 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004507 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004508 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004509 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4510 if (ret)
4511 break;
4512 ret = nfserr_locks_held;
4513 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004514 case NFS4_LOCK_STID:
4515 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4516 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004517 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004518 stp = openlockstateid(s);
4519 ret = nfserr_locks_held;
4520 if (check_for_locks(stp->st_stid.sc_file,
4521 lockowner(stp->st_stateowner)))
4522 break;
4523 unhash_lock_stateid(stp);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004524 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004525 nfs4_put_stid(s);
4526 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004527 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004528 case NFS4_REVOKED_DELEG_STID:
4529 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004530 list_del_init(&dp->dl_recall_lru);
4531 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004532 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004533 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004534 goto out;
4535 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004536 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004537out_unlock:
4538 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004539out:
4540 nfs4_unlock_state();
4541 return ret;
4542}
4543
NeilBrown4c4cd222005-07-07 17:59:27 -07004544static inline int
4545setlkflg (int type)
4546{
4547 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4548 RD_STATE : WR_STATE;
4549}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004551static __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 -04004552{
4553 struct svc_fh *current_fh = &cstate->current_fh;
4554 struct nfs4_stateowner *sop = stp->st_stateowner;
4555 __be32 status;
4556
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004557 status = nfsd4_check_seqid(cstate, sop, seqid);
4558 if (status)
4559 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004560 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4561 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004562 /*
4563 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004564 * nfserr_replay_me from the previous step, and
4565 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004566 */
4567 return nfserr_bad_stateid;
4568 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4569 if (status)
4570 return status;
4571 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004572}
4573
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574/*
4575 * Checks for sequence id mutating operations.
4576 */
Al Virob37ad282006-10-19 23:28:59 -07004577static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004578nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004579 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004580 struct nfs4_ol_stateid **stpp,
4581 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004583 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004584 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004585 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004587 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4588 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004589
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004591 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004592 if (status)
4593 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004594 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004595 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004597 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004598 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004599 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004600 else
4601 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004602 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004603}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004604
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004605static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4606 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004607{
4608 __be32 status;
4609 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004610 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004612 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004613 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004614 if (status)
4615 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004616 oo = openowner(stp->st_stateowner);
4617 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4618 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004619 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004620 }
4621 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004622 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623}
4624
Al Virob37ad282006-10-19 23:28:59 -07004625__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004626nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004627 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628{
Al Virob37ad282006-10-19 23:28:59 -07004629 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004630 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004631 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004632 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633
Al Viroa6a9f182013-09-16 10:57:01 -04004634 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4635 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004637 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004638 if (status)
4639 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
4641 nfs4_lock_state();
4642
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004643 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004644 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004645 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004646 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004647 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004648 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004649 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004650 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004651 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004652 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004653 update_stateid(&stp->st_stid.sc_stateid);
4654 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004655 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004656 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004657
Jeff Layton2a4317c2012-03-21 16:42:43 -04004658 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004659 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004660put_stateid:
4661 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004663 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004664 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 return status;
4666}
4667
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004668static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004670 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004671 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004672 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004673 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004674}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004675
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004676static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4677{
4678 switch (to_access) {
4679 case NFS4_SHARE_ACCESS_READ:
4680 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4681 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4682 break;
4683 case NFS4_SHARE_ACCESS_WRITE:
4684 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4685 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4686 break;
4687 case NFS4_SHARE_ACCESS_BOTH:
4688 break;
4689 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004690 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 }
4692}
4693
Al Virob37ad282006-10-19 23:28:59 -07004694__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004695nfsd4_open_downgrade(struct svc_rqst *rqstp,
4696 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004697 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698{
Al Virob37ad282006-10-19 23:28:59 -07004699 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004700 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004701 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702
Al Viroa6a9f182013-09-16 10:57:01 -04004703 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4704 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004706 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004707 if (od->od_deleg_want)
4708 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4709 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710
4711 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004712 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004713 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004714 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004717 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004718 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004720 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004722 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004723 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004725 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004727 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728
Jeff Laytonce0fc432012-05-11 09:45:14 -04004729 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004731 update_stateid(&stp->st_stid.sc_stateid);
4732 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004734put_stateid:
4735 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004737 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004738 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 return status;
4740}
4741
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004742static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4743{
Trond Myklebustacf92952014-06-30 11:48:37 -04004744 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytond83017f2014-07-29 21:34:42 -04004745 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004746
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004747 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04004748 spin_lock(&clp->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04004749 unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004750
Jeff Laytond83017f2014-07-29 21:34:42 -04004751 if (clp->cl_minorversion) {
4752 put_ol_stateid_locked(s, &reaplist);
4753 spin_unlock(&clp->cl_lock);
4754 free_ol_stateid_reaplist(&reaplist);
4755 } else {
4756 spin_unlock(&clp->cl_lock);
4757 free_ol_stateid_reaplist(&reaplist);
Jeff Laytond3134b12014-07-29 21:34:32 -04004758 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04004759 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004760}
4761
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762/*
4763 * nfs4_unlock_state() called after encode
4764 */
Al Virob37ad282006-10-19 23:28:59 -07004765__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004766nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004767 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768{
Al Virob37ad282006-10-19 23:28:59 -07004769 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004770 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004771 struct net *net = SVC_NET(rqstp);
4772 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773
Al Viroa6a9f182013-09-16 10:57:01 -04004774 dprintk("NFSD: nfsd4_close on file %pd\n",
4775 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776
4777 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004778 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4779 &close->cl_stateid,
4780 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004781 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004782 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004783 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004785 update_stateid(&stp->st_stid.sc_stateid);
4786 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004788 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004789
4790 /* put reference from nfs4_preprocess_seqid_op */
4791 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792out:
Jeff Layton58fb12e2014-07-29 21:34:27 -04004793 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 return status;
4795}
4796
Al Virob37ad282006-10-19 23:28:59 -07004797__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004798nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4799 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004801 struct nfs4_delegation *dp;
4802 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004803 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004804 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004805 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004807 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004808 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
4810 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004811 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004812 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004813 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004814 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004815 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004816 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004817 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004818
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004819 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004820put_stateid:
4821 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004823 nfs4_unlock_state();
4824
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 return status;
4826}
4827
4828
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830
Benny Halevy87df4de2008-12-15 19:42:03 +02004831static inline u64
4832end_offset(u64 start, u64 len)
4833{
4834 u64 end;
4835
4836 end = start + len;
4837 return end >= start ? end: NFS4_MAX_UINT64;
4838}
4839
4840/* last octet in a range */
4841static inline u64
4842last_byte_offset(u64 start, u64 len)
4843{
4844 u64 end;
4845
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004846 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004847 end = start + len;
4848 return end > start ? end - 1: NFS4_MAX_UINT64;
4849}
4850
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851/*
4852 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4853 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4854 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4855 * locking, this prevents us from being completely protocol-compliant. The
4856 * real solution to this problem is to start using unsigned file offsets in
4857 * the VFS, but this is a very deep change!
4858 */
4859static inline void
4860nfs4_transform_lock_offset(struct file_lock *lock)
4861{
4862 if (lock->fl_start < 0)
4863 lock->fl_start = OFFSET_MAX;
4864 if (lock->fl_end < 0)
4865 lock->fl_end = OFFSET_MAX;
4866}
4867
NeilBrownd5b90262006-04-10 22:55:22 -07004868/* Hack!: For now, we're defining this just so we can use a pointer to it
4869 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004870static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004871};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
4873static inline void
4874nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4875{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004876 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877
NeilBrownd5b90262006-04-10 22:55:22 -07004878 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004879 lo = (struct nfs4_lockowner *) fl->fl_owner;
4880 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4881 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004882 if (!deny->ld_owner.data)
4883 /* We just don't care that much */
4884 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004885 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4886 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004887 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004888nevermind:
4889 deny->ld_owner.len = 0;
4890 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004891 deny->ld_clientid.cl_boot = 0;
4892 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 }
4894 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004895 deny->ld_length = NFS4_MAX_UINT64;
4896 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4898 deny->ld_type = NFS4_READ_LT;
4899 if (fl->fl_type != F_RDLCK)
4900 deny->ld_type = NFS4_WRITE_LT;
4901}
4902
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004903static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004904find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004905 struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004907 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004908 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909
Trond Myklebustd4f04892014-07-29 21:34:36 -04004910 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4911 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004912 if (so->so_is_open_owner)
4913 continue;
Trond Myklebustd4f04892014-07-29 21:34:36 -04004914 if (!same_owner_str(so, owner))
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004915 continue;
Jeff Layton5db1c032014-07-29 21:34:28 -04004916 atomic_inc(&so->so_count);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004917 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 }
4919 return NULL;
4920}
4921
Trond Myklebustc58c6612014-07-29 21:34:35 -04004922static struct nfs4_lockowner *
4923find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004924 struct nfs4_client *clp)
Trond Myklebustc58c6612014-07-29 21:34:35 -04004925{
4926 struct nfs4_lockowner *lo;
4927
Trond Myklebustd4f04892014-07-29 21:34:36 -04004928 spin_lock(&clp->cl_lock);
4929 lo = find_lockowner_str_locked(clid, owner, clp);
4930 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004931 return lo;
4932}
4933
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004934static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4935{
Trond Myklebustc58c6612014-07-29 21:34:35 -04004936 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004937}
4938
Jeff Layton6b180f02014-07-29 21:34:26 -04004939static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4940{
4941 struct nfs4_lockowner *lo = lockowner(sop);
4942
4943 kmem_cache_free(lockowner_slab, lo);
4944}
4945
4946static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004947 .so_unhash = nfs4_unhash_lockowner,
4948 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004949};
4950
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951/*
4952 * Alloc a lock owner structure.
4953 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004954 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004956 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004958static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004959alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
4960 struct nfs4_ol_stateid *open_stp,
4961 struct nfsd4_lock *lock)
4962{
Trond Myklebustc58c6612014-07-29 21:34:35 -04004963 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004965 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4966 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004968 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4969 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04004970 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04004971 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04004972 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004973 ret = find_lockowner_str_locked(&clp->cl_clientid,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004974 &lock->lk_new_owner, clp);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004975 if (ret == NULL) {
4976 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004977 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004978 ret = lo;
4979 } else
4980 nfs4_free_lockowner(&lo->lo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04004981 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004982 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983}
4984
Jeff Layton356a95e2014-07-29 21:34:13 -04004985static void
4986init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
4987 struct nfs4_file *fp, struct inode *inode,
4988 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004990 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991
Jeff Layton356a95e2014-07-29 21:34:13 -04004992 lockdep_assert_held(&clp->cl_lock);
4993
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004994 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004995 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004996 stp->st_stateowner = &lo->lo_owner;
Trond Myklebuste4f1dd72014-07-29 21:34:30 -04004997 atomic_inc(&lo->lo_owner.so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07004998 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004999 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04005000 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005001 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005003 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005004 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005005 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005006 spin_lock(&fp->fi_lock);
5007 list_add(&stp->st_perfile, &fp->fi_stateids);
5008 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009}
5010
Jeff Laytonc53530d2014-06-30 11:48:39 -04005011static struct nfs4_ol_stateid *
5012find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5013{
5014 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005015 struct nfs4_client *clp = lo->lo_owner.so_client;
5016
5017 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005018
5019 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005020 if (lst->st_stid.sc_file == fp) {
5021 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005022 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005023 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005024 }
5025 return NULL;
5026}
5027
Jeff Layton356a95e2014-07-29 21:34:13 -04005028static struct nfs4_ol_stateid *
5029find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5030 struct inode *inode, struct nfs4_ol_stateid *ost,
5031 bool *new)
5032{
5033 struct nfs4_stid *ns = NULL;
5034 struct nfs4_ol_stateid *lst;
5035 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5036 struct nfs4_client *clp = oo->oo_owner.so_client;
5037
5038 spin_lock(&clp->cl_lock);
5039 lst = find_lock_stateid(lo, fi);
5040 if (lst == NULL) {
5041 spin_unlock(&clp->cl_lock);
5042 ns = nfs4_alloc_stid(clp, stateid_slab);
5043 if (ns == NULL)
5044 return NULL;
5045
5046 spin_lock(&clp->cl_lock);
5047 lst = find_lock_stateid(lo, fi);
5048 if (likely(!lst)) {
5049 lst = openlockstateid(ns);
5050 init_lock_stateid(lst, lo, fi, inode, ost);
5051 ns = NULL;
5052 *new = true;
5053 }
5054 }
5055 spin_unlock(&clp->cl_lock);
5056 if (ns)
5057 nfs4_put_stid(ns);
5058 return lst;
5059}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005060
NeilBrownfd39ca92005-06-23 22:04:03 -07005061static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062check_lock_length(u64 offset, u64 length)
5063{
Benny Halevy87df4de2008-12-15 19:42:03 +02005064 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 LOFF_OVERFLOW(offset, length)));
5066}
5067
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005068static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005069{
Trond Myklebust11b91642014-07-29 21:34:08 -04005070 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005071
Jeff Layton7214e862014-07-10 14:07:33 -04005072 lockdep_assert_held(&fp->fi_lock);
5073
Jeff Layton82c5ff12012-05-11 09:45:13 -04005074 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005075 return;
Jeff Layton12659652014-07-10 14:07:28 -04005076 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005077 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005078}
5079
Jeff Layton356a95e2014-07-29 21:34:13 -04005080static __be32
5081lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5082 struct nfs4_ol_stateid *ost,
5083 struct nfsd4_lock *lock,
5084 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005085{
Jeff Layton5db1c032014-07-29 21:34:28 -04005086 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005087 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005088 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5089 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005090 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005091 struct nfs4_lockowner *lo;
5092 unsigned int strhashval;
5093
Trond Myklebustd4f04892014-07-29 21:34:36 -04005094 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005095 if (!lo) {
Trond Myklebustd4f04892014-07-29 21:34:36 -04005096 strhashval = ownerstr_hashval(&lock->v.new.owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005097 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5098 if (lo == NULL)
5099 return nfserr_jukebox;
5100 } else {
5101 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005102 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005103 if (!cstate->minorversion &&
5104 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005105 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005106 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005107
Jeff Layton356a95e2014-07-29 21:34:13 -04005108 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005109 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005110 status = nfserr_jukebox;
5111 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005112 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005113 status = nfs_ok;
5114out:
5115 nfs4_put_stateowner(&lo->lo_owner);
5116 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005117}
5118
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119/*
5120 * LOCK operation
5121 */
Al Virob37ad282006-10-19 23:28:59 -07005122__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005123nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005124 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005126 struct nfs4_openowner *open_sop = NULL;
5127 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005128 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005129 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005130 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005131 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005132 struct file_lock *file_lock = NULL;
5133 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005134 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005135 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005136 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005137 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005138 struct net *net = SVC_NET(rqstp);
5139 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140
5141 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5142 (long long) lock->lk_offset,
5143 (long long) lock->lk_length);
5144
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 if (check_lock_length(lock->lk_offset, lock->lk_length))
5146 return nfserr_inval;
5147
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005148 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005149 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005150 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5151 return status;
5152 }
5153
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 nfs4_lock_state();
5155
5156 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005157 if (nfsd4_has_session(cstate))
5158 /* See rfc 5661 18.10.3: given clientid is ignored: */
5159 memcpy(&lock->v.new.clientid,
5160 &cstate->session->se_client->cl_clientid,
5161 sizeof(clientid_t));
5162
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005164 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005168 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 lock->lk_new_open_seqid,
5170 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005171 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005172 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005174 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005175 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005176 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005177 &lock->v.new.clientid))
5178 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005179 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005180 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005181 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005182 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005183 lock->lk_old_lock_seqid,
5184 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005185 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005186 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005187 if (status)
5188 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005189 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005191 lkflg = setlkflg(lock->lk_type);
5192 status = nfs4_check_openmode(lock_stp, lkflg);
5193 if (status)
5194 goto out;
5195
NeilBrown0dd395d2005-07-07 17:59:15 -07005196 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005197 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005198 goto out;
5199 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005200 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005201 goto out;
5202
Jeff Layton21179d82012-08-21 08:03:32 -04005203 file_lock = locks_alloc_lock();
5204 if (!file_lock) {
5205 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5206 status = nfserr_jukebox;
5207 goto out;
5208 }
5209
Trond Myklebust11b91642014-07-29 21:34:08 -04005210 fp = lock_stp->st_stid.sc_file;
Jeff Layton21179d82012-08-21 08:03:32 -04005211 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 switch (lock->lk_type) {
5213 case NFS4_READ_LT:
5214 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005215 spin_lock(&fp->fi_lock);
5216 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005217 if (filp)
5218 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
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_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005221 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 case NFS4_WRITE_LT:
5223 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005224 spin_lock(&fp->fi_lock);
5225 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005226 if (filp)
5227 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005228 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005229 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005230 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 default:
5232 status = nfserr_inval;
5233 goto out;
5234 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005235 if (!filp) {
5236 status = nfserr_openmode;
5237 goto out;
5238 }
Jeff Layton21179d82012-08-21 08:03:32 -04005239 file_lock->fl_owner = (fl_owner_t)lock_sop;
5240 file_lock->fl_pid = current->tgid;
5241 file_lock->fl_file = filp;
5242 file_lock->fl_flags = FL_POSIX;
5243 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5244 file_lock->fl_start = lock->lk_offset;
5245 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5246 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247
Jeff Layton21179d82012-08-21 08:03:32 -04005248 conflock = locks_alloc_lock();
5249 if (!conflock) {
5250 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5251 status = nfserr_jukebox;
5252 goto out;
5253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254
Jeff Layton21179d82012-08-21 08:03:32 -04005255 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005256 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005258 update_stateid(&lock_stp->st_stid.sc_stateid);
5259 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005261 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005262 break;
5263 case (EAGAIN): /* conflock holds conflicting lock */
5264 status = nfserr_denied;
5265 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005266 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005267 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 case (EDEADLK):
5269 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005270 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005271 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005272 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005273 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005274 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276out:
Trond Myklebustde186432014-07-10 14:07:26 -04005277 if (filp)
5278 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005279 if (lock_stp) {
5280 /* Bump seqid manually if the 4.0 replay owner is openowner */
5281 if (cstate->replay_owner &&
5282 cstate->replay_owner != &lock_sop->lo_owner &&
5283 seqid_mutating_err(ntohl(status)))
5284 lock_sop->lo_owner.so_seqid++;
5285
5286 /*
5287 * If this is a new, never-before-used stateid, and we are
5288 * returning an error, then just go ahead and release it.
5289 */
5290 if (status && new)
5291 release_lock_stateid(lock_stp);
5292
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005293 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005294 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005295 if (open_stp)
5296 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005297 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005298 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005299 if (file_lock)
5300 locks_free_lock(file_lock);
5301 if (conflock)
5302 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 return status;
5304}
5305
5306/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005307 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5308 * so we do a temporary open here just to get an open file to pass to
5309 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5310 * inode operation.)
5311 */
Al Viro04da6e92012-04-13 00:00:04 -04005312static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005313{
5314 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005315 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5316 if (!err) {
5317 err = nfserrno(vfs_test_lock(file, lock));
5318 nfsd_close(file);
5319 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005320 return err;
5321}
5322
5323/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 * LOCKT operation
5325 */
Al Virob37ad282006-10-19 23:28:59 -07005326__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005327nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5328 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329{
Jeff Layton21179d82012-08-21 08:03:32 -04005330 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005331 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005332 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005333 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005335 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 return nfserr_grace;
5337
5338 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5339 return nfserr_inval;
5340
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 nfs4_lock_state();
5342
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005343 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005344 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005345 if (status)
5346 goto out;
5347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005349 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351
Jeff Layton21179d82012-08-21 08:03:32 -04005352 file_lock = locks_alloc_lock();
5353 if (!file_lock) {
5354 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5355 status = nfserr_jukebox;
5356 goto out;
5357 }
5358 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 switch (lockt->lt_type) {
5360 case NFS4_READ_LT:
5361 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005362 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 break;
5364 case NFS4_WRITE_LT:
5365 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005366 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 break;
5368 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005369 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 status = nfserr_inval;
5371 goto out;
5372 }
5373
Trond Myklebustd4f04892014-07-29 21:34:36 -04005374 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5375 cstate->clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005376 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005377 file_lock->fl_owner = (fl_owner_t)lo;
5378 file_lock->fl_pid = current->tgid;
5379 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380
Jeff Layton21179d82012-08-21 08:03:32 -04005381 file_lock->fl_start = lockt->lt_offset;
5382 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383
Jeff Layton21179d82012-08-21 08:03:32 -04005384 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385
Jeff Layton21179d82012-08-21 08:03:32 -04005386 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005387 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005388 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005389
Jeff Layton21179d82012-08-21 08:03:32 -04005390 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005392 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 }
5394out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005395 if (lo)
5396 nfs4_put_stateowner(&lo->lo_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005398 if (file_lock)
5399 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 return status;
5401}
5402
Al Virob37ad282006-10-19 23:28:59 -07005403__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005404nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005405 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005407 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005409 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005410 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005411 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005412 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5413
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5415 (long long) locku->lu_offset,
5416 (long long) locku->lu_length);
5417
5418 if (check_lock_length(locku->lu_offset, locku->lu_length))
5419 return nfserr_inval;
5420
5421 nfs4_lock_state();
5422
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005423 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005424 &locku->lu_stateid, NFS4_LOCK_STID,
5425 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005426 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005428 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005429 if (!filp) {
5430 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005431 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005432 }
Jeff Layton21179d82012-08-21 08:03:32 -04005433 file_lock = locks_alloc_lock();
5434 if (!file_lock) {
5435 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5436 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005437 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005438 }
5439 locks_init_lock(file_lock);
5440 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005441 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005442 file_lock->fl_pid = current->tgid;
5443 file_lock->fl_file = filp;
5444 file_lock->fl_flags = FL_POSIX;
5445 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5446 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447
Jeff Layton21179d82012-08-21 08:03:32 -04005448 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5449 locku->lu_length);
5450 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451
Jeff Layton21179d82012-08-21 08:03:32 -04005452 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005453 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005454 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 goto out_nfserr;
5456 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005457 update_stateid(&stp->st_stid.sc_stateid);
5458 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005459fput:
5460 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005461put_stateid:
5462 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005464 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005465 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005466 if (file_lock)
5467 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 return status;
5469
5470out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005471 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005472 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473}
5474
5475/*
5476 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005477 * true: locks held by lockowner
5478 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005480static bool
5481check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482{
5483 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005484 int status = false;
5485 struct file *filp = find_any_file(fp);
5486 struct inode *inode;
5487
5488 if (!filp) {
5489 /* Any valid lock stateid should have some sort of access */
5490 WARN_ON_ONCE(1);
5491 return status;
5492 }
5493
5494 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495
Jeff Layton1c8c6012013-06-21 08:58:15 -04005496 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005498 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005499 status = true;
5500 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005503 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005504 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 return status;
5506}
5507
Al Virob37ad282006-10-19 23:28:59 -07005508__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005509nfsd4_release_lockowner(struct svc_rqst *rqstp,
5510 struct nfsd4_compound_state *cstate,
5511 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512{
5513 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005514 struct nfs4_stateowner *sop;
5515 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005516 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005518 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005519 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005520 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005521 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522
5523 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5524 clid->cl_boot, clid->cl_id);
5525
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 nfs4_lock_state();
5527
Jeff Layton4b24ca72014-06-30 11:48:44 -04005528 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005529 if (status)
5530 goto out;
5531
Trond Myklebustd4f04892014-07-29 21:34:36 -04005532 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005533 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005534 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005535 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005536 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005537
5538 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005539 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005540
Jeff Layton882e9d22014-07-29 21:34:37 -04005541 /* see if there are still any locks associated with it */
5542 lo = lockowner(sop);
5543 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5544 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5545 status = nfserr_locks_held;
5546 spin_unlock(&clp->cl_lock);
5547 goto out;
5548 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005549 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005550
5551 atomic_inc(&sop->so_count);
5552 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005553 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005554 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005555 if (lo)
5556 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557out:
5558 nfs4_unlock_state();
5559 return status;
5560}
5561
5562static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005563alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564{
NeilBrowna55370a2005-06-23 22:03:52 -07005565 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566}
5567
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005568bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005569nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005570{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005571 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005572
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005573 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005574 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005575}
5576
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577/*
5578 * failure => all reset bets are off, nfserr_no_grace...
5579 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005580struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005581nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582{
5583 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005584 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585
NeilBrowna55370a2005-06-23 22:03:52 -07005586 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5587 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005588 if (crp) {
5589 strhashval = clientstr_hashval(name);
5590 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005591 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005592 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005593 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005594 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005595 }
5596 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597}
5598
Jeff Layton2a4317c2012-03-21 16:42:43 -04005599void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005600nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005601{
5602 list_del(&crp->cr_strhash);
5603 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005604 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005605}
5606
5607void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005608nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609{
5610 struct nfs4_client_reclaim *crp = NULL;
5611 int i;
5612
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005614 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5615 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005617 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 }
5619 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005620 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621}
5622
5623/*
5624 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005625struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005626nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627{
5628 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 struct nfs4_client_reclaim *crp = NULL;
5630
Jeff Layton278c9312012-11-12 15:00:52 -05005631 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632
Jeff Layton278c9312012-11-12 15:00:52 -05005633 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005634 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005635 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 return crp;
5637 }
5638 }
5639 return NULL;
5640}
5641
5642/*
5643* Called from OPEN. Look for clientid in reclaim list.
5644*/
Al Virob37ad282006-10-19 23:28:59 -07005645__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005646nfs4_check_open_reclaim(clientid_t *clid,
5647 struct nfsd4_compound_state *cstate,
5648 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005650 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005651
5652 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005653 status = lookup_clientid(clid, cstate, nn);
5654 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005655 return nfserr_reclaim_bad;
5656
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005657 if (nfsd4_client_record_check(cstate->clp))
5658 return nfserr_reclaim_bad;
5659
5660 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661}
5662
Bryan Schumaker65178db2011-11-01 13:35:21 -04005663#ifdef CONFIG_NFSD_FAULT_INJECTION
5664
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005665u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5666{
Jeff Laytond20c11d2014-07-30 08:27:07 -04005667 __be32 ret;
5668 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
5669
5670 spin_lock(&nn->client_lock);
5671 ret = mark_client_expired_locked(clp);
5672 spin_unlock(&nn->client_lock);
5673 if (ret != nfs_ok)
J. Bruce Fields221a6872013-04-01 22:23:49 -04005674 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005675 expire_client(clp);
5676 return 1;
5677}
5678
Bryan Schumaker184c1842012-11-29 11:40:44 -05005679u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
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\n", buf);
5684 return 1;
5685}
5686
5687static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5688 const char *type)
5689{
5690 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005691 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005692 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5693}
5694
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005695static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5696 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005697{
5698 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005699 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005700 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005701 u64 count = 0;
5702
5703 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005704 list_for_each_entry_safe(stp, st_next,
5705 &oop->oo_owner.so_stateids, st_perstateowner) {
5706 list_for_each_entry_safe(lst, lst_next,
5707 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005708 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005709 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005710 if (++count == max)
5711 return count;
5712 }
5713 }
5714 }
5715
5716 return count;
5717}
5718
5719u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5720{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005721 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005722}
5723
Bryan Schumaker184c1842012-11-29 11:40:44 -05005724u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5725{
5726 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5727 nfsd_print_count(clp, count, "locked files");
5728 return count;
5729}
5730
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005731static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5732{
5733 struct nfs4_openowner *oop, *next;
5734 u64 count = 0;
5735
5736 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5737 if (func)
5738 func(oop);
5739 if (++count == max)
5740 break;
5741 }
5742
5743 return count;
5744}
5745
5746u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5747{
5748 return nfsd_foreach_client_open(clp, max, release_openowner);
5749}
5750
Bryan Schumaker184c1842012-11-29 11:40:44 -05005751u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5752{
5753 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5754 nfsd_print_count(clp, count, "open files");
5755 return count;
5756}
5757
Bryan Schumaker269de302012-11-29 11:40:42 -05005758static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5759 struct list_head *victims)
5760{
5761 struct nfs4_delegation *dp, *next;
5762 u64 count = 0;
5763
Benny Halevycdc97502014-05-30 09:09:30 -04005764 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005765 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005766 if (victims) {
5767 /*
5768 * It's not safe to mess with delegations that have a
5769 * non-zero dl_time. They might have already been broken
5770 * and could be processed by the laundromat outside of
5771 * the state_lock. Just leave them be.
5772 */
5773 if (dp->dl_time != 0)
5774 continue;
5775
Jeff Layton42690672014-07-25 07:34:20 -04005776 unhash_delegation_locked(dp);
5777 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005778 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005779 if (++count == max)
5780 break;
5781 }
5782 return count;
5783}
5784
5785u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5786{
5787 struct nfs4_delegation *dp, *next;
5788 LIST_HEAD(victims);
5789 u64 count;
5790
Benny Halevycdc97502014-05-30 09:09:30 -04005791 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005792 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005793 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005794
Jeff Layton2d4a5322014-07-25 07:34:21 -04005795 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5796 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005797 revoke_delegation(dp);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005798 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005799
5800 return count;
5801}
5802
5803u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5804{
Jeff Laytondff13992014-07-08 14:02:49 -04005805 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005806 LIST_HEAD(victims);
5807 u64 count;
5808
Benny Halevycdc97502014-05-30 09:09:30 -04005809 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005810 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005811 while (!list_empty(&victims)) {
5812 dp = list_first_entry(&victims, struct nfs4_delegation,
5813 dl_recall_lru);
5814 list_del_init(&dp->dl_recall_lru);
5815 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005816 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005817 }
Benny Halevycdc97502014-05-30 09:09:30 -04005818 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005819
5820 return count;
5821}
5822
Bryan Schumaker184c1842012-11-29 11:40:44 -05005823u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5824{
5825 u64 count = 0;
5826
Benny Halevycdc97502014-05-30 09:09:30 -04005827 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005828 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005829 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005830
5831 nfsd_print_count(clp, count, "delegations");
5832 return count;
5833}
5834
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005835u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005836{
5837 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005838 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005839 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005840
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005841 if (!nfsd_netns_ready(nn))
5842 return 0;
5843
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005844 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005845 count += func(clp, max - count);
5846 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005847 break;
5848 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005849
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005850 return count;
5851}
5852
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005853struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5854{
5855 struct nfs4_client *clp;
5856 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5857
5858 if (!nfsd_netns_ready(nn))
5859 return NULL;
5860
5861 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5862 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5863 return clp;
5864 }
5865 return NULL;
5866}
5867
Bryan Schumaker65178db2011-11-01 13:35:21 -04005868#endif /* CONFIG_NFSD_FAULT_INJECTION */
5869
Meelap Shahc2f1a552007-07-17 04:04:39 -07005870/*
5871 * Since the lifetime of a delegation isn't limited to that of an open, a
5872 * client may quite reasonably hang on to a delegation as long as it has
5873 * the inode cached. This becomes an obvious problem the first time a
5874 * client's inode cache approaches the size of the server's total memory.
5875 *
5876 * For now we avoid this problem by imposing a hard limit on the number
5877 * of delegations, which varies according to the server's memory size.
5878 */
5879static void
5880set_max_delegations(void)
5881{
5882 /*
5883 * Allow at most 4 delegations per megabyte of RAM. Quick
5884 * estimates suggest that in the worst case (where every delegation
5885 * is for a different inode), a delegation could take about 1.5K,
5886 * giving a worst case usage of about 6% of memory.
5887 */
5888 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5889}
5890
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005891static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005892{
5893 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5894 int i;
5895
5896 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5897 CLIENT_HASH_SIZE, GFP_KERNEL);
5898 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005899 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005900 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5901 CLIENT_HASH_SIZE, GFP_KERNEL);
5902 if (!nn->unconf_id_hashtbl)
5903 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005904 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5905 SESSION_HASH_SIZE, GFP_KERNEL);
5906 if (!nn->sessionid_hashtbl)
5907 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005908
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005909 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005910 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005911 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005912 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005913 for (i = 0; i < SESSION_HASH_SIZE; i++)
5914 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005915 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005916 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005917 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005918 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005919 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005920 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005921
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005922 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005923 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005924
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005925 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005926
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005927err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005928 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005929err_unconf_id:
5930 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005931err:
5932 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005933}
5934
5935static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005936nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005937{
5938 int i;
5939 struct nfs4_client *clp = NULL;
5940 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5941
5942 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5943 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5944 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5945 destroy_client(clp);
5946 }
5947 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005948
Kinglong Mee2b905632014-03-26 22:09:30 +08005949 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5950 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5951 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5952 destroy_client(clp);
5953 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005954 }
5955
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005956 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005957 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005958 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005959 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005960}
5961
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005962int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005963nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005964{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005965 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005966 int ret;
5967
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005968 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005969 if (ret)
5970 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005971 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005972 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005973 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005974 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005975 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005976 nn->nfsd4_grace, net);
5977 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005978 return 0;
5979}
5980
5981/* initialization to perform when the nfsd service is started: */
5982
5983int
5984nfs4_state_start(void)
5985{
5986 int ret;
5987
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005988 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005989 if (ret)
5990 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005991 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005992 if (laundry_wq == NULL) {
5993 ret = -ENOMEM;
5994 goto out_recovery;
5995 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005996 ret = nfsd4_create_callback_queue();
5997 if (ret)
5998 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005999
Meelap Shahc2f1a552007-07-17 04:04:39 -07006000 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006001
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006002 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006003
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006004out_free_laundry:
6005 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05006006out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006007 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008}
6009
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006010void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006011nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006015 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006017 cancel_delayed_work_sync(&nn->laundromat_work);
6018 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05006019
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04006020 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04006022 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006023 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006025 unhash_delegation_locked(dp);
6026 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 }
Benny Halevycdc97502014-05-30 09:09:30 -04006028 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029 list_for_each_safe(pos, next, &reaplist) {
6030 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006031 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04006032 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 }
6034
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006035 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006036 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04006037 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038}
6039
6040void
6041nfs4_state_shutdown(void)
6042{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006043 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006044 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006046
6047static void
6048get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6049{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006050 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6051 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006052}
6053
6054static void
6055put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6056{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006057 if (cstate->minorversion) {
6058 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6059 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6060 }
6061}
6062
6063void
6064clear_current_stateid(struct nfsd4_compound_state *cstate)
6065{
6066 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006067}
6068
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006069/*
6070 * functions to set current state id
6071 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006072void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006073nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6074{
6075 put_stateid(cstate, &odp->od_stateid);
6076}
6077
6078void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006079nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6080{
6081 put_stateid(cstate, &open->op_stateid);
6082}
6083
6084void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006085nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6086{
6087 put_stateid(cstate, &close->cl_stateid);
6088}
6089
6090void
6091nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6092{
6093 put_stateid(cstate, &lock->lk_resp_stateid);
6094}
6095
6096/*
6097 * functions to consume current state id
6098 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006099
6100void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006101nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6102{
6103 get_stateid(cstate, &odp->od_stateid);
6104}
6105
6106void
6107nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6108{
6109 get_stateid(cstate, &drp->dr_stateid);
6110}
6111
6112void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006113nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6114{
6115 get_stateid(cstate, &fsp->fr_stateid);
6116}
6117
6118void
6119nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6120{
6121 get_stateid(cstate, &setattr->sa_stateid);
6122}
6123
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006124void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006125nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6126{
6127 get_stateid(cstate, &close->cl_stateid);
6128}
6129
6130void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006131nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006132{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006133 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006134}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006135
6136void
6137nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6138{
6139 get_stateid(cstate, &read->rd_stateid);
6140}
6141
6142void
6143nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6144{
6145 get_stateid(cstate, &write->wr_stateid);
6146}