blob: 9358cbe2283d87af4a2dc076308c276265f96e55 [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{
Jeff Laytona819ecc2014-07-29 21:34:38 -0400940 struct nfs4_client *clp = sop->so_client;
941
942 might_lock(&clp->cl_lock);
943
944 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -0400945 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -0400946 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -0400947 spin_unlock(&clp->cl_lock);
Jeff Layton6b180f02014-07-29 21:34:26 -0400948 kfree(sop->so_owner.data);
949 sop->so_ops->so_free(sop);
950}
951
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400952static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500953{
Trond Myklebust11b91642014-07-29 21:34:08 -0400954 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -0400955
Jeff Layton1c755dc2014-07-29 21:34:12 -0400956 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
957
Trond Myklebust1d31a252014-07-10 14:07:25 -0400958 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500959 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400960 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500961 list_del(&stp->st_perstateowner);
962}
963
Trond Myklebust60116952014-07-29 21:34:06 -0400964static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500965{
Trond Myklebust60116952014-07-29 21:34:06 -0400966 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400967
Trond Myklebust60116952014-07-29 21:34:06 -0400968 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -0400969 if (stp->st_stateowner)
970 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -0400971 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500972}
973
Jeff Laytonb49e0842014-07-29 21:34:11 -0400974static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500975{
Jeff Laytonb49e0842014-07-29 21:34:11 -0400976 struct nfs4_ol_stateid *stp = openlockstateid(stid);
977 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500978 struct file *file;
979
Jeff Laytonb49e0842014-07-29 21:34:11 -0400980 file = find_any_file(stp->st_stid.sc_file);
981 if (file)
982 filp_close(file, (fl_owner_t)lo);
983 nfs4_free_ol_stateid(stid);
984}
985
Jeff Layton3c1c9952014-07-29 21:34:39 -0400986static void unhash_lock_stateid(struct nfs4_ol_stateid *stp)
987{
988 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
989
990 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
991
992 list_del_init(&stp->st_locks);
993 unhash_generic_stateid(stp);
994 unhash_stid(&stp->st_stid);
995}
996
Jeff Layton5adfd882014-07-29 21:34:31 -0400997static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -0400998{
Jeff Layton1c755dc2014-07-29 21:34:12 -0400999 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1000
1001 spin_lock(&oo->oo_owner.so_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001002 unhash_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001003 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04001004 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001005}
1006
Trond Myklebustc58c6612014-07-29 21:34:35 -04001007static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001008{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001009 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001010
Trond Myklebustd4f04892014-07-29 21:34:36 -04001011 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001012
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001013 list_del_init(&lo->lo_owner.so_strhash);
1014}
1015
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001016static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001017{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001018 struct nfs4_client *clp = lo->lo_owner.so_client;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001019 struct nfs4_ol_stateid *stp;
1020 struct list_head reaplist;
1021
1022 INIT_LIST_HEAD(&reaplist);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001023
Trond Myklebustd4f04892014-07-29 21:34:36 -04001024 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001025 unhash_lockowner_locked(lo);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001026 while (!list_empty(&lo->lo_owner.so_stateids)) {
1027 stp = list_first_entry(&lo->lo_owner.so_stateids,
1028 struct nfs4_ol_stateid, st_perstateowner);
1029 unhash_lock_stateid(stp);
1030 /*
1031 * We now know that no new references can be added to the
1032 * stateid. If ours is the last one, finish the unhashing
1033 * and put it on the list to be reaped.
1034 */
1035 if (atomic_dec_and_test(&stp->st_stid.sc_count)) {
1036 idr_remove(&clp->cl_stateids,
1037 stp->st_stid.sc_stateid.si_opaque.so_id);
1038 list_add(&stp->st_locks, &reaplist);
1039 }
1040 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001041 spin_unlock(&clp->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001042 while (!list_empty(&reaplist)) {
1043 stp = list_first_entry(&reaplist, struct nfs4_ol_stateid,
1044 st_locks);
1045 list_del(&stp->st_locks);
1046 stp->st_stid.sc_free(&stp->st_stid);
1047 }
Jeff Layton6b180f02014-07-29 21:34:26 -04001048 nfs4_put_stateowner(&lo->lo_owner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001049}
1050
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001051static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
Jeff Layton1c755dc2014-07-29 21:34:12 -04001052 __releases(&open_stp->st_stateowner->so_client->cl_lock)
1053 __acquires(&open_stp->st_stateowner->so_client->cl_lock)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001054{
1055 struct nfs4_ol_stateid *stp;
1056
1057 while (!list_empty(&open_stp->st_locks)) {
1058 stp = list_entry(open_stp->st_locks.next,
1059 struct nfs4_ol_stateid, st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001060 spin_unlock(&open_stp->st_stateowner->so_client->cl_lock);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001061 release_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001062 spin_lock(&open_stp->st_stateowner->so_client->cl_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001063 }
1064}
1065
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001066static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -05001067{
Jeff Layton1c755dc2014-07-29 21:34:12 -04001068 spin_lock(&stp->st_stateowner->so_client->cl_lock);
J. Bruce Fields22839632009-01-11 14:27:17 -05001069 unhash_generic_stateid(stp);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001070 release_open_stateid_locks(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001071 spin_unlock(&stp->st_stateowner->so_client->cl_lock);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001072}
1073
1074static void release_open_stateid(struct nfs4_ol_stateid *stp)
1075{
1076 unhash_open_stateid(stp);
Trond Myklebust60116952014-07-29 21:34:06 -04001077 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields22839632009-01-11 14:27:17 -05001078}
1079
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001080static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001081{
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);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001085
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001086 list_del_init(&oo->oo_owner.so_strhash);
1087 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001088}
1089
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001090static void release_last_closed_stateid(struct nfs4_openowner *oo)
1091{
1092 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
1093
1094 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001095 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001096 oo->oo_last_closed_stid = NULL;
Jeff Laytond3134b12014-07-29 21:34:32 -04001097 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001098 }
1099}
1100
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001101static void release_openowner_stateids(struct nfs4_openowner *oo)
1102{
1103 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001104 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001105
Trond Myklebustd4f04892014-07-29 21:34:36 -04001106 lockdep_assert_held(&clp->cl_lock);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001107
1108 while (!list_empty(&oo->oo_owner.so_stateids)) {
1109 stp = list_first_entry(&oo->oo_owner.so_stateids,
1110 struct nfs4_ol_stateid, st_perstateowner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001111 spin_unlock(&clp->cl_lock);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001112 release_open_stateid(stp);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001113 spin_lock(&clp->cl_lock);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001114 }
1115}
1116
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001117static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001118{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001119 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001120
Trond Myklebustd4f04892014-07-29 21:34:36 -04001121 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001122 unhash_openowner_locked(oo);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001123 release_openowner_stateids(oo);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001124 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001125 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001126 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001127}
1128
Marc Eshel5282fd72009-04-03 08:27:52 +03001129static inline int
1130hash_sessionid(struct nfs4_sessionid *sessionid)
1131{
1132 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1133
1134 return sid->sequence % SESSION_HASH_SIZE;
1135}
1136
Trond Myklebust8f199b82012-03-20 15:11:17 -04001137#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001138static inline void
1139dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1140{
1141 u32 *ptr = (u32 *)(&sessionid->data[0]);
1142 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1143}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001144#else
1145static inline void
1146dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1147{
1148}
1149#endif
1150
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001151/*
1152 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1153 * won't be used for replay.
1154 */
1155void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1156{
1157 struct nfs4_stateowner *so = cstate->replay_owner;
1158
1159 if (nfserr == nfserr_replay_me)
1160 return;
1161
1162 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001163 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001164 return;
1165 }
1166 if (!so)
1167 return;
1168 if (so->so_is_open_owner)
1169 release_last_closed_stateid(openowner(so));
1170 so->so_seqid++;
1171 return;
1172}
Marc Eshel5282fd72009-04-03 08:27:52 +03001173
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001174static void
1175gen_sessionid(struct nfsd4_session *ses)
1176{
1177 struct nfs4_client *clp = ses->se_client;
1178 struct nfsd4_sessionid *sid;
1179
1180 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1181 sid->clientid = clp->cl_clientid;
1182 sid->sequence = current_sessionid++;
1183 sid->reserved = 0;
1184}
1185
1186/*
Andy Adamsona6496372009-08-28 08:45:01 -04001187 * The protocol defines ca_maxresponssize_cached to include the size of
1188 * the rpc header, but all we need to cache is the data starting after
1189 * the end of the initial SEQUENCE operation--the rest we regenerate
1190 * each time. Therefore we can advertise a ca_maxresponssize_cached
1191 * value that is the number of bytes in our cache plus a few additional
1192 * bytes. In order to stay on the safe side, and not promise more than
1193 * we can cache, those additional bytes must be the minimum possible: 24
1194 * bytes of rpc header (xid through accept state, with AUTH_NULL
1195 * verifier), 12 for the compound header (with zero-length tag), and 44
1196 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001197 */
Andy Adamsona6496372009-08-28 08:45:01 -04001198#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1199
Andy Adamson557ce262009-08-28 08:45:04 -04001200static void
1201free_session_slots(struct nfsd4_session *ses)
1202{
1203 int i;
1204
1205 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1206 kfree(ses->se_slots[i]);
1207}
1208
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001209/*
1210 * We don't actually need to cache the rpc and session headers, so we
1211 * can allocate a little less for each slot:
1212 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001213static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001214{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001215 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001216
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001217 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1218 size = 0;
1219 else
1220 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1221 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001222}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001223
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001224/*
1225 * XXX: If we run out of reserved DRC memory we could (up to a point)
1226 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001227 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001228 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001229static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001230{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001231 u32 slotsize = slot_bytes(ca);
1232 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001233 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001234
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001235 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001236 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1237 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001238 num = min_t(int, num, avail / slotsize);
1239 nfsd_drc_mem_used += num * slotsize;
1240 spin_unlock(&nfsd_drc_lock);
1241
1242 return num;
1243}
1244
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001245static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001246{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001247 int slotsize = slot_bytes(ca);
1248
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001249 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001250 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001251 spin_unlock(&nfsd_drc_lock);
1252}
1253
Kinglong Mee60810e52014-01-01 00:35:47 +08001254static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1255 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001256{
Kinglong Mee60810e52014-01-01 00:35:47 +08001257 int numslots = fattrs->maxreqs;
1258 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001259 struct nfsd4_session *new;
1260 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001261
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001262 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001263 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1264 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001265
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001266 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001267 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001268 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001269 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001270 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001271 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001272 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001273 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001274 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001275
1276 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1277 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1278
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001279 return new;
1280out_free:
1281 while (i--)
1282 kfree(new->se_slots[i]);
1283 kfree(new);
1284 return NULL;
1285}
1286
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001287static void free_conn(struct nfsd4_conn *c)
1288{
1289 svc_xprt_put(c->cn_xprt);
1290 kfree(c);
1291}
1292
1293static void nfsd4_conn_lost(struct svc_xpt_user *u)
1294{
1295 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1296 struct nfs4_client *clp = c->cn_session->se_client;
1297
1298 spin_lock(&clp->cl_lock);
1299 if (!list_empty(&c->cn_persession)) {
1300 list_del(&c->cn_persession);
1301 free_conn(c);
1302 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001303 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001304 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001305}
1306
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001307static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001308{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001309 struct nfsd4_conn *conn;
1310
1311 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1312 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001313 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001314 svc_xprt_get(rqstp->rq_xprt);
1315 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001316 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001317 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1318 return conn;
1319}
1320
J. Bruce Fields328ead22010-09-29 16:11:06 -04001321static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1322{
1323 conn->cn_session = ses;
1324 list_add(&conn->cn_persession, &ses->se_conns);
1325}
1326
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001327static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1328{
1329 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001330
1331 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001332 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001333 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001334}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001335
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001336static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001337{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001338 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001339 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001340}
1341
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001342static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001343{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001344 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001345
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001346 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001347 ret = nfsd4_register_conn(conn);
1348 if (ret)
1349 /* oops; xprt is already down: */
1350 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001351 /* We may have gained or lost a callback channel: */
1352 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001353}
1354
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001355static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001356{
1357 u32 dir = NFS4_CDFC4_FORE;
1358
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001359 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001360 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001361 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001362}
1363
1364/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001365static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001366{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001367 struct nfs4_client *clp = s->se_client;
1368 struct nfsd4_conn *c;
1369
1370 spin_lock(&clp->cl_lock);
1371 while (!list_empty(&s->se_conns)) {
1372 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1373 list_del_init(&c->cn_persession);
1374 spin_unlock(&clp->cl_lock);
1375
1376 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1377 free_conn(c);
1378
1379 spin_lock(&clp->cl_lock);
1380 }
1381 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001382}
1383
J. Bruce Fields1377b692012-09-11 21:42:40 -04001384static void __free_session(struct nfsd4_session *ses)
1385{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001386 free_session_slots(ses);
1387 kfree(ses);
1388}
1389
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001390static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001391{
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001392 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1393
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001394 lockdep_assert_held(&nn->client_lock);
1395 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001396 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001397 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001398}
Andy Adamson557ce262009-08-28 08:45:04 -04001399
Fengguang Wu135ae822012-11-10 07:20:25 -05001400static 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 -04001401{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001402 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001403 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001404
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001405 new->se_client = clp;
1406 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001407
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001408 INIT_LIST_HEAD(&new->se_conns);
1409
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001410 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001411 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001412 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001413 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001414 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001415 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001416 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001417 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001418 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001419 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001420 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001421 spin_unlock(&nn->client_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001422
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001423 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001424 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001425 /*
1426 * This is a little silly; with sessions there's no real
1427 * use for the callback address. Use the peer address
1428 * as a reasonable default for now, but consider fixing
1429 * the rpc client not to require an address in the
1430 * future:
1431 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001432 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1433 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001434 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001435}
1436
Benny Halevy9089f1b2010-05-12 00:12:26 +03001437/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001438static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001439__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001440{
1441 struct nfsd4_session *elem;
1442 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001443 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001444
1445 dump_sessionid(__func__, sessionid);
1446 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001447 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001448 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001449 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1450 NFS4_MAX_SESSIONID_LEN)) {
1451 return elem;
1452 }
1453 }
1454
1455 dprintk("%s: session not found\n", __func__);
1456 return NULL;
1457}
1458
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001459static struct nfsd4_session *
1460find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1461 __be32 *ret)
1462{
1463 struct nfsd4_session *session;
1464 __be32 status = nfserr_badsession;
1465
1466 session = __find_in_sessionid_hashtbl(sessionid, net);
1467 if (!session)
1468 goto out;
1469 status = nfsd4_get_session_locked(session);
1470 if (status)
1471 session = NULL;
1472out:
1473 *ret = status;
1474 return session;
1475}
1476
Benny Halevy9089f1b2010-05-12 00:12:26 +03001477/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001478static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001479unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001480{
1481 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001482 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001483 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001484 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001485}
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1488static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001489STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001491 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001493 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001494 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return 1;
1496}
1497
1498/*
1499 * XXX Should we use a slab cache ?
1500 * This type of memory management is somewhat inefficient, but we use it
1501 * anyway since SETCLIENTID is not a common operation.
1502 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001503static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
1505 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001506 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001508 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1509 if (clp == NULL)
1510 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001511 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001512 if (clp->cl_name.data == NULL)
1513 goto err_no_name;
1514 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1515 OWNER_HASH_SIZE, GFP_KERNEL);
1516 if (!clp->cl_ownerstr_hashtbl)
1517 goto err_no_hashtbl;
1518 for (i = 0; i < OWNER_HASH_SIZE; i++)
1519 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001520 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001521 INIT_LIST_HEAD(&clp->cl_sessions);
1522 idr_init(&clp->cl_stateids);
1523 atomic_set(&clp->cl_refcount, 0);
1524 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1525 INIT_LIST_HEAD(&clp->cl_idhash);
1526 INIT_LIST_HEAD(&clp->cl_openowners);
1527 INIT_LIST_HEAD(&clp->cl_delegations);
1528 INIT_LIST_HEAD(&clp->cl_lru);
1529 INIT_LIST_HEAD(&clp->cl_callbacks);
1530 INIT_LIST_HEAD(&clp->cl_revoked);
1531 spin_lock_init(&clp->cl_lock);
1532 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001534err_no_hashtbl:
1535 kfree(clp->cl_name.data);
1536err_no_name:
1537 kfree(clp);
1538 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001541static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542free_client(struct nfs4_client *clp)
1543{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001544 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001545
1546 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001547 while (!list_empty(&clp->cl_sessions)) {
1548 struct nfsd4_session *ses;
1549 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1550 se_perclnt);
1551 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001552 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1553 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001554 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001555 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001556 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001557 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001559 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 kfree(clp);
1561}
1562
Benny Halevy84d38ac2010-05-12 00:13:16 +03001563/* must be called under the client_lock */
1564static inline void
1565unhash_client_locked(struct nfs4_client *clp)
1566{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001567 struct nfsd4_session *ses;
1568
Benny Halevy84d38ac2010-05-12 00:13:16 +03001569 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001570 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001571 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1572 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001573 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001574}
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001577destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001579 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001582 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001585 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001586 while (!list_empty(&clp->cl_delegations)) {
1587 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001588 unhash_delegation_locked(dp);
1589 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
Benny Halevycdc97502014-05-30 09:09:30 -04001591 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 while (!list_empty(&reaplist)) {
1593 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001594 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001595 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001597 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001598 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001599 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001600 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001601 }
NeilBrownea1da632005-06-23 22:04:17 -07001602 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001603 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Jeff Laytond3134b12014-07-29 21:34:32 -04001604 atomic_inc(&oo->oo_owner.so_count);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001605 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001607 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001608 if (clp->cl_cb_conn.cb_xprt)
1609 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001610 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001611 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001612 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001613 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001614 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001615 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001616 unhash_client_locked(clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001617 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1618 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001619 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620}
1621
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001622static void expire_client(struct nfs4_client *clp)
1623{
1624 nfsd4_client_record_remove(clp);
1625 destroy_client(clp);
1626}
1627
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001628static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1629{
1630 memcpy(target->cl_verifier.data, source->data,
1631 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
1633
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001634static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1635{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1637 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1638}
1639
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001640static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001641{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001642 if (source->cr_principal) {
1643 target->cr_principal =
1644 kstrdup(source->cr_principal, GFP_KERNEL);
1645 if (target->cr_principal == NULL)
1646 return -ENOMEM;
1647 } else
1648 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001649 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 target->cr_uid = source->cr_uid;
1651 target->cr_gid = source->cr_gid;
1652 target->cr_group_info = source->cr_group_info;
1653 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001654 target->cr_gss_mech = source->cr_gss_mech;
1655 if (source->cr_gss_mech)
1656 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001657 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658}
1659
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001660static long long
1661compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1662{
1663 long long res;
1664
1665 res = o1->len - o2->len;
1666 if (res)
1667 return res;
1668 return (long long)memcmp(o1->data, o2->data, o1->len);
1669}
1670
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001671static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001672{
NeilBrowna55370a2005-06-23 22:03:52 -07001673 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674}
1675
1676static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001677same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1678{
1679 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680}
1681
1682static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001683same_clid(clientid_t *cl1, clientid_t *cl2)
1684{
1685 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001688static bool groups_equal(struct group_info *g1, struct group_info *g2)
1689{
1690 int i;
1691
1692 if (g1->ngroups != g2->ngroups)
1693 return false;
1694 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001695 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001696 return false;
1697 return true;
1698}
1699
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001700/*
1701 * RFC 3530 language requires clid_inuse be returned when the
1702 * "principal" associated with a requests differs from that previously
1703 * used. We use uid, gid's, and gss principal string as our best
1704 * approximation. We also don't want to allow non-gss use of a client
1705 * established using gss: in theory cr_principal should catch that
1706 * change, but in practice cr_principal can be null even in the gss case
1707 * since gssd doesn't always pass down a principal string.
1708 */
1709static bool is_gss_cred(struct svc_cred *cr)
1710{
1711 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1712 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1713}
1714
1715
Vivek Trivedi5559b502012-07-24 21:18:20 +05301716static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001717same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1718{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001719 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001720 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1721 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001722 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1723 return false;
1724 if (cr1->cr_principal == cr2->cr_principal)
1725 return true;
1726 if (!cr1->cr_principal || !cr2->cr_principal)
1727 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301728 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729}
1730
J. Bruce Fields57266a62013-04-13 14:27:29 -04001731static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1732{
1733 struct svc_cred *cr = &rqstp->rq_cred;
1734 u32 service;
1735
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001736 if (!cr->cr_gss_mech)
1737 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001738 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1739 return service == RPC_GSS_SVC_INTEGRITY ||
1740 service == RPC_GSS_SVC_PRIVACY;
1741}
1742
1743static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1744{
1745 struct svc_cred *cr = &rqstp->rq_cred;
1746
1747 if (!cl->cl_mach_cred)
1748 return true;
1749 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1750 return false;
1751 if (!svc_rqst_integrity_protected(rqstp))
1752 return false;
1753 if (!cr->cr_principal)
1754 return false;
1755 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1756}
1757
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001758static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001759{
1760 static u32 current_clientid = 1;
1761
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001762 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 clp->cl_clientid.cl_id = current_clientid++;
1764}
1765
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001766static void gen_confirm(struct nfs4_client *clp)
1767{
Chuck Leverab4684d2012-03-02 17:13:50 -05001768 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001769 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Jeff Laytonf4199922014-06-17 07:44:11 -04001771 /*
1772 * This is opaque to client, so no need to byte-swap. Use
1773 * __force to keep sparse happy
1774 */
1775 verf[0] = (__force __be32)get_seconds();
1776 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001777 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
1779
Jeff Layton4770d722014-07-29 21:34:10 -04001780static struct nfs4_stid *
1781find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001782{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001783 struct nfs4_stid *ret;
1784
1785 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1786 if (!ret || !ret->sc_type)
1787 return NULL;
1788 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001789}
1790
Jeff Layton4770d722014-07-29 21:34:10 -04001791static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001792find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001793{
1794 struct nfs4_stid *s;
1795
Jeff Layton4770d722014-07-29 21:34:10 -04001796 spin_lock(&cl->cl_lock);
1797 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001798 if (s != NULL) {
1799 if (typemask & s->sc_type)
1800 atomic_inc(&s->sc_count);
1801 else
1802 s = NULL;
1803 }
Jeff Layton4770d722014-07-29 21:34:10 -04001804 spin_unlock(&cl->cl_lock);
1805 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001806}
1807
Jeff Layton2216d442012-11-12 15:00:57 -05001808static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001809 struct svc_rqst *rqstp, nfs4_verifier *verf)
1810{
1811 struct nfs4_client *clp;
1812 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001813 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001814 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001815 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001816
1817 clp = alloc_client(name);
1818 if (clp == NULL)
1819 return NULL;
1820
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001821 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1822 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001823 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001824 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001825 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001826 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001827 }
Jeff Layton02e12152014-07-16 10:31:57 -04001828 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001829 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001830 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001831 copy_verf(clp, verf);
1832 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001833 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001834 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001835 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001836 return clp;
1837}
1838
NeilBrownfd39ca92005-06-23 22:04:03 -07001839static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001840add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1841{
1842 struct rb_node **new = &(root->rb_node), *parent = NULL;
1843 struct nfs4_client *clp;
1844
1845 while (*new) {
1846 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1847 parent = *new;
1848
1849 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1850 new = &((*new)->rb_left);
1851 else
1852 new = &((*new)->rb_right);
1853 }
1854
1855 rb_link_node(&new_clp->cl_namenode, parent, new);
1856 rb_insert_color(&new_clp->cl_namenode, root);
1857}
1858
1859static struct nfs4_client *
1860find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1861{
1862 long long cmp;
1863 struct rb_node *node = root->rb_node;
1864 struct nfs4_client *clp;
1865
1866 while (node) {
1867 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1868 cmp = compare_blob(&clp->cl_name, name);
1869 if (cmp > 0)
1870 node = node->rb_left;
1871 else if (cmp < 0)
1872 node = node->rb_right;
1873 else
1874 return clp;
1875 }
1876 return NULL;
1877}
1878
1879static void
1880add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
1882 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001883 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001885 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001886 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001888 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001889 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890}
1891
NeilBrownfd39ca92005-06-23 22:04:03 -07001892static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893move_to_confirmed(struct nfs4_client *clp)
1894{
1895 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001896 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001899 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001900 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001901 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001902 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 renew_client(clp);
1904}
1905
1906static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001907find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
1909 struct nfs4_client *clp;
1910 unsigned int idhashval = clientid_hashval(clid->cl_id);
1911
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001912 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001913 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001914 if ((bool)clp->cl_minorversion != sessions)
1915 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001916 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
1920 return NULL;
1921}
1922
1923static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001924find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1925{
1926 struct list_head *tbl = nn->conf_id_hashtbl;
1927
1928 return find_client_in_id_table(tbl, clid, sessions);
1929}
1930
1931static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001932find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001934 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001936 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937}
1938
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001939static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001940{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001941 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001942}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001943
NeilBrown28ce6052005-06-23 22:03:56 -07001944static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001945find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001946{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001947 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001948}
1949
1950static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001951find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001952{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001953 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001954}
1955
NeilBrownfd39ca92005-06-23 22:04:03 -07001956static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001957gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001959 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001960 struct sockaddr *sa = svc_addr(rqstp);
1961 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001962 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Jeff Layton7077ecb2009-08-14 12:57:58 -04001964 /* Currently, we only support tcp and tcp6 for the callback channel */
1965 if (se->se_callback_netid_len == 3 &&
1966 !memcmp(se->se_callback_netid_val, "tcp", 3))
1967 expected_family = AF_INET;
1968 else if (se->se_callback_netid_len == 4 &&
1969 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1970 expected_family = AF_INET6;
1971 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 goto out_err;
1973
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001974 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001975 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001976 (struct sockaddr *)&conn->cb_addr,
1977 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001978
J. Bruce Fields07263f12010-05-31 19:09:40 -04001979 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001981
J. Bruce Fields07263f12010-05-31 19:09:40 -04001982 if (conn->cb_addr.ss_family == AF_INET6)
1983 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001984
J. Bruce Fields07263f12010-05-31 19:09:40 -04001985 conn->cb_prog = se->se_callback_prog;
1986 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001987 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 return;
1989out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001990 conn->cb_addr.ss_family = AF_UNSPEC;
1991 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001992 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 "will not receive delegations\n",
1994 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 return;
1997}
1998
Andy Adamson074fe892009-04-03 08:28:15 +03001999/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002000 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002001 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002002static void
Andy Adamson074fe892009-04-03 08:28:15 +03002003nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2004{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002005 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002006 struct nfsd4_slot *slot = resp->cstate.slot;
2007 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002008
Andy Adamson557ce262009-08-28 08:45:04 -04002009 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002010
Andy Adamson557ce262009-08-28 08:45:04 -04002011 slot->sl_opcnt = resp->opcnt;
2012 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002013
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002014 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002015 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002016 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002017 return;
2018 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002019 base = resp->cstate.data_offset;
2020 slot->sl_datalen = buf->len - base;
2021 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04002022 WARN("%s: sessions DRC could not cache compound\n", __func__);
2023 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002024}
2025
2026/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002027 * Encode the replay sequence operation from the slot values.
2028 * If cachethis is FALSE encode the uncached rep error on the next
2029 * operation which sets resp->p and increments resp->opcnt for
2030 * nfs4svc_encode_compoundres.
2031 *
Andy Adamson074fe892009-04-03 08:28:15 +03002032 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002033static __be32
2034nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2035 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002036{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002037 struct nfsd4_op *op;
2038 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002039
Andy Adamsonabfabf82009-07-23 19:02:18 -04002040 /* Encode the replayed sequence operation */
2041 op = &args->ops[resp->opcnt - 1];
2042 nfsd4_encode_operation(resp, op);
2043
2044 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002045 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002046 op = &args->ops[resp->opcnt++];
2047 op->status = nfserr_retry_uncached_rep;
2048 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002049 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002050 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002051}
2052
2053/*
Andy Adamson557ce262009-08-28 08:45:04 -04002054 * The sequence operation is not cached because we can use the slot and
2055 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002056 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002057static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002058nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2059 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002060{
Andy Adamson557ce262009-08-28 08:45:04 -04002061 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002062 struct xdr_stream *xdr = &resp->xdr;
2063 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002064 __be32 status;
2065
Andy Adamson557ce262009-08-28 08:45:04 -04002066 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002067
Andy Adamsonabfabf82009-07-23 19:02:18 -04002068 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002069 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002070 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002071
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002072 p = xdr_reserve_space(xdr, slot->sl_datalen);
2073 if (!p) {
2074 WARN_ON_ONCE(1);
2075 return nfserr_serverfault;
2076 }
2077 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2078 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002079
Andy Adamson557ce262009-08-28 08:45:04 -04002080 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002081 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002082}
2083
Andy Adamson0733d212009-04-03 08:28:01 +03002084/*
2085 * Set the exchange_id flags returned by the server.
2086 */
2087static void
2088nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2089{
2090 /* pNFS is not supported */
2091 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2092
2093 /* Referrals are supported, Migration is not. */
2094 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2095
2096 /* set the wire flags to return to client. */
2097 clid->flags = new->cl_exchange_flags;
2098}
2099
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002100static bool client_has_state(struct nfs4_client *clp)
2101{
2102 /*
2103 * Note clp->cl_openowners check isn't quite right: there's no
2104 * need to count owners without stateid's.
2105 *
2106 * Also note we should probably be using this in 4.0 case too.
2107 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002108 return !list_empty(&clp->cl_openowners)
2109 || !list_empty(&clp->cl_delegations)
2110 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002111}
2112
Al Virob37ad282006-10-19 23:28:59 -07002113__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002114nfsd4_exchange_id(struct svc_rqst *rqstp,
2115 struct nfsd4_compound_state *cstate,
2116 struct nfsd4_exchange_id *exid)
2117{
Andy Adamson0733d212009-04-03 08:28:01 +03002118 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002119 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002120 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002121 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002122 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002123 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002124 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002125
Jeff Layton363168b2009-08-14 12:57:56 -04002126 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002127 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002128 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002129 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002130 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002131
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002132 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002133 return nfserr_inval;
2134
Andy Adamson0733d212009-04-03 08:28:01 +03002135 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002136 case SP4_MACH_CRED:
2137 if (!svc_rqst_integrity_protected(rqstp))
2138 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002139 case SP4_NONE:
2140 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002141 default: /* checked by xdr code */
2142 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002143 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002144 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002145 }
2146
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002147 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03002148 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002149 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002150 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002151 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2152 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2153
J. Bruce Fields136e6582012-05-12 20:37:23 -04002154 if (update) {
2155 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002156 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002157 goto out;
2158 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002159 if (!mach_creds_match(conf, rqstp)) {
2160 status = nfserr_wrong_cred;
2161 goto out;
2162 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002163 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002164 status = nfserr_perm;
2165 goto out;
2166 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002167 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002168 status = nfserr_not_same;
2169 goto out;
2170 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002171 /* case 6 */
2172 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2173 new = conf;
2174 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002175 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002176 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002177 if (client_has_state(conf)) {
2178 status = nfserr_clid_inuse;
2179 goto out;
2180 }
Andy Adamson0733d212009-04-03 08:28:01 +03002181 expire_client(conf);
2182 goto out_new;
2183 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002184 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002185 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002186 new = conf;
2187 goto out_copy;
2188 }
2189 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04002190 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002191 }
2192
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002193 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002194 status = nfserr_noent;
2195 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002196 }
2197
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002198 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002199 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03002200 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002201
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002202 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002203out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05002204 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03002205 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04002206 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03002207 goto out;
2208 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002209 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002210 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002211
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002212 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002213 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03002214out_copy:
2215 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2216 exid->clientid.cl_id = new->cl_clientid.cl_id;
2217
J. Bruce Fields778df3f2012-05-25 21:40:23 -04002218 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03002219 nfsd4_set_ex_flags(new, exid);
2220
2221 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04002222 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002223 status = nfs_ok;
2224
2225out:
2226 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03002227 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002228}
2229
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002230static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002231check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002232{
Andy Adamson88e588d2009-07-23 19:02:15 -04002233 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2234 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002235
2236 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002237 if (slot_inuse) {
2238 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002239 return nfserr_jukebox;
2240 else
2241 return nfserr_seq_misordered;
2242 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002243 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002244 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002245 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002246 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002247 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002248 return nfserr_seq_misordered;
2249}
2250
Andy Adamson49557cc2009-07-23 19:02:16 -04002251/*
2252 * Cache the create session result into the create session single DRC
2253 * slot cache by saving the xdr structure. sl_seqid has been set.
2254 * Do this for solo or embedded create session operations.
2255 */
2256static void
2257nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002258 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002259{
2260 slot->sl_status = nfserr;
2261 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2262}
2263
2264static __be32
2265nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2266 struct nfsd4_clid_slot *slot)
2267{
2268 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2269 return slot->sl_status;
2270}
2271
Mi Jinlong1b74c252011-07-14 14:50:17 +08002272#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2273 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2274 1 + /* MIN tag is length with zero, only length */ \
2275 3 + /* version, opcount, opcode */ \
2276 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2277 /* seqid, slotID, slotID, cache */ \
2278 4 ) * sizeof(__be32))
2279
2280#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2281 2 + /* verifier: AUTH_NULL, length 0 */\
2282 1 + /* status */ \
2283 1 + /* MIN tag is length with zero, only length */ \
2284 3 + /* opcount, opcode, opstatus*/ \
2285 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2286 /* seqid, slotID, slotID, slotID, status */ \
2287 5 ) * sizeof(__be32))
2288
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002289static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002290{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002291 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2292
J. Bruce Fields373cd402013-04-08 15:42:12 -04002293 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2294 return nfserr_toosmall;
2295 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2296 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002297 ca->headerpadsz = 0;
2298 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2299 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2300 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2301 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2302 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2303 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2304 /*
2305 * Note decreasing slot size below client's request may make it
2306 * difficult for client to function correctly, whereas
2307 * decreasing the number of slots will (just?) affect
2308 * performance. When short on memory we therefore prefer to
2309 * decrease number of slots instead of their size. Clients that
2310 * request larger slots than they need will get poor results:
2311 */
2312 ca->maxreqs = nfsd4_get_drc_mem(ca);
2313 if (!ca->maxreqs)
2314 return nfserr_jukebox;
2315
J. Bruce Fields373cd402013-04-08 15:42:12 -04002316 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002317}
2318
Kinglong Mee8a891632013-12-23 18:11:02 +08002319#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2320 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2321#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2322 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2323
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002324static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2325{
2326 ca->headerpadsz = 0;
2327
2328 /*
2329 * These RPC_MAX_HEADER macros are overkill, especially since we
2330 * don't even do gss on the backchannel yet. But this is still
2331 * less than 1k. Tighten up this estimate in the unlikely event
2332 * it turns out to be a problem for some client:
2333 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002334 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002335 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002336 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002337 return nfserr_toosmall;
2338 ca->maxresp_cached = 0;
2339 if (ca->maxops < 2)
2340 return nfserr_toosmall;
2341
2342 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002343}
2344
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002345static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2346{
2347 switch (cbs->flavor) {
2348 case RPC_AUTH_NULL:
2349 case RPC_AUTH_UNIX:
2350 return nfs_ok;
2351 default:
2352 /*
2353 * GSS case: the spec doesn't allow us to return this
2354 * error. But it also doesn't allow us not to support
2355 * GSS.
2356 * I'd rather this fail hard than return some error the
2357 * client might think it can already handle:
2358 */
2359 return nfserr_encr_alg_unsupp;
2360 }
2361}
2362
Andy Adamson069b6ad2009-04-03 08:27:58 +03002363__be32
2364nfsd4_create_session(struct svc_rqst *rqstp,
2365 struct nfsd4_compound_state *cstate,
2366 struct nfsd4_create_session *cr_ses)
2367{
Jeff Layton363168b2009-08-14 12:57:56 -04002368 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002369 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002370 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002371 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002372 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002373 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002374 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002375
Mi Jinlonga62573d2011-03-23 17:57:07 +08002376 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2377 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002378 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2379 if (status)
2380 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002381 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002382 if (status)
2383 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002384 status = check_backchannel_attrs(&cr_ses->back_channel);
2385 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002386 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002387 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002388 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002389 if (!new)
2390 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002391 conn = alloc_conn_from_crses(rqstp, cr_ses);
2392 if (!conn)
2393 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002394
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002395 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002396 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002397 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002398 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002399
2400 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002401 status = nfserr_wrong_cred;
2402 if (!mach_creds_match(conf, rqstp))
2403 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002404 cs_slot = &conf->cl_cs_slot;
2405 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002406 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002407 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002408 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002409 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002410 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002411 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002412 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002413 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002414 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002415 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002416 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002417 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002418 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002419 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002420 status = nfserr_wrong_cred;
2421 if (!mach_creds_match(unconf, rqstp))
2422 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002423 cs_slot = &unconf->cl_cs_slot;
2424 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002425 if (status) {
2426 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002427 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002428 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002429 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002430 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002431 if (old) {
2432 status = mark_client_expired(old);
2433 if (status)
2434 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002435 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002436 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002437 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002438 conf = unconf;
2439 } else {
2440 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002441 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002442 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002443 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002444 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002445 * We do not support RDMA or persistent sessions
2446 */
2447 cr_ses->flags &= ~SESSION4_PERSIST;
2448 cr_ses->flags &= ~SESSION4_RDMA;
2449
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002450 init_session(rqstp, new, conf, cr_ses);
2451 nfsd4_init_conn(rqstp, conn, new);
2452
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002453 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002454 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002455 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002456 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002457
Andy Adamson49557cc2009-07-23 19:02:16 -04002458 /* cache solo and embedded create sessions under the state lock */
2459 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002460 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002461 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002462out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08002463 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002464 free_conn(conn);
2465out_free_session:
2466 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002467out_release_drc_mem:
2468 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002469 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002470}
2471
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002472static __be32 nfsd4_map_bcts_dir(u32 *dir)
2473{
2474 switch (*dir) {
2475 case NFS4_CDFC4_FORE:
2476 case NFS4_CDFC4_BACK:
2477 return nfs_ok;
2478 case NFS4_CDFC4_FORE_OR_BOTH:
2479 case NFS4_CDFC4_BACK_OR_BOTH:
2480 *dir = NFS4_CDFC4_BOTH;
2481 return nfs_ok;
2482 };
2483 return nfserr_inval;
2484}
2485
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002486__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2487{
2488 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002489 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002490 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002491
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002492 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2493 if (status)
2494 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002495 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002496 session->se_cb_prog = bc->bc_cb_program;
2497 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002498 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002499
2500 nfsd4_probe_callback(session->se_client);
2501
2502 return nfs_ok;
2503}
2504
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002505__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2506 struct nfsd4_compound_state *cstate,
2507 struct nfsd4_bind_conn_to_session *bcts)
2508{
2509 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002510 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002511 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002512 struct net *net = SVC_NET(rqstp);
2513 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002514
2515 if (!nfsd4_last_compound_op(rqstp))
2516 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002517 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002518 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002519 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002520 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002521 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002522 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002523 status = nfserr_wrong_cred;
2524 if (!mach_creds_match(session->se_client, rqstp))
2525 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002526 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002527 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002528 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002529 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002530 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002531 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002532 goto out;
2533 nfsd4_init_conn(rqstp, conn, session);
2534 status = nfs_ok;
2535out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002536 nfsd4_put_session(session);
2537out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002538 nfs4_unlock_state();
2539 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002540}
2541
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002542static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2543{
2544 if (!session)
2545 return 0;
2546 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2547}
2548
Andy Adamson069b6ad2009-04-03 08:27:58 +03002549__be32
2550nfsd4_destroy_session(struct svc_rqst *r,
2551 struct nfsd4_compound_state *cstate,
2552 struct nfsd4_destroy_session *sessionid)
2553{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002554 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002555 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002556 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002557 struct net *net = SVC_NET(r);
2558 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002559
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002560 nfs4_lock_state();
2561 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002562 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002563 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002564 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002565 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002566 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002567 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002568 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002569 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002570 if (!ses)
2571 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002572 status = nfserr_wrong_cred;
2573 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002574 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002575 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002576 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002577 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002578 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002579 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002580
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002581 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002582
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002583 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002584 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002585out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002586 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002587out_client_lock:
2588 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002589out:
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002590 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002591 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002592}
2593
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002594static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002595{
2596 struct nfsd4_conn *c;
2597
2598 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002599 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002600 return c;
2601 }
2602 }
2603 return NULL;
2604}
2605
J. Bruce Fields57266a62013-04-13 14:27:29 -04002606static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002607{
2608 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002609 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002610 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002611 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002612
2613 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002614 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002615 if (c)
2616 goto out_free;
2617 status = nfserr_conn_not_bound_to_session;
2618 if (clp->cl_mach_cred)
2619 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002620 __nfsd4_hash_conn(new, ses);
2621 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002622 ret = nfsd4_register_conn(new);
2623 if (ret)
2624 /* oops; xprt is already down: */
2625 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002626 return nfs_ok;
2627out_free:
2628 spin_unlock(&clp->cl_lock);
2629 free_conn(new);
2630 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002631}
2632
Mi Jinlong868b89c2011-04-27 09:09:58 +08002633static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2634{
2635 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2636
2637 return args->opcnt > session->se_fchannel.maxops;
2638}
2639
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002640static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2641 struct nfsd4_session *session)
2642{
2643 struct xdr_buf *xb = &rqstp->rq_arg;
2644
2645 return xb->len > session->se_fchannel.maxreq_sz;
2646}
2647
Andy Adamson069b6ad2009-04-03 08:27:58 +03002648__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002649nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002650 struct nfsd4_compound_state *cstate,
2651 struct nfsd4_sequence *seq)
2652{
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002653 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002654 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002655 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002656 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002657 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002658 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002659 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002660 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002661 struct net *net = SVC_NET(rqstp);
2662 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002663
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002664 if (resp->opcnt != 1)
2665 return nfserr_sequence_pos;
2666
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002667 /*
2668 * Will be either used or freed by nfsd4_sequence_check_conn
2669 * below.
2670 */
2671 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2672 if (!conn)
2673 return nfserr_jukebox;
2674
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002675 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002676 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002677 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002678 goto out_no_session;
2679 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002680
Mi Jinlong868b89c2011-04-27 09:09:58 +08002681 status = nfserr_too_many_ops;
2682 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002683 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002684
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002685 status = nfserr_req_too_big;
2686 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002687 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002688
Benny Halevyb85d4c02009-04-03 08:28:08 +03002689 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002690 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002691 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002692
Andy Adamson557ce262009-08-28 08:45:04 -04002693 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002694 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2695
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002696 /* We do not negotiate the number of slots yet, so set the
2697 * maxslots to the session maxreqs which is used to encode
2698 * sr_highest_slotid and the sr_target_slot id to maxslots */
2699 seq->maxslots = session->se_fchannel.maxreqs;
2700
J. Bruce Fields73e79482012-02-13 16:39:00 -05002701 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2702 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002703 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002704 status = nfserr_seq_misordered;
2705 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002706 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002707 cstate->slot = slot;
2708 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002709 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002710 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002711 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002712 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002713 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002714 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002715 }
2716 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002717 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002718
J. Bruce Fields57266a62013-04-13 14:27:29 -04002719 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002720 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002721 if (status)
2722 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002723
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002724 buflen = (seq->cachethis) ?
2725 session->se_fchannel.maxresp_cached :
2726 session->se_fchannel.maxresp_sz;
2727 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2728 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002729 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002730 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002731 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002732
2733 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002734 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002735 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002736 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002737 if (seq->cachethis)
2738 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002739 else
2740 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002741
2742 cstate->slot = slot;
2743 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002744 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002745
Benny Halevyb85d4c02009-04-03 08:28:08 +03002746out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002747 switch (clp->cl_cb_state) {
2748 case NFSD4_CB_DOWN:
2749 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2750 break;
2751 case NFSD4_CB_FAULT:
2752 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2753 break;
2754 default:
2755 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002756 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002757 if (!list_empty(&clp->cl_revoked))
2758 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002759out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002760 if (conn)
2761 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002762 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002763 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002764out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002765 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002766 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002767}
2768
Trond Myklebustb6076642014-06-30 11:48:35 -04002769void
2770nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2771{
2772 struct nfsd4_compound_state *cs = &resp->cstate;
2773
2774 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002775 if (cs->status != nfserr_replay_cache) {
2776 nfsd4_store_cache_entry(resp);
2777 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2778 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002779 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002780 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002781 } else if (cs->clp)
2782 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002783}
2784
Mi Jinlong345c2842011-10-20 17:51:39 +08002785__be32
2786nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2787{
2788 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002789 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002790 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002791
2792 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002793 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002794 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002795 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002796
2797 if (conf) {
2798 clp = conf;
2799
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002800 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002801 status = nfserr_clientid_busy;
2802 goto out;
2803 }
2804 } else if (unconf)
2805 clp = unconf;
2806 else {
2807 status = nfserr_stale_clientid;
2808 goto out;
2809 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002810 if (!mach_creds_match(clp, rqstp)) {
2811 status = nfserr_wrong_cred;
2812 goto out;
2813 }
Mi Jinlong345c2842011-10-20 17:51:39 +08002814 expire_client(clp);
2815out:
2816 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002817 return status;
2818}
2819
Andy Adamson069b6ad2009-04-03 08:27:58 +03002820__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002821nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2822{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002823 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002824
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002825 if (rc->rca_one_fs) {
2826 if (!cstate->current_fh.fh_dentry)
2827 return nfserr_nofilehandle;
2828 /*
2829 * We don't take advantage of the rca_one_fs case.
2830 * That's OK, it's optional, we can safely ignore it.
2831 */
2832 return nfs_ok;
2833 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002834
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002835 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002836 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002837 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2838 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002839 goto out;
2840
2841 status = nfserr_stale_clientid;
2842 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002843 /*
2844 * The following error isn't really legal.
2845 * But we only get here if the client just explicitly
2846 * destroyed the client. Surely it no longer cares what
2847 * error it gets back on an operation for the dead
2848 * client.
2849 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002850 goto out;
2851
2852 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002853 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002854out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002855 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002856 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002857}
2858
2859__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002860nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2861 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002863 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002865 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002866 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002867 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2868
J. Bruce Fields63db4632012-05-18 21:54:19 -04002869 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002871 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002872 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002873 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002875 if (clp_used_exchangeid(conf))
2876 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002877 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002878 char addr_str[INET6_ADDRSTRLEN];
2879 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2880 sizeof(addr_str));
2881 dprintk("NFSD: setclientid: string in use by client "
2882 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 goto out;
2884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002886 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002887 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002890 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002891 if (new == NULL)
2892 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002893 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002894 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002896 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002897 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002898 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002899 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002900 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2902 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2903 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2904 status = nfs_ok;
2905out:
2906 nfs4_unlock_state();
2907 return status;
2908}
2909
2910
Al Virob37ad282006-10-19 23:28:59 -07002911__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002912nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2913 struct nfsd4_compound_state *cstate,
2914 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915{
NeilBrown21ab45a2005-06-23 22:04:14 -07002916 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2918 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002919 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002920 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002922 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002925
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002926 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002927 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002928 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002929 * We try hard to give out unique clientid's, so if we get an
2930 * attempt to confirm the same clientid with a different cred,
2931 * there's a bug somewhere. Let's charitably assume it's our
2932 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002933 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002934 status = nfserr_serverfault;
2935 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2936 goto out;
2937 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2938 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002939 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002940 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2941 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002943 else /* case 4: client hasn't noticed we rebooted yet? */
2944 status = nfserr_stale_clientid;
2945 goto out;
2946 }
2947 status = nfs_ok;
2948 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002949 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2950 nfsd4_probe_callback(conf);
2951 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002952 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002953 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002954 if (conf) {
2955 status = mark_client_expired(conf);
2956 if (status)
2957 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002958 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002959 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002960 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002961 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 nfs4_unlock_state();
2965 return status;
2966}
2967
J. Bruce Fields32513b42011-10-13 16:00:16 -04002968static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002970 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2971}
2972
2973/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04002974static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04002975{
Trond Myklebustca943212014-07-23 16:17:39 -04002976 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
Trond Myklebust950e0112014-06-30 11:48:31 -04002978 lockdep_assert_held(&state_lock);
2979
J. Bruce Fields32513b42011-10-13 16:00:16 -04002980 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04002981 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002982 INIT_LIST_HEAD(&fp->fi_stateids);
2983 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04002984 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002985 fp->fi_had_conflict = false;
2986 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04002987 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002988 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2989 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04002990 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991}
2992
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002993void
NeilBrowne60d4392005-06-23 22:03:01 -07002994nfsd4_free_slabs(void)
2995{
Christoph Hellwigabf11352014-05-21 07:43:03 -07002996 kmem_cache_destroy(openowner_slab);
2997 kmem_cache_destroy(lockowner_slab);
2998 kmem_cache_destroy(file_slab);
2999 kmem_cache_destroy(stateid_slab);
3000 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003001}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Bryan Schumaker72083392011-11-01 15:24:59 -04003003int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004nfsd4_init_slabs(void)
3005{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003006 openowner_slab = kmem_cache_create("nfsd4_openowners",
3007 sizeof(struct nfs4_openowner), 0, 0, NULL);
3008 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003009 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003010 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003011 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003012 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003013 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003014 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003015 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003016 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003017 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003018 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003019 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003020 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003021 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003022 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003023 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003024 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003025 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003027
3028out_free_stateid_slab:
3029 kmem_cache_destroy(stateid_slab);
3030out_free_file_slab:
3031 kmem_cache_destroy(file_slab);
3032out_free_lockowner_slab:
3033 kmem_cache_destroy(lockowner_slab);
3034out_free_openowner_slab:
3035 kmem_cache_destroy(openowner_slab);
3036out:
NeilBrowne60d4392005-06-23 22:03:01 -07003037 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3038 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039}
3040
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003041static void init_nfs4_replay(struct nfs4_replay *rp)
3042{
3043 rp->rp_status = nfserr_serverfault;
3044 rp->rp_buflen = 0;
3045 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003046 mutex_init(&rp->rp_mutex);
3047}
3048
3049static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3050 struct nfs4_stateowner *so)
3051{
3052 if (!nfsd4_has_session(cstate)) {
3053 mutex_lock(&so->so_replay.rp_mutex);
3054 cstate->replay_owner = so;
3055 atomic_inc(&so->so_count);
3056 }
3057}
3058
3059void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3060{
3061 struct nfs4_stateowner *so = cstate->replay_owner;
3062
3063 if (so != NULL) {
3064 cstate->replay_owner = NULL;
3065 mutex_unlock(&so->so_replay.rp_mutex);
3066 nfs4_put_stateowner(so);
3067 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003068}
3069
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003070static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071{
3072 struct nfs4_stateowner *sop;
3073
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003074 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003075 if (!sop)
3076 return NULL;
3077
3078 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3079 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003080 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003081 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003083 sop->so_owner.len = owner->len;
3084
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003085 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003086 sop->so_client = clp;
3087 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003088 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003089 return sop;
3090}
3091
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003092static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003093{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003094 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003095
Trond Myklebustd4f04892014-07-29 21:34:36 -04003096 list_add(&oo->oo_owner.so_strhash,
3097 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003098 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099}
3100
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003101static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3102{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003103 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003104}
3105
Jeff Layton6b180f02014-07-29 21:34:26 -04003106static void nfs4_free_openowner(struct nfs4_stateowner *so)
3107{
3108 struct nfs4_openowner *oo = openowner(so);
3109
3110 kmem_cache_free(openowner_slab, oo);
3111}
3112
3113static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003114 .so_unhash = nfs4_unhash_openowner,
3115 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003116};
3117
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003118static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003119alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003120 struct nfsd4_compound_state *cstate)
3121{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003122 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003123 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003125 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3126 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003128 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003129 oo->oo_owner.so_is_open_owner = 1;
3130 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003131 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003132 if (nfsd4_has_session(cstate))
3133 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003134 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003135 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003136 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003137 spin_lock(&clp->cl_lock);
3138 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003139 if (ret == NULL) {
3140 hash_openowner(oo, clp, strhashval);
3141 ret = oo;
3142 } else
3143 nfs4_free_openowner(&oo->oo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003144 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003145 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146}
3147
J. Bruce Fields996e0932011-10-17 11:14:48 -04003148static 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 -04003149 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003151 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003152 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003153 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003154 stp->st_stateowner = &oo->oo_owner;
Jeff Laytond3134b12014-07-29 21:34:32 -04003155 atomic_inc(&stp->st_stateowner->so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07003156 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003157 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 stp->st_access_bmap = 0;
3159 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003160 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003161 spin_lock(&oo->oo_owner.so_client->cl_lock);
3162 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003163 spin_lock(&fp->fi_lock);
3164 list_add(&stp->st_perfile, &fp->fi_stateids);
3165 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003166 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167}
3168
Jeff Laytond3134b12014-07-29 21:34:32 -04003169/*
3170 * In the 4.0 case we need to keep the owners around a little while to handle
3171 * CLOSE replay. We still do need to release any file access that is held by
3172 * them before returning however.
3173 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003175move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176{
Jeff Laytond3134b12014-07-29 21:34:32 -04003177 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3178 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3179 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003180
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003181 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
Jeff Laytonb401be222014-07-29 21:34:33 -04003183 /*
3184 * We know that we hold one reference via nfsd4_close, and another
3185 * "persistent" reference for the client. If the refcount is higher
3186 * than 2, then there are still calls in progress that are using this
3187 * stateid. We can't put the sc_file reference until they are finished.
3188 * Wait for the refcount to drop to 2. Since it has been unhashed,
3189 * there should be no danger of the refcount going back up again at
3190 * this point.
3191 */
3192 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3193
Jeff Laytond3134b12014-07-29 21:34:32 -04003194 release_all_access(s);
3195 if (s->st_stid.sc_file) {
3196 put_nfs4_file(s->st_stid.sc_file);
3197 s->st_stid.sc_file = NULL;
3198 }
3199 release_last_closed_stateid(oo);
3200 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003201 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003202 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203}
3204
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205/* search file_hashtbl[] for file */
3206static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003207find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208{
Trond Myklebustca943212014-07-23 16:17:39 -04003209 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 struct nfs4_file *fp;
3211
Trond Myklebust950e0112014-06-30 11:48:31 -04003212 lockdep_assert_held(&state_lock);
3213
Jeff Layton89876f82013-04-02 09:01:59 -04003214 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003215 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003216 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 }
3220 return NULL;
3221}
3222
Trond Myklebust950e0112014-06-30 11:48:31 -04003223static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003224find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003225{
3226 struct nfs4_file *fp;
3227
3228 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003229 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003230 spin_unlock(&state_lock);
3231 return fp;
3232}
3233
3234static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003235find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003236{
3237 struct nfs4_file *fp;
3238
3239 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003240 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003241 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003242 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003243 fp = new;
3244 }
3245 spin_unlock(&state_lock);
3246
3247 return fp;
3248}
3249
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003250/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 * Called to check deny when READ with all zero stateid or
3252 * WRITE with all zero or all one stateid
3253 */
Al Virob37ad282006-10-19 23:28:59 -07003254static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3256{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003258 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
Trond Myklebustca943212014-07-23 16:17:39 -04003260 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003261 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003262 return ret;
3263 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003264 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003265 if (fp->fi_share_deny & deny_type)
3266 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003267 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003268 put_nfs4_file(fp);
3269 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270}
3271
Jeff Layton02e12152014-07-16 10:31:57 -04003272void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273{
Trond Myklebust11b91642014-07-29 21:34:08 -04003274 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3275 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003276
Trond Myklebust11b91642014-07-29 21:34:08 -04003277 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003278
Jeff Layton02e12152014-07-16 10:31:57 -04003279 /*
3280 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003281 * already holding inode->i_lock.
3282 *
Jeff Laytondff13992014-07-08 14:02:49 -04003283 * If the dl_time != 0, then we know that it has already been
3284 * queued for a lease break. Don't queue it again.
3285 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003286 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003287 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003288 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003289 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003290 }
Jeff Layton02e12152014-07-16 10:31:57 -04003291 spin_unlock(&state_lock);
3292}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
Jeff Layton02e12152014-07-16 10:31:57 -04003294static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3295{
3296 /*
3297 * We're assuming the state code never drops its reference
3298 * without first removing the lease. Since we're in this lease
3299 * callback (and since the lease code is serialized by the kernel
3300 * lock) we know the server hasn't removed the lease yet, we know
3301 * it's safe to take a reference.
3302 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003303 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003304 nfsd4_cb_recall(dp);
3305}
3306
Jeff Layton1c8c6012013-06-21 08:58:15 -04003307/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003308static void nfsd_break_deleg_cb(struct file_lock *fl)
3309{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003310 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3311 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003312
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003313 if (!fp) {
3314 WARN(1, "(%p)->fl_owner NULL\n", fl);
3315 return;
3316 }
3317 if (fp->fi_had_conflict) {
3318 WARN(1, "duplicate break on %p\n", fp);
3319 return;
3320 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003321 /*
3322 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003323 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003324 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003325 */
3326 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327
Jeff Layton02e12152014-07-16 10:31:57 -04003328 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003329 fp->fi_had_conflict = true;
3330 /*
3331 * If there are no delegations on the list, then we can't count on this
3332 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3333 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3334 * true should keep any new delegations from being hashed.
3335 */
3336 if (list_empty(&fp->fi_delegations))
3337 fl->fl_break_time = jiffies;
3338 else
3339 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3340 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003341 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342}
3343
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344static
3345int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3346{
3347 if (arg & F_UNLCK)
3348 return lease_modify(onlist, arg);
3349 else
3350 return -EAGAIN;
3351}
3352
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003353static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003354 .lm_break = nfsd_break_deleg_cb,
3355 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356};
3357
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003358static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3359{
3360 if (nfsd4_has_session(cstate))
3361 return nfs_ok;
3362 if (seqid == so->so_seqid - 1)
3363 return nfserr_replay_me;
3364 if (seqid == so->so_seqid)
3365 return nfs_ok;
3366 return nfserr_bad_seqid;
3367}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
Jeff Layton4b24ca72014-06-30 11:48:44 -04003369static __be32 lookup_clientid(clientid_t *clid,
3370 struct nfsd4_compound_state *cstate,
3371 struct nfsd_net *nn)
3372{
3373 struct nfs4_client *found;
3374
3375 if (cstate->clp) {
3376 found = cstate->clp;
3377 if (!same_clid(&found->cl_clientid, clid))
3378 return nfserr_stale_clientid;
3379 return nfs_ok;
3380 }
3381
3382 if (STALE_CLIENTID(clid, nn))
3383 return nfserr_stale_clientid;
3384
3385 /*
3386 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3387 * cached already then we know this is for is for v4.0 and "sessions"
3388 * will be false.
3389 */
3390 WARN_ON_ONCE(cstate->session);
3391 found = find_confirmed_client(clid, false, nn);
3392 if (!found)
3393 return nfserr_expired;
3394
3395 /* Cache the nfs4_client in cstate! */
3396 cstate->clp = found;
3397 atomic_inc(&found->cl_refcount);
3398 return nfs_ok;
3399}
3400
Al Virob37ad282006-10-19 23:28:59 -07003401__be32
Andy Adamson66689582009-04-03 08:28:45 +03003402nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003403 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 clientid_t *clientid = &open->op_clientid;
3406 struct nfs4_client *clp = NULL;
3407 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003408 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003409 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003411 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003413 /*
3414 * In case we need it later, after we've already created the
3415 * file and don't want to risk a further failure:
3416 */
3417 open->op_file = nfsd4_alloc_file();
3418 if (open->op_file == NULL)
3419 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420
Trond Myklebust2d91e892014-06-30 11:48:46 -04003421 status = lookup_clientid(clientid, cstate, nn);
3422 if (status)
3423 return status;
3424 clp = cstate->clp;
3425
Trond Myklebustd4f04892014-07-29 21:34:36 -04003426 strhashval = ownerstr_hashval(&open->op_owner);
3427 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003428 open->op_openowner = oo;
3429 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003430 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003432 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003433 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003434 release_openowner(oo);
3435 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003436 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003437 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003438 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3439 if (status)
3440 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003441 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003442new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003443 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003444 if (oo == NULL)
3445 return nfserr_jukebox;
3446 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003447alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003448 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003449 if (!open->op_stp)
3450 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003451 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452}
3453
Al Virob37ad282006-10-19 23:28:59 -07003454static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003455nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3456{
3457 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3458 return nfserr_openmode;
3459 else
3460 return nfs_ok;
3461}
3462
Daniel Mackc47d8322011-05-16 16:38:14 +02003463static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003464{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003465 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3466}
3467
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003468static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003469{
3470 struct nfs4_stid *ret;
3471
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003472 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003473 if (!ret)
3474 return NULL;
3475 return delegstateid(ret);
3476}
3477
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003478static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3479{
3480 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3481 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3482}
3483
Al Virob37ad282006-10-19 23:28:59 -07003484static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003485nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003486 struct nfs4_delegation **dp)
3487{
3488 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003489 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003490 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003491
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003492 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3493 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003494 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003495 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003496 status = nfs4_check_delegmode(deleg, flags);
3497 if (status) {
3498 nfs4_put_stid(&deleg->dl_stid);
3499 goto out;
3500 }
3501 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003502out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003503 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003504 return nfs_ok;
3505 if (status)
3506 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003507 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003508 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003509}
3510
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003511static struct nfs4_ol_stateid *
3512nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003514 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003515 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
Trond Myklebust1d31a252014-07-10 14:07:25 -04003517 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003518 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 /* ignore lock owners */
3520 if (local->st_stateowner->so_is_open_owner == 0)
3521 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003522 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003523 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003524 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003525 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003528 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003529 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530}
3531
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003532static inline int nfs4_access_to_access(u32 nfs4_access)
3533{
3534 int flags = 0;
3535
3536 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3537 flags |= NFSD_MAY_READ;
3538 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3539 flags |= NFSD_MAY_WRITE;
3540 return flags;
3541}
3542
Al Virob37ad282006-10-19 23:28:59 -07003543static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3545 struct nfsd4_open *open)
3546{
3547 struct iattr iattr = {
3548 .ia_valid = ATTR_SIZE,
3549 .ia_size = 0,
3550 };
3551 if (!open->op_truncate)
3552 return 0;
3553 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003554 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3556}
3557
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003558static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003559 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3560 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003561{
Trond Myklebustde186432014-07-10 14:07:26 -04003562 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003563 __be32 status;
3564 int oflag = nfs4_access_to_omode(open->op_share_access);
3565 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003566 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003567
Trond Myklebustde186432014-07-10 14:07:26 -04003568 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003569
3570 /*
3571 * Are we trying to set a deny mode that would conflict with
3572 * current access?
3573 */
3574 status = nfs4_file_check_deny(fp, open->op_share_deny);
3575 if (status != nfs_ok) {
3576 spin_unlock(&fp->fi_lock);
3577 goto out;
3578 }
3579
3580 /* set access to the file */
3581 status = nfs4_file_get_access(fp, open->op_share_access);
3582 if (status != nfs_ok) {
3583 spin_unlock(&fp->fi_lock);
3584 goto out;
3585 }
3586
3587 /* Set access bits in stateid */
3588 old_access_bmap = stp->st_access_bmap;
3589 set_access(open->op_share_access, stp);
3590
3591 /* Set new deny mask */
3592 old_deny_bmap = stp->st_deny_bmap;
3593 set_deny(open->op_share_deny, stp);
3594 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3595
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003596 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003597 spin_unlock(&fp->fi_lock);
3598 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003599 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003600 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003601 spin_lock(&fp->fi_lock);
3602 if (!fp->fi_fds[oflag]) {
3603 fp->fi_fds[oflag] = filp;
3604 filp = NULL;
3605 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003606 }
Trond Myklebustde186432014-07-10 14:07:26 -04003607 spin_unlock(&fp->fi_lock);
3608 if (filp)
3609 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003610
3611 status = nfsd4_truncate(rqstp, cur_fh, open);
3612 if (status)
3613 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003614out:
3615 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003616out_put_access:
3617 stp->st_access_bmap = old_access_bmap;
3618 nfs4_file_put_access(fp, open->op_share_access);
3619 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3620 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003621}
3622
Al Virob37ad282006-10-19 23:28:59 -07003623static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003624nfs4_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 -07003625{
Al Virob37ad282006-10-19 23:28:59 -07003626 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003627 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003629 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003630 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003631
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003632 /* test and set deny mode */
3633 spin_lock(&fp->fi_lock);
3634 status = nfs4_file_check_deny(fp, open->op_share_deny);
3635 if (status == nfs_ok) {
3636 old_deny_bmap = stp->st_deny_bmap;
3637 set_deny(open->op_share_deny, stp);
3638 fp->fi_share_deny |=
3639 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3640 }
3641 spin_unlock(&fp->fi_lock);
3642
3643 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003646 status = nfsd4_truncate(rqstp, cur_fh, open);
3647 if (status != nfs_ok)
3648 reset_union_bmap_deny(old_deny_bmap, stp);
3649 return status;
3650}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003653nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003655 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656}
3657
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003658/* Should we give out recallable state?: */
3659static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3660{
3661 if (clp->cl_cb_state == NFSD4_CB_UP)
3662 return true;
3663 /*
3664 * In the sessions case, since we don't have to establish a
3665 * separate connection for callbacks, we assume it's OK
3666 * until we hear otherwise:
3667 */
3668 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3669}
3670
Jeff Laytond564fbe2014-07-16 10:31:58 -04003671static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003672{
3673 struct file_lock *fl;
3674
3675 fl = locks_alloc_lock();
3676 if (!fl)
3677 return NULL;
3678 locks_init_lock(fl);
3679 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003680 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003681 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3682 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003683 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003684 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003685 return fl;
3686}
3687
J. Bruce Fields99c41512013-05-21 16:21:25 -04003688static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003689{
Trond Myklebust11b91642014-07-29 21:34:08 -04003690 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003691 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003692 struct file *filp;
3693 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003694
Jeff Laytond564fbe2014-07-16 10:31:58 -04003695 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003696 if (!fl)
3697 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003698 filp = find_readable_file(fp);
3699 if (!filp) {
3700 /* We should always have a readable file here */
3701 WARN_ON_ONCE(1);
3702 return -EBADF;
3703 }
3704 fl->fl_file = filp;
3705 status = vfs_setlease(filp, fl->fl_type, &fl);
3706 if (status) {
3707 locks_free_lock(fl);
3708 goto out_fput;
3709 }
Benny Halevycdc97502014-05-30 09:09:30 -04003710 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003711 spin_lock(&fp->fi_lock);
3712 /* Did the lease get broken before we took the lock? */
3713 status = -EAGAIN;
3714 if (fp->fi_had_conflict)
3715 goto out_unlock;
3716 /* Race breaker */
3717 if (fp->fi_lease) {
3718 status = 0;
3719 atomic_inc(&fp->fi_delegees);
3720 hash_delegation_locked(dp, fp);
3721 goto out_unlock;
3722 }
3723 fp->fi_lease = fl;
3724 fp->fi_deleg_file = filp;
3725 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003726 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003727 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003728 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003729 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003730out_unlock:
3731 spin_unlock(&fp->fi_lock);
3732 spin_unlock(&state_lock);
3733out_fput:
3734 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003735 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003736}
3737
Jeff Layton0b266932014-07-25 07:34:25 -04003738static struct nfs4_delegation *
3739nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3740 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003741{
Jeff Layton0b266932014-07-25 07:34:25 -04003742 int status;
3743 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003744
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003745 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003746 return ERR_PTR(-EAGAIN);
3747
3748 dp = alloc_init_deleg(clp, fh);
3749 if (!dp)
3750 return ERR_PTR(-ENOMEM);
3751
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003752 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003753 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003754 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003755 dp->dl_stid.sc_file = fp;
Jeff Layton417c6622014-07-21 09:34:57 -04003756 if (!fp->fi_lease) {
3757 spin_unlock(&fp->fi_lock);
3758 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003759 status = nfs4_setlease(dp);
3760 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003761 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003762 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003763 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003764 status = -EAGAIN;
3765 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003766 }
Benny Halevy931ee562014-05-30 09:09:27 -04003767 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003768 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003769out_unlock:
3770 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003771 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003772out:
3773 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003774 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003775 return ERR_PTR(status);
3776 }
3777 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003778}
3779
Benny Halevy4aa89132012-02-21 14:16:44 -08003780static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3781{
3782 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3783 if (status == -EAGAIN)
3784 open->op_why_no_deleg = WND4_CONTENTION;
3785 else {
3786 open->op_why_no_deleg = WND4_RESOURCE;
3787 switch (open->op_deleg_want) {
3788 case NFS4_SHARE_WANT_READ_DELEG:
3789 case NFS4_SHARE_WANT_WRITE_DELEG:
3790 case NFS4_SHARE_WANT_ANY_DELEG:
3791 break;
3792 case NFS4_SHARE_WANT_CANCEL:
3793 open->op_why_no_deleg = WND4_CANCELLED;
3794 break;
3795 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003796 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003797 }
3798 }
3799}
3800
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801/*
3802 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003803 *
3804 * Note we don't support write delegations, and won't until the vfs has
3805 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 */
3807static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003808nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3809 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810{
3811 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003812 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3813 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003814 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003815 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003817 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003818 open->op_recall = 0;
3819 switch (open->op_claim_type) {
3820 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003821 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003822 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003823 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3824 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003825 break;
3826 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003827 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003828 /*
3829 * Let's not give out any delegations till everyone's
3830 * had the chance to reclaim theirs....
3831 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003832 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003833 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003834 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003835 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003836 /*
3837 * Also, if the file was opened for write or
3838 * create, there's a good chance the client's
3839 * about to write to it, resulting in an
3840 * immediate recall (since we don't support
3841 * write delegations):
3842 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003843 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003844 goto out_no_deleg;
3845 if (open->op_create == NFS4_OPEN_CREATE)
3846 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003847 break;
3848 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003849 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003850 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003851 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003852 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003853 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003855 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003857 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003858 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003859 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003860 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003861 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003862out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003863 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3864 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003865 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003866 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003867 open->op_recall = 1;
3868 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003869
3870 /* 4.1 client asking for a delegation? */
3871 if (open->op_deleg_want)
3872 nfsd4_open_deleg_none_ext(open, status);
3873 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874}
3875
Benny Halevye27f49c2012-02-21 14:16:54 -08003876static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3877 struct nfs4_delegation *dp)
3878{
3879 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3880 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3881 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3882 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3883 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3884 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3885 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3886 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3887 }
3888 /* Otherwise the client must be confused wanting a delegation
3889 * it already has, therefore we don't return
3890 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3891 */
3892}
3893
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894/*
3895 * called with nfs4_lock_state() held.
3896 */
Al Virob37ad282006-10-19 23:28:59 -07003897__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3899{
Andy Adamson66689582009-04-03 08:28:45 +03003900 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003901 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003903 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003904 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003905 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 /*
3908 * Lookup file; if found, lookup stateid and check open request,
3909 * and check for delegations in the process of being recalled.
3910 * If not found, create the nfs4_file struct
3911 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003912 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04003913 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04003914 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003915 if (status)
3916 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003917 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04003919 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003920 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003921 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003922 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003923 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 }
3925
3926 /*
3927 * OPEN the file, or upgrade an existing OPEN.
3928 * If truncate fails, the OPEN fails.
3929 */
3930 if (stp) {
3931 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003932 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 if (status)
3934 goto out;
3935 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003936 stp = open->op_stp;
3937 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003938 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003939 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3940 if (status) {
3941 release_open_stateid(stp);
3942 goto out;
3943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003945 update_stateid(&stp->st_stid.sc_stateid);
3946 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947
Benny Halevyd24433c2012-02-16 20:57:17 +02003948 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02003949 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3950 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3951 open->op_why_no_deleg = WND4_NOT_WANTED;
3952 goto nodeleg;
3953 }
3954 }
3955
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 /*
3957 * Attempt to hand out a delegation. No error return, because the
3958 * OPEN succeeds even if we fail.
3959 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003960 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003961nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 status = nfs_ok;
3963
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003964 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003965 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003967 /* 4.1 client trying to upgrade/downgrade delegation? */
3968 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003969 open->op_deleg_want)
3970 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003971
NeilBrown13cd2182005-06-23 22:03:10 -07003972 if (fp)
3973 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003974 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003975 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 /*
3977 * To finish the open response, we just need to set the rflags.
3978 */
3979 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003980 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003981 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003983 if (dp)
3984 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003985 if (stp)
3986 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987
3988 return status;
3989}
3990
Jeff Layton58fb12e2014-07-29 21:34:27 -04003991void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
3992 struct nfsd4_open *open, __be32 status)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003993{
3994 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04003995 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003996
Jeff Laytond3134b12014-07-29 21:34:32 -04003997 nfsd4_cstate_assign_replay(cstate, so);
3998 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003999 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004000 if (open->op_file)
4001 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004002 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004003 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004004}
4005
Al Virob37ad282006-10-19 23:28:59 -07004006__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004007nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4008 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009{
4010 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004011 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004012 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013
4014 nfs4_lock_state();
4015 dprintk("process_renew(%08x/%08x): starting\n",
4016 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004017 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004018 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004020 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004022 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004023 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 goto out;
4025 status = nfs_ok;
4026out:
4027 nfs4_unlock_state();
4028 return status;
4029}
4030
NeilBrowna76b4312005-06-23 22:04:01 -07004031static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004032nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004033{
Jeff Layton33dcc482012-04-10 11:08:48 -04004034 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004035 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004036 return;
4037
NeilBrowna76b4312005-06-23 22:04:01 -07004038 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004039 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004040 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004041 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004042 /*
4043 * Now that every NFSv4 client has had the chance to recover and
4044 * to see the (possibly new, possibly shorter) lease time, we
4045 * can safely set the next grace time to the current lease time:
4046 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03004047 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07004048}
4049
NeilBrownfd39ca92005-06-23 22:04:03 -07004050static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004051nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052{
4053 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004054 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 struct nfs4_delegation *dp;
4056 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004057 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004058 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059
4060 nfs4_lock_state();
4061
4062 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004063 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004064 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004065 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004066 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 clp = list_entry(pos, struct nfs4_client, cl_lru);
4068 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4069 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004070 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 break;
4072 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004073 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004074 dprintk("NFSD: client in use (clientid %08x)\n",
4075 clp->cl_clientid.cl_id);
4076 continue;
4077 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004078 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004079 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004080 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004081 list_for_each_safe(pos, next, &reaplist) {
4082 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 dprintk("NFSD: purging unused client (clientid %08x)\n",
4084 clp->cl_clientid.cl_id);
4085 expire_client(clp);
4086 }
Benny Halevycdc97502014-05-30 09:09:30 -04004087 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004088 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004090 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4091 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004093 t = dp->dl_time - cutoff;
4094 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 break;
4096 }
Jeff Layton42690672014-07-25 07:34:20 -04004097 unhash_delegation_locked(dp);
4098 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 }
Benny Halevycdc97502014-05-30 09:09:30 -04004100 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004101 while (!list_empty(&reaplist)) {
4102 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4103 dl_recall_lru);
4104 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004105 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004107 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004108 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
4109 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004110 t = oo->oo_time - cutoff;
4111 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 break;
4113 }
Jeff Laytond3134b12014-07-29 21:34:32 -04004114 release_last_closed_stateid(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04004116 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04004118 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119}
4120
Harvey Harrisona254b242008-02-20 12:49:00 -08004121static struct workqueue_struct *laundry_wq;
4122static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004123
4124static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004125laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126{
4127 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004128 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4129 work);
4130 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4131 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004133 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004135 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136}
4137
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004138static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004139{
Trond Myklebust11b91642014-07-29 21:34:08 -04004140 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004141 return nfserr_bad_stateid;
4142 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143}
4144
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004146access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004148 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4149 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4150 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151}
4152
4153static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004154access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004156 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4157 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158}
4159
4160static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004161__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162{
Al Virob37ad282006-10-19 23:28:59 -07004163 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
J. Bruce Fields02921912010-07-29 15:16:59 -04004165 /* For lock stateid's, we test the parent open, not the lock: */
4166 if (stp->st_openstp)
4167 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004168 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004170 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 goto out;
4172 status = nfs_ok;
4173out:
4174 return status;
4175}
4176
Al Virob37ad282006-10-19 23:28:59 -07004177static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004178check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004180 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004182 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004183 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 * conflicting state; so we must wait out the grace period. */
4185 return nfserr_grace;
4186 } else if (flags & WR_STATE)
4187 return nfs4_share_conflict(current_fh,
4188 NFS4_SHARE_DENY_WRITE);
4189 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4190 return nfs4_share_conflict(current_fh,
4191 NFS4_SHARE_DENY_READ);
4192}
4193
4194/*
4195 * Allow READ/WRITE during grace period on recovered state only for files
4196 * that are not able to provide mandatory locking.
4197 */
4198static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004199grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004201 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202}
4203
J. Bruce Fields81b82962011-08-23 11:03:29 -04004204/* Returns true iff a is later than b: */
4205static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4206{
Jim Rees1a9357f2013-05-17 17:33:00 -04004207 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004208}
4209
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004210static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004211{
Andy Adamson66689582009-04-03 08:28:45 +03004212 /*
4213 * When sessions are used the stateid generation number is ignored
4214 * when it is zero.
4215 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004216 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004217 return nfs_ok;
4218
4219 if (in->si_generation == ref->si_generation)
4220 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004221
J. Bruce Fields0836f582008-01-26 19:08:12 -05004222 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004223 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004224 return nfserr_bad_stateid;
4225 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004226 * However, we could see a stateid from the past, even from a
4227 * non-buggy client. For example, if the client sends a lock
4228 * while some IO is outstanding, the lock may bump si_generation
4229 * while the IO is still in flight. The client could avoid that
4230 * situation by waiting for responses on all the IO requests,
4231 * but better performance may result in retrying IO that
4232 * receives an old_stateid error if requests are rarely
4233 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004234 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004235 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004236}
4237
Chuck Lever7df302f2012-05-29 13:56:37 -04004238static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004239{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004240 struct nfs4_stid *s;
4241 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004242 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004243
Chuck Lever7df302f2012-05-29 13:56:37 -04004244 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004245 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004246 /* Client debugging aid. */
4247 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4248 char addr_str[INET6_ADDRSTRLEN];
4249 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4250 sizeof(addr_str));
4251 pr_warn_ratelimited("NFSD: client %s testing state ID "
4252 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004253 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004254 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004255 spin_lock(&cl->cl_lock);
4256 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004257 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004258 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004259 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004260 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004261 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004262 switch (s->sc_type) {
4263 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004264 status = nfs_ok;
4265 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004266 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004267 status = nfserr_deleg_revoked;
4268 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004269 case NFS4_OPEN_STID:
4270 case NFS4_LOCK_STID:
4271 ols = openlockstateid(s);
4272 if (ols->st_stateowner->so_is_open_owner
4273 && !(openowner(ols->st_stateowner)->oo_flags
4274 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004275 status = nfserr_bad_stateid;
4276 else
4277 status = nfs_ok;
4278 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004279 default:
4280 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004281 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004282 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004283 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004284 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004285 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004286out_unlock:
4287 spin_unlock(&cl->cl_lock);
4288 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004289}
4290
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004291static __be32
4292nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4293 stateid_t *stateid, unsigned char typemask,
4294 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004295{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004296 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004297
4298 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4299 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004300 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004301 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004302 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004303 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004304 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004305 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004306 if (status)
4307 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004308 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004309 if (!*s)
4310 return nfserr_bad_stateid;
4311 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004312}
4313
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314/*
4315* Checks for stateid operations
4316*/
Al Virob37ad282006-10-19 23:28:59 -07004317__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004318nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004319 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004321 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004322 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004324 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004326 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004327 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004328 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 if (filpp)
4331 *filpp = NULL;
4332
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004333 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 return nfserr_grace;
4335
4336 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004337 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338
Trond Myklebust14bcab12014-04-18 14:44:07 -04004339 nfs4_lock_state();
4340
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004341 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004342 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004343 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004344 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004345 goto unlock_state;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004346 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4347 if (status)
4348 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004349 switch (s->sc_type) {
4350 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004351 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004352 status = nfs4_check_delegmode(dp, flags);
4353 if (status)
4354 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004355 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004356 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004357 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004358 WARN_ON_ONCE(1);
4359 status = nfserr_serverfault;
4360 goto out;
4361 }
Trond Myklebustde186432014-07-10 14:07:26 -04004362 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004363 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004364 break;
4365 case NFS4_OPEN_STID:
4366 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004367 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004368 status = nfs4_check_fh(current_fh, stp);
4369 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004371 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004372 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004374 status = nfs4_check_openmode(stp, flags);
4375 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004377 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004378 struct nfs4_file *fp = stp->st_stid.sc_file;
4379
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004380 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004381 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004382 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004383 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004384 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004385 break;
4386 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004387 status = nfserr_bad_stateid;
4388 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 }
4390 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004391 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004392 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004394 nfs4_put_stid(s);
4395unlock_state:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004396 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 return status;
4398}
4399
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004400static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004401nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004402{
J. Bruce Fieldsa1b8ff42014-05-20 15:55:21 -04004403 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4404
Trond Myklebust11b91642014-07-29 21:34:08 -04004405 if (check_for_locks(stp->st_stid.sc_file, lo))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004406 return nfserr_locks_held;
Jeff Layton5adfd882014-07-29 21:34:31 -04004407 release_lock_stateid(stp);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004408 return nfs_ok;
4409}
4410
4411/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004412 * Test if the stateid is valid
4413 */
4414__be32
4415nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4416 struct nfsd4_test_stateid *test_stateid)
4417{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004418 struct nfsd4_test_stateid_id *stateid;
4419 struct nfs4_client *cl = cstate->session->se_client;
4420
4421 nfs4_lock_state();
4422 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004423 stateid->ts_id_status =
4424 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004425 nfs4_unlock_state();
4426
Bryan Schumaker17456802011-07-13 10:50:48 -04004427 return nfs_ok;
4428}
4429
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004430__be32
4431nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4432 struct nfsd4_free_stateid *free_stateid)
4433{
4434 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004435 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004436 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004437 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004438 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004439
4440 nfs4_lock_state();
Jeff Layton1af71cc2014-07-29 21:34:14 -04004441 spin_lock(&cl->cl_lock);
4442 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004443 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004444 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004445 switch (s->sc_type) {
4446 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004447 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004448 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004449 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004450 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4451 if (ret)
4452 break;
4453 ret = nfserr_locks_held;
4454 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004455 case NFS4_LOCK_STID:
4456 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4457 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004458 break;
4459 spin_unlock(&cl->cl_lock);
4460 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4461 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004462 case NFS4_REVOKED_DELEG_STID:
4463 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004464 list_del_init(&dp->dl_recall_lru);
4465 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004466 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004467 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004468 goto out;
4469 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004470 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004471out_unlock:
4472 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004473out:
4474 nfs4_unlock_state();
4475 return ret;
4476}
4477
NeilBrown4c4cd222005-07-07 17:59:27 -07004478static inline int
4479setlkflg (int type)
4480{
4481 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4482 RD_STATE : WR_STATE;
4483}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004485static __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 -04004486{
4487 struct svc_fh *current_fh = &cstate->current_fh;
4488 struct nfs4_stateowner *sop = stp->st_stateowner;
4489 __be32 status;
4490
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004491 status = nfsd4_check_seqid(cstate, sop, seqid);
4492 if (status)
4493 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004494 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4495 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004496 /*
4497 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004498 * nfserr_replay_me from the previous step, and
4499 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004500 */
4501 return nfserr_bad_stateid;
4502 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4503 if (status)
4504 return status;
4505 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004506}
4507
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508/*
4509 * Checks for sequence id mutating operations.
4510 */
Al Virob37ad282006-10-19 23:28:59 -07004511static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004512nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004513 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004514 struct nfs4_ol_stateid **stpp,
4515 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004517 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004518 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004519 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004521 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4522 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004523
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004525 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004526 if (status)
4527 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004528 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004529 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004531 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004532 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004533 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004534 else
4535 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004536 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004537}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004538
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004539static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4540 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004541{
4542 __be32 status;
4543 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004544 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004546 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004547 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004548 if (status)
4549 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004550 oo = openowner(stp->st_stateowner);
4551 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4552 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004553 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004554 }
4555 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004556 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557}
4558
Al Virob37ad282006-10-19 23:28:59 -07004559__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004560nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004561 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562{
Al Virob37ad282006-10-19 23:28:59 -07004563 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004564 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004565 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004566 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567
Al Viroa6a9f182013-09-16 10:57:01 -04004568 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4569 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004571 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004572 if (status)
4573 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574
4575 nfs4_lock_state();
4576
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004577 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004578 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004579 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004580 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004581 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004582 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004583 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004584 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004585 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004586 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004587 update_stateid(&stp->st_stid.sc_stateid);
4588 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004589 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004590 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004591
Jeff Layton2a4317c2012-03-21 16:42:43 -04004592 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004593 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004594put_stateid:
4595 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004597 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004598 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 return status;
4600}
4601
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004602static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004604 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004605 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004606 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004607 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004608}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004609
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004610static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4611{
4612 switch (to_access) {
4613 case NFS4_SHARE_ACCESS_READ:
4614 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4615 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4616 break;
4617 case NFS4_SHARE_ACCESS_WRITE:
4618 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4619 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4620 break;
4621 case NFS4_SHARE_ACCESS_BOTH:
4622 break;
4623 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004624 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 }
4626}
4627
Al Virob37ad282006-10-19 23:28:59 -07004628__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004629nfsd4_open_downgrade(struct svc_rqst *rqstp,
4630 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004631 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632{
Al Virob37ad282006-10-19 23:28:59 -07004633 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004634 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004635 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636
Al Viroa6a9f182013-09-16 10:57:01 -04004637 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4638 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004640 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004641 if (od->od_deleg_want)
4642 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4643 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644
4645 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004646 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004647 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004648 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004651 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004652 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004654 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004656 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004657 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004659 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004661 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
Jeff Laytonce0fc432012-05-11 09:45:14 -04004663 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004665 update_stateid(&stp->st_stid.sc_stateid);
4666 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004668put_stateid:
4669 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004671 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004672 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 return status;
4674}
4675
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004676static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4677{
Trond Myklebustacf92952014-06-30 11:48:37 -04004678 struct nfs4_client *clp = s->st_stid.sc_client;
Trond Myklebustacf92952014-06-30 11:48:37 -04004679
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004680 s->st_stid.sc_type = NFS4_CLOSED_STID;
Trond Myklebustacf92952014-06-30 11:48:37 -04004681 unhash_open_stateid(s);
4682
Jeff Laytond3134b12014-07-29 21:34:32 -04004683 if (clp->cl_minorversion)
Trond Myklebust60116952014-07-29 21:34:06 -04004684 nfs4_put_stid(&s->st_stid);
Jeff Laytond3134b12014-07-29 21:34:32 -04004685 else
4686 move_to_close_lru(s, clp->net);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004687}
4688
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689/*
4690 * nfs4_unlock_state() called after encode
4691 */
Al Virob37ad282006-10-19 23:28:59 -07004692__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004693nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004694 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695{
Al Virob37ad282006-10-19 23:28:59 -07004696 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004697 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004698 struct net *net = SVC_NET(rqstp);
4699 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700
Al Viroa6a9f182013-09-16 10:57:01 -04004701 dprintk("NFSD: nfsd4_close on file %pd\n",
4702 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
4704 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004705 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4706 &close->cl_stateid,
4707 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004708 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004709 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004710 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004712 update_stateid(&stp->st_stid.sc_stateid);
4713 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004715 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004716
4717 /* put reference from nfs4_preprocess_seqid_op */
4718 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719out:
Jeff Layton58fb12e2014-07-29 21:34:27 -04004720 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 return status;
4722}
4723
Al Virob37ad282006-10-19 23:28:59 -07004724__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004725nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4726 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004728 struct nfs4_delegation *dp;
4729 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004730 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004731 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004732 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004734 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004735 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
4737 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004738 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004739 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004740 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004741 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004742 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004743 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004744 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004745
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004746 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004747put_stateid:
4748 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004750 nfs4_unlock_state();
4751
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 return status;
4753}
4754
4755
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757
Benny Halevy87df4de2008-12-15 19:42:03 +02004758static inline u64
4759end_offset(u64 start, u64 len)
4760{
4761 u64 end;
4762
4763 end = start + len;
4764 return end >= start ? end: NFS4_MAX_UINT64;
4765}
4766
4767/* last octet in a range */
4768static inline u64
4769last_byte_offset(u64 start, u64 len)
4770{
4771 u64 end;
4772
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004773 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004774 end = start + len;
4775 return end > start ? end - 1: NFS4_MAX_UINT64;
4776}
4777
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778/*
4779 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4780 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4781 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4782 * locking, this prevents us from being completely protocol-compliant. The
4783 * real solution to this problem is to start using unsigned file offsets in
4784 * the VFS, but this is a very deep change!
4785 */
4786static inline void
4787nfs4_transform_lock_offset(struct file_lock *lock)
4788{
4789 if (lock->fl_start < 0)
4790 lock->fl_start = OFFSET_MAX;
4791 if (lock->fl_end < 0)
4792 lock->fl_end = OFFSET_MAX;
4793}
4794
NeilBrownd5b90262006-04-10 22:55:22 -07004795/* Hack!: For now, we're defining this just so we can use a pointer to it
4796 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004797static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004798};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799
4800static inline void
4801nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4802{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004803 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
NeilBrownd5b90262006-04-10 22:55:22 -07004805 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004806 lo = (struct nfs4_lockowner *) fl->fl_owner;
4807 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4808 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004809 if (!deny->ld_owner.data)
4810 /* We just don't care that much */
4811 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004812 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4813 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004814 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004815nevermind:
4816 deny->ld_owner.len = 0;
4817 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004818 deny->ld_clientid.cl_boot = 0;
4819 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 }
4821 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004822 deny->ld_length = NFS4_MAX_UINT64;
4823 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4825 deny->ld_type = NFS4_READ_LT;
4826 if (fl->fl_type != F_RDLCK)
4827 deny->ld_type = NFS4_WRITE_LT;
4828}
4829
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004830static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004831find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004832 struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004834 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004835 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
Trond Myklebustd4f04892014-07-29 21:34:36 -04004837 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4838 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004839 if (so->so_is_open_owner)
4840 continue;
Trond Myklebustd4f04892014-07-29 21:34:36 -04004841 if (!same_owner_str(so, owner))
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004842 continue;
Jeff Layton5db1c032014-07-29 21:34:28 -04004843 atomic_inc(&so->so_count);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004844 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 }
4846 return NULL;
4847}
4848
Trond Myklebustc58c6612014-07-29 21:34:35 -04004849static struct nfs4_lockowner *
4850find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004851 struct nfs4_client *clp)
Trond Myklebustc58c6612014-07-29 21:34:35 -04004852{
4853 struct nfs4_lockowner *lo;
4854
Trond Myklebustd4f04892014-07-29 21:34:36 -04004855 spin_lock(&clp->cl_lock);
4856 lo = find_lockowner_str_locked(clid, owner, clp);
4857 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004858 return lo;
4859}
4860
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004861static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4862{
Trond Myklebustc58c6612014-07-29 21:34:35 -04004863 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004864}
4865
Jeff Layton6b180f02014-07-29 21:34:26 -04004866static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4867{
4868 struct nfs4_lockowner *lo = lockowner(sop);
4869
4870 kmem_cache_free(lockowner_slab, lo);
4871}
4872
4873static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004874 .so_unhash = nfs4_unhash_lockowner,
4875 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004876};
4877
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878/*
4879 * Alloc a lock owner structure.
4880 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004881 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004883 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004885static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004886alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
4887 struct nfs4_ol_stateid *open_stp,
4888 struct nfsd4_lock *lock)
4889{
Trond Myklebustc58c6612014-07-29 21:34:35 -04004890 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004892 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4893 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004895 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4896 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04004897 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04004898 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04004899 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004900 ret = find_lockowner_str_locked(&clp->cl_clientid,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004901 &lock->lk_new_owner, clp);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004902 if (ret == NULL) {
4903 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004904 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004905 ret = lo;
4906 } else
4907 nfs4_free_lockowner(&lo->lo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04004908 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004909 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910}
4911
Jeff Layton356a95e2014-07-29 21:34:13 -04004912static void
4913init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
4914 struct nfs4_file *fp, struct inode *inode,
4915 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004917 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918
Jeff Layton356a95e2014-07-29 21:34:13 -04004919 lockdep_assert_held(&clp->cl_lock);
4920
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004921 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004922 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004923 stp->st_stateowner = &lo->lo_owner;
Trond Myklebuste4f1dd72014-07-29 21:34:30 -04004924 atomic_inc(&lo->lo_owner.so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07004925 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004926 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04004927 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004928 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004930 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004931 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04004932 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004933 spin_lock(&fp->fi_lock);
4934 list_add(&stp->st_perfile, &fp->fi_stateids);
4935 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936}
4937
Jeff Laytonc53530d2014-06-30 11:48:39 -04004938static struct nfs4_ol_stateid *
4939find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4940{
4941 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04004942 struct nfs4_client *clp = lo->lo_owner.so_client;
4943
4944 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004945
4946 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004947 if (lst->st_stid.sc_file == fp) {
4948 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004949 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004950 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004951 }
4952 return NULL;
4953}
4954
Jeff Layton356a95e2014-07-29 21:34:13 -04004955static struct nfs4_ol_stateid *
4956find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
4957 struct inode *inode, struct nfs4_ol_stateid *ost,
4958 bool *new)
4959{
4960 struct nfs4_stid *ns = NULL;
4961 struct nfs4_ol_stateid *lst;
4962 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4963 struct nfs4_client *clp = oo->oo_owner.so_client;
4964
4965 spin_lock(&clp->cl_lock);
4966 lst = find_lock_stateid(lo, fi);
4967 if (lst == NULL) {
4968 spin_unlock(&clp->cl_lock);
4969 ns = nfs4_alloc_stid(clp, stateid_slab);
4970 if (ns == NULL)
4971 return NULL;
4972
4973 spin_lock(&clp->cl_lock);
4974 lst = find_lock_stateid(lo, fi);
4975 if (likely(!lst)) {
4976 lst = openlockstateid(ns);
4977 init_lock_stateid(lst, lo, fi, inode, ost);
4978 ns = NULL;
4979 *new = true;
4980 }
4981 }
4982 spin_unlock(&clp->cl_lock);
4983 if (ns)
4984 nfs4_put_stid(ns);
4985 return lst;
4986}
Jeff Laytonc53530d2014-06-30 11:48:39 -04004987
NeilBrownfd39ca92005-06-23 22:04:03 -07004988static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989check_lock_length(u64 offset, u64 length)
4990{
Benny Halevy87df4de2008-12-15 19:42:03 +02004991 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 LOFF_OVERFLOW(offset, length)));
4993}
4994
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004995static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004996{
Trond Myklebust11b91642014-07-29 21:34:08 -04004997 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004998
Jeff Layton7214e862014-07-10 14:07:33 -04004999 lockdep_assert_held(&fp->fi_lock);
5000
Jeff Layton82c5ff12012-05-11 09:45:13 -04005001 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005002 return;
Jeff Layton12659652014-07-10 14:07:28 -04005003 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005004 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005005}
5006
Jeff Layton356a95e2014-07-29 21:34:13 -04005007static __be32
5008lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5009 struct nfs4_ol_stateid *ost,
5010 struct nfsd4_lock *lock,
5011 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005012{
Jeff Layton5db1c032014-07-29 21:34:28 -04005013 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005014 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005015 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5016 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005017 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005018 struct nfs4_lockowner *lo;
5019 unsigned int strhashval;
5020
Trond Myklebustd4f04892014-07-29 21:34:36 -04005021 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005022 if (!lo) {
Trond Myklebustd4f04892014-07-29 21:34:36 -04005023 strhashval = ownerstr_hashval(&lock->v.new.owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005024 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5025 if (lo == NULL)
5026 return nfserr_jukebox;
5027 } else {
5028 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005029 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005030 if (!cstate->minorversion &&
5031 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005032 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005033 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005034
Jeff Layton356a95e2014-07-29 21:34:13 -04005035 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005036 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005037 status = nfserr_jukebox;
5038 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005039 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005040 status = nfs_ok;
5041out:
5042 nfs4_put_stateowner(&lo->lo_owner);
5043 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005044}
5045
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046/*
5047 * LOCK operation
5048 */
Al Virob37ad282006-10-19 23:28:59 -07005049__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005050nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005051 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005053 struct nfs4_openowner *open_sop = NULL;
5054 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005055 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005056 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005057 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005058 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005059 struct file_lock *file_lock = NULL;
5060 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005061 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005062 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005063 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005064 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005065 struct net *net = SVC_NET(rqstp);
5066 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067
5068 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5069 (long long) lock->lk_offset,
5070 (long long) lock->lk_length);
5071
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 if (check_lock_length(lock->lk_offset, lock->lk_length))
5073 return nfserr_inval;
5074
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005075 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005076 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005077 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5078 return status;
5079 }
5080
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 nfs4_lock_state();
5082
5083 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005084 if (nfsd4_has_session(cstate))
5085 /* See rfc 5661 18.10.3: given clientid is ignored: */
5086 memcpy(&lock->v.new.clientid,
5087 &cstate->session->se_client->cl_clientid,
5088 sizeof(clientid_t));
5089
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005091 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005095 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 lock->lk_new_open_seqid,
5097 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005098 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005099 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005101 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005102 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005103 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005104 &lock->v.new.clientid))
5105 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005106 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005107 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005108 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005109 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005110 lock->lk_old_lock_seqid,
5111 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005112 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005113 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005114 if (status)
5115 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005116 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005118 lkflg = setlkflg(lock->lk_type);
5119 status = nfs4_check_openmode(lock_stp, lkflg);
5120 if (status)
5121 goto out;
5122
NeilBrown0dd395d2005-07-07 17:59:15 -07005123 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005124 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005125 goto out;
5126 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005127 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005128 goto out;
5129
Jeff Layton21179d82012-08-21 08:03:32 -04005130 file_lock = locks_alloc_lock();
5131 if (!file_lock) {
5132 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5133 status = nfserr_jukebox;
5134 goto out;
5135 }
5136
Trond Myklebust11b91642014-07-29 21:34:08 -04005137 fp = lock_stp->st_stid.sc_file;
Jeff Layton21179d82012-08-21 08:03:32 -04005138 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 switch (lock->lk_type) {
5140 case NFS4_READ_LT:
5141 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005142 spin_lock(&fp->fi_lock);
5143 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005144 if (filp)
5145 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005146 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005147 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005148 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 case NFS4_WRITE_LT:
5150 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005151 spin_lock(&fp->fi_lock);
5152 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005153 if (filp)
5154 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005155 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005156 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005157 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 default:
5159 status = nfserr_inval;
5160 goto out;
5161 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005162 if (!filp) {
5163 status = nfserr_openmode;
5164 goto out;
5165 }
Jeff Layton21179d82012-08-21 08:03:32 -04005166 file_lock->fl_owner = (fl_owner_t)lock_sop;
5167 file_lock->fl_pid = current->tgid;
5168 file_lock->fl_file = filp;
5169 file_lock->fl_flags = FL_POSIX;
5170 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5171 file_lock->fl_start = lock->lk_offset;
5172 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5173 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174
Jeff Layton21179d82012-08-21 08:03:32 -04005175 conflock = locks_alloc_lock();
5176 if (!conflock) {
5177 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5178 status = nfserr_jukebox;
5179 goto out;
5180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181
Jeff Layton21179d82012-08-21 08:03:32 -04005182 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005183 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005185 update_stateid(&lock_stp->st_stid.sc_stateid);
5186 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005188 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005189 break;
5190 case (EAGAIN): /* conflock holds conflicting lock */
5191 status = nfserr_denied;
5192 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005193 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005194 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 case (EDEADLK):
5196 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005197 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005198 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005199 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005200 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005201 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203out:
Trond Myklebustde186432014-07-10 14:07:26 -04005204 if (filp)
5205 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005206 if (lock_stp) {
5207 /* Bump seqid manually if the 4.0 replay owner is openowner */
5208 if (cstate->replay_owner &&
5209 cstate->replay_owner != &lock_sop->lo_owner &&
5210 seqid_mutating_err(ntohl(status)))
5211 lock_sop->lo_owner.so_seqid++;
5212
5213 /*
5214 * If this is a new, never-before-used stateid, and we are
5215 * returning an error, then just go ahead and release it.
5216 */
5217 if (status && new)
5218 release_lock_stateid(lock_stp);
5219
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005220 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005221 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005222 if (open_stp)
5223 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005224 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005225 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005226 if (file_lock)
5227 locks_free_lock(file_lock);
5228 if (conflock)
5229 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 return status;
5231}
5232
5233/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005234 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5235 * so we do a temporary open here just to get an open file to pass to
5236 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5237 * inode operation.)
5238 */
Al Viro04da6e92012-04-13 00:00:04 -04005239static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005240{
5241 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005242 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5243 if (!err) {
5244 err = nfserrno(vfs_test_lock(file, lock));
5245 nfsd_close(file);
5246 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005247 return err;
5248}
5249
5250/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 * LOCKT operation
5252 */
Al Virob37ad282006-10-19 23:28:59 -07005253__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005254nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5255 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256{
Jeff Layton21179d82012-08-21 08:03:32 -04005257 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005258 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005259 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005260 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005262 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 return nfserr_grace;
5264
5265 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5266 return nfserr_inval;
5267
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 nfs4_lock_state();
5269
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005270 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005271 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005272 if (status)
5273 goto out;
5274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005276 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278
Jeff Layton21179d82012-08-21 08:03:32 -04005279 file_lock = locks_alloc_lock();
5280 if (!file_lock) {
5281 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5282 status = nfserr_jukebox;
5283 goto out;
5284 }
5285 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 switch (lockt->lt_type) {
5287 case NFS4_READ_LT:
5288 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005289 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 break;
5291 case NFS4_WRITE_LT:
5292 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005293 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 break;
5295 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005296 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 status = nfserr_inval;
5298 goto out;
5299 }
5300
Trond Myklebustd4f04892014-07-29 21:34:36 -04005301 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5302 cstate->clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005303 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005304 file_lock->fl_owner = (fl_owner_t)lo;
5305 file_lock->fl_pid = current->tgid;
5306 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307
Jeff Layton21179d82012-08-21 08:03:32 -04005308 file_lock->fl_start = lockt->lt_offset;
5309 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310
Jeff Layton21179d82012-08-21 08:03:32 -04005311 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312
Jeff Layton21179d82012-08-21 08:03:32 -04005313 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005314 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005315 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005316
Jeff Layton21179d82012-08-21 08:03:32 -04005317 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005319 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 }
5321out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005322 if (lo)
5323 nfs4_put_stateowner(&lo->lo_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005325 if (file_lock)
5326 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 return status;
5328}
5329
Al Virob37ad282006-10-19 23:28:59 -07005330__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005331nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005332 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005334 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005336 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005337 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005338 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005339 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5340
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5342 (long long) locku->lu_offset,
5343 (long long) locku->lu_length);
5344
5345 if (check_lock_length(locku->lu_offset, locku->lu_length))
5346 return nfserr_inval;
5347
5348 nfs4_lock_state();
5349
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005350 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005351 &locku->lu_stateid, NFS4_LOCK_STID,
5352 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005353 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005355 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005356 if (!filp) {
5357 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005358 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005359 }
Jeff Layton21179d82012-08-21 08:03:32 -04005360 file_lock = locks_alloc_lock();
5361 if (!file_lock) {
5362 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5363 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005364 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005365 }
5366 locks_init_lock(file_lock);
5367 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005368 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005369 file_lock->fl_pid = current->tgid;
5370 file_lock->fl_file = filp;
5371 file_lock->fl_flags = FL_POSIX;
5372 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5373 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374
Jeff Layton21179d82012-08-21 08:03:32 -04005375 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5376 locku->lu_length);
5377 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378
Jeff Layton21179d82012-08-21 08:03:32 -04005379 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005380 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005381 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 goto out_nfserr;
5383 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005384 update_stateid(&stp->st_stid.sc_stateid);
5385 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005386fput:
5387 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005388put_stateid:
5389 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005391 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005392 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005393 if (file_lock)
5394 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 return status;
5396
5397out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005398 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005399 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400}
5401
5402/*
5403 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005404 * true: locks held by lockowner
5405 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005407static bool
5408check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409{
5410 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005411 int status = false;
5412 struct file *filp = find_any_file(fp);
5413 struct inode *inode;
5414
5415 if (!filp) {
5416 /* Any valid lock stateid should have some sort of access */
5417 WARN_ON_ONCE(1);
5418 return status;
5419 }
5420
5421 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422
Jeff Layton1c8c6012013-06-21 08:58:15 -04005423 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005425 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005426 status = true;
5427 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005430 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005431 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 return status;
5433}
5434
Al Virob37ad282006-10-19 23:28:59 -07005435__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005436nfsd4_release_lockowner(struct svc_rqst *rqstp,
5437 struct nfsd4_compound_state *cstate,
5438 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439{
5440 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005441 struct nfs4_stateowner *sop;
5442 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005443 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005445 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005446 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005447 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005448 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449
5450 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5451 clid->cl_boot, clid->cl_id);
5452
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 nfs4_lock_state();
5454
Jeff Layton4b24ca72014-06-30 11:48:44 -04005455 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005456 if (status)
5457 goto out;
5458
Trond Myklebustd4f04892014-07-29 21:34:36 -04005459 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005460 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005461 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005462 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005463 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005464
5465 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005466 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005467
Jeff Layton882e9d22014-07-29 21:34:37 -04005468 /* see if there are still any locks associated with it */
5469 lo = lockowner(sop);
5470 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5471 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5472 status = nfserr_locks_held;
5473 spin_unlock(&clp->cl_lock);
5474 goto out;
5475 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005476 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005477
5478 atomic_inc(&sop->so_count);
5479 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005480 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005481 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005482 if (lo)
5483 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484out:
5485 nfs4_unlock_state();
5486 return status;
5487}
5488
5489static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005490alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491{
NeilBrowna55370a2005-06-23 22:03:52 -07005492 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493}
5494
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005495bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005496nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005497{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005498 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005499
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005500 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005501 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005502}
5503
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504/*
5505 * failure => all reset bets are off, nfserr_no_grace...
5506 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005507struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005508nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509{
5510 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005511 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512
NeilBrowna55370a2005-06-23 22:03:52 -07005513 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5514 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005515 if (crp) {
5516 strhashval = clientstr_hashval(name);
5517 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005518 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005519 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005520 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005521 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005522 }
5523 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524}
5525
Jeff Layton2a4317c2012-03-21 16:42:43 -04005526void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005527nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005528{
5529 list_del(&crp->cr_strhash);
5530 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005531 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005532}
5533
5534void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005535nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536{
5537 struct nfs4_client_reclaim *crp = NULL;
5538 int i;
5539
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005541 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5542 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005544 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 }
5546 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005547 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548}
5549
5550/*
5551 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005552struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005553nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554{
5555 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 struct nfs4_client_reclaim *crp = NULL;
5557
Jeff Layton278c9312012-11-12 15:00:52 -05005558 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559
Jeff Layton278c9312012-11-12 15:00:52 -05005560 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005561 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005562 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 return crp;
5564 }
5565 }
5566 return NULL;
5567}
5568
5569/*
5570* Called from OPEN. Look for clientid in reclaim list.
5571*/
Al Virob37ad282006-10-19 23:28:59 -07005572__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005573nfs4_check_open_reclaim(clientid_t *clid,
5574 struct nfsd4_compound_state *cstate,
5575 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005577 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005578
5579 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005580 status = lookup_clientid(clid, cstate, nn);
5581 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005582 return nfserr_reclaim_bad;
5583
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005584 if (nfsd4_client_record_check(cstate->clp))
5585 return nfserr_reclaim_bad;
5586
5587 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588}
5589
Bryan Schumaker65178db2011-11-01 13:35:21 -04005590#ifdef CONFIG_NFSD_FAULT_INJECTION
5591
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005592u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5593{
J. Bruce Fields221a6872013-04-01 22:23:49 -04005594 if (mark_client_expired(clp))
5595 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005596 expire_client(clp);
5597 return 1;
5598}
5599
Bryan Schumaker184c1842012-11-29 11:40:44 -05005600u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5601{
5602 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005603 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005604 printk(KERN_INFO "NFS Client: %s\n", buf);
5605 return 1;
5606}
5607
5608static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5609 const char *type)
5610{
5611 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005612 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005613 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5614}
5615
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005616static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5617 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005618{
5619 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005620 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005621 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005622 u64 count = 0;
5623
5624 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005625 list_for_each_entry_safe(stp, st_next,
5626 &oop->oo_owner.so_stateids, st_perstateowner) {
5627 list_for_each_entry_safe(lst, lst_next,
5628 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005629 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005630 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005631 if (++count == max)
5632 return count;
5633 }
5634 }
5635 }
5636
5637 return count;
5638}
5639
5640u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5641{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005642 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005643}
5644
Bryan Schumaker184c1842012-11-29 11:40:44 -05005645u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5646{
5647 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5648 nfsd_print_count(clp, count, "locked files");
5649 return count;
5650}
5651
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005652static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5653{
5654 struct nfs4_openowner *oop, *next;
5655 u64 count = 0;
5656
5657 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5658 if (func)
5659 func(oop);
5660 if (++count == max)
5661 break;
5662 }
5663
5664 return count;
5665}
5666
5667u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5668{
5669 return nfsd_foreach_client_open(clp, max, release_openowner);
5670}
5671
Bryan Schumaker184c1842012-11-29 11:40:44 -05005672u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5673{
5674 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5675 nfsd_print_count(clp, count, "open files");
5676 return count;
5677}
5678
Bryan Schumaker269de302012-11-29 11:40:42 -05005679static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5680 struct list_head *victims)
5681{
5682 struct nfs4_delegation *dp, *next;
5683 u64 count = 0;
5684
Benny Halevycdc97502014-05-30 09:09:30 -04005685 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005686 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005687 if (victims) {
5688 /*
5689 * It's not safe to mess with delegations that have a
5690 * non-zero dl_time. They might have already been broken
5691 * and could be processed by the laundromat outside of
5692 * the state_lock. Just leave them be.
5693 */
5694 if (dp->dl_time != 0)
5695 continue;
5696
Jeff Layton42690672014-07-25 07:34:20 -04005697 unhash_delegation_locked(dp);
5698 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005699 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005700 if (++count == max)
5701 break;
5702 }
5703 return count;
5704}
5705
5706u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5707{
5708 struct nfs4_delegation *dp, *next;
5709 LIST_HEAD(victims);
5710 u64 count;
5711
Benny Halevycdc97502014-05-30 09:09:30 -04005712 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005713 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005714 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005715
Jeff Layton2d4a5322014-07-25 07:34:21 -04005716 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5717 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005718 revoke_delegation(dp);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005719 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005720
5721 return count;
5722}
5723
5724u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5725{
Jeff Laytondff13992014-07-08 14:02:49 -04005726 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005727 LIST_HEAD(victims);
5728 u64 count;
5729
Benny Halevycdc97502014-05-30 09:09:30 -04005730 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005731 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005732 while (!list_empty(&victims)) {
5733 dp = list_first_entry(&victims, struct nfs4_delegation,
5734 dl_recall_lru);
5735 list_del_init(&dp->dl_recall_lru);
5736 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005737 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005738 }
Benny Halevycdc97502014-05-30 09:09:30 -04005739 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005740
5741 return count;
5742}
5743
Bryan Schumaker184c1842012-11-29 11:40:44 -05005744u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5745{
5746 u64 count = 0;
5747
Benny Halevycdc97502014-05-30 09:09:30 -04005748 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005749 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005750 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005751
5752 nfsd_print_count(clp, count, "delegations");
5753 return count;
5754}
5755
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005756u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005757{
5758 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005759 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005760 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005761
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005762 if (!nfsd_netns_ready(nn))
5763 return 0;
5764
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005765 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005766 count += func(clp, max - count);
5767 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005768 break;
5769 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005770
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005771 return count;
5772}
5773
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005774struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5775{
5776 struct nfs4_client *clp;
5777 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5778
5779 if (!nfsd_netns_ready(nn))
5780 return NULL;
5781
5782 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5783 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5784 return clp;
5785 }
5786 return NULL;
5787}
5788
Bryan Schumaker65178db2011-11-01 13:35:21 -04005789#endif /* CONFIG_NFSD_FAULT_INJECTION */
5790
Meelap Shahc2f1a552007-07-17 04:04:39 -07005791/*
5792 * Since the lifetime of a delegation isn't limited to that of an open, a
5793 * client may quite reasonably hang on to a delegation as long as it has
5794 * the inode cached. This becomes an obvious problem the first time a
5795 * client's inode cache approaches the size of the server's total memory.
5796 *
5797 * For now we avoid this problem by imposing a hard limit on the number
5798 * of delegations, which varies according to the server's memory size.
5799 */
5800static void
5801set_max_delegations(void)
5802{
5803 /*
5804 * Allow at most 4 delegations per megabyte of RAM. Quick
5805 * estimates suggest that in the worst case (where every delegation
5806 * is for a different inode), a delegation could take about 1.5K,
5807 * giving a worst case usage of about 6% of memory.
5808 */
5809 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5810}
5811
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005812static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005813{
5814 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5815 int i;
5816
5817 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5818 CLIENT_HASH_SIZE, GFP_KERNEL);
5819 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005820 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005821 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5822 CLIENT_HASH_SIZE, GFP_KERNEL);
5823 if (!nn->unconf_id_hashtbl)
5824 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005825 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5826 SESSION_HASH_SIZE, GFP_KERNEL);
5827 if (!nn->sessionid_hashtbl)
5828 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005829
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005830 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005831 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005832 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005833 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005834 for (i = 0; i < SESSION_HASH_SIZE; i++)
5835 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005836 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005837 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005838 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005839 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005840 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005841 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005842
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005843 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005844 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005845
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005846 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005847
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005848err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005849 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005850err_unconf_id:
5851 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005852err:
5853 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005854}
5855
5856static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005857nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005858{
5859 int i;
5860 struct nfs4_client *clp = NULL;
5861 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5862
5863 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5864 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5865 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5866 destroy_client(clp);
5867 }
5868 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005869
Kinglong Mee2b905632014-03-26 22:09:30 +08005870 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5871 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5872 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5873 destroy_client(clp);
5874 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005875 }
5876
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005877 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005878 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005879 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005880 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005881}
5882
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005883int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005884nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005885{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005886 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005887 int ret;
5888
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005889 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005890 if (ret)
5891 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005892 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005893 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005894 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005895 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005896 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005897 nn->nfsd4_grace, net);
5898 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005899 return 0;
5900}
5901
5902/* initialization to perform when the nfsd service is started: */
5903
5904int
5905nfs4_state_start(void)
5906{
5907 int ret;
5908
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005909 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005910 if (ret)
5911 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005912 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005913 if (laundry_wq == NULL) {
5914 ret = -ENOMEM;
5915 goto out_recovery;
5916 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005917 ret = nfsd4_create_callback_queue();
5918 if (ret)
5919 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005920
Meelap Shahc2f1a552007-07-17 04:04:39 -07005921 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005922
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005923 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005924
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005925out_free_laundry:
5926 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005927out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005928 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929}
5930
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005931void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005932nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005936 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005938 cancel_delayed_work_sync(&nn->laundromat_work);
5939 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005940
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005941 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04005943 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005944 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005946 unhash_delegation_locked(dp);
5947 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 }
Benny Halevycdc97502014-05-30 09:09:30 -04005949 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 list_for_each_safe(pos, next, &reaplist) {
5951 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005952 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04005953 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 }
5955
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005956 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005957 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005958 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959}
5960
5961void
5962nfs4_state_shutdown(void)
5963{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005964 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005965 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005967
5968static void
5969get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5970{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005971 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5972 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005973}
5974
5975static void
5976put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5977{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005978 if (cstate->minorversion) {
5979 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5980 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5981 }
5982}
5983
5984void
5985clear_current_stateid(struct nfsd4_compound_state *cstate)
5986{
5987 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005988}
5989
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005990/*
5991 * functions to set current state id
5992 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005993void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005994nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5995{
5996 put_stateid(cstate, &odp->od_stateid);
5997}
5998
5999void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006000nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6001{
6002 put_stateid(cstate, &open->op_stateid);
6003}
6004
6005void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006006nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6007{
6008 put_stateid(cstate, &close->cl_stateid);
6009}
6010
6011void
6012nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6013{
6014 put_stateid(cstate, &lock->lk_resp_stateid);
6015}
6016
6017/*
6018 * functions to consume current state id
6019 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006020
6021void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006022nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6023{
6024 get_stateid(cstate, &odp->od_stateid);
6025}
6026
6027void
6028nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6029{
6030 get_stateid(cstate, &drp->dr_stateid);
6031}
6032
6033void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006034nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6035{
6036 get_stateid(cstate, &fsp->fr_stateid);
6037}
6038
6039void
6040nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6041{
6042 get_stateid(cstate, &setattr->sa_stateid);
6043}
6044
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006045void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006046nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6047{
6048 get_stateid(cstate, &close->cl_stateid);
6049}
6050
6051void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006052nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006053{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006054 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006055}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006056
6057void
6058nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6059{
6060 get_stateid(cstate, &read->rd_stateid);
6061}
6062
6063void
6064nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6065{
6066 get_stateid(cstate, &write->wr_stateid);
6067}