blob: 4af4e5eff491374d99c16230bd1431a3ed189763 [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
140static __be32 mark_client_expired(struct nfs4_client *clp)
141{
142 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
143 __be32 ret;
144
145 spin_lock(&nn->client_lock);
146 ret = mark_client_expired_locked(clp);
147 spin_unlock(&nn->client_lock);
148 return ret;
149}
150
151static __be32 get_client_locked(struct nfs4_client *clp)
152{
153 if (is_client_expired(clp))
154 return nfserr_expired;
155 atomic_inc(&clp->cl_refcount);
156 return nfs_ok;
157}
158
159/* must be called under the client_lock */
160static inline void
161renew_client_locked(struct nfs4_client *clp)
162{
163 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
164
165 if (is_client_expired(clp)) {
166 WARN_ON(1);
167 printk("%s: client (clientid %08x/%08x) already expired\n",
168 __func__,
169 clp->cl_clientid.cl_boot,
170 clp->cl_clientid.cl_id);
171 return;
172 }
173
174 dprintk("renewing client (clientid %08x/%08x)\n",
175 clp->cl_clientid.cl_boot,
176 clp->cl_clientid.cl_id);
177 list_move_tail(&clp->cl_lru, &nn->client_lru);
178 clp->cl_time = get_seconds();
179}
180
181static inline void
182renew_client(struct nfs4_client *clp)
183{
184 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
185
186 spin_lock(&nn->client_lock);
187 renew_client_locked(clp);
188 spin_unlock(&nn->client_lock);
189}
190
Fengguang Wuba138432013-04-16 22:14:15 -0400191static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400192{
193 if (!atomic_dec_and_test(&clp->cl_refcount))
194 return;
195 if (!is_client_expired(clp))
196 renew_client_locked(clp);
197}
198
Jeff Layton4b24ca72014-06-30 11:48:44 -0400199static void put_client_renew(struct nfs4_client *clp)
200{
201 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
202
Jeff Laytond6c249b2014-07-08 14:02:50 -0400203 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
204 return;
205 if (!is_client_expired(clp))
206 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400207 spin_unlock(&nn->client_lock);
208}
209
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400210static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
211{
212 __be32 status;
213
214 if (is_session_dead(ses))
215 return nfserr_badsession;
216 status = get_client_locked(ses->se_client);
217 if (status)
218 return status;
219 atomic_inc(&ses->se_ref);
220 return nfs_ok;
221}
222
223static void nfsd4_put_session_locked(struct nfsd4_session *ses)
224{
225 struct nfs4_client *clp = ses->se_client;
226
227 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
228 free_session(ses);
229 put_client_renew_locked(clp);
230}
231
232static void nfsd4_put_session(struct nfsd4_session *ses)
233{
234 struct nfs4_client *clp = ses->se_client;
235 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
236
237 spin_lock(&nn->client_lock);
238 nfsd4_put_session_locked(ses);
239 spin_unlock(&nn->client_lock);
240}
241
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400242static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400243same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400244{
245 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400246 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400247}
248
249static struct nfs4_openowner *
250find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400251 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400252{
253 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400254
Trond Myklebustd4f04892014-07-29 21:34:36 -0400255 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400256
Trond Myklebustd4f04892014-07-29 21:34:36 -0400257 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
258 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400259 if (!so->so_is_open_owner)
260 continue;
Trond Myklebustd4f04892014-07-29 21:34:36 -0400261 if (same_owner_str(so, &open->op_owner)) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400262 atomic_inc(&so->so_count);
Trond Myklebustd4f04892014-07-29 21:34:36 -0400263 return openowner(so);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400264 }
265 }
266 return NULL;
267}
268
269static struct nfs4_openowner *
270find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400271 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400272{
273 struct nfs4_openowner *oo;
274
Trond Myklebustd4f04892014-07-29 21:34:36 -0400275 spin_lock(&clp->cl_lock);
276 oo = find_openstateowner_str_locked(hashval, open, clp);
277 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400278 return oo;
279}
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281static inline u32
282opaque_hashval(const void *ptr, int nbytes)
283{
284 unsigned char *cptr = (unsigned char *) ptr;
285
286 u32 x = 0;
287 while (nbytes--) {
288 x *= 37;
289 x += *cptr++;
290 }
291 return x;
292}
293
J. Bruce Fields32513b42011-10-13 16:00:16 -0400294static void nfsd4_free_file(struct nfs4_file *f)
295{
296 kmem_cache_free(file_slab, f);
297}
298
NeilBrown13cd2182005-06-23 22:03:10 -0700299static inline void
300put_nfs4_file(struct nfs4_file *fi)
301{
Jeff Layton02e12152014-07-16 10:31:57 -0400302 might_lock(&state_lock);
303
Benny Halevycdc97502014-05-30 09:09:30 -0400304 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton89876f82013-04-02 09:01:59 -0400305 hlist_del(&fi->fi_hash);
Benny Halevycdc97502014-05-30 09:09:30 -0400306 spin_unlock(&state_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400307 nfsd4_free_file(fi);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800308 }
NeilBrown13cd2182005-06-23 22:03:10 -0700309}
310
311static inline void
312get_nfs4_file(struct nfs4_file *fi)
313{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800314 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700315}
316
Trond Myklebustde186432014-07-10 14:07:26 -0400317static struct file *
318__nfs4_get_fd(struct nfs4_file *f, int oflag)
319{
320 if (f->fi_fds[oflag])
321 return get_file(f->fi_fds[oflag]);
322 return NULL;
323}
324
325static struct file *
326find_writeable_file_locked(struct nfs4_file *f)
327{
328 struct file *ret;
329
330 lockdep_assert_held(&f->fi_lock);
331
332 ret = __nfs4_get_fd(f, O_WRONLY);
333 if (!ret)
334 ret = __nfs4_get_fd(f, O_RDWR);
335 return ret;
336}
337
338static struct file *
339find_writeable_file(struct nfs4_file *f)
340{
341 struct file *ret;
342
343 spin_lock(&f->fi_lock);
344 ret = find_writeable_file_locked(f);
345 spin_unlock(&f->fi_lock);
346
347 return ret;
348}
349
350static struct file *find_readable_file_locked(struct nfs4_file *f)
351{
352 struct file *ret;
353
354 lockdep_assert_held(&f->fi_lock);
355
356 ret = __nfs4_get_fd(f, O_RDONLY);
357 if (!ret)
358 ret = __nfs4_get_fd(f, O_RDWR);
359 return ret;
360}
361
362static struct file *
363find_readable_file(struct nfs4_file *f)
364{
365 struct file *ret;
366
367 spin_lock(&f->fi_lock);
368 ret = find_readable_file_locked(f);
369 spin_unlock(&f->fi_lock);
370
371 return ret;
372}
373
374static struct file *
375find_any_file(struct nfs4_file *f)
376{
377 struct file *ret;
378
379 spin_lock(&f->fi_lock);
380 ret = __nfs4_get_fd(f, O_RDWR);
381 if (!ret) {
382 ret = __nfs4_get_fd(f, O_WRONLY);
383 if (!ret)
384 ret = __nfs4_get_fd(f, O_RDONLY);
385 }
386 spin_unlock(&f->fi_lock);
387 return ret;
388}
389
Trond Myklebust02a35082014-07-25 07:34:22 -0400390static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800391unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700392
393/*
394 * Open owner state (share locks)
395 */
396
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500397/* hash tables for lock and open owners */
398#define OWNER_HASH_BITS 8
399#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
400#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700401
Trond Myklebustd4f04892014-07-29 21:34:36 -0400402static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400403{
404 unsigned int ret;
405
406 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500407 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400408}
NeilBrownef0f3392006-04-10 22:55:41 -0700409
NeilBrownef0f3392006-04-10 22:55:41 -0700410/* hash table for nfs4_file */
411#define FILE_HASH_BITS 8
412#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800413
Trond Myklebustca943212014-07-23 16:17:39 -0400414static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400415{
Trond Myklebustca943212014-07-23 16:17:39 -0400416 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
417}
418
419static unsigned int file_hashval(struct knfsd_fh *fh)
420{
421 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
422}
423
424static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
425{
426 return fh1->fh_size == fh2->fh_size &&
427 !memcmp(fh1->fh_base.fh_pad,
428 fh2->fh_base.fh_pad,
429 fh1->fh_size);
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400430}
431
Jeff Layton89876f82013-04-02 09:01:59 -0400432static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700433
Jeff Layton12659652014-07-10 14:07:28 -0400434static void
435__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400436{
Jeff Layton7214e862014-07-10 14:07:33 -0400437 lockdep_assert_held(&fp->fi_lock);
438
Jeff Layton12659652014-07-10 14:07:28 -0400439 if (access & NFS4_SHARE_ACCESS_WRITE)
440 atomic_inc(&fp->fi_access[O_WRONLY]);
441 if (access & NFS4_SHARE_ACCESS_READ)
442 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400443}
444
Jeff Layton12659652014-07-10 14:07:28 -0400445static __be32
446nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400447{
Jeff Layton7214e862014-07-10 14:07:33 -0400448 lockdep_assert_held(&fp->fi_lock);
449
Jeff Layton12659652014-07-10 14:07:28 -0400450 /* Does this access mode make sense? */
451 if (access & ~NFS4_SHARE_ACCESS_BOTH)
452 return nfserr_inval;
453
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400454 /* Does it conflict with a deny mode already set? */
455 if ((access & fp->fi_share_deny) != 0)
456 return nfserr_share_denied;
457
Jeff Layton12659652014-07-10 14:07:28 -0400458 __nfs4_file_get_access(fp, access);
459 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400460}
461
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400462static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
463{
464 /* Common case is that there is no deny mode. */
465 if (deny) {
466 /* Does this deny mode make sense? */
467 if (deny & ~NFS4_SHARE_DENY_BOTH)
468 return nfserr_inval;
469
470 if ((deny & NFS4_SHARE_DENY_READ) &&
471 atomic_read(&fp->fi_access[O_RDONLY]))
472 return nfserr_share_denied;
473
474 if ((deny & NFS4_SHARE_DENY_WRITE) &&
475 atomic_read(&fp->fi_access[O_WRONLY]))
476 return nfserr_share_denied;
477 }
478 return nfs_ok;
479}
480
J. Bruce Fields998db522010-08-07 09:21:41 -0400481static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400482{
Trond Myklebustde186432014-07-10 14:07:26 -0400483 might_lock(&fp->fi_lock);
484
485 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
486 struct file *f1 = NULL;
487 struct file *f2 = NULL;
488
Jeff Layton6d338b52014-07-10 14:07:29 -0400489 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400490 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400491 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400492 spin_unlock(&fp->fi_lock);
493 if (f1)
494 fput(f1);
495 if (f2)
496 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400497 }
498}
499
Jeff Layton12659652014-07-10 14:07:28 -0400500static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400501{
Jeff Layton12659652014-07-10 14:07:28 -0400502 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
503
504 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400505 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400506 if (access & NFS4_SHARE_ACCESS_READ)
507 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400508}
509
Trond Myklebust60116952014-07-29 21:34:06 -0400510static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
511 struct kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400512{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500513 struct nfs4_stid *stid;
514 int new_id;
515
Trond Myklebustf8338832014-07-25 07:34:19 -0400516 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500517 if (!stid)
518 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400519
Jeff Layton4770d722014-07-29 21:34:10 -0400520 idr_preload(GFP_KERNEL);
521 spin_lock(&cl->cl_lock);
522 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
523 spin_unlock(&cl->cl_lock);
524 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700525 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500526 goto out_free;
527 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500528 stid->sc_stateid.si_opaque.so_id = new_id;
529 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
530 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400531 atomic_set(&stid->sc_count, 1);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500532
J. Bruce Fields996e0932011-10-17 11:14:48 -0400533 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500534 * It shouldn't be a problem to reuse an opaque stateid value.
535 * I don't think it is for 4.1. But with 4.0 I worry that, for
536 * example, a stray write retransmission could be accepted by
537 * the server when it should have been rejected. Therefore,
538 * adopt a trick from the sctp code to attempt to maximize the
539 * amount of time until an id is reused, by ensuring they always
540 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400541 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500542 return stid;
543out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800544 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500545 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400546}
547
Jeff Laytonb49e0842014-07-29 21:34:11 -0400548static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400549{
Trond Myklebust60116952014-07-29 21:34:06 -0400550 struct nfs4_stid *stid;
551 struct nfs4_ol_stateid *stp;
552
553 stid = nfs4_alloc_stid(clp, stateid_slab);
554 if (!stid)
555 return NULL;
556
557 stp = openlockstateid(stid);
558 stp->st_stid.sc_free = nfs4_free_ol_stateid;
559 return stp;
560}
561
562static void nfs4_free_deleg(struct nfs4_stid *stid)
563{
Trond Myklebust60116952014-07-29 21:34:06 -0400564 kmem_cache_free(deleg_slab, stid);
565 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400566}
567
NeilBrown6282cd52014-06-04 17:39:26 +1000568/*
569 * When we recall a delegation, we should be careful not to hand it
570 * out again straight away.
571 * To ensure this we keep a pair of bloom filters ('new' and 'old')
572 * in which the filehandles of recalled delegations are "stored".
573 * If a filehandle appear in either filter, a delegation is blocked.
574 * When a delegation is recalled, the filehandle is stored in the "new"
575 * filter.
576 * Every 30 seconds we swap the filters and clear the "new" one,
577 * unless both are empty of course.
578 *
579 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
580 * low 3 bytes as hash-table indices.
581 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400582 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000583 * is used to manage concurrent access. Testing does not need the lock
584 * except when swapping the two filters.
585 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400586static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000587static struct bloom_pair {
588 int entries, old_entries;
589 time_t swap_time;
590 int new; /* index into 'set' */
591 DECLARE_BITMAP(set[2], 256);
592} blocked_delegations;
593
594static int delegation_blocked(struct knfsd_fh *fh)
595{
596 u32 hash;
597 struct bloom_pair *bd = &blocked_delegations;
598
599 if (bd->entries == 0)
600 return 0;
601 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400602 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000603 if (seconds_since_boot() - bd->swap_time > 30) {
604 bd->entries -= bd->old_entries;
605 bd->old_entries = bd->entries;
606 memset(bd->set[bd->new], 0,
607 sizeof(bd->set[0]));
608 bd->new = 1-bd->new;
609 bd->swap_time = seconds_since_boot();
610 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400611 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000612 }
613 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
614 if (test_bit(hash&255, bd->set[0]) &&
615 test_bit((hash>>8)&255, bd->set[0]) &&
616 test_bit((hash>>16)&255, bd->set[0]))
617 return 1;
618
619 if (test_bit(hash&255, bd->set[1]) &&
620 test_bit((hash>>8)&255, bd->set[1]) &&
621 test_bit((hash>>16)&255, bd->set[1]))
622 return 1;
623
624 return 0;
625}
626
627static void block_delegations(struct knfsd_fh *fh)
628{
629 u32 hash;
630 struct bloom_pair *bd = &blocked_delegations;
631
632 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
633
Jeff Laytonf54fe962014-07-25 07:34:26 -0400634 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000635 __set_bit(hash&255, bd->set[bd->new]);
636 __set_bit((hash>>8)&255, bd->set[bd->new]);
637 __set_bit((hash>>16)&255, bd->set[bd->new]);
638 if (bd->entries == 0)
639 bd->swap_time = seconds_since_boot();
640 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400641 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000642}
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644static struct nfs4_delegation *
Jeff Laytonf9416e22014-07-25 07:34:23 -0400645alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400648 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400651 n = atomic_long_inc_return(&num_delegations);
652 if (n < 0 || n > max_delegations)
653 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000654 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400655 goto out_dec;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400656 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700657 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400658 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400659
660 dp->dl_stid.sc_free = nfs4_free_deleg;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400661 /*
662 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400663 * meaning of seqid 0 isn't meaningful, really, but let's avoid
664 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400665 */
666 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700667 INIT_LIST_HEAD(&dp->dl_perfile);
668 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 INIT_LIST_HEAD(&dp->dl_recall_lru);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400670 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Jeff Layton02e12152014-07-16 10:31:57 -0400671 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400673out_dec:
674 atomic_long_dec(&num_delegations);
675 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
678void
Trond Myklebust60116952014-07-29 21:34:06 -0400679nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Trond Myklebust11b91642014-07-29 21:34:08 -0400681 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400682 struct nfs4_client *clp = s->sc_client;
683
Jeff Layton4770d722014-07-29 21:34:10 -0400684 might_lock(&clp->cl_lock);
685
Jeff Laytonb401be222014-07-29 21:34:33 -0400686 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
687 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400688 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400689 }
Trond Myklebust60116952014-07-29 21:34:06 -0400690 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400691 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400692 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400693 if (fp)
694 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500697static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Jeff Layton417c6622014-07-21 09:34:57 -0400699 lockdep_assert_held(&state_lock);
700
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -0500701 if (!fp->fi_lease)
702 return;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500703 if (atomic_dec_and_test(&fp->fi_delegees)) {
704 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
705 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400706 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500707 fp->fi_deleg_file = NULL;
708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400711static void unhash_stid(struct nfs4_stid *s)
712{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500713 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400714}
715
Benny Halevy931ee562014-05-30 09:09:27 -0400716static void
717hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
718{
Benny Halevycdc97502014-05-30 09:09:30 -0400719 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400720 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400721
Trond Myklebust67cb1272014-07-29 21:34:17 -0400722 atomic_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400723 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400724 list_add(&dp->dl_perfile, &fp->fi_delegations);
725 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
726}
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728static void
Jeff Layton42690672014-07-25 07:34:20 -0400729unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Trond Myklebust11b91642014-07-29 21:34:08 -0400731 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400732
Jeff Layton42690672014-07-25 07:34:20 -0400733 lockdep_assert_held(&state_lock);
734
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400735 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400736 /* Ensure that deleg break won't try to requeue it */
737 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400738 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400739 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400741 list_del_init(&dp->dl_perfile);
742 spin_unlock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400743 if (fp)
Trond Myklebustf8338832014-07-25 07:34:19 -0400744 nfs4_put_deleg_lease(fp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400745}
746
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400747static void destroy_delegation(struct nfs4_delegation *dp)
748{
Jeff Layton42690672014-07-25 07:34:20 -0400749 spin_lock(&state_lock);
750 unhash_delegation_locked(dp);
751 spin_unlock(&state_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400752 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400753}
754
755static void revoke_delegation(struct nfs4_delegation *dp)
756{
757 struct nfs4_client *clp = dp->dl_stid.sc_client;
758
Jeff Layton2d4a5322014-07-25 07:34:21 -0400759 WARN_ON(!list_empty(&dp->dl_recall_lru));
760
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400761 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400762 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400763 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400764 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400765 spin_lock(&clp->cl_lock);
766 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
767 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400768 }
769}
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771/*
772 * SETCLIENTID state
773 */
774
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400775static unsigned int clientid_hashval(u32 id)
776{
777 return id & CLIENT_HASH_MASK;
778}
779
780static unsigned int clientstr_hashval(const char *name)
781{
782 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
783}
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400786 * We store the NONE, READ, WRITE, and BOTH bits separately in the
787 * st_{access,deny}_bmap field of the stateid, in order to track not
788 * only what share bits are currently in force, but also what
789 * combinations of share bits previous opens have used. This allows us
790 * to enforce the recommendation of rfc 3530 14.2.19 that the server
791 * return an error if the client attempt to downgrade to a combination
792 * of share bits not explicable by closing some of its previous opens.
793 *
794 * XXX: This enforcement is actually incomplete, since we don't keep
795 * track of access/deny bit combinations; so, e.g., we allow:
796 *
797 * OPEN allow read, deny write
798 * OPEN allow both, deny none
799 * DOWNGRADE allow read, deny none
800 *
801 * which we should reject.
802 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400803static unsigned int
804bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400805 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400806 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400807
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400808 for (i = 1; i < 4; i++) {
809 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400810 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400811 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400812 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400813}
814
Jeff Layton82c5ff12012-05-11 09:45:13 -0400815/* set share access for a given stateid */
816static inline void
817set_access(u32 access, struct nfs4_ol_stateid *stp)
818{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400819 unsigned char mask = 1 << access;
820
821 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
822 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400823}
824
825/* clear share access for a given stateid */
826static inline void
827clear_access(u32 access, struct nfs4_ol_stateid *stp)
828{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400829 unsigned char mask = 1 << access;
830
831 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
832 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400833}
834
835/* test whether a given stateid has access */
836static inline bool
837test_access(u32 access, struct nfs4_ol_stateid *stp)
838{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400839 unsigned char mask = 1 << access;
840
841 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400842}
843
Jeff Laytonce0fc432012-05-11 09:45:14 -0400844/* set share deny for a given stateid */
845static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400846set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400847{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400848 unsigned char mask = 1 << deny;
849
850 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
851 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400852}
853
854/* clear share deny for a given stateid */
855static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400856clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400857{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400858 unsigned char mask = 1 << deny;
859
860 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
861 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400862}
863
864/* test whether a given stateid is denying specific access */
865static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400866test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400867{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400868 unsigned char mask = 1 << deny;
869
870 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400871}
872
873static int nfs4_access_to_omode(u32 access)
874{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400875 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400876 case NFS4_SHARE_ACCESS_READ:
877 return O_RDONLY;
878 case NFS4_SHARE_ACCESS_WRITE:
879 return O_WRONLY;
880 case NFS4_SHARE_ACCESS_BOTH:
881 return O_RDWR;
882 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500883 WARN_ON_ONCE(1);
884 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400885}
886
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400887/*
888 * A stateid that had a deny mode associated with it is being released
889 * or downgraded. Recalculate the deny mode on the file.
890 */
891static void
892recalculate_deny_mode(struct nfs4_file *fp)
893{
894 struct nfs4_ol_stateid *stp;
895
896 spin_lock(&fp->fi_lock);
897 fp->fi_share_deny = 0;
898 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
899 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
900 spin_unlock(&fp->fi_lock);
901}
902
903static void
904reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
905{
906 int i;
907 bool change = false;
908
909 for (i = 1; i < 4; i++) {
910 if ((i & deny) != i) {
911 change = true;
912 clear_deny(i, stp);
913 }
914 }
915
916 /* Recalculate per-file deny mode if there was a change */
917 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -0400918 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400919}
920
Jeff Layton82c5ff12012-05-11 09:45:13 -0400921/* release all access and file references for a given stateid */
922static void
923release_all_access(struct nfs4_ol_stateid *stp)
924{
925 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -0400926 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400927
928 if (fp && stp->st_deny_bmap != 0)
929 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400930
931 for (i = 1; i < 4; i++) {
932 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -0400933 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400934 clear_access(i, stp);
935 }
936}
937
Jeff Layton6b180f02014-07-29 21:34:26 -0400938static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
939{
940 if (!atomic_dec_and_test(&sop->so_count))
941 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -0400942 sop->so_ops->so_unhash(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -0400943 kfree(sop->so_owner.data);
944 sop->so_ops->so_free(sop);
945}
946
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400947static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500948{
Trond Myklebust11b91642014-07-29 21:34:08 -0400949 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -0400950
Jeff Layton1c755dc2014-07-29 21:34:12 -0400951 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
952
Trond Myklebust1d31a252014-07-10 14:07:25 -0400953 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500954 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400955 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500956 list_del(&stp->st_perstateowner);
957}
958
Trond Myklebust60116952014-07-29 21:34:06 -0400959static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500960{
Trond Myklebust60116952014-07-29 21:34:06 -0400961 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400962
Trond Myklebust60116952014-07-29 21:34:06 -0400963 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -0400964 if (stp->st_stateowner)
965 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -0400966 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500967}
968
Jeff Laytonb49e0842014-07-29 21:34:11 -0400969static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500970{
Jeff Laytonb49e0842014-07-29 21:34:11 -0400971 struct nfs4_ol_stateid *stp = openlockstateid(stid);
972 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500973 struct file *file;
974
Jeff Laytonb49e0842014-07-29 21:34:11 -0400975 file = find_any_file(stp->st_stid.sc_file);
976 if (file)
977 filp_close(file, (fl_owner_t)lo);
978 nfs4_free_ol_stateid(stid);
979}
980
Jeff Layton5adfd882014-07-29 21:34:31 -0400981static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -0400982{
Jeff Layton1c755dc2014-07-29 21:34:12 -0400983 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
984
985 spin_lock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400986 list_del(&stp->st_locks);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500987 unhash_generic_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400988 unhash_stid(&stp->st_stid);
Jeff Layton1c755dc2014-07-29 21:34:12 -0400989 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400990 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500991}
992
Trond Myklebustc58c6612014-07-29 21:34:35 -0400993static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500994{
Trond Myklebustd4f04892014-07-29 21:34:36 -0400995 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -0400996
Trond Myklebustd4f04892014-07-29 21:34:36 -0400997 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -0400998
Jeff Layton8f4b54c2014-07-29 21:34:29 -0400999 list_del_init(&lo->lo_owner.so_strhash);
1000}
1001
1002static void release_lockowner_stateids(struct nfs4_lockowner *lo)
1003{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001004 struct nfs4_client *clp = lo->lo_owner.so_client;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001005 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001006
Trond Myklebustd4f04892014-07-29 21:34:36 -04001007 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001008
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001009 while (!list_empty(&lo->lo_owner.so_stateids)) {
1010 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001011 struct nfs4_ol_stateid, st_perstateowner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001012 spin_unlock(&clp->cl_lock);
Jeff Layton5adfd882014-07-29 21:34:31 -04001013 release_lock_stateid(stp);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001014 spin_lock(&clp->cl_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001015 }
1016}
1017
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001018static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001019{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001020 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001021
Trond Myklebustd4f04892014-07-29 21:34:36 -04001022 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001023 unhash_lockowner_locked(lo);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001024 release_lockowner_stateids(lo);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001025 spin_unlock(&clp->cl_lock);
Jeff Layton6b180f02014-07-29 21:34:26 -04001026 nfs4_put_stateowner(&lo->lo_owner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001027}
1028
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001029static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
Jeff Layton1c755dc2014-07-29 21:34:12 -04001030 __releases(&open_stp->st_stateowner->so_client->cl_lock)
1031 __acquires(&open_stp->st_stateowner->so_client->cl_lock)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001032{
1033 struct nfs4_ol_stateid *stp;
1034
1035 while (!list_empty(&open_stp->st_locks)) {
1036 stp = list_entry(open_stp->st_locks.next,
1037 struct nfs4_ol_stateid, st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001038 spin_unlock(&open_stp->st_stateowner->so_client->cl_lock);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001039 release_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001040 spin_lock(&open_stp->st_stateowner->so_client->cl_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001041 }
1042}
1043
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001044static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -05001045{
Jeff Layton1c755dc2014-07-29 21:34:12 -04001046 spin_lock(&stp->st_stateowner->so_client->cl_lock);
J. Bruce Fields22839632009-01-11 14:27:17 -05001047 unhash_generic_stateid(stp);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001048 release_open_stateid_locks(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001049 spin_unlock(&stp->st_stateowner->so_client->cl_lock);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001050}
1051
1052static void release_open_stateid(struct nfs4_ol_stateid *stp)
1053{
1054 unhash_open_stateid(stp);
Trond Myklebust60116952014-07-29 21:34:06 -04001055 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields22839632009-01-11 14:27:17 -05001056}
1057
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001058static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001059{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001060 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001061
Trond Myklebustd4f04892014-07-29 21:34:36 -04001062 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001063
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001064 list_del_init(&oo->oo_owner.so_strhash);
1065 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001066}
1067
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001068static void release_last_closed_stateid(struct nfs4_openowner *oo)
1069{
1070 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
1071
1072 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001073 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001074 oo->oo_last_closed_stid = NULL;
Jeff Laytond3134b12014-07-29 21:34:32 -04001075 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001076 }
1077}
1078
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001079static void release_openowner_stateids(struct nfs4_openowner *oo)
1080{
1081 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001082 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001083
Trond Myklebustd4f04892014-07-29 21:34:36 -04001084 lockdep_assert_held(&clp->cl_lock);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001085
1086 while (!list_empty(&oo->oo_owner.so_stateids)) {
1087 stp = list_first_entry(&oo->oo_owner.so_stateids,
1088 struct nfs4_ol_stateid, st_perstateowner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001089 spin_unlock(&clp->cl_lock);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001090 release_open_stateid(stp);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001091 spin_lock(&clp->cl_lock);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001092 }
1093}
1094
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001095static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001096{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001097 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001098
Trond Myklebustd4f04892014-07-29 21:34:36 -04001099 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001100 unhash_openowner_locked(oo);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001101 release_openowner_stateids(oo);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001102 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001103 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001104 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001105}
1106
Marc Eshel5282fd72009-04-03 08:27:52 +03001107static inline int
1108hash_sessionid(struct nfs4_sessionid *sessionid)
1109{
1110 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1111
1112 return sid->sequence % SESSION_HASH_SIZE;
1113}
1114
Trond Myklebust8f199b82012-03-20 15:11:17 -04001115#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001116static inline void
1117dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1118{
1119 u32 *ptr = (u32 *)(&sessionid->data[0]);
1120 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1121}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001122#else
1123static inline void
1124dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1125{
1126}
1127#endif
1128
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001129/*
1130 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1131 * won't be used for replay.
1132 */
1133void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1134{
1135 struct nfs4_stateowner *so = cstate->replay_owner;
1136
1137 if (nfserr == nfserr_replay_me)
1138 return;
1139
1140 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001141 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001142 return;
1143 }
1144 if (!so)
1145 return;
1146 if (so->so_is_open_owner)
1147 release_last_closed_stateid(openowner(so));
1148 so->so_seqid++;
1149 return;
1150}
Marc Eshel5282fd72009-04-03 08:27:52 +03001151
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001152static void
1153gen_sessionid(struct nfsd4_session *ses)
1154{
1155 struct nfs4_client *clp = ses->se_client;
1156 struct nfsd4_sessionid *sid;
1157
1158 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1159 sid->clientid = clp->cl_clientid;
1160 sid->sequence = current_sessionid++;
1161 sid->reserved = 0;
1162}
1163
1164/*
Andy Adamsona6496372009-08-28 08:45:01 -04001165 * The protocol defines ca_maxresponssize_cached to include the size of
1166 * the rpc header, but all we need to cache is the data starting after
1167 * the end of the initial SEQUENCE operation--the rest we regenerate
1168 * each time. Therefore we can advertise a ca_maxresponssize_cached
1169 * value that is the number of bytes in our cache plus a few additional
1170 * bytes. In order to stay on the safe side, and not promise more than
1171 * we can cache, those additional bytes must be the minimum possible: 24
1172 * bytes of rpc header (xid through accept state, with AUTH_NULL
1173 * verifier), 12 for the compound header (with zero-length tag), and 44
1174 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001175 */
Andy Adamsona6496372009-08-28 08:45:01 -04001176#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1177
Andy Adamson557ce262009-08-28 08:45:04 -04001178static void
1179free_session_slots(struct nfsd4_session *ses)
1180{
1181 int i;
1182
1183 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1184 kfree(ses->se_slots[i]);
1185}
1186
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001187/*
1188 * We don't actually need to cache the rpc and session headers, so we
1189 * can allocate a little less for each slot:
1190 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001191static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001192{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001193 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001194
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001195 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1196 size = 0;
1197 else
1198 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1199 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001200}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001201
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001202/*
1203 * XXX: If we run out of reserved DRC memory we could (up to a point)
1204 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001205 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001206 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001207static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001208{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001209 u32 slotsize = slot_bytes(ca);
1210 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001211 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001212
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001213 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001214 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1215 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001216 num = min_t(int, num, avail / slotsize);
1217 nfsd_drc_mem_used += num * slotsize;
1218 spin_unlock(&nfsd_drc_lock);
1219
1220 return num;
1221}
1222
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001223static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001224{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001225 int slotsize = slot_bytes(ca);
1226
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001227 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001228 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001229 spin_unlock(&nfsd_drc_lock);
1230}
1231
Kinglong Mee60810e52014-01-01 00:35:47 +08001232static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1233 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001234{
Kinglong Mee60810e52014-01-01 00:35:47 +08001235 int numslots = fattrs->maxreqs;
1236 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001237 struct nfsd4_session *new;
1238 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001239
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001240 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001241 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1242 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001243
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001244 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001245 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001246 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001247 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001248 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001249 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001250 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001251 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001252 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001253
1254 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1255 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1256
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001257 return new;
1258out_free:
1259 while (i--)
1260 kfree(new->se_slots[i]);
1261 kfree(new);
1262 return NULL;
1263}
1264
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001265static void free_conn(struct nfsd4_conn *c)
1266{
1267 svc_xprt_put(c->cn_xprt);
1268 kfree(c);
1269}
1270
1271static void nfsd4_conn_lost(struct svc_xpt_user *u)
1272{
1273 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1274 struct nfs4_client *clp = c->cn_session->se_client;
1275
1276 spin_lock(&clp->cl_lock);
1277 if (!list_empty(&c->cn_persession)) {
1278 list_del(&c->cn_persession);
1279 free_conn(c);
1280 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001281 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001282 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001283}
1284
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001285static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001286{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001287 struct nfsd4_conn *conn;
1288
1289 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1290 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001291 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001292 svc_xprt_get(rqstp->rq_xprt);
1293 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001294 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001295 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1296 return conn;
1297}
1298
J. Bruce Fields328ead22010-09-29 16:11:06 -04001299static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1300{
1301 conn->cn_session = ses;
1302 list_add(&conn->cn_persession, &ses->se_conns);
1303}
1304
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001305static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1306{
1307 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001308
1309 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001310 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001311 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001312}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001313
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001314static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001315{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001316 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001317 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001318}
1319
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001320static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001321{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001322 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001323
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001324 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001325 ret = nfsd4_register_conn(conn);
1326 if (ret)
1327 /* oops; xprt is already down: */
1328 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001329 /* We may have gained or lost a callback channel: */
1330 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001331}
1332
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001333static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001334{
1335 u32 dir = NFS4_CDFC4_FORE;
1336
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001337 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001338 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001339 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001340}
1341
1342/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001343static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001344{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001345 struct nfs4_client *clp = s->se_client;
1346 struct nfsd4_conn *c;
1347
1348 spin_lock(&clp->cl_lock);
1349 while (!list_empty(&s->se_conns)) {
1350 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1351 list_del_init(&c->cn_persession);
1352 spin_unlock(&clp->cl_lock);
1353
1354 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1355 free_conn(c);
1356
1357 spin_lock(&clp->cl_lock);
1358 }
1359 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001360}
1361
J. Bruce Fields1377b692012-09-11 21:42:40 -04001362static void __free_session(struct nfsd4_session *ses)
1363{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001364 free_session_slots(ses);
1365 kfree(ses);
1366}
1367
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001368static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001369{
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001370 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1371
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001372 lockdep_assert_held(&nn->client_lock);
1373 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001374 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001375 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001376}
Andy Adamson557ce262009-08-28 08:45:04 -04001377
Fengguang Wu135ae822012-11-10 07:20:25 -05001378static 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 -04001379{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001380 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001381 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001382
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001383 new->se_client = clp;
1384 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001385
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001386 INIT_LIST_HEAD(&new->se_conns);
1387
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001388 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001389 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001390 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001391 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001392 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001393 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001394 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001395 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001396 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001397 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001398 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001399 spin_unlock(&nn->client_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001400
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001401 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001402 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001403 /*
1404 * This is a little silly; with sessions there's no real
1405 * use for the callback address. Use the peer address
1406 * as a reasonable default for now, but consider fixing
1407 * the rpc client not to require an address in the
1408 * future:
1409 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001410 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1411 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001412 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001413}
1414
Benny Halevy9089f1b2010-05-12 00:12:26 +03001415/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001416static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001417__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001418{
1419 struct nfsd4_session *elem;
1420 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001421 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001422
1423 dump_sessionid(__func__, sessionid);
1424 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001425 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001426 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001427 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1428 NFS4_MAX_SESSIONID_LEN)) {
1429 return elem;
1430 }
1431 }
1432
1433 dprintk("%s: session not found\n", __func__);
1434 return NULL;
1435}
1436
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001437static struct nfsd4_session *
1438find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1439 __be32 *ret)
1440{
1441 struct nfsd4_session *session;
1442 __be32 status = nfserr_badsession;
1443
1444 session = __find_in_sessionid_hashtbl(sessionid, net);
1445 if (!session)
1446 goto out;
1447 status = nfsd4_get_session_locked(session);
1448 if (status)
1449 session = NULL;
1450out:
1451 *ret = status;
1452 return session;
1453}
1454
Benny Halevy9089f1b2010-05-12 00:12:26 +03001455/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001456static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001457unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001458{
1459 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001460 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001461 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001462 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001463}
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1466static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001467STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001469 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001471 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001472 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 return 1;
1474}
1475
1476/*
1477 * XXX Should we use a slab cache ?
1478 * This type of memory management is somewhat inefficient, but we use it
1479 * anyway since SETCLIENTID is not a common operation.
1480 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001481static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
1483 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001484 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001486 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1487 if (clp == NULL)
1488 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001489 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001490 if (clp->cl_name.data == NULL)
1491 goto err_no_name;
1492 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1493 OWNER_HASH_SIZE, GFP_KERNEL);
1494 if (!clp->cl_ownerstr_hashtbl)
1495 goto err_no_hashtbl;
1496 for (i = 0; i < OWNER_HASH_SIZE; i++)
1497 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001498 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001499 INIT_LIST_HEAD(&clp->cl_sessions);
1500 idr_init(&clp->cl_stateids);
1501 atomic_set(&clp->cl_refcount, 0);
1502 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1503 INIT_LIST_HEAD(&clp->cl_idhash);
1504 INIT_LIST_HEAD(&clp->cl_openowners);
1505 INIT_LIST_HEAD(&clp->cl_delegations);
1506 INIT_LIST_HEAD(&clp->cl_lru);
1507 INIT_LIST_HEAD(&clp->cl_callbacks);
1508 INIT_LIST_HEAD(&clp->cl_revoked);
1509 spin_lock_init(&clp->cl_lock);
1510 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001512err_no_hashtbl:
1513 kfree(clp->cl_name.data);
1514err_no_name:
1515 kfree(clp);
1516 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001519static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520free_client(struct nfs4_client *clp)
1521{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001522 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001523
1524 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001525 while (!list_empty(&clp->cl_sessions)) {
1526 struct nfsd4_session *ses;
1527 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1528 se_perclnt);
1529 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001530 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1531 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001532 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001533 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001534 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001535 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001537 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 kfree(clp);
1539}
1540
Benny Halevy84d38ac2010-05-12 00:13:16 +03001541/* must be called under the client_lock */
1542static inline void
1543unhash_client_locked(struct nfs4_client *clp)
1544{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001545 struct nfsd4_session *ses;
1546
Benny Halevy84d38ac2010-05-12 00:13:16 +03001547 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001548 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001549 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1550 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001551 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001552}
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001555destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001557 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001560 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001563 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001564 while (!list_empty(&clp->cl_delegations)) {
1565 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001566 unhash_delegation_locked(dp);
1567 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
Benny Halevycdc97502014-05-30 09:09:30 -04001569 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 while (!list_empty(&reaplist)) {
1571 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001572 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001573 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001575 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001576 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001577 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001578 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001579 }
NeilBrownea1da632005-06-23 22:04:17 -07001580 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001581 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Jeff Laytond3134b12014-07-29 21:34:32 -04001582 atomic_inc(&oo->oo_owner.so_count);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001583 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001585 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001586 if (clp->cl_cb_conn.cb_xprt)
1587 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001588 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001589 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001590 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001591 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001592 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001593 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001594 unhash_client_locked(clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001595 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1596 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001597 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
1599
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001600static void expire_client(struct nfs4_client *clp)
1601{
1602 nfsd4_client_record_remove(clp);
1603 destroy_client(clp);
1604}
1605
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001606static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1607{
1608 memcpy(target->cl_verifier.data, source->data,
1609 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
1611
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001612static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1613{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1615 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1616}
1617
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001618static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001619{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001620 if (source->cr_principal) {
1621 target->cr_principal =
1622 kstrdup(source->cr_principal, GFP_KERNEL);
1623 if (target->cr_principal == NULL)
1624 return -ENOMEM;
1625 } else
1626 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001627 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 target->cr_uid = source->cr_uid;
1629 target->cr_gid = source->cr_gid;
1630 target->cr_group_info = source->cr_group_info;
1631 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001632 target->cr_gss_mech = source->cr_gss_mech;
1633 if (source->cr_gss_mech)
1634 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001635 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636}
1637
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001638static long long
1639compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1640{
1641 long long res;
1642
1643 res = o1->len - o2->len;
1644 if (res)
1645 return res;
1646 return (long long)memcmp(o1->data, o2->data, o1->len);
1647}
1648
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001649static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001650{
NeilBrowna55370a2005-06-23 22:03:52 -07001651 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
1654static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001655same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1656{
1657 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658}
1659
1660static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001661same_clid(clientid_t *cl1, clientid_t *cl2)
1662{
1663 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
1665
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001666static bool groups_equal(struct group_info *g1, struct group_info *g2)
1667{
1668 int i;
1669
1670 if (g1->ngroups != g2->ngroups)
1671 return false;
1672 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001673 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001674 return false;
1675 return true;
1676}
1677
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001678/*
1679 * RFC 3530 language requires clid_inuse be returned when the
1680 * "principal" associated with a requests differs from that previously
1681 * used. We use uid, gid's, and gss principal string as our best
1682 * approximation. We also don't want to allow non-gss use of a client
1683 * established using gss: in theory cr_principal should catch that
1684 * change, but in practice cr_principal can be null even in the gss case
1685 * since gssd doesn't always pass down a principal string.
1686 */
1687static bool is_gss_cred(struct svc_cred *cr)
1688{
1689 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1690 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1691}
1692
1693
Vivek Trivedi5559b502012-07-24 21:18:20 +05301694static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001695same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1696{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001697 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001698 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1699 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001700 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1701 return false;
1702 if (cr1->cr_principal == cr2->cr_principal)
1703 return true;
1704 if (!cr1->cr_principal || !cr2->cr_principal)
1705 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301706 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
1708
J. Bruce Fields57266a62013-04-13 14:27:29 -04001709static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1710{
1711 struct svc_cred *cr = &rqstp->rq_cred;
1712 u32 service;
1713
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001714 if (!cr->cr_gss_mech)
1715 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001716 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1717 return service == RPC_GSS_SVC_INTEGRITY ||
1718 service == RPC_GSS_SVC_PRIVACY;
1719}
1720
1721static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1722{
1723 struct svc_cred *cr = &rqstp->rq_cred;
1724
1725 if (!cl->cl_mach_cred)
1726 return true;
1727 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1728 return false;
1729 if (!svc_rqst_integrity_protected(rqstp))
1730 return false;
1731 if (!cr->cr_principal)
1732 return false;
1733 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1734}
1735
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001736static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001737{
1738 static u32 current_clientid = 1;
1739
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001740 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 clp->cl_clientid.cl_id = current_clientid++;
1742}
1743
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001744static void gen_confirm(struct nfs4_client *clp)
1745{
Chuck Leverab4684d2012-03-02 17:13:50 -05001746 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001747 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Jeff Laytonf4199922014-06-17 07:44:11 -04001749 /*
1750 * This is opaque to client, so no need to byte-swap. Use
1751 * __force to keep sparse happy
1752 */
1753 verf[0] = (__force __be32)get_seconds();
1754 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001755 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
Jeff Layton4770d722014-07-29 21:34:10 -04001758static struct nfs4_stid *
1759find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001760{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001761 struct nfs4_stid *ret;
1762
1763 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1764 if (!ret || !ret->sc_type)
1765 return NULL;
1766 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001767}
1768
Jeff Layton4770d722014-07-29 21:34:10 -04001769static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001770find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001771{
1772 struct nfs4_stid *s;
1773
Jeff Layton4770d722014-07-29 21:34:10 -04001774 spin_lock(&cl->cl_lock);
1775 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001776 if (s != NULL) {
1777 if (typemask & s->sc_type)
1778 atomic_inc(&s->sc_count);
1779 else
1780 s = NULL;
1781 }
Jeff Layton4770d722014-07-29 21:34:10 -04001782 spin_unlock(&cl->cl_lock);
1783 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001784}
1785
Jeff Layton2216d442012-11-12 15:00:57 -05001786static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001787 struct svc_rqst *rqstp, nfs4_verifier *verf)
1788{
1789 struct nfs4_client *clp;
1790 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001791 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001792 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001793 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001794
1795 clp = alloc_client(name);
1796 if (clp == NULL)
1797 return NULL;
1798
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001799 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1800 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001801 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001802 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001803 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001804 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001805 }
Jeff Layton02e12152014-07-16 10:31:57 -04001806 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001807 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001808 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001809 copy_verf(clp, verf);
1810 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001811 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001812 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001813 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001814 return clp;
1815}
1816
NeilBrownfd39ca92005-06-23 22:04:03 -07001817static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001818add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1819{
1820 struct rb_node **new = &(root->rb_node), *parent = NULL;
1821 struct nfs4_client *clp;
1822
1823 while (*new) {
1824 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1825 parent = *new;
1826
1827 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1828 new = &((*new)->rb_left);
1829 else
1830 new = &((*new)->rb_right);
1831 }
1832
1833 rb_link_node(&new_clp->cl_namenode, parent, new);
1834 rb_insert_color(&new_clp->cl_namenode, root);
1835}
1836
1837static struct nfs4_client *
1838find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1839{
1840 long long cmp;
1841 struct rb_node *node = root->rb_node;
1842 struct nfs4_client *clp;
1843
1844 while (node) {
1845 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1846 cmp = compare_blob(&clp->cl_name, name);
1847 if (cmp > 0)
1848 node = node->rb_left;
1849 else if (cmp < 0)
1850 node = node->rb_right;
1851 else
1852 return clp;
1853 }
1854 return NULL;
1855}
1856
1857static void
1858add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
1860 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001861 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001863 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001864 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001866 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001867 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869
NeilBrownfd39ca92005-06-23 22:04:03 -07001870static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871move_to_confirmed(struct nfs4_client *clp)
1872{
1873 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001874 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001877 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001878 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001879 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001880 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 renew_client(clp);
1882}
1883
1884static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001885find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
1887 struct nfs4_client *clp;
1888 unsigned int idhashval = clientid_hashval(clid->cl_id);
1889
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001890 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001891 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001892 if ((bool)clp->cl_minorversion != sessions)
1893 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001894 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
1898 return NULL;
1899}
1900
1901static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001902find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1903{
1904 struct list_head *tbl = nn->conf_id_hashtbl;
1905
1906 return find_client_in_id_table(tbl, clid, sessions);
1907}
1908
1909static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001910find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001912 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001914 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
1916
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001917static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001918{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001919 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001920}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001921
NeilBrown28ce6052005-06-23 22:03:56 -07001922static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001923find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001924{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001925 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001926}
1927
1928static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001929find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001930{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001931 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001932}
1933
NeilBrownfd39ca92005-06-23 22:04:03 -07001934static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001935gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001937 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001938 struct sockaddr *sa = svc_addr(rqstp);
1939 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001940 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Jeff Layton7077ecb2009-08-14 12:57:58 -04001942 /* Currently, we only support tcp and tcp6 for the callback channel */
1943 if (se->se_callback_netid_len == 3 &&
1944 !memcmp(se->se_callback_netid_val, "tcp", 3))
1945 expected_family = AF_INET;
1946 else if (se->se_callback_netid_len == 4 &&
1947 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1948 expected_family = AF_INET6;
1949 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 goto out_err;
1951
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001952 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001953 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001954 (struct sockaddr *)&conn->cb_addr,
1955 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001956
J. Bruce Fields07263f12010-05-31 19:09:40 -04001957 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001959
J. Bruce Fields07263f12010-05-31 19:09:40 -04001960 if (conn->cb_addr.ss_family == AF_INET6)
1961 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001962
J. Bruce Fields07263f12010-05-31 19:09:40 -04001963 conn->cb_prog = se->se_callback_prog;
1964 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001965 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 return;
1967out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001968 conn->cb_addr.ss_family = AF_UNSPEC;
1969 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001970 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 "will not receive delegations\n",
1972 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 return;
1975}
1976
Andy Adamson074fe892009-04-03 08:28:15 +03001977/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04001978 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001979 */
Trond Myklebustb6076642014-06-30 11:48:35 -04001980static void
Andy Adamson074fe892009-04-03 08:28:15 +03001981nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1982{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001983 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04001984 struct nfsd4_slot *slot = resp->cstate.slot;
1985 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001986
Andy Adamson557ce262009-08-28 08:45:04 -04001987 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001988
Andy Adamson557ce262009-08-28 08:45:04 -04001989 slot->sl_opcnt = resp->opcnt;
1990 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001991
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001992 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001993 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001994 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001995 return;
1996 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001997 base = resp->cstate.data_offset;
1998 slot->sl_datalen = buf->len - base;
1999 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04002000 WARN("%s: sessions DRC could not cache compound\n", __func__);
2001 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002002}
2003
2004/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002005 * Encode the replay sequence operation from the slot values.
2006 * If cachethis is FALSE encode the uncached rep error on the next
2007 * operation which sets resp->p and increments resp->opcnt for
2008 * nfs4svc_encode_compoundres.
2009 *
Andy Adamson074fe892009-04-03 08:28:15 +03002010 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002011static __be32
2012nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2013 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002014{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002015 struct nfsd4_op *op;
2016 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002017
Andy Adamsonabfabf82009-07-23 19:02:18 -04002018 /* Encode the replayed sequence operation */
2019 op = &args->ops[resp->opcnt - 1];
2020 nfsd4_encode_operation(resp, op);
2021
2022 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002023 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002024 op = &args->ops[resp->opcnt++];
2025 op->status = nfserr_retry_uncached_rep;
2026 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002027 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002028 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002029}
2030
2031/*
Andy Adamson557ce262009-08-28 08:45:04 -04002032 * The sequence operation is not cached because we can use the slot and
2033 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002034 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002035static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002036nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2037 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002038{
Andy Adamson557ce262009-08-28 08:45:04 -04002039 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002040 struct xdr_stream *xdr = &resp->xdr;
2041 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002042 __be32 status;
2043
Andy Adamson557ce262009-08-28 08:45:04 -04002044 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002045
Andy Adamsonabfabf82009-07-23 19:02:18 -04002046 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002047 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002048 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002049
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002050 p = xdr_reserve_space(xdr, slot->sl_datalen);
2051 if (!p) {
2052 WARN_ON_ONCE(1);
2053 return nfserr_serverfault;
2054 }
2055 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2056 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002057
Andy Adamson557ce262009-08-28 08:45:04 -04002058 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002059 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002060}
2061
Andy Adamson0733d212009-04-03 08:28:01 +03002062/*
2063 * Set the exchange_id flags returned by the server.
2064 */
2065static void
2066nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2067{
2068 /* pNFS is not supported */
2069 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2070
2071 /* Referrals are supported, Migration is not. */
2072 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2073
2074 /* set the wire flags to return to client. */
2075 clid->flags = new->cl_exchange_flags;
2076}
2077
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002078static bool client_has_state(struct nfs4_client *clp)
2079{
2080 /*
2081 * Note clp->cl_openowners check isn't quite right: there's no
2082 * need to count owners without stateid's.
2083 *
2084 * Also note we should probably be using this in 4.0 case too.
2085 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002086 return !list_empty(&clp->cl_openowners)
2087 || !list_empty(&clp->cl_delegations)
2088 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002089}
2090
Al Virob37ad282006-10-19 23:28:59 -07002091__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002092nfsd4_exchange_id(struct svc_rqst *rqstp,
2093 struct nfsd4_compound_state *cstate,
2094 struct nfsd4_exchange_id *exid)
2095{
Andy Adamson0733d212009-04-03 08:28:01 +03002096 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002097 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002098 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002099 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002100 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002101 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002102 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002103
Jeff Layton363168b2009-08-14 12:57:56 -04002104 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002105 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002106 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002107 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002108 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002109
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002110 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002111 return nfserr_inval;
2112
Andy Adamson0733d212009-04-03 08:28:01 +03002113 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002114 case SP4_MACH_CRED:
2115 if (!svc_rqst_integrity_protected(rqstp))
2116 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002117 case SP4_NONE:
2118 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002119 default: /* checked by xdr code */
2120 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002121 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002122 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002123 }
2124
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002125 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03002126 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002127 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002128 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002129 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2130 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2131
J. Bruce Fields136e6582012-05-12 20:37:23 -04002132 if (update) {
2133 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002134 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002135 goto out;
2136 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002137 if (!mach_creds_match(conf, rqstp)) {
2138 status = nfserr_wrong_cred;
2139 goto out;
2140 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002141 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002142 status = nfserr_perm;
2143 goto out;
2144 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002145 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002146 status = nfserr_not_same;
2147 goto out;
2148 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002149 /* case 6 */
2150 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2151 new = conf;
2152 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002153 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002154 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002155 if (client_has_state(conf)) {
2156 status = nfserr_clid_inuse;
2157 goto out;
2158 }
Andy Adamson0733d212009-04-03 08:28:01 +03002159 expire_client(conf);
2160 goto out_new;
2161 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002162 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002163 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002164 new = conf;
2165 goto out_copy;
2166 }
2167 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04002168 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002169 }
2170
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002171 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002172 status = nfserr_noent;
2173 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002174 }
2175
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002176 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002177 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03002178 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002179
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002180 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002181out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05002182 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03002183 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04002184 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03002185 goto out;
2186 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002187 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002188 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002189
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002190 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002191 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03002192out_copy:
2193 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2194 exid->clientid.cl_id = new->cl_clientid.cl_id;
2195
J. Bruce Fields778df3f2012-05-25 21:40:23 -04002196 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03002197 nfsd4_set_ex_flags(new, exid);
2198
2199 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04002200 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002201 status = nfs_ok;
2202
2203out:
2204 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03002205 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002206}
2207
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002208static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002209check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002210{
Andy Adamson88e588d2009-07-23 19:02:15 -04002211 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2212 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002213
2214 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002215 if (slot_inuse) {
2216 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002217 return nfserr_jukebox;
2218 else
2219 return nfserr_seq_misordered;
2220 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002221 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002222 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002223 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002224 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002225 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002226 return nfserr_seq_misordered;
2227}
2228
Andy Adamson49557cc2009-07-23 19:02:16 -04002229/*
2230 * Cache the create session result into the create session single DRC
2231 * slot cache by saving the xdr structure. sl_seqid has been set.
2232 * Do this for solo or embedded create session operations.
2233 */
2234static void
2235nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002236 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002237{
2238 slot->sl_status = nfserr;
2239 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2240}
2241
2242static __be32
2243nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2244 struct nfsd4_clid_slot *slot)
2245{
2246 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2247 return slot->sl_status;
2248}
2249
Mi Jinlong1b74c252011-07-14 14:50:17 +08002250#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2251 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2252 1 + /* MIN tag is length with zero, only length */ \
2253 3 + /* version, opcount, opcode */ \
2254 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2255 /* seqid, slotID, slotID, cache */ \
2256 4 ) * sizeof(__be32))
2257
2258#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2259 2 + /* verifier: AUTH_NULL, length 0 */\
2260 1 + /* status */ \
2261 1 + /* MIN tag is length with zero, only length */ \
2262 3 + /* opcount, opcode, opstatus*/ \
2263 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2264 /* seqid, slotID, slotID, slotID, status */ \
2265 5 ) * sizeof(__be32))
2266
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002267static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002268{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002269 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2270
J. Bruce Fields373cd402013-04-08 15:42:12 -04002271 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2272 return nfserr_toosmall;
2273 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2274 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002275 ca->headerpadsz = 0;
2276 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2277 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2278 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2279 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2280 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2281 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2282 /*
2283 * Note decreasing slot size below client's request may make it
2284 * difficult for client to function correctly, whereas
2285 * decreasing the number of slots will (just?) affect
2286 * performance. When short on memory we therefore prefer to
2287 * decrease number of slots instead of their size. Clients that
2288 * request larger slots than they need will get poor results:
2289 */
2290 ca->maxreqs = nfsd4_get_drc_mem(ca);
2291 if (!ca->maxreqs)
2292 return nfserr_jukebox;
2293
J. Bruce Fields373cd402013-04-08 15:42:12 -04002294 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002295}
2296
Kinglong Mee8a891632013-12-23 18:11:02 +08002297#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2298 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2299#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2300 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2301
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002302static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2303{
2304 ca->headerpadsz = 0;
2305
2306 /*
2307 * These RPC_MAX_HEADER macros are overkill, especially since we
2308 * don't even do gss on the backchannel yet. But this is still
2309 * less than 1k. Tighten up this estimate in the unlikely event
2310 * it turns out to be a problem for some client:
2311 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002312 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002313 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002314 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002315 return nfserr_toosmall;
2316 ca->maxresp_cached = 0;
2317 if (ca->maxops < 2)
2318 return nfserr_toosmall;
2319
2320 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002321}
2322
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002323static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2324{
2325 switch (cbs->flavor) {
2326 case RPC_AUTH_NULL:
2327 case RPC_AUTH_UNIX:
2328 return nfs_ok;
2329 default:
2330 /*
2331 * GSS case: the spec doesn't allow us to return this
2332 * error. But it also doesn't allow us not to support
2333 * GSS.
2334 * I'd rather this fail hard than return some error the
2335 * client might think it can already handle:
2336 */
2337 return nfserr_encr_alg_unsupp;
2338 }
2339}
2340
Andy Adamson069b6ad2009-04-03 08:27:58 +03002341__be32
2342nfsd4_create_session(struct svc_rqst *rqstp,
2343 struct nfsd4_compound_state *cstate,
2344 struct nfsd4_create_session *cr_ses)
2345{
Jeff Layton363168b2009-08-14 12:57:56 -04002346 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002347 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002348 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002349 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002350 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002351 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002352 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002353
Mi Jinlonga62573d2011-03-23 17:57:07 +08002354 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2355 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002356 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2357 if (status)
2358 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002359 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002360 if (status)
2361 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002362 status = check_backchannel_attrs(&cr_ses->back_channel);
2363 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002364 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002365 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002366 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002367 if (!new)
2368 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002369 conn = alloc_conn_from_crses(rqstp, cr_ses);
2370 if (!conn)
2371 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002372
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002373 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002374 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002375 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002376 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002377
2378 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002379 status = nfserr_wrong_cred;
2380 if (!mach_creds_match(conf, rqstp))
2381 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002382 cs_slot = &conf->cl_cs_slot;
2383 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002384 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002385 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002386 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002387 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002388 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002389 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002390 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002391 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002392 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002393 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002394 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002395 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002396 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002397 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002398 status = nfserr_wrong_cred;
2399 if (!mach_creds_match(unconf, rqstp))
2400 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002401 cs_slot = &unconf->cl_cs_slot;
2402 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002403 if (status) {
2404 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002405 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002406 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002407 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002408 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002409 if (old) {
2410 status = mark_client_expired(old);
2411 if (status)
2412 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002413 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002414 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002415 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002416 conf = unconf;
2417 } else {
2418 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002419 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002420 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002421 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002422 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002423 * We do not support RDMA or persistent sessions
2424 */
2425 cr_ses->flags &= ~SESSION4_PERSIST;
2426 cr_ses->flags &= ~SESSION4_RDMA;
2427
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002428 init_session(rqstp, new, conf, cr_ses);
2429 nfsd4_init_conn(rqstp, conn, new);
2430
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002431 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002432 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002433 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002434 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002435
Andy Adamson49557cc2009-07-23 19:02:16 -04002436 /* cache solo and embedded create sessions under the state lock */
2437 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002438 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002439 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002440out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08002441 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002442 free_conn(conn);
2443out_free_session:
2444 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002445out_release_drc_mem:
2446 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002447 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002448}
2449
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002450static __be32 nfsd4_map_bcts_dir(u32 *dir)
2451{
2452 switch (*dir) {
2453 case NFS4_CDFC4_FORE:
2454 case NFS4_CDFC4_BACK:
2455 return nfs_ok;
2456 case NFS4_CDFC4_FORE_OR_BOTH:
2457 case NFS4_CDFC4_BACK_OR_BOTH:
2458 *dir = NFS4_CDFC4_BOTH;
2459 return nfs_ok;
2460 };
2461 return nfserr_inval;
2462}
2463
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002464__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2465{
2466 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002467 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002468 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002469
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002470 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2471 if (status)
2472 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002473 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002474 session->se_cb_prog = bc->bc_cb_program;
2475 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002476 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002477
2478 nfsd4_probe_callback(session->se_client);
2479
2480 return nfs_ok;
2481}
2482
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002483__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2484 struct nfsd4_compound_state *cstate,
2485 struct nfsd4_bind_conn_to_session *bcts)
2486{
2487 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002488 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002489 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002490 struct net *net = SVC_NET(rqstp);
2491 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002492
2493 if (!nfsd4_last_compound_op(rqstp))
2494 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002495 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002496 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002497 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002498 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002499 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002500 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002501 status = nfserr_wrong_cred;
2502 if (!mach_creds_match(session->se_client, rqstp))
2503 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002504 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002505 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002506 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002507 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002508 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002509 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002510 goto out;
2511 nfsd4_init_conn(rqstp, conn, session);
2512 status = nfs_ok;
2513out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002514 nfsd4_put_session(session);
2515out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002516 nfs4_unlock_state();
2517 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002518}
2519
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002520static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2521{
2522 if (!session)
2523 return 0;
2524 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2525}
2526
Andy Adamson069b6ad2009-04-03 08:27:58 +03002527__be32
2528nfsd4_destroy_session(struct svc_rqst *r,
2529 struct nfsd4_compound_state *cstate,
2530 struct nfsd4_destroy_session *sessionid)
2531{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002532 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002533 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002534 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002535 struct net *net = SVC_NET(r);
2536 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002537
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002538 nfs4_lock_state();
2539 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002540 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002541 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002542 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002543 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002544 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002545 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002546 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002547 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002548 if (!ses)
2549 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002550 status = nfserr_wrong_cred;
2551 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002552 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002553 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002554 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002555 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002556 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002557 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002558
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002559 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002560
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002561 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002562 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002563out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002564 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002565out_client_lock:
2566 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002567out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002568 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002569 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002570}
2571
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002572static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002573{
2574 struct nfsd4_conn *c;
2575
2576 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002577 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002578 return c;
2579 }
2580 }
2581 return NULL;
2582}
2583
J. Bruce Fields57266a62013-04-13 14:27:29 -04002584static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002585{
2586 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002587 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002588 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002589 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002590
2591 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002592 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002593 if (c)
2594 goto out_free;
2595 status = nfserr_conn_not_bound_to_session;
2596 if (clp->cl_mach_cred)
2597 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002598 __nfsd4_hash_conn(new, ses);
2599 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002600 ret = nfsd4_register_conn(new);
2601 if (ret)
2602 /* oops; xprt is already down: */
2603 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002604 return nfs_ok;
2605out_free:
2606 spin_unlock(&clp->cl_lock);
2607 free_conn(new);
2608 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002609}
2610
Mi Jinlong868b89c2011-04-27 09:09:58 +08002611static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2612{
2613 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2614
2615 return args->opcnt > session->se_fchannel.maxops;
2616}
2617
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002618static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2619 struct nfsd4_session *session)
2620{
2621 struct xdr_buf *xb = &rqstp->rq_arg;
2622
2623 return xb->len > session->se_fchannel.maxreq_sz;
2624}
2625
Andy Adamson069b6ad2009-04-03 08:27:58 +03002626__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002627nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002628 struct nfsd4_compound_state *cstate,
2629 struct nfsd4_sequence *seq)
2630{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002631 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002632 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002633 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002634 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002635 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002636 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002637 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002638 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002639 struct net *net = SVC_NET(rqstp);
2640 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002641
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002642 if (resp->opcnt != 1)
2643 return nfserr_sequence_pos;
2644
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002645 /*
2646 * Will be either used or freed by nfsd4_sequence_check_conn
2647 * below.
2648 */
2649 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2650 if (!conn)
2651 return nfserr_jukebox;
2652
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002653 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002654 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002655 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002656 goto out_no_session;
2657 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002658
Mi Jinlong868b89c2011-04-27 09:09:58 +08002659 status = nfserr_too_many_ops;
2660 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002661 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002662
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002663 status = nfserr_req_too_big;
2664 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002665 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002666
Benny Halevyb85d4c02009-04-03 08:28:08 +03002667 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002668 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002669 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002670
Andy Adamson557ce262009-08-28 08:45:04 -04002671 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002672 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2673
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002674 /* We do not negotiate the number of slots yet, so set the
2675 * maxslots to the session maxreqs which is used to encode
2676 * sr_highest_slotid and the sr_target_slot id to maxslots */
2677 seq->maxslots = session->se_fchannel.maxreqs;
2678
J. Bruce Fields73e79482012-02-13 16:39:00 -05002679 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2680 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002681 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002682 status = nfserr_seq_misordered;
2683 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002684 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002685 cstate->slot = slot;
2686 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002687 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002688 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002689 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002690 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002691 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002692 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002693 }
2694 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002695 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002696
J. Bruce Fields57266a62013-04-13 14:27:29 -04002697 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002698 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002699 if (status)
2700 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002701
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002702 buflen = (seq->cachethis) ?
2703 session->se_fchannel.maxresp_cached :
2704 session->se_fchannel.maxresp_sz;
2705 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2706 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002707 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002708 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002709 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002710
2711 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002712 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002713 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002714 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002715 if (seq->cachethis)
2716 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002717 else
2718 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002719
2720 cstate->slot = slot;
2721 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002722 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002723
Benny Halevyb85d4c02009-04-03 08:28:08 +03002724out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002725 switch (clp->cl_cb_state) {
2726 case NFSD4_CB_DOWN:
2727 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2728 break;
2729 case NFSD4_CB_FAULT:
2730 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2731 break;
2732 default:
2733 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002734 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002735 if (!list_empty(&clp->cl_revoked))
2736 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002737out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002738 if (conn)
2739 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002740 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002741 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002742out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002743 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002744 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002745}
2746
Trond Myklebustb6076642014-06-30 11:48:35 -04002747void
2748nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2749{
2750 struct nfsd4_compound_state *cs = &resp->cstate;
2751
2752 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002753 if (cs->status != nfserr_replay_cache) {
2754 nfsd4_store_cache_entry(resp);
2755 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2756 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002757 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002758 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002759 } else if (cs->clp)
2760 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002761}
2762
Mi Jinlong345c2842011-10-20 17:51:39 +08002763__be32
2764nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2765{
2766 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002767 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002768 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002769
2770 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002771 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002772 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002773 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002774
2775 if (conf) {
2776 clp = conf;
2777
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002778 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002779 status = nfserr_clientid_busy;
2780 goto out;
2781 }
2782 } else if (unconf)
2783 clp = unconf;
2784 else {
2785 status = nfserr_stale_clientid;
2786 goto out;
2787 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002788 if (!mach_creds_match(clp, rqstp)) {
2789 status = nfserr_wrong_cred;
2790 goto out;
2791 }
Mi Jinlong345c2842011-10-20 17:51:39 +08002792 expire_client(clp);
2793out:
2794 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002795 return status;
2796}
2797
Andy Adamson069b6ad2009-04-03 08:27:58 +03002798__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002799nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2800{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002801 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002802
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002803 if (rc->rca_one_fs) {
2804 if (!cstate->current_fh.fh_dentry)
2805 return nfserr_nofilehandle;
2806 /*
2807 * We don't take advantage of the rca_one_fs case.
2808 * That's OK, it's optional, we can safely ignore it.
2809 */
2810 return nfs_ok;
2811 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002812
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002813 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002814 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002815 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2816 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002817 goto out;
2818
2819 status = nfserr_stale_clientid;
2820 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002821 /*
2822 * The following error isn't really legal.
2823 * But we only get here if the client just explicitly
2824 * destroyed the client. Surely it no longer cares what
2825 * error it gets back on an operation for the dead
2826 * client.
2827 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002828 goto out;
2829
2830 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002831 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002832out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002833 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002834 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002835}
2836
2837__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002838nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2839 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002841 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002843 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002844 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002845 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2846
J. Bruce Fields63db4632012-05-18 21:54:19 -04002847 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002849 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002850 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002851 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002853 if (clp_used_exchangeid(conf))
2854 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002855 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002856 char addr_str[INET6_ADDRSTRLEN];
2857 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2858 sizeof(addr_str));
2859 dprintk("NFSD: setclientid: string in use by client "
2860 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 goto out;
2862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002864 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002865 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002868 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002869 if (new == NULL)
2870 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002871 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002872 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002874 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002875 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002876 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002877 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002878 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2880 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2881 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2882 status = nfs_ok;
2883out:
2884 nfs4_unlock_state();
2885 return status;
2886}
2887
2888
Al Virob37ad282006-10-19 23:28:59 -07002889__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002890nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2891 struct nfsd4_compound_state *cstate,
2892 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893{
NeilBrown21ab45a2005-06-23 22:04:14 -07002894 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2896 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002897 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002898 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002900 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002903
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002904 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002905 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002906 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002907 * We try hard to give out unique clientid's, so if we get an
2908 * attempt to confirm the same clientid with a different cred,
2909 * there's a bug somewhere. Let's charitably assume it's our
2910 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002911 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002912 status = nfserr_serverfault;
2913 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2914 goto out;
2915 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2916 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002917 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002918 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2919 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002921 else /* case 4: client hasn't noticed we rebooted yet? */
2922 status = nfserr_stale_clientid;
2923 goto out;
2924 }
2925 status = nfs_ok;
2926 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002927 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2928 nfsd4_probe_callback(conf);
2929 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002930 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002931 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002932 if (conf) {
2933 status = mark_client_expired(conf);
2934 if (status)
2935 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002936 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002937 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002938 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002939 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 nfs4_unlock_state();
2943 return status;
2944}
2945
J. Bruce Fields32513b42011-10-13 16:00:16 -04002946static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002948 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2949}
2950
2951/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04002952static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04002953{
Trond Myklebustca943212014-07-23 16:17:39 -04002954 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
Trond Myklebust950e0112014-06-30 11:48:31 -04002956 lockdep_assert_held(&state_lock);
2957
J. Bruce Fields32513b42011-10-13 16:00:16 -04002958 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04002959 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002960 INIT_LIST_HEAD(&fp->fi_stateids);
2961 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04002962 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002963 fp->fi_had_conflict = false;
2964 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04002965 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002966 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2967 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04002968 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969}
2970
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002971void
NeilBrowne60d4392005-06-23 22:03:01 -07002972nfsd4_free_slabs(void)
2973{
Christoph Hellwigabf11352014-05-21 07:43:03 -07002974 kmem_cache_destroy(openowner_slab);
2975 kmem_cache_destroy(lockowner_slab);
2976 kmem_cache_destroy(file_slab);
2977 kmem_cache_destroy(stateid_slab);
2978 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002979}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Bryan Schumaker72083392011-11-01 15:24:59 -04002981int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982nfsd4_init_slabs(void)
2983{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002984 openowner_slab = kmem_cache_create("nfsd4_openowners",
2985 sizeof(struct nfs4_openowner), 0, 0, NULL);
2986 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002987 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002988 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002989 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002990 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002991 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07002992 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002993 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002994 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002995 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07002996 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002997 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002998 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002999 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003000 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003001 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003002 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003003 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003005
3006out_free_stateid_slab:
3007 kmem_cache_destroy(stateid_slab);
3008out_free_file_slab:
3009 kmem_cache_destroy(file_slab);
3010out_free_lockowner_slab:
3011 kmem_cache_destroy(lockowner_slab);
3012out_free_openowner_slab:
3013 kmem_cache_destroy(openowner_slab);
3014out:
NeilBrowne60d4392005-06-23 22:03:01 -07003015 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3016 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017}
3018
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003019static void init_nfs4_replay(struct nfs4_replay *rp)
3020{
3021 rp->rp_status = nfserr_serverfault;
3022 rp->rp_buflen = 0;
3023 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003024 mutex_init(&rp->rp_mutex);
3025}
3026
3027static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3028 struct nfs4_stateowner *so)
3029{
3030 if (!nfsd4_has_session(cstate)) {
3031 mutex_lock(&so->so_replay.rp_mutex);
3032 cstate->replay_owner = so;
3033 atomic_inc(&so->so_count);
3034 }
3035}
3036
3037void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3038{
3039 struct nfs4_stateowner *so = cstate->replay_owner;
3040
3041 if (so != NULL) {
3042 cstate->replay_owner = NULL;
3043 mutex_unlock(&so->so_replay.rp_mutex);
3044 nfs4_put_stateowner(so);
3045 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003046}
3047
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003048static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
3050 struct nfs4_stateowner *sop;
3051
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003052 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003053 if (!sop)
3054 return NULL;
3055
3056 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3057 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003058 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003059 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003061 sop->so_owner.len = owner->len;
3062
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003063 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003064 sop->so_client = clp;
3065 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003066 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003067 return sop;
3068}
3069
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003070static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003071{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003072 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003073
Trond Myklebustd4f04892014-07-29 21:34:36 -04003074 list_add(&oo->oo_owner.so_strhash,
3075 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003076 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077}
3078
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003079static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3080{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003081 struct nfs4_client *clp = so->so_client;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003082
Trond Myklebustd4f04892014-07-29 21:34:36 -04003083 spin_lock(&clp->cl_lock);
3084 unhash_openowner_locked(openowner(so));
3085 spin_unlock(&clp->cl_lock);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003086}
3087
Jeff Layton6b180f02014-07-29 21:34:26 -04003088static void nfs4_free_openowner(struct nfs4_stateowner *so)
3089{
3090 struct nfs4_openowner *oo = openowner(so);
3091
3092 kmem_cache_free(openowner_slab, oo);
3093}
3094
3095static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003096 .so_unhash = nfs4_unhash_openowner,
3097 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003098};
3099
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003100static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003101alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003102 struct nfsd4_compound_state *cstate)
3103{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003104 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003105 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003107 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3108 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003110 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003111 oo->oo_owner.so_is_open_owner = 1;
3112 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003113 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003114 if (nfsd4_has_session(cstate))
3115 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003116 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003117 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003118 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003119 spin_lock(&clp->cl_lock);
3120 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003121 if (ret == NULL) {
3122 hash_openowner(oo, clp, strhashval);
3123 ret = oo;
3124 } else
3125 nfs4_free_openowner(&oo->oo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003126 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003127 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128}
3129
J. Bruce Fields996e0932011-10-17 11:14:48 -04003130static 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 -04003131 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003133 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003134 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003135 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003136 stp->st_stateowner = &oo->oo_owner;
Jeff Laytond3134b12014-07-29 21:34:32 -04003137 atomic_inc(&stp->st_stateowner->so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07003138 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003139 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 stp->st_access_bmap = 0;
3141 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003142 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003143 spin_lock(&oo->oo_owner.so_client->cl_lock);
3144 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003145 spin_lock(&fp->fi_lock);
3146 list_add(&stp->st_perfile, &fp->fi_stateids);
3147 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003148 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149}
3150
Jeff Laytond3134b12014-07-29 21:34:32 -04003151/*
3152 * In the 4.0 case we need to keep the owners around a little while to handle
3153 * CLOSE replay. We still do need to release any file access that is held by
3154 * them before returning however.
3155 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003157move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158{
Jeff Laytond3134b12014-07-29 21:34:32 -04003159 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3160 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3161 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003162
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003163 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
Jeff Laytonb401be222014-07-29 21:34:33 -04003165 /*
3166 * We know that we hold one reference via nfsd4_close, and another
3167 * "persistent" reference for the client. If the refcount is higher
3168 * than 2, then there are still calls in progress that are using this
3169 * stateid. We can't put the sc_file reference until they are finished.
3170 * Wait for the refcount to drop to 2. Since it has been unhashed,
3171 * there should be no danger of the refcount going back up again at
3172 * this point.
3173 */
3174 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3175
Jeff Laytond3134b12014-07-29 21:34:32 -04003176 release_all_access(s);
3177 if (s->st_stid.sc_file) {
3178 put_nfs4_file(s->st_stid.sc_file);
3179 s->st_stid.sc_file = NULL;
3180 }
3181 release_last_closed_stateid(oo);
3182 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003183 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003184 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185}
3186
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187/* search file_hashtbl[] for file */
3188static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003189find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
Trond Myklebustca943212014-07-23 16:17:39 -04003191 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 struct nfs4_file *fp;
3193
Trond Myklebust950e0112014-06-30 11:48:31 -04003194 lockdep_assert_held(&state_lock);
3195
Jeff Layton89876f82013-04-02 09:01:59 -04003196 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003197 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003198 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 }
3202 return NULL;
3203}
3204
Trond Myklebust950e0112014-06-30 11:48:31 -04003205static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003206find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003207{
3208 struct nfs4_file *fp;
3209
3210 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003211 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003212 spin_unlock(&state_lock);
3213 return fp;
3214}
3215
3216static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003217find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003218{
3219 struct nfs4_file *fp;
3220
3221 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003222 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003223 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003224 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003225 fp = new;
3226 }
3227 spin_unlock(&state_lock);
3228
3229 return fp;
3230}
3231
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003232/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 * Called to check deny when READ with all zero stateid or
3234 * WRITE with all zero or all one stateid
3235 */
Al Virob37ad282006-10-19 23:28:59 -07003236static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3238{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003240 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
Trond Myklebustca943212014-07-23 16:17:39 -04003242 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003243 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003244 return ret;
3245 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003246 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003247 if (fp->fi_share_deny & deny_type)
3248 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003249 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003250 put_nfs4_file(fp);
3251 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252}
3253
Jeff Layton02e12152014-07-16 10:31:57 -04003254void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255{
Trond Myklebust11b91642014-07-29 21:34:08 -04003256 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3257 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003258
Trond Myklebust11b91642014-07-29 21:34:08 -04003259 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003260
Jeff Layton02e12152014-07-16 10:31:57 -04003261 /*
3262 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003263 * already holding inode->i_lock.
3264 *
Jeff Laytondff13992014-07-08 14:02:49 -04003265 * If the dl_time != 0, then we know that it has already been
3266 * queued for a lease break. Don't queue it again.
3267 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003268 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003269 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003270 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003271 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003272 }
Jeff Layton02e12152014-07-16 10:31:57 -04003273 spin_unlock(&state_lock);
3274}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
Jeff Layton02e12152014-07-16 10:31:57 -04003276static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3277{
3278 /*
3279 * We're assuming the state code never drops its reference
3280 * without first removing the lease. Since we're in this lease
3281 * callback (and since the lease code is serialized by the kernel
3282 * lock) we know the server hasn't removed the lease yet, we know
3283 * it's safe to take a reference.
3284 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003285 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003286 nfsd4_cb_recall(dp);
3287}
3288
Jeff Layton1c8c6012013-06-21 08:58:15 -04003289/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003290static void nfsd_break_deleg_cb(struct file_lock *fl)
3291{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003292 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3293 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003294
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003295 if (!fp) {
3296 WARN(1, "(%p)->fl_owner NULL\n", fl);
3297 return;
3298 }
3299 if (fp->fi_had_conflict) {
3300 WARN(1, "duplicate break on %p\n", fp);
3301 return;
3302 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003303 /*
3304 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003305 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003306 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003307 */
3308 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Jeff Layton02e12152014-07-16 10:31:57 -04003310 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003311 fp->fi_had_conflict = true;
3312 /*
3313 * If there are no delegations on the list, then we can't count on this
3314 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3315 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3316 * true should keep any new delegations from being hashed.
3317 */
3318 if (list_empty(&fp->fi_delegations))
3319 fl->fl_break_time = jiffies;
3320 else
3321 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3322 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003323 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324}
3325
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326static
3327int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3328{
3329 if (arg & F_UNLCK)
3330 return lease_modify(onlist, arg);
3331 else
3332 return -EAGAIN;
3333}
3334
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003335static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003336 .lm_break = nfsd_break_deleg_cb,
3337 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338};
3339
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003340static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3341{
3342 if (nfsd4_has_session(cstate))
3343 return nfs_ok;
3344 if (seqid == so->so_seqid - 1)
3345 return nfserr_replay_me;
3346 if (seqid == so->so_seqid)
3347 return nfs_ok;
3348 return nfserr_bad_seqid;
3349}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350
Jeff Layton4b24ca72014-06-30 11:48:44 -04003351static __be32 lookup_clientid(clientid_t *clid,
3352 struct nfsd4_compound_state *cstate,
3353 struct nfsd_net *nn)
3354{
3355 struct nfs4_client *found;
3356
3357 if (cstate->clp) {
3358 found = cstate->clp;
3359 if (!same_clid(&found->cl_clientid, clid))
3360 return nfserr_stale_clientid;
3361 return nfs_ok;
3362 }
3363
3364 if (STALE_CLIENTID(clid, nn))
3365 return nfserr_stale_clientid;
3366
3367 /*
3368 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3369 * cached already then we know this is for is for v4.0 and "sessions"
3370 * will be false.
3371 */
3372 WARN_ON_ONCE(cstate->session);
3373 found = find_confirmed_client(clid, false, nn);
3374 if (!found)
3375 return nfserr_expired;
3376
3377 /* Cache the nfs4_client in cstate! */
3378 cstate->clp = found;
3379 atomic_inc(&found->cl_refcount);
3380 return nfs_ok;
3381}
3382
Al Virob37ad282006-10-19 23:28:59 -07003383__be32
Andy Adamson66689582009-04-03 08:28:45 +03003384nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003385 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 clientid_t *clientid = &open->op_clientid;
3388 struct nfs4_client *clp = NULL;
3389 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003390 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003391 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003393 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003395 /*
3396 * In case we need it later, after we've already created the
3397 * file and don't want to risk a further failure:
3398 */
3399 open->op_file = nfsd4_alloc_file();
3400 if (open->op_file == NULL)
3401 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402
Trond Myklebust2d91e892014-06-30 11:48:46 -04003403 status = lookup_clientid(clientid, cstate, nn);
3404 if (status)
3405 return status;
3406 clp = cstate->clp;
3407
Trond Myklebustd4f04892014-07-29 21:34:36 -04003408 strhashval = ownerstr_hashval(&open->op_owner);
3409 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003410 open->op_openowner = oo;
3411 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003412 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003414 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003415 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003416 release_openowner(oo);
3417 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003418 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003419 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003420 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3421 if (status)
3422 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003423 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003424new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003425 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003426 if (oo == NULL)
3427 return nfserr_jukebox;
3428 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003429alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003430 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003431 if (!open->op_stp)
3432 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003433 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434}
3435
Al Virob37ad282006-10-19 23:28:59 -07003436static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003437nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3438{
3439 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3440 return nfserr_openmode;
3441 else
3442 return nfs_ok;
3443}
3444
Daniel Mackc47d8322011-05-16 16:38:14 +02003445static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003446{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003447 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3448}
3449
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003450static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003451{
3452 struct nfs4_stid *ret;
3453
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003454 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003455 if (!ret)
3456 return NULL;
3457 return delegstateid(ret);
3458}
3459
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003460static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3461{
3462 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3463 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3464}
3465
Al Virob37ad282006-10-19 23:28:59 -07003466static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003467nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003468 struct nfs4_delegation **dp)
3469{
3470 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003471 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003472 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003473
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003474 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3475 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003476 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003477 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003478 status = nfs4_check_delegmode(deleg, flags);
3479 if (status) {
3480 nfs4_put_stid(&deleg->dl_stid);
3481 goto out;
3482 }
3483 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003484out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003485 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003486 return nfs_ok;
3487 if (status)
3488 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003489 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003490 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003491}
3492
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003493static struct nfs4_ol_stateid *
3494nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003496 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003497 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498
Trond Myklebust1d31a252014-07-10 14:07:25 -04003499 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003500 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 /* ignore lock owners */
3502 if (local->st_stateowner->so_is_open_owner == 0)
3503 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003504 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003505 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003506 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003507 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003510 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003511 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512}
3513
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003514static inline int nfs4_access_to_access(u32 nfs4_access)
3515{
3516 int flags = 0;
3517
3518 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3519 flags |= NFSD_MAY_READ;
3520 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3521 flags |= NFSD_MAY_WRITE;
3522 return flags;
3523}
3524
Al Virob37ad282006-10-19 23:28:59 -07003525static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3527 struct nfsd4_open *open)
3528{
3529 struct iattr iattr = {
3530 .ia_valid = ATTR_SIZE,
3531 .ia_size = 0,
3532 };
3533 if (!open->op_truncate)
3534 return 0;
3535 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003536 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3538}
3539
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003540static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003541 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3542 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003543{
Trond Myklebustde186432014-07-10 14:07:26 -04003544 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003545 __be32 status;
3546 int oflag = nfs4_access_to_omode(open->op_share_access);
3547 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003548 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003549
Trond Myklebustde186432014-07-10 14:07:26 -04003550 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003551
3552 /*
3553 * Are we trying to set a deny mode that would conflict with
3554 * current access?
3555 */
3556 status = nfs4_file_check_deny(fp, open->op_share_deny);
3557 if (status != nfs_ok) {
3558 spin_unlock(&fp->fi_lock);
3559 goto out;
3560 }
3561
3562 /* set access to the file */
3563 status = nfs4_file_get_access(fp, open->op_share_access);
3564 if (status != nfs_ok) {
3565 spin_unlock(&fp->fi_lock);
3566 goto out;
3567 }
3568
3569 /* Set access bits in stateid */
3570 old_access_bmap = stp->st_access_bmap;
3571 set_access(open->op_share_access, stp);
3572
3573 /* Set new deny mask */
3574 old_deny_bmap = stp->st_deny_bmap;
3575 set_deny(open->op_share_deny, stp);
3576 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3577
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003578 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003579 spin_unlock(&fp->fi_lock);
3580 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003581 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003582 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003583 spin_lock(&fp->fi_lock);
3584 if (!fp->fi_fds[oflag]) {
3585 fp->fi_fds[oflag] = filp;
3586 filp = NULL;
3587 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003588 }
Trond Myklebustde186432014-07-10 14:07:26 -04003589 spin_unlock(&fp->fi_lock);
3590 if (filp)
3591 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003592
3593 status = nfsd4_truncate(rqstp, cur_fh, open);
3594 if (status)
3595 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003596out:
3597 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003598out_put_access:
3599 stp->st_access_bmap = old_access_bmap;
3600 nfs4_file_put_access(fp, open->op_share_access);
3601 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3602 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003603}
3604
Al Virob37ad282006-10-19 23:28:59 -07003605static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003606nfs4_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 -07003607{
Al Virob37ad282006-10-19 23:28:59 -07003608 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003609 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003611 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003612 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003613
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003614 /* test and set deny mode */
3615 spin_lock(&fp->fi_lock);
3616 status = nfs4_file_check_deny(fp, open->op_share_deny);
3617 if (status == nfs_ok) {
3618 old_deny_bmap = stp->st_deny_bmap;
3619 set_deny(open->op_share_deny, stp);
3620 fp->fi_share_deny |=
3621 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3622 }
3623 spin_unlock(&fp->fi_lock);
3624
3625 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003628 status = nfsd4_truncate(rqstp, cur_fh, open);
3629 if (status != nfs_ok)
3630 reset_union_bmap_deny(old_deny_bmap, stp);
3631 return status;
3632}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003635nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003637 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638}
3639
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003640/* Should we give out recallable state?: */
3641static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3642{
3643 if (clp->cl_cb_state == NFSD4_CB_UP)
3644 return true;
3645 /*
3646 * In the sessions case, since we don't have to establish a
3647 * separate connection for callbacks, we assume it's OK
3648 * until we hear otherwise:
3649 */
3650 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3651}
3652
Jeff Laytond564fbe2014-07-16 10:31:58 -04003653static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003654{
3655 struct file_lock *fl;
3656
3657 fl = locks_alloc_lock();
3658 if (!fl)
3659 return NULL;
3660 locks_init_lock(fl);
3661 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003662 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003663 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3664 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003665 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003666 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003667 return fl;
3668}
3669
J. Bruce Fields99c41512013-05-21 16:21:25 -04003670static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003671{
Trond Myklebust11b91642014-07-29 21:34:08 -04003672 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003673 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003674 struct file *filp;
3675 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003676
Jeff Laytond564fbe2014-07-16 10:31:58 -04003677 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003678 if (!fl)
3679 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003680 filp = find_readable_file(fp);
3681 if (!filp) {
3682 /* We should always have a readable file here */
3683 WARN_ON_ONCE(1);
3684 return -EBADF;
3685 }
3686 fl->fl_file = filp;
3687 status = vfs_setlease(filp, fl->fl_type, &fl);
3688 if (status) {
3689 locks_free_lock(fl);
3690 goto out_fput;
3691 }
Benny Halevycdc97502014-05-30 09:09:30 -04003692 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003693 spin_lock(&fp->fi_lock);
3694 /* Did the lease get broken before we took the lock? */
3695 status = -EAGAIN;
3696 if (fp->fi_had_conflict)
3697 goto out_unlock;
3698 /* Race breaker */
3699 if (fp->fi_lease) {
3700 status = 0;
3701 atomic_inc(&fp->fi_delegees);
3702 hash_delegation_locked(dp, fp);
3703 goto out_unlock;
3704 }
3705 fp->fi_lease = fl;
3706 fp->fi_deleg_file = filp;
3707 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003708 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003709 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003710 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003711 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003712out_unlock:
3713 spin_unlock(&fp->fi_lock);
3714 spin_unlock(&state_lock);
3715out_fput:
3716 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003717 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003718}
3719
Jeff Layton0b266932014-07-25 07:34:25 -04003720static struct nfs4_delegation *
3721nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3722 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003723{
Jeff Layton0b266932014-07-25 07:34:25 -04003724 int status;
3725 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003726
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003727 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003728 return ERR_PTR(-EAGAIN);
3729
3730 dp = alloc_init_deleg(clp, fh);
3731 if (!dp)
3732 return ERR_PTR(-ENOMEM);
3733
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003734 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003735 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003736 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003737 dp->dl_stid.sc_file = fp;
Jeff Layton417c6622014-07-21 09:34:57 -04003738 if (!fp->fi_lease) {
3739 spin_unlock(&fp->fi_lock);
3740 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003741 status = nfs4_setlease(dp);
3742 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003743 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003744 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003745 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003746 status = -EAGAIN;
3747 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003748 }
Benny Halevy931ee562014-05-30 09:09:27 -04003749 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003750 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003751out_unlock:
3752 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003753 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003754out:
3755 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003756 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003757 return ERR_PTR(status);
3758 }
3759 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003760}
3761
Benny Halevy4aa89132012-02-21 14:16:44 -08003762static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3763{
3764 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3765 if (status == -EAGAIN)
3766 open->op_why_no_deleg = WND4_CONTENTION;
3767 else {
3768 open->op_why_no_deleg = WND4_RESOURCE;
3769 switch (open->op_deleg_want) {
3770 case NFS4_SHARE_WANT_READ_DELEG:
3771 case NFS4_SHARE_WANT_WRITE_DELEG:
3772 case NFS4_SHARE_WANT_ANY_DELEG:
3773 break;
3774 case NFS4_SHARE_WANT_CANCEL:
3775 open->op_why_no_deleg = WND4_CANCELLED;
3776 break;
3777 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003778 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003779 }
3780 }
3781}
3782
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783/*
3784 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003785 *
3786 * Note we don't support write delegations, and won't until the vfs has
3787 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 */
3789static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003790nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3791 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792{
3793 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003794 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3795 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003796 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003797 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003799 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003800 open->op_recall = 0;
3801 switch (open->op_claim_type) {
3802 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003803 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003804 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003805 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3806 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003807 break;
3808 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003809 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003810 /*
3811 * Let's not give out any delegations till everyone's
3812 * had the chance to reclaim theirs....
3813 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003814 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003815 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003816 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003817 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003818 /*
3819 * Also, if the file was opened for write or
3820 * create, there's a good chance the client's
3821 * about to write to it, resulting in an
3822 * immediate recall (since we don't support
3823 * write delegations):
3824 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003825 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003826 goto out_no_deleg;
3827 if (open->op_create == NFS4_OPEN_CREATE)
3828 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003829 break;
3830 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003831 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003832 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003833 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003834 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003835 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003837 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003839 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003840 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003841 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003842 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003843 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003844out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003845 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3846 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003847 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003848 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003849 open->op_recall = 1;
3850 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003851
3852 /* 4.1 client asking for a delegation? */
3853 if (open->op_deleg_want)
3854 nfsd4_open_deleg_none_ext(open, status);
3855 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856}
3857
Benny Halevye27f49c2012-02-21 14:16:54 -08003858static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3859 struct nfs4_delegation *dp)
3860{
3861 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3862 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3863 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3864 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3865 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3866 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3867 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3868 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3869 }
3870 /* Otherwise the client must be confused wanting a delegation
3871 * it already has, therefore we don't return
3872 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3873 */
3874}
3875
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876/*
3877 * called with nfs4_lock_state() held.
3878 */
Al Virob37ad282006-10-19 23:28:59 -07003879__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3881{
Andy Adamson66689582009-04-03 08:28:45 +03003882 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003883 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003885 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003886 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003887 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 /*
3890 * Lookup file; if found, lookup stateid and check open request,
3891 * and check for delegations in the process of being recalled.
3892 * If not found, create the nfs4_file struct
3893 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003894 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04003895 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04003896 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003897 if (status)
3898 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003899 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04003901 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003902 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003903 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003904 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003905 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 }
3907
3908 /*
3909 * OPEN the file, or upgrade an existing OPEN.
3910 * If truncate fails, the OPEN fails.
3911 */
3912 if (stp) {
3913 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003914 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 if (status)
3916 goto out;
3917 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003918 stp = open->op_stp;
3919 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003920 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003921 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3922 if (status) {
3923 release_open_stateid(stp);
3924 goto out;
3925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003927 update_stateid(&stp->st_stid.sc_stateid);
3928 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929
Benny Halevyd24433c2012-02-16 20:57:17 +02003930 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02003931 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3932 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3933 open->op_why_no_deleg = WND4_NOT_WANTED;
3934 goto nodeleg;
3935 }
3936 }
3937
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 /*
3939 * Attempt to hand out a delegation. No error return, because the
3940 * OPEN succeeds even if we fail.
3941 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003942 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003943nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 status = nfs_ok;
3945
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003946 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003947 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003949 /* 4.1 client trying to upgrade/downgrade delegation? */
3950 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003951 open->op_deleg_want)
3952 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003953
NeilBrown13cd2182005-06-23 22:03:10 -07003954 if (fp)
3955 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003956 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003957 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 /*
3959 * To finish the open response, we just need to set the rflags.
3960 */
3961 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003962 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003963 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003965 if (dp)
3966 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003967 if (stp)
3968 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969
3970 return status;
3971}
3972
Jeff Layton58fb12e2014-07-29 21:34:27 -04003973void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
3974 struct nfsd4_open *open, __be32 status)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003975{
3976 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04003977 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003978
Jeff Laytond3134b12014-07-29 21:34:32 -04003979 nfsd4_cstate_assign_replay(cstate, so);
3980 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003981 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003982 if (open->op_file)
3983 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003984 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04003985 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003986}
3987
Al Virob37ad282006-10-19 23:28:59 -07003988__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003989nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3990 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991{
3992 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003993 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003994 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
3996 nfs4_lock_state();
3997 dprintk("process_renew(%08x/%08x): starting\n",
3998 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003999 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004000 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004002 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004004 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004005 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 goto out;
4007 status = nfs_ok;
4008out:
4009 nfs4_unlock_state();
4010 return status;
4011}
4012
NeilBrowna76b4312005-06-23 22:04:01 -07004013static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004014nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004015{
Jeff Layton33dcc482012-04-10 11:08:48 -04004016 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004017 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004018 return;
4019
NeilBrowna76b4312005-06-23 22:04:01 -07004020 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004021 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004022 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004023 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004024 /*
4025 * Now that every NFSv4 client has had the chance to recover and
4026 * to see the (possibly new, possibly shorter) lease time, we
4027 * can safely set the next grace time to the current lease time:
4028 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03004029 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07004030}
4031
NeilBrownfd39ca92005-06-23 22:04:03 -07004032static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004033nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034{
4035 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004036 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 struct nfs4_delegation *dp;
4038 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004039 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004040 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
4042 nfs4_lock_state();
4043
4044 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004045 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004046 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004047 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004048 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 clp = list_entry(pos, struct nfs4_client, cl_lru);
4050 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4051 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004052 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 break;
4054 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004055 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004056 dprintk("NFSD: client in use (clientid %08x)\n",
4057 clp->cl_clientid.cl_id);
4058 continue;
4059 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004060 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004061 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004062 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004063 list_for_each_safe(pos, next, &reaplist) {
4064 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 dprintk("NFSD: purging unused client (clientid %08x)\n",
4066 clp->cl_clientid.cl_id);
4067 expire_client(clp);
4068 }
Benny Halevycdc97502014-05-30 09:09:30 -04004069 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004070 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004072 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4073 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004075 t = dp->dl_time - cutoff;
4076 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 break;
4078 }
Jeff Layton42690672014-07-25 07:34:20 -04004079 unhash_delegation_locked(dp);
4080 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 }
Benny Halevycdc97502014-05-30 09:09:30 -04004082 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004083 while (!list_empty(&reaplist)) {
4084 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4085 dl_recall_lru);
4086 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004087 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004089 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004090 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
4091 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004092 t = oo->oo_time - cutoff;
4093 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 break;
4095 }
Jeff Laytond3134b12014-07-29 21:34:32 -04004096 release_last_closed_stateid(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04004098 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04004100 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101}
4102
Harvey Harrisona254b242008-02-20 12:49:00 -08004103static struct workqueue_struct *laundry_wq;
4104static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004105
4106static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004107laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108{
4109 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004110 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4111 work);
4112 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4113 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004115 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004117 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118}
4119
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004120static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004121{
Trond Myklebust11b91642014-07-29 21:34:08 -04004122 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004123 return nfserr_bad_stateid;
4124 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125}
4126
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004128access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004130 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4131 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4132 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133}
4134
4135static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004136access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004138 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4139 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140}
4141
4142static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004143__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144{
Al Virob37ad282006-10-19 23:28:59 -07004145 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146
J. Bruce Fields02921912010-07-29 15:16:59 -04004147 /* For lock stateid's, we test the parent open, not the lock: */
4148 if (stp->st_openstp)
4149 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004150 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004152 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 goto out;
4154 status = nfs_ok;
4155out:
4156 return status;
4157}
4158
Al Virob37ad282006-10-19 23:28:59 -07004159static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004160check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004162 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004164 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004165 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 * conflicting state; so we must wait out the grace period. */
4167 return nfserr_grace;
4168 } else if (flags & WR_STATE)
4169 return nfs4_share_conflict(current_fh,
4170 NFS4_SHARE_DENY_WRITE);
4171 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4172 return nfs4_share_conflict(current_fh,
4173 NFS4_SHARE_DENY_READ);
4174}
4175
4176/*
4177 * Allow READ/WRITE during grace period on recovered state only for files
4178 * that are not able to provide mandatory locking.
4179 */
4180static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004181grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004183 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184}
4185
J. Bruce Fields81b82962011-08-23 11:03:29 -04004186/* Returns true iff a is later than b: */
4187static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4188{
Jim Rees1a9357f2013-05-17 17:33:00 -04004189 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004190}
4191
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004192static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004193{
Andy Adamson66689582009-04-03 08:28:45 +03004194 /*
4195 * When sessions are used the stateid generation number is ignored
4196 * when it is zero.
4197 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004198 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004199 return nfs_ok;
4200
4201 if (in->si_generation == ref->si_generation)
4202 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004203
J. Bruce Fields0836f582008-01-26 19:08:12 -05004204 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004205 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004206 return nfserr_bad_stateid;
4207 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004208 * However, we could see a stateid from the past, even from a
4209 * non-buggy client. For example, if the client sends a lock
4210 * while some IO is outstanding, the lock may bump si_generation
4211 * while the IO is still in flight. The client could avoid that
4212 * situation by waiting for responses on all the IO requests,
4213 * but better performance may result in retrying IO that
4214 * receives an old_stateid error if requests are rarely
4215 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004216 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004217 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004218}
4219
Chuck Lever7df302f2012-05-29 13:56:37 -04004220static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004221{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004222 struct nfs4_stid *s;
4223 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004224 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004225
Chuck Lever7df302f2012-05-29 13:56:37 -04004226 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004227 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004228 /* Client debugging aid. */
4229 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4230 char addr_str[INET6_ADDRSTRLEN];
4231 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4232 sizeof(addr_str));
4233 pr_warn_ratelimited("NFSD: client %s testing state ID "
4234 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004235 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004236 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004237 spin_lock(&cl->cl_lock);
4238 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004239 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004240 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004241 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004242 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004243 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004244 switch (s->sc_type) {
4245 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004246 status = nfs_ok;
4247 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004248 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004249 status = nfserr_deleg_revoked;
4250 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004251 case NFS4_OPEN_STID:
4252 case NFS4_LOCK_STID:
4253 ols = openlockstateid(s);
4254 if (ols->st_stateowner->so_is_open_owner
4255 && !(openowner(ols->st_stateowner)->oo_flags
4256 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004257 status = nfserr_bad_stateid;
4258 else
4259 status = nfs_ok;
4260 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004261 default:
4262 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004263 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004264 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004265 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004266 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004267 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004268out_unlock:
4269 spin_unlock(&cl->cl_lock);
4270 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004271}
4272
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004273static __be32
4274nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4275 stateid_t *stateid, unsigned char typemask,
4276 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004277{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004278 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004279
4280 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4281 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004282 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004283 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004284 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004285 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004286 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004287 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004288 if (status)
4289 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004290 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004291 if (!*s)
4292 return nfserr_bad_stateid;
4293 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004294}
4295
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296/*
4297* Checks for stateid operations
4298*/
Al Virob37ad282006-10-19 23:28:59 -07004299__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004300nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004301 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004303 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004304 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004306 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004308 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004309 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004310 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 if (filpp)
4313 *filpp = NULL;
4314
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004315 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 return nfserr_grace;
4317
4318 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004319 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320
Trond Myklebust14bcab12014-04-18 14:44:07 -04004321 nfs4_lock_state();
4322
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004323 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004324 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004325 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004326 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004327 goto unlock_state;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004328 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4329 if (status)
4330 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004331 switch (s->sc_type) {
4332 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004333 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004334 status = nfs4_check_delegmode(dp, flags);
4335 if (status)
4336 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004337 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004338 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004339 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004340 WARN_ON_ONCE(1);
4341 status = nfserr_serverfault;
4342 goto out;
4343 }
Trond Myklebustde186432014-07-10 14:07:26 -04004344 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004345 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004346 break;
4347 case NFS4_OPEN_STID:
4348 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004349 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004350 status = nfs4_check_fh(current_fh, stp);
4351 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004353 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004354 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004356 status = nfs4_check_openmode(stp, flags);
4357 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004359 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004360 struct nfs4_file *fp = stp->st_stid.sc_file;
4361
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004362 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004363 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004364 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004365 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004366 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004367 break;
4368 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004369 status = nfserr_bad_stateid;
4370 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 }
4372 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004373 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004374 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004376 nfs4_put_stid(s);
4377unlock_state:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004378 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 return status;
4380}
4381
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004382static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004383nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004384{
J. Bruce Fieldsa1b8ff42014-05-20 15:55:21 -04004385 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4386
Trond Myklebust11b91642014-07-29 21:34:08 -04004387 if (check_for_locks(stp->st_stid.sc_file, lo))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004388 return nfserr_locks_held;
Jeff Layton5adfd882014-07-29 21:34:31 -04004389 release_lock_stateid(stp);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004390 return nfs_ok;
4391}
4392
4393/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004394 * Test if the stateid is valid
4395 */
4396__be32
4397nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4398 struct nfsd4_test_stateid *test_stateid)
4399{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004400 struct nfsd4_test_stateid_id *stateid;
4401 struct nfs4_client *cl = cstate->session->se_client;
4402
4403 nfs4_lock_state();
4404 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004405 stateid->ts_id_status =
4406 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004407 nfs4_unlock_state();
4408
Bryan Schumaker17456802011-07-13 10:50:48 -04004409 return nfs_ok;
4410}
4411
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004412__be32
4413nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4414 struct nfsd4_free_stateid *free_stateid)
4415{
4416 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004417 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004418 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004419 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004420 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004421
4422 nfs4_lock_state();
Jeff Layton1af71cc2014-07-29 21:34:14 -04004423 spin_lock(&cl->cl_lock);
4424 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004425 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004426 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004427 switch (s->sc_type) {
4428 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004429 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004430 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004431 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004432 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4433 if (ret)
4434 break;
4435 ret = nfserr_locks_held;
4436 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004437 case NFS4_LOCK_STID:
4438 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4439 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004440 break;
4441 spin_unlock(&cl->cl_lock);
4442 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4443 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004444 case NFS4_REVOKED_DELEG_STID:
4445 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004446 list_del_init(&dp->dl_recall_lru);
4447 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004448 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004449 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004450 goto out;
4451 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004452 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004453out_unlock:
4454 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004455out:
4456 nfs4_unlock_state();
4457 return ret;
4458}
4459
NeilBrown4c4cd222005-07-07 17:59:27 -07004460static inline int
4461setlkflg (int type)
4462{
4463 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4464 RD_STATE : WR_STATE;
4465}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004467static __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 -04004468{
4469 struct svc_fh *current_fh = &cstate->current_fh;
4470 struct nfs4_stateowner *sop = stp->st_stateowner;
4471 __be32 status;
4472
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004473 status = nfsd4_check_seqid(cstate, sop, seqid);
4474 if (status)
4475 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004476 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4477 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004478 /*
4479 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004480 * nfserr_replay_me from the previous step, and
4481 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004482 */
4483 return nfserr_bad_stateid;
4484 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4485 if (status)
4486 return status;
4487 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004488}
4489
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490/*
4491 * Checks for sequence id mutating operations.
4492 */
Al Virob37ad282006-10-19 23:28:59 -07004493static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004494nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004495 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004496 struct nfs4_ol_stateid **stpp,
4497 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004499 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004500 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004501 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004503 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4504 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004505
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004507 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004508 if (status)
4509 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004510 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004511 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004513 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004514 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004515 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004516 else
4517 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004518 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004519}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004520
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004521static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4522 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004523{
4524 __be32 status;
4525 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004526 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004528 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004529 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004530 if (status)
4531 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004532 oo = openowner(stp->st_stateowner);
4533 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4534 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004535 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004536 }
4537 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004538 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539}
4540
Al Virob37ad282006-10-19 23:28:59 -07004541__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004542nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004543 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544{
Al Virob37ad282006-10-19 23:28:59 -07004545 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004546 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004547 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004548 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549
Al Viroa6a9f182013-09-16 10:57:01 -04004550 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4551 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004553 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004554 if (status)
4555 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556
4557 nfs4_lock_state();
4558
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004559 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004560 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004561 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004562 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004563 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004564 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004565 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004566 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004567 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004568 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004569 update_stateid(&stp->st_stid.sc_stateid);
4570 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004571 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004572 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004573
Jeff Layton2a4317c2012-03-21 16:42:43 -04004574 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004575 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004576put_stateid:
4577 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004579 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004580 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 return status;
4582}
4583
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004584static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004586 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004587 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004588 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004589 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004590}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004591
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004592static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4593{
4594 switch (to_access) {
4595 case NFS4_SHARE_ACCESS_READ:
4596 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4597 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4598 break;
4599 case NFS4_SHARE_ACCESS_WRITE:
4600 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4601 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4602 break;
4603 case NFS4_SHARE_ACCESS_BOTH:
4604 break;
4605 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004606 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 }
4608}
4609
Al Virob37ad282006-10-19 23:28:59 -07004610__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004611nfsd4_open_downgrade(struct svc_rqst *rqstp,
4612 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004613 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614{
Al Virob37ad282006-10-19 23:28:59 -07004615 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004616 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004617 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618
Al Viroa6a9f182013-09-16 10:57:01 -04004619 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4620 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004622 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004623 if (od->od_deleg_want)
4624 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4625 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626
4627 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004628 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004629 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004630 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004633 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004634 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004636 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004638 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004639 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004641 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004643 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644
Jeff Laytonce0fc432012-05-11 09:45:14 -04004645 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004647 update_stateid(&stp->st_stid.sc_stateid);
4648 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004650put_stateid:
4651 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004653 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004654 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 return status;
4656}
4657
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004658static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4659{
Trond Myklebustacf92952014-06-30 11:48:37 -04004660 struct nfs4_client *clp = s->st_stid.sc_client;
Trond Myklebustacf92952014-06-30 11:48:37 -04004661
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004662 s->st_stid.sc_type = NFS4_CLOSED_STID;
Trond Myklebustacf92952014-06-30 11:48:37 -04004663 unhash_open_stateid(s);
4664
Jeff Laytond3134b12014-07-29 21:34:32 -04004665 if (clp->cl_minorversion)
Trond Myklebust60116952014-07-29 21:34:06 -04004666 nfs4_put_stid(&s->st_stid);
Jeff Laytond3134b12014-07-29 21:34:32 -04004667 else
4668 move_to_close_lru(s, clp->net);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004669}
4670
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671/*
4672 * nfs4_unlock_state() called after encode
4673 */
Al Virob37ad282006-10-19 23:28:59 -07004674__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004675nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004676 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677{
Al Virob37ad282006-10-19 23:28:59 -07004678 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004679 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004680 struct net *net = SVC_NET(rqstp);
4681 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682
Al Viroa6a9f182013-09-16 10:57:01 -04004683 dprintk("NFSD: nfsd4_close on file %pd\n",
4684 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
4686 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004687 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4688 &close->cl_stateid,
4689 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004690 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004691 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004692 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004694 update_stateid(&stp->st_stid.sc_stateid);
4695 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004697 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004698
4699 /* put reference from nfs4_preprocess_seqid_op */
4700 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701out:
Jeff Layton58fb12e2014-07-29 21:34:27 -04004702 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 return status;
4704}
4705
Al Virob37ad282006-10-19 23:28:59 -07004706__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004707nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4708 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004710 struct nfs4_delegation *dp;
4711 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004712 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004713 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004714 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004716 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004717 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718
4719 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004720 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004721 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004722 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004723 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004724 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004725 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004726 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004727
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004728 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004729put_stateid:
4730 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004732 nfs4_unlock_state();
4733
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 return status;
4735}
4736
4737
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739
Benny Halevy87df4de2008-12-15 19:42:03 +02004740static inline u64
4741end_offset(u64 start, u64 len)
4742{
4743 u64 end;
4744
4745 end = start + len;
4746 return end >= start ? end: NFS4_MAX_UINT64;
4747}
4748
4749/* last octet in a range */
4750static inline u64
4751last_byte_offset(u64 start, u64 len)
4752{
4753 u64 end;
4754
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004755 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004756 end = start + len;
4757 return end > start ? end - 1: NFS4_MAX_UINT64;
4758}
4759
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760/*
4761 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4762 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4763 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4764 * locking, this prevents us from being completely protocol-compliant. The
4765 * real solution to this problem is to start using unsigned file offsets in
4766 * the VFS, but this is a very deep change!
4767 */
4768static inline void
4769nfs4_transform_lock_offset(struct file_lock *lock)
4770{
4771 if (lock->fl_start < 0)
4772 lock->fl_start = OFFSET_MAX;
4773 if (lock->fl_end < 0)
4774 lock->fl_end = OFFSET_MAX;
4775}
4776
NeilBrownd5b90262006-04-10 22:55:22 -07004777/* Hack!: For now, we're defining this just so we can use a pointer to it
4778 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004779static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004780};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781
4782static inline void
4783nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4784{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004785 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786
NeilBrownd5b90262006-04-10 22:55:22 -07004787 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004788 lo = (struct nfs4_lockowner *) fl->fl_owner;
4789 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4790 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004791 if (!deny->ld_owner.data)
4792 /* We just don't care that much */
4793 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004794 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4795 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004796 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004797nevermind:
4798 deny->ld_owner.len = 0;
4799 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004800 deny->ld_clientid.cl_boot = 0;
4801 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 }
4803 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004804 deny->ld_length = NFS4_MAX_UINT64;
4805 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4807 deny->ld_type = NFS4_READ_LT;
4808 if (fl->fl_type != F_RDLCK)
4809 deny->ld_type = NFS4_WRITE_LT;
4810}
4811
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004812static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004813find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004814 struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004816 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004817 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818
Trond Myklebustd4f04892014-07-29 21:34:36 -04004819 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4820 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004821 if (so->so_is_open_owner)
4822 continue;
Trond Myklebustd4f04892014-07-29 21:34:36 -04004823 if (!same_owner_str(so, owner))
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004824 continue;
Jeff Layton5db1c032014-07-29 21:34:28 -04004825 atomic_inc(&so->so_count);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004826 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 }
4828 return NULL;
4829}
4830
Trond Myklebustc58c6612014-07-29 21:34:35 -04004831static struct nfs4_lockowner *
4832find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004833 struct nfs4_client *clp)
Trond Myklebustc58c6612014-07-29 21:34:35 -04004834{
4835 struct nfs4_lockowner *lo;
4836
Trond Myklebustd4f04892014-07-29 21:34:36 -04004837 spin_lock(&clp->cl_lock);
4838 lo = find_lockowner_str_locked(clid, owner, clp);
4839 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004840 return lo;
4841}
4842
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004843static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4844{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004845 struct nfs4_client *clp = sop->so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04004846
Trond Myklebustd4f04892014-07-29 21:34:36 -04004847 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004848 unhash_lockowner_locked(lockowner(sop));
Trond Myklebustd4f04892014-07-29 21:34:36 -04004849 spin_unlock(&clp->cl_lock);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004850}
4851
Jeff Layton6b180f02014-07-29 21:34:26 -04004852static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4853{
4854 struct nfs4_lockowner *lo = lockowner(sop);
4855
4856 kmem_cache_free(lockowner_slab, lo);
4857}
4858
4859static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004860 .so_unhash = nfs4_unhash_lockowner,
4861 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004862};
4863
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864/*
4865 * Alloc a lock owner structure.
4866 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004867 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004869 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004871static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004872alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
4873 struct nfs4_ol_stateid *open_stp,
4874 struct nfsd4_lock *lock)
4875{
Trond Myklebustc58c6612014-07-29 21:34:35 -04004876 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004878 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4879 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004881 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4882 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04004883 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04004884 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04004885 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004886 ret = find_lockowner_str_locked(&clp->cl_clientid,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004887 &lock->lk_new_owner, clp);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004888 if (ret == NULL) {
4889 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004890 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004891 ret = lo;
4892 } else
4893 nfs4_free_lockowner(&lo->lo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04004894 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004895 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896}
4897
Jeff Layton356a95e2014-07-29 21:34:13 -04004898static void
4899init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
4900 struct nfs4_file *fp, struct inode *inode,
4901 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004903 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904
Jeff Layton356a95e2014-07-29 21:34:13 -04004905 lockdep_assert_held(&clp->cl_lock);
4906
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004907 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004908 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004909 stp->st_stateowner = &lo->lo_owner;
Trond Myklebuste4f1dd72014-07-29 21:34:30 -04004910 atomic_inc(&lo->lo_owner.so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07004911 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004912 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04004913 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004914 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004916 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004917 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04004918 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004919 spin_lock(&fp->fi_lock);
4920 list_add(&stp->st_perfile, &fp->fi_stateids);
4921 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922}
4923
Jeff Laytonc53530d2014-06-30 11:48:39 -04004924static struct nfs4_ol_stateid *
4925find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4926{
4927 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04004928 struct nfs4_client *clp = lo->lo_owner.so_client;
4929
4930 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004931
4932 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004933 if (lst->st_stid.sc_file == fp) {
4934 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004935 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004936 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004937 }
4938 return NULL;
4939}
4940
Jeff Layton356a95e2014-07-29 21:34:13 -04004941static struct nfs4_ol_stateid *
4942find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
4943 struct inode *inode, struct nfs4_ol_stateid *ost,
4944 bool *new)
4945{
4946 struct nfs4_stid *ns = NULL;
4947 struct nfs4_ol_stateid *lst;
4948 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4949 struct nfs4_client *clp = oo->oo_owner.so_client;
4950
4951 spin_lock(&clp->cl_lock);
4952 lst = find_lock_stateid(lo, fi);
4953 if (lst == NULL) {
4954 spin_unlock(&clp->cl_lock);
4955 ns = nfs4_alloc_stid(clp, stateid_slab);
4956 if (ns == NULL)
4957 return NULL;
4958
4959 spin_lock(&clp->cl_lock);
4960 lst = find_lock_stateid(lo, fi);
4961 if (likely(!lst)) {
4962 lst = openlockstateid(ns);
4963 init_lock_stateid(lst, lo, fi, inode, ost);
4964 ns = NULL;
4965 *new = true;
4966 }
4967 }
4968 spin_unlock(&clp->cl_lock);
4969 if (ns)
4970 nfs4_put_stid(ns);
4971 return lst;
4972}
Jeff Laytonc53530d2014-06-30 11:48:39 -04004973
NeilBrownfd39ca92005-06-23 22:04:03 -07004974static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975check_lock_length(u64 offset, u64 length)
4976{
Benny Halevy87df4de2008-12-15 19:42:03 +02004977 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 LOFF_OVERFLOW(offset, length)));
4979}
4980
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004981static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004982{
Trond Myklebust11b91642014-07-29 21:34:08 -04004983 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004984
Jeff Layton7214e862014-07-10 14:07:33 -04004985 lockdep_assert_held(&fp->fi_lock);
4986
Jeff Layton82c5ff12012-05-11 09:45:13 -04004987 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004988 return;
Jeff Layton12659652014-07-10 14:07:28 -04004989 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004990 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004991}
4992
Jeff Layton356a95e2014-07-29 21:34:13 -04004993static __be32
4994lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
4995 struct nfs4_ol_stateid *ost,
4996 struct nfsd4_lock *lock,
4997 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004998{
Jeff Layton5db1c032014-07-29 21:34:28 -04004999 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005000 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005001 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5002 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005003 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005004 struct nfs4_lockowner *lo;
5005 unsigned int strhashval;
5006
Trond Myklebustd4f04892014-07-29 21:34:36 -04005007 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005008 if (!lo) {
Trond Myklebustd4f04892014-07-29 21:34:36 -04005009 strhashval = ownerstr_hashval(&lock->v.new.owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005010 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5011 if (lo == NULL)
5012 return nfserr_jukebox;
5013 } else {
5014 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005015 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005016 if (!cstate->minorversion &&
5017 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005018 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005019 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005020
Jeff Layton356a95e2014-07-29 21:34:13 -04005021 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005022 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005023 status = nfserr_jukebox;
5024 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005025 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005026 status = nfs_ok;
5027out:
5028 nfs4_put_stateowner(&lo->lo_owner);
5029 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005030}
5031
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032/*
5033 * LOCK operation
5034 */
Al Virob37ad282006-10-19 23:28:59 -07005035__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005036nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005037 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005039 struct nfs4_openowner *open_sop = NULL;
5040 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005041 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005042 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005043 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005044 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005045 struct file_lock *file_lock = NULL;
5046 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005047 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005048 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005049 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005050 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005051 struct net *net = SVC_NET(rqstp);
5052 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053
5054 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5055 (long long) lock->lk_offset,
5056 (long long) lock->lk_length);
5057
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 if (check_lock_length(lock->lk_offset, lock->lk_length))
5059 return nfserr_inval;
5060
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005061 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005062 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005063 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5064 return status;
5065 }
5066
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 nfs4_lock_state();
5068
5069 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005070 if (nfsd4_has_session(cstate))
5071 /* See rfc 5661 18.10.3: given clientid is ignored: */
5072 memcpy(&lock->v.new.clientid,
5073 &cstate->session->se_client->cl_clientid,
5074 sizeof(clientid_t));
5075
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005077 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005081 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 lock->lk_new_open_seqid,
5083 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005084 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005085 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005087 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005088 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005089 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005090 &lock->v.new.clientid))
5091 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005092 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005093 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005094 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005095 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005096 lock->lk_old_lock_seqid,
5097 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005098 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005099 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005100 if (status)
5101 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005102 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005104 lkflg = setlkflg(lock->lk_type);
5105 status = nfs4_check_openmode(lock_stp, lkflg);
5106 if (status)
5107 goto out;
5108
NeilBrown0dd395d2005-07-07 17:59:15 -07005109 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005110 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005111 goto out;
5112 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005113 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005114 goto out;
5115
Jeff Layton21179d82012-08-21 08:03:32 -04005116 file_lock = locks_alloc_lock();
5117 if (!file_lock) {
5118 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5119 status = nfserr_jukebox;
5120 goto out;
5121 }
5122
Trond Myklebust11b91642014-07-29 21:34:08 -04005123 fp = lock_stp->st_stid.sc_file;
Jeff Layton21179d82012-08-21 08:03:32 -04005124 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 switch (lock->lk_type) {
5126 case NFS4_READ_LT:
5127 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005128 spin_lock(&fp->fi_lock);
5129 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005130 if (filp)
5131 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005132 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005133 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005134 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 case NFS4_WRITE_LT:
5136 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005137 spin_lock(&fp->fi_lock);
5138 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005139 if (filp)
5140 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005141 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005142 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005143 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 default:
5145 status = nfserr_inval;
5146 goto out;
5147 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005148 if (!filp) {
5149 status = nfserr_openmode;
5150 goto out;
5151 }
Jeff Layton21179d82012-08-21 08:03:32 -04005152 file_lock->fl_owner = (fl_owner_t)lock_sop;
5153 file_lock->fl_pid = current->tgid;
5154 file_lock->fl_file = filp;
5155 file_lock->fl_flags = FL_POSIX;
5156 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5157 file_lock->fl_start = lock->lk_offset;
5158 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5159 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160
Jeff Layton21179d82012-08-21 08:03:32 -04005161 conflock = locks_alloc_lock();
5162 if (!conflock) {
5163 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5164 status = nfserr_jukebox;
5165 goto out;
5166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167
Jeff Layton21179d82012-08-21 08:03:32 -04005168 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005169 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005171 update_stateid(&lock_stp->st_stid.sc_stateid);
5172 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005174 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005175 break;
5176 case (EAGAIN): /* conflock holds conflicting lock */
5177 status = nfserr_denied;
5178 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005179 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005180 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 case (EDEADLK):
5182 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005183 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005184 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005185 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005186 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005187 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189out:
Trond Myklebustde186432014-07-10 14:07:26 -04005190 if (filp)
5191 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005192 if (lock_stp) {
5193 /* Bump seqid manually if the 4.0 replay owner is openowner */
5194 if (cstate->replay_owner &&
5195 cstate->replay_owner != &lock_sop->lo_owner &&
5196 seqid_mutating_err(ntohl(status)))
5197 lock_sop->lo_owner.so_seqid++;
5198
5199 /*
5200 * If this is a new, never-before-used stateid, and we are
5201 * returning an error, then just go ahead and release it.
5202 */
5203 if (status && new)
5204 release_lock_stateid(lock_stp);
5205
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005206 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005207 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005208 if (open_stp)
5209 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005210 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005211 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005212 if (file_lock)
5213 locks_free_lock(file_lock);
5214 if (conflock)
5215 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 return status;
5217}
5218
5219/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005220 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5221 * so we do a temporary open here just to get an open file to pass to
5222 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5223 * inode operation.)
5224 */
Al Viro04da6e92012-04-13 00:00:04 -04005225static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005226{
5227 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005228 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5229 if (!err) {
5230 err = nfserrno(vfs_test_lock(file, lock));
5231 nfsd_close(file);
5232 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005233 return err;
5234}
5235
5236/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 * LOCKT operation
5238 */
Al Virob37ad282006-10-19 23:28:59 -07005239__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005240nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5241 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242{
Jeff Layton21179d82012-08-21 08:03:32 -04005243 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005244 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005245 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005246 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005248 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 return nfserr_grace;
5250
5251 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5252 return nfserr_inval;
5253
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 nfs4_lock_state();
5255
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005256 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005257 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005258 if (status)
5259 goto out;
5260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005262 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264
Jeff Layton21179d82012-08-21 08:03:32 -04005265 file_lock = locks_alloc_lock();
5266 if (!file_lock) {
5267 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5268 status = nfserr_jukebox;
5269 goto out;
5270 }
5271 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 switch (lockt->lt_type) {
5273 case NFS4_READ_LT:
5274 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005275 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 break;
5277 case NFS4_WRITE_LT:
5278 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005279 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 break;
5281 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005282 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 status = nfserr_inval;
5284 goto out;
5285 }
5286
Trond Myklebustd4f04892014-07-29 21:34:36 -04005287 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5288 cstate->clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005289 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005290 file_lock->fl_owner = (fl_owner_t)lo;
5291 file_lock->fl_pid = current->tgid;
5292 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293
Jeff Layton21179d82012-08-21 08:03:32 -04005294 file_lock->fl_start = lockt->lt_offset;
5295 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296
Jeff Layton21179d82012-08-21 08:03:32 -04005297 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298
Jeff Layton21179d82012-08-21 08:03:32 -04005299 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005300 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005301 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005302
Jeff Layton21179d82012-08-21 08:03:32 -04005303 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005305 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 }
5307out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005308 if (lo)
5309 nfs4_put_stateowner(&lo->lo_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005311 if (file_lock)
5312 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 return status;
5314}
5315
Al Virob37ad282006-10-19 23:28:59 -07005316__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005317nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005318 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005320 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005322 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005323 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005324 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005325 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5326
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5328 (long long) locku->lu_offset,
5329 (long long) locku->lu_length);
5330
5331 if (check_lock_length(locku->lu_offset, locku->lu_length))
5332 return nfserr_inval;
5333
5334 nfs4_lock_state();
5335
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005336 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005337 &locku->lu_stateid, NFS4_LOCK_STID,
5338 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005339 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005341 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005342 if (!filp) {
5343 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005344 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005345 }
Jeff Layton21179d82012-08-21 08:03:32 -04005346 file_lock = locks_alloc_lock();
5347 if (!file_lock) {
5348 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5349 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005350 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005351 }
5352 locks_init_lock(file_lock);
5353 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005354 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005355 file_lock->fl_pid = current->tgid;
5356 file_lock->fl_file = filp;
5357 file_lock->fl_flags = FL_POSIX;
5358 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5359 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360
Jeff Layton21179d82012-08-21 08:03:32 -04005361 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5362 locku->lu_length);
5363 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364
Jeff Layton21179d82012-08-21 08:03:32 -04005365 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005366 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005367 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 goto out_nfserr;
5369 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005370 update_stateid(&stp->st_stid.sc_stateid);
5371 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005372fput:
5373 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005374put_stateid:
5375 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005377 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005378 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005379 if (file_lock)
5380 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 return status;
5382
5383out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005384 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005385 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386}
5387
5388/*
5389 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005390 * true: locks held by lockowner
5391 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005393static bool
5394check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395{
5396 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005397 int status = false;
5398 struct file *filp = find_any_file(fp);
5399 struct inode *inode;
5400
5401 if (!filp) {
5402 /* Any valid lock stateid should have some sort of access */
5403 WARN_ON_ONCE(1);
5404 return status;
5405 }
5406
5407 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408
Jeff Layton1c8c6012013-06-21 08:58:15 -04005409 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005411 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005412 status = true;
5413 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005416 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005417 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 return status;
5419}
5420
Al Virob37ad282006-10-19 23:28:59 -07005421__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005422nfsd4_release_lockowner(struct svc_rqst *rqstp,
5423 struct nfsd4_compound_state *cstate,
5424 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425{
5426 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Laytonfd449072014-06-30 11:48:41 -04005427 struct nfs4_stateowner *sop = NULL, *tmp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005428 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005429 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005431 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005432 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005433 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005434 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435
5436 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5437 clid->cl_boot, clid->cl_id);
5438
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 nfs4_lock_state();
5440
Jeff Layton4b24ca72014-06-30 11:48:44 -04005441 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005442 if (status)
5443 goto out;
5444
NeilBrown3e9e3db2005-06-23 22:04:20 -07005445 status = nfserr_locks_held;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05005446
Trond Myklebustd4f04892014-07-29 21:34:36 -04005447 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005448 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005449 spin_lock(&clp->cl_lock);
5450 list_for_each_entry(tmp, &clp->cl_ownerstr_hashtbl[hashval],
5451 so_strhash) {
Jeff Laytonfd449072014-06-30 11:48:41 -04005452 if (tmp->so_is_open_owner)
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005453 continue;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005454 if (same_owner_str(tmp, owner)) {
Jeff Laytonfd449072014-06-30 11:48:41 -04005455 sop = tmp;
Jeff Layton5adfd882014-07-29 21:34:31 -04005456 atomic_inc(&sop->so_count);
Jeff Laytonfd449072014-06-30 11:48:41 -04005457 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07005459 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005460
5461 /* No matching owner found, maybe a replay? Just declare victory... */
5462 if (!sop) {
Trond Myklebustd4f04892014-07-29 21:34:36 -04005463 spin_unlock(&clp->cl_lock);
Jeff Laytonfd449072014-06-30 11:48:41 -04005464 status = nfs_ok;
5465 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005467
5468 lo = lockowner(sop);
5469 /* see if there are still any locks associated with it */
5470 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
Jeff Layton5adfd882014-07-29 21:34:31 -04005471 if (check_for_locks(stp->st_stid.sc_file, lo)) {
Trond Myklebustc58c6612014-07-29 21:34:35 -04005472 spin_unlock(&clp->cl_lock);
Jeff Laytonfd449072014-06-30 11:48:41 -04005473 goto out;
Jeff Layton5adfd882014-07-29 21:34:31 -04005474 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005475 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005476 spin_unlock(&clp->cl_lock);
Jeff Laytonfd449072014-06-30 11:48:41 -04005477
5478 status = nfs_ok;
Trond Myklebustc58c6612014-07-29 21:34:35 -04005479 sop = NULL;
Jeff Laytonfd449072014-06-30 11:48:41 -04005480 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481out:
Trond Myklebustc58c6612014-07-29 21:34:35 -04005482 if (sop)
5483 nfs4_put_stateowner(sop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 nfs4_unlock_state();
5485 return status;
5486}
5487
5488static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005489alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490{
NeilBrowna55370a2005-06-23 22:03:52 -07005491 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492}
5493
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005494bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005495nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005496{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005497 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005498
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005499 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005500 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005501}
5502
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503/*
5504 * failure => all reset bets are off, nfserr_no_grace...
5505 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005506struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005507nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508{
5509 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005510 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511
NeilBrowna55370a2005-06-23 22:03:52 -07005512 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5513 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005514 if (crp) {
5515 strhashval = clientstr_hashval(name);
5516 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005517 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005518 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005519 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005520 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005521 }
5522 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523}
5524
Jeff Layton2a4317c2012-03-21 16:42:43 -04005525void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005526nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005527{
5528 list_del(&crp->cr_strhash);
5529 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005530 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005531}
5532
5533void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005534nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535{
5536 struct nfs4_client_reclaim *crp = NULL;
5537 int i;
5538
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005540 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5541 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005543 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 }
5545 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005546 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547}
5548
5549/*
5550 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005551struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005552nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553{
5554 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 struct nfs4_client_reclaim *crp = NULL;
5556
Jeff Layton278c9312012-11-12 15:00:52 -05005557 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558
Jeff Layton278c9312012-11-12 15:00:52 -05005559 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005560 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005561 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 return crp;
5563 }
5564 }
5565 return NULL;
5566}
5567
5568/*
5569* Called from OPEN. Look for clientid in reclaim list.
5570*/
Al Virob37ad282006-10-19 23:28:59 -07005571__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005572nfs4_check_open_reclaim(clientid_t *clid,
5573 struct nfsd4_compound_state *cstate,
5574 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005576 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005577
5578 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005579 status = lookup_clientid(clid, cstate, nn);
5580 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005581 return nfserr_reclaim_bad;
5582
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005583 if (nfsd4_client_record_check(cstate->clp))
5584 return nfserr_reclaim_bad;
5585
5586 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587}
5588
Bryan Schumaker65178db2011-11-01 13:35:21 -04005589#ifdef CONFIG_NFSD_FAULT_INJECTION
5590
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005591u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5592{
J. Bruce Fields221a6872013-04-01 22:23:49 -04005593 if (mark_client_expired(clp))
5594 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005595 expire_client(clp);
5596 return 1;
5597}
5598
Bryan Schumaker184c1842012-11-29 11:40:44 -05005599u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5600{
5601 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005602 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005603 printk(KERN_INFO "NFS Client: %s\n", buf);
5604 return 1;
5605}
5606
5607static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5608 const char *type)
5609{
5610 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005611 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005612 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5613}
5614
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005615static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5616 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005617{
5618 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005619 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005620 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005621 u64 count = 0;
5622
5623 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005624 list_for_each_entry_safe(stp, st_next,
5625 &oop->oo_owner.so_stateids, st_perstateowner) {
5626 list_for_each_entry_safe(lst, lst_next,
5627 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005628 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005629 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005630 if (++count == max)
5631 return count;
5632 }
5633 }
5634 }
5635
5636 return count;
5637}
5638
5639u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5640{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005641 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005642}
5643
Bryan Schumaker184c1842012-11-29 11:40:44 -05005644u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5645{
5646 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5647 nfsd_print_count(clp, count, "locked files");
5648 return count;
5649}
5650
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005651static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5652{
5653 struct nfs4_openowner *oop, *next;
5654 u64 count = 0;
5655
5656 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5657 if (func)
5658 func(oop);
5659 if (++count == max)
5660 break;
5661 }
5662
5663 return count;
5664}
5665
5666u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5667{
5668 return nfsd_foreach_client_open(clp, max, release_openowner);
5669}
5670
Bryan Schumaker184c1842012-11-29 11:40:44 -05005671u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5672{
5673 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5674 nfsd_print_count(clp, count, "open files");
5675 return count;
5676}
5677
Bryan Schumaker269de302012-11-29 11:40:42 -05005678static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5679 struct list_head *victims)
5680{
5681 struct nfs4_delegation *dp, *next;
5682 u64 count = 0;
5683
Benny Halevycdc97502014-05-30 09:09:30 -04005684 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005685 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005686 if (victims) {
5687 /*
5688 * It's not safe to mess with delegations that have a
5689 * non-zero dl_time. They might have already been broken
5690 * and could be processed by the laundromat outside of
5691 * the state_lock. Just leave them be.
5692 */
5693 if (dp->dl_time != 0)
5694 continue;
5695
Jeff Layton42690672014-07-25 07:34:20 -04005696 unhash_delegation_locked(dp);
5697 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005698 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005699 if (++count == max)
5700 break;
5701 }
5702 return count;
5703}
5704
5705u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5706{
5707 struct nfs4_delegation *dp, *next;
5708 LIST_HEAD(victims);
5709 u64 count;
5710
Benny Halevycdc97502014-05-30 09:09:30 -04005711 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005712 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005713 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005714
Jeff Layton2d4a5322014-07-25 07:34:21 -04005715 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5716 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005717 revoke_delegation(dp);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005718 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005719
5720 return count;
5721}
5722
5723u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5724{
Jeff Laytondff13992014-07-08 14:02:49 -04005725 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005726 LIST_HEAD(victims);
5727 u64 count;
5728
Benny Halevycdc97502014-05-30 09:09:30 -04005729 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005730 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005731 while (!list_empty(&victims)) {
5732 dp = list_first_entry(&victims, struct nfs4_delegation,
5733 dl_recall_lru);
5734 list_del_init(&dp->dl_recall_lru);
5735 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005736 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005737 }
Benny Halevycdc97502014-05-30 09:09:30 -04005738 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005739
5740 return count;
5741}
5742
Bryan Schumaker184c1842012-11-29 11:40:44 -05005743u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5744{
5745 u64 count = 0;
5746
Benny Halevycdc97502014-05-30 09:09:30 -04005747 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005748 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005749 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005750
5751 nfsd_print_count(clp, count, "delegations");
5752 return count;
5753}
5754
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005755u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005756{
5757 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005758 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005759 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005760
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005761 if (!nfsd_netns_ready(nn))
5762 return 0;
5763
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005764 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005765 count += func(clp, max - count);
5766 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005767 break;
5768 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005769
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005770 return count;
5771}
5772
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005773struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5774{
5775 struct nfs4_client *clp;
5776 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5777
5778 if (!nfsd_netns_ready(nn))
5779 return NULL;
5780
5781 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5782 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5783 return clp;
5784 }
5785 return NULL;
5786}
5787
Bryan Schumaker65178db2011-11-01 13:35:21 -04005788#endif /* CONFIG_NFSD_FAULT_INJECTION */
5789
Meelap Shahc2f1a552007-07-17 04:04:39 -07005790/*
5791 * Since the lifetime of a delegation isn't limited to that of an open, a
5792 * client may quite reasonably hang on to a delegation as long as it has
5793 * the inode cached. This becomes an obvious problem the first time a
5794 * client's inode cache approaches the size of the server's total memory.
5795 *
5796 * For now we avoid this problem by imposing a hard limit on the number
5797 * of delegations, which varies according to the server's memory size.
5798 */
5799static void
5800set_max_delegations(void)
5801{
5802 /*
5803 * Allow at most 4 delegations per megabyte of RAM. Quick
5804 * estimates suggest that in the worst case (where every delegation
5805 * is for a different inode), a delegation could take about 1.5K,
5806 * giving a worst case usage of about 6% of memory.
5807 */
5808 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5809}
5810
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005811static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005812{
5813 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5814 int i;
5815
5816 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5817 CLIENT_HASH_SIZE, GFP_KERNEL);
5818 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005819 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005820 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5821 CLIENT_HASH_SIZE, GFP_KERNEL);
5822 if (!nn->unconf_id_hashtbl)
5823 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005824 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5825 SESSION_HASH_SIZE, GFP_KERNEL);
5826 if (!nn->sessionid_hashtbl)
5827 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005828
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005829 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005830 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005831 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005832 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005833 for (i = 0; i < SESSION_HASH_SIZE; i++)
5834 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005835 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005836 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005837 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005838 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005839 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005840 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005841
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005842 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005843 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005844
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005845 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005846
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005847err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005848 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005849err_unconf_id:
5850 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005851err:
5852 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005853}
5854
5855static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005856nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005857{
5858 int i;
5859 struct nfs4_client *clp = NULL;
5860 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5861
5862 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5863 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5864 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5865 destroy_client(clp);
5866 }
5867 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005868
Kinglong Mee2b905632014-03-26 22:09:30 +08005869 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5870 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5871 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5872 destroy_client(clp);
5873 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005874 }
5875
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005876 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005877 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005878 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005879 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005880}
5881
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005882int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005883nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005884{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005885 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005886 int ret;
5887
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005888 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005889 if (ret)
5890 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005891 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005892 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005893 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005894 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005895 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005896 nn->nfsd4_grace, net);
5897 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005898 return 0;
5899}
5900
5901/* initialization to perform when the nfsd service is started: */
5902
5903int
5904nfs4_state_start(void)
5905{
5906 int ret;
5907
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005908 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005909 if (ret)
5910 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005911 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005912 if (laundry_wq == NULL) {
5913 ret = -ENOMEM;
5914 goto out_recovery;
5915 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005916 ret = nfsd4_create_callback_queue();
5917 if (ret)
5918 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005919
Meelap Shahc2f1a552007-07-17 04:04:39 -07005920 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005921
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005922 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005923
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005924out_free_laundry:
5925 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005926out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005927 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928}
5929
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005930void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005931nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005935 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005937 cancel_delayed_work_sync(&nn->laundromat_work);
5938 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005939
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005940 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04005942 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005943 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005945 unhash_delegation_locked(dp);
5946 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 }
Benny Halevycdc97502014-05-30 09:09:30 -04005948 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949 list_for_each_safe(pos, next, &reaplist) {
5950 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005951 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04005952 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 }
5954
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005955 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005956 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005957 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958}
5959
5960void
5961nfs4_state_shutdown(void)
5962{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005963 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005964 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005966
5967static void
5968get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5969{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005970 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5971 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005972}
5973
5974static void
5975put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5976{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005977 if (cstate->minorversion) {
5978 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5979 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5980 }
5981}
5982
5983void
5984clear_current_stateid(struct nfsd4_compound_state *cstate)
5985{
5986 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005987}
5988
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005989/*
5990 * functions to set current state id
5991 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005992void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005993nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5994{
5995 put_stateid(cstate, &odp->od_stateid);
5996}
5997
5998void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005999nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6000{
6001 put_stateid(cstate, &open->op_stateid);
6002}
6003
6004void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006005nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6006{
6007 put_stateid(cstate, &close->cl_stateid);
6008}
6009
6010void
6011nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6012{
6013 put_stateid(cstate, &lock->lk_resp_stateid);
6014}
6015
6016/*
6017 * functions to consume current state id
6018 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006019
6020void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006021nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6022{
6023 get_stateid(cstate, &odp->od_stateid);
6024}
6025
6026void
6027nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6028{
6029 get_stateid(cstate, &drp->dr_stateid);
6030}
6031
6032void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006033nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6034{
6035 get_stateid(cstate, &fsp->fr_stateid);
6036}
6037
6038void
6039nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6040{
6041 get_stateid(cstate, &setattr->sa_stateid);
6042}
6043
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006044void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006045nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6046{
6047 get_stateid(cstate, &close->cl_stateid);
6048}
6049
6050void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006051nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006052{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006053 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006054}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006055
6056void
6057nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6058{
6059 get_stateid(cstate, &read->rd_stateid);
6060}
6061
6062void
6063nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6064{
6065 get_stateid(cstate, &write->wr_stateid);
6066}