blob: cb630db015b0c9710863d1bbf14ab437e75a0a2c [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
Jeff Layton4ae098d2014-07-29 21:34:43 -0400952static void unhash_ol_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 Layton2c41beb2014-07-29 21:34:41 -0400986/*
987 * Put the persistent reference to an already unhashed generic stateid, while
988 * holding the cl_lock. If it's the last reference, then put it onto the
989 * reaplist for later destruction.
990 */
991static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
992 struct list_head *reaplist)
993{
994 struct nfs4_stid *s = &stp->st_stid;
995 struct nfs4_client *clp = s->sc_client;
996
997 lockdep_assert_held(&clp->cl_lock);
998
999 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1000
1001 if (!atomic_dec_and_test(&s->sc_count)) {
1002 wake_up_all(&close_wq);
1003 return;
1004 }
1005
1006 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1007 list_add(&stp->st_locks, reaplist);
1008}
1009
Jeff Layton3c1c9952014-07-29 21:34:39 -04001010static void unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1011{
1012 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1013
1014 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
1015
1016 list_del_init(&stp->st_locks);
Jeff Layton4ae098d2014-07-29 21:34:43 -04001017 unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001018 unhash_stid(&stp->st_stid);
1019}
1020
Jeff Layton5adfd882014-07-29 21:34:31 -04001021static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001022{
Jeff Layton1c755dc2014-07-29 21:34:12 -04001023 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1024
1025 spin_lock(&oo->oo_owner.so_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001026 unhash_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001027 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04001028 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001029}
1030
Trond Myklebustc58c6612014-07-29 21:34:35 -04001031static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001032{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001033 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001034
Trond Myklebustd4f04892014-07-29 21:34:36 -04001035 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001036
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001037 list_del_init(&lo->lo_owner.so_strhash);
1038}
1039
Jeff Layton2c41beb2014-07-29 21:34:41 -04001040/*
1041 * Free a list of generic stateids that were collected earlier after being
1042 * fully unhashed.
1043 */
1044static void
1045free_ol_stateid_reaplist(struct list_head *reaplist)
1046{
1047 struct nfs4_ol_stateid *stp;
1048
1049 might_sleep();
1050
1051 while (!list_empty(reaplist)) {
1052 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1053 st_locks);
1054 list_del(&stp->st_locks);
1055 stp->st_stid.sc_free(&stp->st_stid);
1056 }
1057}
1058
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001059static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001060{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001061 struct nfs4_client *clp = lo->lo_owner.so_client;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001062 struct nfs4_ol_stateid *stp;
1063 struct list_head reaplist;
1064
1065 INIT_LIST_HEAD(&reaplist);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001066
Trond Myklebustd4f04892014-07-29 21:34:36 -04001067 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001068 unhash_lockowner_locked(lo);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001069 while (!list_empty(&lo->lo_owner.so_stateids)) {
1070 stp = list_first_entry(&lo->lo_owner.so_stateids,
1071 struct nfs4_ol_stateid, st_perstateowner);
1072 unhash_lock_stateid(stp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001073 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001074 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001075 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001076 free_ol_stateid_reaplist(&reaplist);
Jeff Layton6b180f02014-07-29 21:34:26 -04001077 nfs4_put_stateowner(&lo->lo_owner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001078}
1079
Jeff Laytond83017f2014-07-29 21:34:42 -04001080static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1081 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001082{
1083 struct nfs4_ol_stateid *stp;
1084
1085 while (!list_empty(&open_stp->st_locks)) {
1086 stp = list_entry(open_stp->st_locks.next,
1087 struct nfs4_ol_stateid, st_locks);
Jeff Laytond83017f2014-07-29 21:34:42 -04001088 unhash_lock_stateid(stp);
1089 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001090 }
1091}
1092
Jeff Laytond83017f2014-07-29 21:34:42 -04001093static void unhash_open_stateid(struct nfs4_ol_stateid *stp,
1094 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001095{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001096 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1097
Jeff Layton4ae098d2014-07-29 21:34:43 -04001098 unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001099 release_open_stateid_locks(stp, reaplist);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001100}
1101
1102static void release_open_stateid(struct nfs4_ol_stateid *stp)
1103{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001104 LIST_HEAD(reaplist);
1105
1106 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04001107 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001108 put_ol_stateid_locked(stp, &reaplist);
1109 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1110 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001111}
1112
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001113static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001114{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001115 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001116
Trond Myklebustd4f04892014-07-29 21:34:36 -04001117 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001118
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001119 list_del_init(&oo->oo_owner.so_strhash);
1120 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001121}
1122
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001123static void release_last_closed_stateid(struct nfs4_openowner *oo)
1124{
1125 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
1126
1127 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001128 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001129 oo->oo_last_closed_stid = NULL;
Jeff Laytond3134b12014-07-29 21:34:32 -04001130 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001131 }
1132}
1133
Jeff Layton2c41beb2014-07-29 21:34:41 -04001134static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001135{
1136 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001137 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001138 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001139
Jeff Layton2c41beb2014-07-29 21:34:41 -04001140 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001141
Trond Myklebustd4f04892014-07-29 21:34:36 -04001142 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001143 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001144 while (!list_empty(&oo->oo_owner.so_stateids)) {
1145 stp = list_first_entry(&oo->oo_owner.so_stateids,
1146 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytond83017f2014-07-29 21:34:42 -04001147 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001148 put_ol_stateid_locked(stp, &reaplist);
1149 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001150 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001151 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001152 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001153 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001154}
1155
Marc Eshel5282fd72009-04-03 08:27:52 +03001156static inline int
1157hash_sessionid(struct nfs4_sessionid *sessionid)
1158{
1159 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1160
1161 return sid->sequence % SESSION_HASH_SIZE;
1162}
1163
Trond Myklebust8f199b82012-03-20 15:11:17 -04001164#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001165static inline void
1166dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1167{
1168 u32 *ptr = (u32 *)(&sessionid->data[0]);
1169 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1170}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001171#else
1172static inline void
1173dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1174{
1175}
1176#endif
1177
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001178/*
1179 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1180 * won't be used for replay.
1181 */
1182void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1183{
1184 struct nfs4_stateowner *so = cstate->replay_owner;
1185
1186 if (nfserr == nfserr_replay_me)
1187 return;
1188
1189 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001190 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001191 return;
1192 }
1193 if (!so)
1194 return;
1195 if (so->so_is_open_owner)
1196 release_last_closed_stateid(openowner(so));
1197 so->so_seqid++;
1198 return;
1199}
Marc Eshel5282fd72009-04-03 08:27:52 +03001200
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001201static void
1202gen_sessionid(struct nfsd4_session *ses)
1203{
1204 struct nfs4_client *clp = ses->se_client;
1205 struct nfsd4_sessionid *sid;
1206
1207 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1208 sid->clientid = clp->cl_clientid;
1209 sid->sequence = current_sessionid++;
1210 sid->reserved = 0;
1211}
1212
1213/*
Andy Adamsona6496372009-08-28 08:45:01 -04001214 * The protocol defines ca_maxresponssize_cached to include the size of
1215 * the rpc header, but all we need to cache is the data starting after
1216 * the end of the initial SEQUENCE operation--the rest we regenerate
1217 * each time. Therefore we can advertise a ca_maxresponssize_cached
1218 * value that is the number of bytes in our cache plus a few additional
1219 * bytes. In order to stay on the safe side, and not promise more than
1220 * we can cache, those additional bytes must be the minimum possible: 24
1221 * bytes of rpc header (xid through accept state, with AUTH_NULL
1222 * verifier), 12 for the compound header (with zero-length tag), and 44
1223 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001224 */
Andy Adamsona6496372009-08-28 08:45:01 -04001225#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1226
Andy Adamson557ce262009-08-28 08:45:04 -04001227static void
1228free_session_slots(struct nfsd4_session *ses)
1229{
1230 int i;
1231
1232 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1233 kfree(ses->se_slots[i]);
1234}
1235
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001236/*
1237 * We don't actually need to cache the rpc and session headers, so we
1238 * can allocate a little less for each slot:
1239 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001240static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001241{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001242 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001243
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001244 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1245 size = 0;
1246 else
1247 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1248 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001249}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001250
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001251/*
1252 * XXX: If we run out of reserved DRC memory we could (up to a point)
1253 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001254 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001255 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001256static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001257{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001258 u32 slotsize = slot_bytes(ca);
1259 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001260 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001261
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001262 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001263 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1264 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001265 num = min_t(int, num, avail / slotsize);
1266 nfsd_drc_mem_used += num * slotsize;
1267 spin_unlock(&nfsd_drc_lock);
1268
1269 return num;
1270}
1271
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001272static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001273{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001274 int slotsize = slot_bytes(ca);
1275
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001276 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001277 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001278 spin_unlock(&nfsd_drc_lock);
1279}
1280
Kinglong Mee60810e52014-01-01 00:35:47 +08001281static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1282 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001283{
Kinglong Mee60810e52014-01-01 00:35:47 +08001284 int numslots = fattrs->maxreqs;
1285 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001286 struct nfsd4_session *new;
1287 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001288
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001289 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001290 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1291 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001292
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001293 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001294 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001295 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001296 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001297 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001298 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001299 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001300 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001301 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001302
1303 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1304 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1305
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001306 return new;
1307out_free:
1308 while (i--)
1309 kfree(new->se_slots[i]);
1310 kfree(new);
1311 return NULL;
1312}
1313
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001314static void free_conn(struct nfsd4_conn *c)
1315{
1316 svc_xprt_put(c->cn_xprt);
1317 kfree(c);
1318}
1319
1320static void nfsd4_conn_lost(struct svc_xpt_user *u)
1321{
1322 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1323 struct nfs4_client *clp = c->cn_session->se_client;
1324
1325 spin_lock(&clp->cl_lock);
1326 if (!list_empty(&c->cn_persession)) {
1327 list_del(&c->cn_persession);
1328 free_conn(c);
1329 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001330 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001331 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001332}
1333
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001334static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001335{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001336 struct nfsd4_conn *conn;
1337
1338 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1339 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001340 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001341 svc_xprt_get(rqstp->rq_xprt);
1342 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001343 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001344 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1345 return conn;
1346}
1347
J. Bruce Fields328ead22010-09-29 16:11:06 -04001348static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1349{
1350 conn->cn_session = ses;
1351 list_add(&conn->cn_persession, &ses->se_conns);
1352}
1353
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001354static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1355{
1356 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001357
1358 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001359 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001360 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001361}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001362
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001363static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001364{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001365 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001366 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001367}
1368
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001369static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001370{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001371 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001372
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001373 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001374 ret = nfsd4_register_conn(conn);
1375 if (ret)
1376 /* oops; xprt is already down: */
1377 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001378 /* We may have gained or lost a callback channel: */
1379 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001380}
1381
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001382static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001383{
1384 u32 dir = NFS4_CDFC4_FORE;
1385
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001386 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001387 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001388 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001389}
1390
1391/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001392static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001393{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001394 struct nfs4_client *clp = s->se_client;
1395 struct nfsd4_conn *c;
1396
1397 spin_lock(&clp->cl_lock);
1398 while (!list_empty(&s->se_conns)) {
1399 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1400 list_del_init(&c->cn_persession);
1401 spin_unlock(&clp->cl_lock);
1402
1403 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1404 free_conn(c);
1405
1406 spin_lock(&clp->cl_lock);
1407 }
1408 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001409}
1410
J. Bruce Fields1377b692012-09-11 21:42:40 -04001411static void __free_session(struct nfsd4_session *ses)
1412{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001413 free_session_slots(ses);
1414 kfree(ses);
1415}
1416
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001417static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001418{
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001419 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1420
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001421 lockdep_assert_held(&nn->client_lock);
1422 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001423 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001424 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001425}
Andy Adamson557ce262009-08-28 08:45:04 -04001426
Fengguang Wu135ae822012-11-10 07:20:25 -05001427static 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 -04001428{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001429 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001430 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001431
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001432 new->se_client = clp;
1433 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001434
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001435 INIT_LIST_HEAD(&new->se_conns);
1436
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001437 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001438 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001439 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001440 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001441 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001442 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001443 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001444 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001445 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001446 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001447 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001448 spin_unlock(&nn->client_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001449
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001450 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001451 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001452 /*
1453 * This is a little silly; with sessions there's no real
1454 * use for the callback address. Use the peer address
1455 * as a reasonable default for now, but consider fixing
1456 * the rpc client not to require an address in the
1457 * future:
1458 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001459 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1460 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001461 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001462}
1463
Benny Halevy9089f1b2010-05-12 00:12:26 +03001464/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001465static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001466__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001467{
1468 struct nfsd4_session *elem;
1469 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001470 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001471
1472 dump_sessionid(__func__, sessionid);
1473 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001474 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001475 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001476 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1477 NFS4_MAX_SESSIONID_LEN)) {
1478 return elem;
1479 }
1480 }
1481
1482 dprintk("%s: session not found\n", __func__);
1483 return NULL;
1484}
1485
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001486static struct nfsd4_session *
1487find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1488 __be32 *ret)
1489{
1490 struct nfsd4_session *session;
1491 __be32 status = nfserr_badsession;
1492
1493 session = __find_in_sessionid_hashtbl(sessionid, net);
1494 if (!session)
1495 goto out;
1496 status = nfsd4_get_session_locked(session);
1497 if (status)
1498 session = NULL;
1499out:
1500 *ret = status;
1501 return session;
1502}
1503
Benny Halevy9089f1b2010-05-12 00:12:26 +03001504/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001505static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001506unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001507{
1508 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001509 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001510 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001511 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001512}
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1515static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001516STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001518 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001520 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001521 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return 1;
1523}
1524
1525/*
1526 * XXX Should we use a slab cache ?
1527 * This type of memory management is somewhat inefficient, but we use it
1528 * anyway since SETCLIENTID is not a common operation.
1529 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001530static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
1532 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001533 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001535 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1536 if (clp == NULL)
1537 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001538 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001539 if (clp->cl_name.data == NULL)
1540 goto err_no_name;
1541 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1542 OWNER_HASH_SIZE, GFP_KERNEL);
1543 if (!clp->cl_ownerstr_hashtbl)
1544 goto err_no_hashtbl;
1545 for (i = 0; i < OWNER_HASH_SIZE; i++)
1546 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001547 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001548 INIT_LIST_HEAD(&clp->cl_sessions);
1549 idr_init(&clp->cl_stateids);
1550 atomic_set(&clp->cl_refcount, 0);
1551 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1552 INIT_LIST_HEAD(&clp->cl_idhash);
1553 INIT_LIST_HEAD(&clp->cl_openowners);
1554 INIT_LIST_HEAD(&clp->cl_delegations);
1555 INIT_LIST_HEAD(&clp->cl_lru);
1556 INIT_LIST_HEAD(&clp->cl_callbacks);
1557 INIT_LIST_HEAD(&clp->cl_revoked);
1558 spin_lock_init(&clp->cl_lock);
1559 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001561err_no_hashtbl:
1562 kfree(clp->cl_name.data);
1563err_no_name:
1564 kfree(clp);
1565 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001568static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569free_client(struct nfs4_client *clp)
1570{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001571 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001572
1573 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001574 while (!list_empty(&clp->cl_sessions)) {
1575 struct nfsd4_session *ses;
1576 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1577 se_perclnt);
1578 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001579 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1580 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001581 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001582 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001583 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001584 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001586 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 kfree(clp);
1588}
1589
Benny Halevy84d38ac2010-05-12 00:13:16 +03001590/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001591static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001592unhash_client_locked(struct nfs4_client *clp)
1593{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001594 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001595 struct nfsd4_session *ses;
1596
Trond Myklebust4beb3452014-07-30 08:27:02 -04001597 /* Mark the client as expired! */
1598 clp->cl_time = 0;
1599 /* Make it invisible */
1600 if (!list_empty(&clp->cl_idhash)) {
1601 list_del_init(&clp->cl_idhash);
1602 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1603 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1604 else
1605 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1606 }
1607 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001608 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001609 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1610 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001611 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001612}
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001615unhash_client(struct nfs4_client *clp)
1616{
1617 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1618
1619 spin_lock(&nn->client_lock);
1620 unhash_client_locked(clp);
1621 spin_unlock(&nn->client_lock);
1622}
1623
1624static void
1625__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001627 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001630 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001633 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001634 while (!list_empty(&clp->cl_delegations)) {
1635 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001636 unhash_delegation_locked(dp);
1637 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
Benny Halevycdc97502014-05-30 09:09:30 -04001639 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 while (!list_empty(&reaplist)) {
1641 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001642 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001643 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001645 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001646 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001647 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001648 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001649 }
NeilBrownea1da632005-06-23 22:04:17 -07001650 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001651 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Jeff Laytond3134b12014-07-29 21:34:32 -04001652 atomic_inc(&oo->oo_owner.so_count);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001653 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001655 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001656 if (clp->cl_cb_conn.cb_xprt)
1657 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001658 spin_lock(&nn->client_lock);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001659 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1660 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001661 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662}
1663
Trond Myklebust4beb3452014-07-30 08:27:02 -04001664static void
1665destroy_client(struct nfs4_client *clp)
1666{
1667 unhash_client(clp);
1668 __destroy_client(clp);
1669}
1670
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001671static void expire_client(struct nfs4_client *clp)
1672{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001673 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001674 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001675 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001676}
1677
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001678static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1679{
1680 memcpy(target->cl_verifier.data, source->data,
1681 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682}
1683
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001684static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1685{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1687 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1688}
1689
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001690static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001691{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001692 if (source->cr_principal) {
1693 target->cr_principal =
1694 kstrdup(source->cr_principal, GFP_KERNEL);
1695 if (target->cr_principal == NULL)
1696 return -ENOMEM;
1697 } else
1698 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001699 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 target->cr_uid = source->cr_uid;
1701 target->cr_gid = source->cr_gid;
1702 target->cr_group_info = source->cr_group_info;
1703 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001704 target->cr_gss_mech = source->cr_gss_mech;
1705 if (source->cr_gss_mech)
1706 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001707 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}
1709
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001710static long long
1711compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1712{
1713 long long res;
1714
1715 res = o1->len - o2->len;
1716 if (res)
1717 return res;
1718 return (long long)memcmp(o1->data, o2->data, o1->len);
1719}
1720
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001721static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001722{
NeilBrowna55370a2005-06-23 22:03:52 -07001723 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
1726static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001727same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1728{
1729 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}
1731
1732static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001733same_clid(clientid_t *cl1, clientid_t *cl2)
1734{
1735 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736}
1737
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001738static bool groups_equal(struct group_info *g1, struct group_info *g2)
1739{
1740 int i;
1741
1742 if (g1->ngroups != g2->ngroups)
1743 return false;
1744 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001745 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001746 return false;
1747 return true;
1748}
1749
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001750/*
1751 * RFC 3530 language requires clid_inuse be returned when the
1752 * "principal" associated with a requests differs from that previously
1753 * used. We use uid, gid's, and gss principal string as our best
1754 * approximation. We also don't want to allow non-gss use of a client
1755 * established using gss: in theory cr_principal should catch that
1756 * change, but in practice cr_principal can be null even in the gss case
1757 * since gssd doesn't always pass down a principal string.
1758 */
1759static bool is_gss_cred(struct svc_cred *cr)
1760{
1761 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1762 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1763}
1764
1765
Vivek Trivedi5559b502012-07-24 21:18:20 +05301766static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001767same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1768{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001769 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001770 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1771 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001772 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1773 return false;
1774 if (cr1->cr_principal == cr2->cr_principal)
1775 return true;
1776 if (!cr1->cr_principal || !cr2->cr_principal)
1777 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301778 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779}
1780
J. Bruce Fields57266a62013-04-13 14:27:29 -04001781static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1782{
1783 struct svc_cred *cr = &rqstp->rq_cred;
1784 u32 service;
1785
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001786 if (!cr->cr_gss_mech)
1787 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001788 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1789 return service == RPC_GSS_SVC_INTEGRITY ||
1790 service == RPC_GSS_SVC_PRIVACY;
1791}
1792
1793static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1794{
1795 struct svc_cred *cr = &rqstp->rq_cred;
1796
1797 if (!cl->cl_mach_cred)
1798 return true;
1799 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1800 return false;
1801 if (!svc_rqst_integrity_protected(rqstp))
1802 return false;
1803 if (!cr->cr_principal)
1804 return false;
1805 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1806}
1807
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001808static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001809{
1810 static u32 current_clientid = 1;
1811
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001812 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 clp->cl_clientid.cl_id = current_clientid++;
1814}
1815
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001816static void gen_confirm(struct nfs4_client *clp)
1817{
Chuck Leverab4684d2012-03-02 17:13:50 -05001818 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001819 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Jeff Laytonf4199922014-06-17 07:44:11 -04001821 /*
1822 * This is opaque to client, so no need to byte-swap. Use
1823 * __force to keep sparse happy
1824 */
1825 verf[0] = (__force __be32)get_seconds();
1826 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001827 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
1829
Jeff Layton4770d722014-07-29 21:34:10 -04001830static struct nfs4_stid *
1831find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001832{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001833 struct nfs4_stid *ret;
1834
1835 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1836 if (!ret || !ret->sc_type)
1837 return NULL;
1838 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001839}
1840
Jeff Layton4770d722014-07-29 21:34:10 -04001841static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001842find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001843{
1844 struct nfs4_stid *s;
1845
Jeff Layton4770d722014-07-29 21:34:10 -04001846 spin_lock(&cl->cl_lock);
1847 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001848 if (s != NULL) {
1849 if (typemask & s->sc_type)
1850 atomic_inc(&s->sc_count);
1851 else
1852 s = NULL;
1853 }
Jeff Layton4770d722014-07-29 21:34:10 -04001854 spin_unlock(&cl->cl_lock);
1855 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001856}
1857
Jeff Layton2216d442012-11-12 15:00:57 -05001858static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001859 struct svc_rqst *rqstp, nfs4_verifier *verf)
1860{
1861 struct nfs4_client *clp;
1862 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001863 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001864 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001865 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001866
1867 clp = alloc_client(name);
1868 if (clp == NULL)
1869 return NULL;
1870
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001871 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1872 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001873 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001874 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001875 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001876 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001877 }
Jeff Layton02e12152014-07-16 10:31:57 -04001878 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001879 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001880 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001881 copy_verf(clp, verf);
1882 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001883 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001884 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001885 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001886 return clp;
1887}
1888
NeilBrownfd39ca92005-06-23 22:04:03 -07001889static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001890add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1891{
1892 struct rb_node **new = &(root->rb_node), *parent = NULL;
1893 struct nfs4_client *clp;
1894
1895 while (*new) {
1896 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1897 parent = *new;
1898
1899 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1900 new = &((*new)->rb_left);
1901 else
1902 new = &((*new)->rb_right);
1903 }
1904
1905 rb_link_node(&new_clp->cl_namenode, parent, new);
1906 rb_insert_color(&new_clp->cl_namenode, root);
1907}
1908
1909static struct nfs4_client *
1910find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1911{
1912 long long cmp;
1913 struct rb_node *node = root->rb_node;
1914 struct nfs4_client *clp;
1915
1916 while (node) {
1917 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1918 cmp = compare_blob(&clp->cl_name, name);
1919 if (cmp > 0)
1920 node = node->rb_left;
1921 else if (cmp < 0)
1922 node = node->rb_right;
1923 else
1924 return clp;
1925 }
1926 return NULL;
1927}
1928
1929static void
1930add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
1932 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001933 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001935 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001936 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001938 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001939 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940}
1941
NeilBrownfd39ca92005-06-23 22:04:03 -07001942static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943move_to_confirmed(struct nfs4_client *clp)
1944{
1945 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001946 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
1948 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001949 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001950 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001951 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001952 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 renew_client(clp);
1954}
1955
1956static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001957find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
1959 struct nfs4_client *clp;
1960 unsigned int idhashval = clientid_hashval(clid->cl_id);
1961
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001962 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001963 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001964 if ((bool)clp->cl_minorversion != sessions)
1965 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001966 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 }
1970 return NULL;
1971}
1972
1973static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001974find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1975{
1976 struct list_head *tbl = nn->conf_id_hashtbl;
1977
1978 return find_client_in_id_table(tbl, clid, sessions);
1979}
1980
1981static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001982find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001984 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001986 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987}
1988
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001989static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001990{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001991 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001992}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001993
NeilBrown28ce6052005-06-23 22:03:56 -07001994static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001995find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001996{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001997 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001998}
1999
2000static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002001find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002002{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002003 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002004}
2005
NeilBrownfd39ca92005-06-23 22:04:03 -07002006static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002007gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002009 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002010 struct sockaddr *sa = svc_addr(rqstp);
2011 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002012 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Jeff Layton7077ecb2009-08-14 12:57:58 -04002014 /* Currently, we only support tcp and tcp6 for the callback channel */
2015 if (se->se_callback_netid_len == 3 &&
2016 !memcmp(se->se_callback_netid_val, "tcp", 3))
2017 expected_family = AF_INET;
2018 else if (se->se_callback_netid_len == 4 &&
2019 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2020 expected_family = AF_INET6;
2021 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 goto out_err;
2023
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002024 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002025 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002026 (struct sockaddr *)&conn->cb_addr,
2027 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002028
J. Bruce Fields07263f12010-05-31 19:09:40 -04002029 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002031
J. Bruce Fields07263f12010-05-31 19:09:40 -04002032 if (conn->cb_addr.ss_family == AF_INET6)
2033 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002034
J. Bruce Fields07263f12010-05-31 19:09:40 -04002035 conn->cb_prog = se->se_callback_prog;
2036 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002037 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 return;
2039out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002040 conn->cb_addr.ss_family = AF_UNSPEC;
2041 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002042 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 "will not receive delegations\n",
2044 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 return;
2047}
2048
Andy Adamson074fe892009-04-03 08:28:15 +03002049/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002050 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002051 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002052static void
Andy Adamson074fe892009-04-03 08:28:15 +03002053nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2054{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002055 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002056 struct nfsd4_slot *slot = resp->cstate.slot;
2057 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002058
Andy Adamson557ce262009-08-28 08:45:04 -04002059 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002060
Andy Adamson557ce262009-08-28 08:45:04 -04002061 slot->sl_opcnt = resp->opcnt;
2062 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002063
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002064 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002065 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002066 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002067 return;
2068 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002069 base = resp->cstate.data_offset;
2070 slot->sl_datalen = buf->len - base;
2071 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04002072 WARN("%s: sessions DRC could not cache compound\n", __func__);
2073 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002074}
2075
2076/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002077 * Encode the replay sequence operation from the slot values.
2078 * If cachethis is FALSE encode the uncached rep error on the next
2079 * operation which sets resp->p and increments resp->opcnt for
2080 * nfs4svc_encode_compoundres.
2081 *
Andy Adamson074fe892009-04-03 08:28:15 +03002082 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002083static __be32
2084nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2085 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002086{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002087 struct nfsd4_op *op;
2088 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002089
Andy Adamsonabfabf82009-07-23 19:02:18 -04002090 /* Encode the replayed sequence operation */
2091 op = &args->ops[resp->opcnt - 1];
2092 nfsd4_encode_operation(resp, op);
2093
2094 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002095 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002096 op = &args->ops[resp->opcnt++];
2097 op->status = nfserr_retry_uncached_rep;
2098 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002099 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002100 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002101}
2102
2103/*
Andy Adamson557ce262009-08-28 08:45:04 -04002104 * The sequence operation is not cached because we can use the slot and
2105 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002106 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002107static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002108nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2109 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002110{
Andy Adamson557ce262009-08-28 08:45:04 -04002111 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002112 struct xdr_stream *xdr = &resp->xdr;
2113 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002114 __be32 status;
2115
Andy Adamson557ce262009-08-28 08:45:04 -04002116 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002117
Andy Adamsonabfabf82009-07-23 19:02:18 -04002118 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002119 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002120 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002121
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002122 p = xdr_reserve_space(xdr, slot->sl_datalen);
2123 if (!p) {
2124 WARN_ON_ONCE(1);
2125 return nfserr_serverfault;
2126 }
2127 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2128 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002129
Andy Adamson557ce262009-08-28 08:45:04 -04002130 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002131 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002132}
2133
Andy Adamson0733d212009-04-03 08:28:01 +03002134/*
2135 * Set the exchange_id flags returned by the server.
2136 */
2137static void
2138nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2139{
2140 /* pNFS is not supported */
2141 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2142
2143 /* Referrals are supported, Migration is not. */
2144 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2145
2146 /* set the wire flags to return to client. */
2147 clid->flags = new->cl_exchange_flags;
2148}
2149
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002150static bool client_has_state(struct nfs4_client *clp)
2151{
2152 /*
2153 * Note clp->cl_openowners check isn't quite right: there's no
2154 * need to count owners without stateid's.
2155 *
2156 * Also note we should probably be using this in 4.0 case too.
2157 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002158 return !list_empty(&clp->cl_openowners)
2159 || !list_empty(&clp->cl_delegations)
2160 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002161}
2162
Al Virob37ad282006-10-19 23:28:59 -07002163__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002164nfsd4_exchange_id(struct svc_rqst *rqstp,
2165 struct nfsd4_compound_state *cstate,
2166 struct nfsd4_exchange_id *exid)
2167{
Andy Adamson0733d212009-04-03 08:28:01 +03002168 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002169 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002170 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002171 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002172 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002173 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002174 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002175
Jeff Layton363168b2009-08-14 12:57:56 -04002176 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002177 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002178 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002179 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002180 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002181
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002182 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002183 return nfserr_inval;
2184
Andy Adamson0733d212009-04-03 08:28:01 +03002185 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002186 case SP4_MACH_CRED:
2187 if (!svc_rqst_integrity_protected(rqstp))
2188 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002189 case SP4_NONE:
2190 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002191 default: /* checked by xdr code */
2192 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002193 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002194 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002195 }
2196
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002197 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03002198 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002199 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002200 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002201 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2202 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2203
J. Bruce Fields136e6582012-05-12 20:37:23 -04002204 if (update) {
2205 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002206 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002207 goto out;
2208 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002209 if (!mach_creds_match(conf, rqstp)) {
2210 status = nfserr_wrong_cred;
2211 goto out;
2212 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002213 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002214 status = nfserr_perm;
2215 goto out;
2216 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002217 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002218 status = nfserr_not_same;
2219 goto out;
2220 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002221 /* case 6 */
2222 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2223 new = conf;
2224 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002225 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002226 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002227 if (client_has_state(conf)) {
2228 status = nfserr_clid_inuse;
2229 goto out;
2230 }
Andy Adamson0733d212009-04-03 08:28:01 +03002231 expire_client(conf);
2232 goto out_new;
2233 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002234 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002235 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002236 new = conf;
2237 goto out_copy;
2238 }
2239 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04002240 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002241 }
2242
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002243 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002244 status = nfserr_noent;
2245 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002246 }
2247
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002248 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002249 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03002250 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002251
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002252 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002253out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05002254 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03002255 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04002256 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03002257 goto out;
2258 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002259 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002260 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002261
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002262 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002263 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03002264out_copy:
2265 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2266 exid->clientid.cl_id = new->cl_clientid.cl_id;
2267
J. Bruce Fields778df3f2012-05-25 21:40:23 -04002268 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03002269 nfsd4_set_ex_flags(new, exid);
2270
2271 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04002272 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002273 status = nfs_ok;
2274
2275out:
2276 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03002277 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002278}
2279
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002280static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002281check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002282{
Andy Adamson88e588d2009-07-23 19:02:15 -04002283 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2284 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002285
2286 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002287 if (slot_inuse) {
2288 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002289 return nfserr_jukebox;
2290 else
2291 return nfserr_seq_misordered;
2292 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002293 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002294 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002295 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002296 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002297 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002298 return nfserr_seq_misordered;
2299}
2300
Andy Adamson49557cc2009-07-23 19:02:16 -04002301/*
2302 * Cache the create session result into the create session single DRC
2303 * slot cache by saving the xdr structure. sl_seqid has been set.
2304 * Do this for solo or embedded create session operations.
2305 */
2306static void
2307nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002308 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002309{
2310 slot->sl_status = nfserr;
2311 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2312}
2313
2314static __be32
2315nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2316 struct nfsd4_clid_slot *slot)
2317{
2318 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2319 return slot->sl_status;
2320}
2321
Mi Jinlong1b74c252011-07-14 14:50:17 +08002322#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2323 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2324 1 + /* MIN tag is length with zero, only length */ \
2325 3 + /* version, opcount, opcode */ \
2326 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2327 /* seqid, slotID, slotID, cache */ \
2328 4 ) * sizeof(__be32))
2329
2330#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2331 2 + /* verifier: AUTH_NULL, length 0 */\
2332 1 + /* status */ \
2333 1 + /* MIN tag is length with zero, only length */ \
2334 3 + /* opcount, opcode, opstatus*/ \
2335 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2336 /* seqid, slotID, slotID, slotID, status */ \
2337 5 ) * sizeof(__be32))
2338
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002339static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002340{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002341 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2342
J. Bruce Fields373cd402013-04-08 15:42:12 -04002343 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2344 return nfserr_toosmall;
2345 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2346 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002347 ca->headerpadsz = 0;
2348 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2349 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2350 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2351 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2352 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2353 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2354 /*
2355 * Note decreasing slot size below client's request may make it
2356 * difficult for client to function correctly, whereas
2357 * decreasing the number of slots will (just?) affect
2358 * performance. When short on memory we therefore prefer to
2359 * decrease number of slots instead of their size. Clients that
2360 * request larger slots than they need will get poor results:
2361 */
2362 ca->maxreqs = nfsd4_get_drc_mem(ca);
2363 if (!ca->maxreqs)
2364 return nfserr_jukebox;
2365
J. Bruce Fields373cd402013-04-08 15:42:12 -04002366 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002367}
2368
Kinglong Mee8a891632013-12-23 18:11:02 +08002369#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2370 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2371#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2372 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2373
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002374static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2375{
2376 ca->headerpadsz = 0;
2377
2378 /*
2379 * These RPC_MAX_HEADER macros are overkill, especially since we
2380 * don't even do gss on the backchannel yet. But this is still
2381 * less than 1k. Tighten up this estimate in the unlikely event
2382 * it turns out to be a problem for some client:
2383 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002384 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002385 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002386 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002387 return nfserr_toosmall;
2388 ca->maxresp_cached = 0;
2389 if (ca->maxops < 2)
2390 return nfserr_toosmall;
2391
2392 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002393}
2394
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002395static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2396{
2397 switch (cbs->flavor) {
2398 case RPC_AUTH_NULL:
2399 case RPC_AUTH_UNIX:
2400 return nfs_ok;
2401 default:
2402 /*
2403 * GSS case: the spec doesn't allow us to return this
2404 * error. But it also doesn't allow us not to support
2405 * GSS.
2406 * I'd rather this fail hard than return some error the
2407 * client might think it can already handle:
2408 */
2409 return nfserr_encr_alg_unsupp;
2410 }
2411}
2412
Andy Adamson069b6ad2009-04-03 08:27:58 +03002413__be32
2414nfsd4_create_session(struct svc_rqst *rqstp,
2415 struct nfsd4_compound_state *cstate,
2416 struct nfsd4_create_session *cr_ses)
2417{
Jeff Layton363168b2009-08-14 12:57:56 -04002418 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002419 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002420 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002421 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002422 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002423 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002424 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002425
Mi Jinlonga62573d2011-03-23 17:57:07 +08002426 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2427 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002428 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2429 if (status)
2430 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002431 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002432 if (status)
2433 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002434 status = check_backchannel_attrs(&cr_ses->back_channel);
2435 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002436 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002437 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002438 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002439 if (!new)
2440 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002441 conn = alloc_conn_from_crses(rqstp, cr_ses);
2442 if (!conn)
2443 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002444
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002445 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002446 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002447 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002448 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002449
2450 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002451 status = nfserr_wrong_cred;
2452 if (!mach_creds_match(conf, rqstp))
2453 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002454 cs_slot = &conf->cl_cs_slot;
2455 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002456 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002457 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002458 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002459 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002460 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002461 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002462 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002463 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002464 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002465 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002466 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002467 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002468 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002469 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002470 status = nfserr_wrong_cred;
2471 if (!mach_creds_match(unconf, rqstp))
2472 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002473 cs_slot = &unconf->cl_cs_slot;
2474 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002475 if (status) {
2476 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002477 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002478 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002479 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002480 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002481 if (old) {
2482 status = mark_client_expired(old);
2483 if (status)
2484 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002485 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002486 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002487 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002488 conf = unconf;
2489 } else {
2490 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002491 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002492 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002493 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002494 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002495 * We do not support RDMA or persistent sessions
2496 */
2497 cr_ses->flags &= ~SESSION4_PERSIST;
2498 cr_ses->flags &= ~SESSION4_RDMA;
2499
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002500 init_session(rqstp, new, conf, cr_ses);
2501 nfsd4_init_conn(rqstp, conn, new);
2502
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002503 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002504 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002505 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002506 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002507
Andy Adamson49557cc2009-07-23 19:02:16 -04002508 /* cache solo and embedded create sessions under the state lock */
2509 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002510 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002511 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002512out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08002513 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002514 free_conn(conn);
2515out_free_session:
2516 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002517out_release_drc_mem:
2518 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002519 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002520}
2521
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002522static __be32 nfsd4_map_bcts_dir(u32 *dir)
2523{
2524 switch (*dir) {
2525 case NFS4_CDFC4_FORE:
2526 case NFS4_CDFC4_BACK:
2527 return nfs_ok;
2528 case NFS4_CDFC4_FORE_OR_BOTH:
2529 case NFS4_CDFC4_BACK_OR_BOTH:
2530 *dir = NFS4_CDFC4_BOTH;
2531 return nfs_ok;
2532 };
2533 return nfserr_inval;
2534}
2535
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002536__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2537{
2538 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002539 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002540 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002541
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002542 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2543 if (status)
2544 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002545 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002546 session->se_cb_prog = bc->bc_cb_program;
2547 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002548 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002549
2550 nfsd4_probe_callback(session->se_client);
2551
2552 return nfs_ok;
2553}
2554
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002555__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2556 struct nfsd4_compound_state *cstate,
2557 struct nfsd4_bind_conn_to_session *bcts)
2558{
2559 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002560 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002561 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002562 struct net *net = SVC_NET(rqstp);
2563 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002564
2565 if (!nfsd4_last_compound_op(rqstp))
2566 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002567 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002568 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002569 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002570 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002571 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002572 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002573 status = nfserr_wrong_cred;
2574 if (!mach_creds_match(session->se_client, rqstp))
2575 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002576 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002577 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002578 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002579 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002580 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002581 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002582 goto out;
2583 nfsd4_init_conn(rqstp, conn, session);
2584 status = nfs_ok;
2585out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002586 nfsd4_put_session(session);
2587out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002588 nfs4_unlock_state();
2589 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002590}
2591
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002592static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2593{
2594 if (!session)
2595 return 0;
2596 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2597}
2598
Andy Adamson069b6ad2009-04-03 08:27:58 +03002599__be32
2600nfsd4_destroy_session(struct svc_rqst *r,
2601 struct nfsd4_compound_state *cstate,
2602 struct nfsd4_destroy_session *sessionid)
2603{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002604 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002605 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002606 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002607 struct net *net = SVC_NET(r);
2608 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002609
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002610 nfs4_lock_state();
2611 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002612 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002613 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002614 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002615 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002616 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002617 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002618 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002619 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002620 if (!ses)
2621 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002622 status = nfserr_wrong_cred;
2623 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002624 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002625 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002626 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002627 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002628 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002629 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002630
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002631 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002632
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002633 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002634 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002635out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002636 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002637out_client_lock:
2638 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002639out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002640 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002641 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002642}
2643
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002644static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002645{
2646 struct nfsd4_conn *c;
2647
2648 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002649 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002650 return c;
2651 }
2652 }
2653 return NULL;
2654}
2655
J. Bruce Fields57266a62013-04-13 14:27:29 -04002656static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002657{
2658 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002659 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002660 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002661 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002662
2663 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002664 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002665 if (c)
2666 goto out_free;
2667 status = nfserr_conn_not_bound_to_session;
2668 if (clp->cl_mach_cred)
2669 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002670 __nfsd4_hash_conn(new, ses);
2671 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002672 ret = nfsd4_register_conn(new);
2673 if (ret)
2674 /* oops; xprt is already down: */
2675 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002676 return nfs_ok;
2677out_free:
2678 spin_unlock(&clp->cl_lock);
2679 free_conn(new);
2680 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002681}
2682
Mi Jinlong868b89c2011-04-27 09:09:58 +08002683static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2684{
2685 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2686
2687 return args->opcnt > session->se_fchannel.maxops;
2688}
2689
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002690static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2691 struct nfsd4_session *session)
2692{
2693 struct xdr_buf *xb = &rqstp->rq_arg;
2694
2695 return xb->len > session->se_fchannel.maxreq_sz;
2696}
2697
Andy Adamson069b6ad2009-04-03 08:27:58 +03002698__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002699nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002700 struct nfsd4_compound_state *cstate,
2701 struct nfsd4_sequence *seq)
2702{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002703 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002704 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002705 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002706 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002707 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002708 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002709 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002710 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002711 struct net *net = SVC_NET(rqstp);
2712 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002713
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002714 if (resp->opcnt != 1)
2715 return nfserr_sequence_pos;
2716
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002717 /*
2718 * Will be either used or freed by nfsd4_sequence_check_conn
2719 * below.
2720 */
2721 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2722 if (!conn)
2723 return nfserr_jukebox;
2724
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002725 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002726 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002727 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002728 goto out_no_session;
2729 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002730
Mi Jinlong868b89c2011-04-27 09:09:58 +08002731 status = nfserr_too_many_ops;
2732 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002733 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002734
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002735 status = nfserr_req_too_big;
2736 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002737 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002738
Benny Halevyb85d4c02009-04-03 08:28:08 +03002739 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002740 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002741 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002742
Andy Adamson557ce262009-08-28 08:45:04 -04002743 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002744 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2745
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002746 /* We do not negotiate the number of slots yet, so set the
2747 * maxslots to the session maxreqs which is used to encode
2748 * sr_highest_slotid and the sr_target_slot id to maxslots */
2749 seq->maxslots = session->se_fchannel.maxreqs;
2750
J. Bruce Fields73e79482012-02-13 16:39:00 -05002751 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2752 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002753 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002754 status = nfserr_seq_misordered;
2755 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002756 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002757 cstate->slot = slot;
2758 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002759 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002760 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002761 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002762 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002763 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002764 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002765 }
2766 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002767 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002768
J. Bruce Fields57266a62013-04-13 14:27:29 -04002769 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002770 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002771 if (status)
2772 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002773
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002774 buflen = (seq->cachethis) ?
2775 session->se_fchannel.maxresp_cached :
2776 session->se_fchannel.maxresp_sz;
2777 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2778 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002779 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002780 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002781 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002782
2783 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002784 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002785 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002786 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002787 if (seq->cachethis)
2788 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002789 else
2790 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002791
2792 cstate->slot = slot;
2793 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002794 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002795
Benny Halevyb85d4c02009-04-03 08:28:08 +03002796out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002797 switch (clp->cl_cb_state) {
2798 case NFSD4_CB_DOWN:
2799 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2800 break;
2801 case NFSD4_CB_FAULT:
2802 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2803 break;
2804 default:
2805 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002806 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002807 if (!list_empty(&clp->cl_revoked))
2808 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002809out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002810 if (conn)
2811 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002812 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002813 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002814out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002815 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002816 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002817}
2818
Trond Myklebustb6076642014-06-30 11:48:35 -04002819void
2820nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2821{
2822 struct nfsd4_compound_state *cs = &resp->cstate;
2823
2824 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002825 if (cs->status != nfserr_replay_cache) {
2826 nfsd4_store_cache_entry(resp);
2827 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2828 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002829 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002830 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002831 } else if (cs->clp)
2832 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002833}
2834
Mi Jinlong345c2842011-10-20 17:51:39 +08002835__be32
2836nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2837{
2838 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002839 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002840 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002841
2842 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002843 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002844 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002845 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002846
2847 if (conf) {
2848 clp = conf;
2849
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002850 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002851 status = nfserr_clientid_busy;
2852 goto out;
2853 }
2854 } else if (unconf)
2855 clp = unconf;
2856 else {
2857 status = nfserr_stale_clientid;
2858 goto out;
2859 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002860 if (!mach_creds_match(clp, rqstp)) {
2861 status = nfserr_wrong_cred;
2862 goto out;
2863 }
Mi Jinlong345c2842011-10-20 17:51:39 +08002864 expire_client(clp);
2865out:
2866 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002867 return status;
2868}
2869
Andy Adamson069b6ad2009-04-03 08:27:58 +03002870__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002871nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2872{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002873 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002874
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002875 if (rc->rca_one_fs) {
2876 if (!cstate->current_fh.fh_dentry)
2877 return nfserr_nofilehandle;
2878 /*
2879 * We don't take advantage of the rca_one_fs case.
2880 * That's OK, it's optional, we can safely ignore it.
2881 */
2882 return nfs_ok;
2883 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002884
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002885 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002886 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002887 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2888 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002889 goto out;
2890
2891 status = nfserr_stale_clientid;
2892 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002893 /*
2894 * The following error isn't really legal.
2895 * But we only get here if the client just explicitly
2896 * destroyed the client. Surely it no longer cares what
2897 * error it gets back on an operation for the dead
2898 * client.
2899 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002900 goto out;
2901
2902 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002903 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002904out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002905 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002906 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002907}
2908
2909__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002910nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2911 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002913 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002915 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002916 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002917 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2918
J. Bruce Fields63db4632012-05-18 21:54:19 -04002919 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002921 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002922 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002923 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002925 if (clp_used_exchangeid(conf))
2926 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002927 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002928 char addr_str[INET6_ADDRSTRLEN];
2929 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2930 sizeof(addr_str));
2931 dprintk("NFSD: setclientid: string in use by client "
2932 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 goto out;
2934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002936 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002937 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002940 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002941 if (new == NULL)
2942 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002943 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002944 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002946 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002947 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002948 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002949 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002950 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2952 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2953 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2954 status = nfs_ok;
2955out:
2956 nfs4_unlock_state();
2957 return status;
2958}
2959
2960
Al Virob37ad282006-10-19 23:28:59 -07002961__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002962nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2963 struct nfsd4_compound_state *cstate,
2964 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965{
NeilBrown21ab45a2005-06-23 22:04:14 -07002966 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2968 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002969 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002970 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002972 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002975
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002976 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002977 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002978 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002979 * We try hard to give out unique clientid's, so if we get an
2980 * attempt to confirm the same clientid with a different cred,
2981 * there's a bug somewhere. Let's charitably assume it's our
2982 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002983 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002984 status = nfserr_serverfault;
2985 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2986 goto out;
2987 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2988 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002989 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002990 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2991 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002993 else /* case 4: client hasn't noticed we rebooted yet? */
2994 status = nfserr_stale_clientid;
2995 goto out;
2996 }
2997 status = nfs_ok;
2998 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002999 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
3000 nfsd4_probe_callback(conf);
3001 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003002 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003003 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003004 if (conf) {
3005 status = mark_client_expired(conf);
3006 if (status)
3007 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04003008 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003009 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003010 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04003011 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07003012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 nfs4_unlock_state();
3015 return status;
3016}
3017
J. Bruce Fields32513b42011-10-13 16:00:16 -04003018static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003020 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3021}
3022
3023/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003024static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003025{
Trond Myklebustca943212014-07-23 16:17:39 -04003026 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
Trond Myklebust950e0112014-06-30 11:48:31 -04003028 lockdep_assert_held(&state_lock);
3029
J. Bruce Fields32513b42011-10-13 16:00:16 -04003030 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003031 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003032 INIT_LIST_HEAD(&fp->fi_stateids);
3033 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003034 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003035 fp->fi_had_conflict = false;
3036 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003037 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003038 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3039 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04003040 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041}
3042
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003043void
NeilBrowne60d4392005-06-23 22:03:01 -07003044nfsd4_free_slabs(void)
3045{
Christoph Hellwigabf11352014-05-21 07:43:03 -07003046 kmem_cache_destroy(openowner_slab);
3047 kmem_cache_destroy(lockowner_slab);
3048 kmem_cache_destroy(file_slab);
3049 kmem_cache_destroy(stateid_slab);
3050 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003051}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Bryan Schumaker72083392011-11-01 15:24:59 -04003053int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054nfsd4_init_slabs(void)
3055{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003056 openowner_slab = kmem_cache_create("nfsd4_openowners",
3057 sizeof(struct nfs4_openowner), 0, 0, NULL);
3058 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003059 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003060 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003061 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003062 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003063 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003064 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003065 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003066 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003067 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003068 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003069 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003070 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003071 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003072 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003073 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003074 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003075 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003077
3078out_free_stateid_slab:
3079 kmem_cache_destroy(stateid_slab);
3080out_free_file_slab:
3081 kmem_cache_destroy(file_slab);
3082out_free_lockowner_slab:
3083 kmem_cache_destroy(lockowner_slab);
3084out_free_openowner_slab:
3085 kmem_cache_destroy(openowner_slab);
3086out:
NeilBrowne60d4392005-06-23 22:03:01 -07003087 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3088 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089}
3090
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003091static void init_nfs4_replay(struct nfs4_replay *rp)
3092{
3093 rp->rp_status = nfserr_serverfault;
3094 rp->rp_buflen = 0;
3095 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003096 mutex_init(&rp->rp_mutex);
3097}
3098
3099static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3100 struct nfs4_stateowner *so)
3101{
3102 if (!nfsd4_has_session(cstate)) {
3103 mutex_lock(&so->so_replay.rp_mutex);
3104 cstate->replay_owner = so;
3105 atomic_inc(&so->so_count);
3106 }
3107}
3108
3109void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3110{
3111 struct nfs4_stateowner *so = cstate->replay_owner;
3112
3113 if (so != NULL) {
3114 cstate->replay_owner = NULL;
3115 mutex_unlock(&so->so_replay.rp_mutex);
3116 nfs4_put_stateowner(so);
3117 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003118}
3119
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003120static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121{
3122 struct nfs4_stateowner *sop;
3123
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003124 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003125 if (!sop)
3126 return NULL;
3127
3128 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3129 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003130 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003131 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003133 sop->so_owner.len = owner->len;
3134
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003135 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003136 sop->so_client = clp;
3137 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003138 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003139 return sop;
3140}
3141
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003142static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003143{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003144 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003145
Trond Myklebustd4f04892014-07-29 21:34:36 -04003146 list_add(&oo->oo_owner.so_strhash,
3147 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003148 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149}
3150
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003151static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3152{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003153 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003154}
3155
Jeff Layton6b180f02014-07-29 21:34:26 -04003156static void nfs4_free_openowner(struct nfs4_stateowner *so)
3157{
3158 struct nfs4_openowner *oo = openowner(so);
3159
3160 kmem_cache_free(openowner_slab, oo);
3161}
3162
3163static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003164 .so_unhash = nfs4_unhash_openowner,
3165 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003166};
3167
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003168static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003169alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003170 struct nfsd4_compound_state *cstate)
3171{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003172 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003173 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003175 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3176 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003178 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003179 oo->oo_owner.so_is_open_owner = 1;
3180 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003181 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003182 if (nfsd4_has_session(cstate))
3183 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003184 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003185 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003186 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003187 spin_lock(&clp->cl_lock);
3188 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003189 if (ret == NULL) {
3190 hash_openowner(oo, clp, strhashval);
3191 ret = oo;
3192 } else
3193 nfs4_free_openowner(&oo->oo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003194 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003195 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196}
3197
J. Bruce Fields996e0932011-10-17 11:14:48 -04003198static 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 -04003199 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003201 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003202 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003203 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003204 stp->st_stateowner = &oo->oo_owner;
Jeff Laytond3134b12014-07-29 21:34:32 -04003205 atomic_inc(&stp->st_stateowner->so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07003206 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003207 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 stp->st_access_bmap = 0;
3209 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003210 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003211 spin_lock(&oo->oo_owner.so_client->cl_lock);
3212 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003213 spin_lock(&fp->fi_lock);
3214 list_add(&stp->st_perfile, &fp->fi_stateids);
3215 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003216 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217}
3218
Jeff Laytond3134b12014-07-29 21:34:32 -04003219/*
3220 * In the 4.0 case we need to keep the owners around a little while to handle
3221 * CLOSE replay. We still do need to release any file access that is held by
3222 * them before returning however.
3223 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003225move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226{
Jeff Laytond3134b12014-07-29 21:34:32 -04003227 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3228 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3229 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003230
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003231 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
Jeff Laytonb401be222014-07-29 21:34:33 -04003233 /*
3234 * We know that we hold one reference via nfsd4_close, and another
3235 * "persistent" reference for the client. If the refcount is higher
3236 * than 2, then there are still calls in progress that are using this
3237 * stateid. We can't put the sc_file reference until they are finished.
3238 * Wait for the refcount to drop to 2. Since it has been unhashed,
3239 * there should be no danger of the refcount going back up again at
3240 * this point.
3241 */
3242 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3243
Jeff Laytond3134b12014-07-29 21:34:32 -04003244 release_all_access(s);
3245 if (s->st_stid.sc_file) {
3246 put_nfs4_file(s->st_stid.sc_file);
3247 s->st_stid.sc_file = NULL;
3248 }
3249 release_last_closed_stateid(oo);
3250 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003251 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003252 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253}
3254
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255/* search file_hashtbl[] for file */
3256static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003257find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258{
Trond Myklebustca943212014-07-23 16:17:39 -04003259 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 struct nfs4_file *fp;
3261
Trond Myklebust950e0112014-06-30 11:48:31 -04003262 lockdep_assert_held(&state_lock);
3263
Jeff Layton89876f82013-04-02 09:01:59 -04003264 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003265 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003266 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 }
3270 return NULL;
3271}
3272
Trond Myklebust950e0112014-06-30 11:48:31 -04003273static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003274find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003275{
3276 struct nfs4_file *fp;
3277
3278 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003279 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003280 spin_unlock(&state_lock);
3281 return fp;
3282}
3283
3284static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003285find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003286{
3287 struct nfs4_file *fp;
3288
3289 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003290 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003291 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003292 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003293 fp = new;
3294 }
3295 spin_unlock(&state_lock);
3296
3297 return fp;
3298}
3299
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003300/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 * Called to check deny when READ with all zero stateid or
3302 * WRITE with all zero or all one stateid
3303 */
Al Virob37ad282006-10-19 23:28:59 -07003304static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3306{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003308 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Trond Myklebustca943212014-07-23 16:17:39 -04003310 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003311 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003312 return ret;
3313 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003314 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003315 if (fp->fi_share_deny & deny_type)
3316 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003317 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003318 put_nfs4_file(fp);
3319 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320}
3321
Jeff Layton02e12152014-07-16 10:31:57 -04003322void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323{
Trond Myklebust11b91642014-07-29 21:34:08 -04003324 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3325 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003326
Trond Myklebust11b91642014-07-29 21:34:08 -04003327 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003328
Jeff Layton02e12152014-07-16 10:31:57 -04003329 /*
3330 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003331 * already holding inode->i_lock.
3332 *
Jeff Laytondff13992014-07-08 14:02:49 -04003333 * If the dl_time != 0, then we know that it has already been
3334 * queued for a lease break. Don't queue it again.
3335 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003336 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003337 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003338 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003339 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003340 }
Jeff Layton02e12152014-07-16 10:31:57 -04003341 spin_unlock(&state_lock);
3342}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343
Jeff Layton02e12152014-07-16 10:31:57 -04003344static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3345{
3346 /*
3347 * We're assuming the state code never drops its reference
3348 * without first removing the lease. Since we're in this lease
3349 * callback (and since the lease code is serialized by the kernel
3350 * lock) we know the server hasn't removed the lease yet, we know
3351 * it's safe to take a reference.
3352 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003353 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003354 nfsd4_cb_recall(dp);
3355}
3356
Jeff Layton1c8c6012013-06-21 08:58:15 -04003357/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003358static void nfsd_break_deleg_cb(struct file_lock *fl)
3359{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003360 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3361 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003362
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003363 if (!fp) {
3364 WARN(1, "(%p)->fl_owner NULL\n", fl);
3365 return;
3366 }
3367 if (fp->fi_had_conflict) {
3368 WARN(1, "duplicate break on %p\n", fp);
3369 return;
3370 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003371 /*
3372 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003373 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003374 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003375 */
3376 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377
Jeff Layton02e12152014-07-16 10:31:57 -04003378 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003379 fp->fi_had_conflict = true;
3380 /*
3381 * If there are no delegations on the list, then we can't count on this
3382 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3383 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3384 * true should keep any new delegations from being hashed.
3385 */
3386 if (list_empty(&fp->fi_delegations))
3387 fl->fl_break_time = jiffies;
3388 else
3389 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3390 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003391 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392}
3393
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394static
3395int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3396{
3397 if (arg & F_UNLCK)
3398 return lease_modify(onlist, arg);
3399 else
3400 return -EAGAIN;
3401}
3402
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003403static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003404 .lm_break = nfsd_break_deleg_cb,
3405 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406};
3407
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003408static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3409{
3410 if (nfsd4_has_session(cstate))
3411 return nfs_ok;
3412 if (seqid == so->so_seqid - 1)
3413 return nfserr_replay_me;
3414 if (seqid == so->so_seqid)
3415 return nfs_ok;
3416 return nfserr_bad_seqid;
3417}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
Jeff Layton4b24ca72014-06-30 11:48:44 -04003419static __be32 lookup_clientid(clientid_t *clid,
3420 struct nfsd4_compound_state *cstate,
3421 struct nfsd_net *nn)
3422{
3423 struct nfs4_client *found;
3424
3425 if (cstate->clp) {
3426 found = cstate->clp;
3427 if (!same_clid(&found->cl_clientid, clid))
3428 return nfserr_stale_clientid;
3429 return nfs_ok;
3430 }
3431
3432 if (STALE_CLIENTID(clid, nn))
3433 return nfserr_stale_clientid;
3434
3435 /*
3436 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3437 * cached already then we know this is for is for v4.0 and "sessions"
3438 * will be false.
3439 */
3440 WARN_ON_ONCE(cstate->session);
3441 found = find_confirmed_client(clid, false, nn);
3442 if (!found)
3443 return nfserr_expired;
3444
3445 /* Cache the nfs4_client in cstate! */
3446 cstate->clp = found;
3447 atomic_inc(&found->cl_refcount);
3448 return nfs_ok;
3449}
3450
Al Virob37ad282006-10-19 23:28:59 -07003451__be32
Andy Adamson66689582009-04-03 08:28:45 +03003452nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003453 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 clientid_t *clientid = &open->op_clientid;
3456 struct nfs4_client *clp = NULL;
3457 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003458 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003459 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003461 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003463 /*
3464 * In case we need it later, after we've already created the
3465 * file and don't want to risk a further failure:
3466 */
3467 open->op_file = nfsd4_alloc_file();
3468 if (open->op_file == NULL)
3469 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
Trond Myklebust2d91e892014-06-30 11:48:46 -04003471 status = lookup_clientid(clientid, cstate, nn);
3472 if (status)
3473 return status;
3474 clp = cstate->clp;
3475
Trond Myklebustd4f04892014-07-29 21:34:36 -04003476 strhashval = ownerstr_hashval(&open->op_owner);
3477 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003478 open->op_openowner = oo;
3479 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003480 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003482 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003483 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003484 release_openowner(oo);
3485 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003486 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003487 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003488 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3489 if (status)
3490 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003491 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003492new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003493 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003494 if (oo == NULL)
3495 return nfserr_jukebox;
3496 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003497alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003498 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003499 if (!open->op_stp)
3500 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003501 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502}
3503
Al Virob37ad282006-10-19 23:28:59 -07003504static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003505nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3506{
3507 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3508 return nfserr_openmode;
3509 else
3510 return nfs_ok;
3511}
3512
Daniel Mackc47d8322011-05-16 16:38:14 +02003513static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003514{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003515 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3516}
3517
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003518static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003519{
3520 struct nfs4_stid *ret;
3521
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003522 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003523 if (!ret)
3524 return NULL;
3525 return delegstateid(ret);
3526}
3527
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003528static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3529{
3530 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3531 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3532}
3533
Al Virob37ad282006-10-19 23:28:59 -07003534static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003535nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003536 struct nfs4_delegation **dp)
3537{
3538 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003539 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003540 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003541
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003542 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3543 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003544 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003545 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003546 status = nfs4_check_delegmode(deleg, flags);
3547 if (status) {
3548 nfs4_put_stid(&deleg->dl_stid);
3549 goto out;
3550 }
3551 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003552out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003553 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003554 return nfs_ok;
3555 if (status)
3556 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003557 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003558 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003559}
3560
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003561static struct nfs4_ol_stateid *
3562nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003564 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003565 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Trond Myklebust1d31a252014-07-10 14:07:25 -04003567 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003568 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 /* ignore lock owners */
3570 if (local->st_stateowner->so_is_open_owner == 0)
3571 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003572 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003573 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003574 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003575 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003578 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003579 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580}
3581
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003582static inline int nfs4_access_to_access(u32 nfs4_access)
3583{
3584 int flags = 0;
3585
3586 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3587 flags |= NFSD_MAY_READ;
3588 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3589 flags |= NFSD_MAY_WRITE;
3590 return flags;
3591}
3592
Al Virob37ad282006-10-19 23:28:59 -07003593static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3595 struct nfsd4_open *open)
3596{
3597 struct iattr iattr = {
3598 .ia_valid = ATTR_SIZE,
3599 .ia_size = 0,
3600 };
3601 if (!open->op_truncate)
3602 return 0;
3603 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003604 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3606}
3607
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003608static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003609 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3610 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003611{
Trond Myklebustde186432014-07-10 14:07:26 -04003612 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003613 __be32 status;
3614 int oflag = nfs4_access_to_omode(open->op_share_access);
3615 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003616 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003617
Trond Myklebustde186432014-07-10 14:07:26 -04003618 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003619
3620 /*
3621 * Are we trying to set a deny mode that would conflict with
3622 * current access?
3623 */
3624 status = nfs4_file_check_deny(fp, open->op_share_deny);
3625 if (status != nfs_ok) {
3626 spin_unlock(&fp->fi_lock);
3627 goto out;
3628 }
3629
3630 /* set access to the file */
3631 status = nfs4_file_get_access(fp, open->op_share_access);
3632 if (status != nfs_ok) {
3633 spin_unlock(&fp->fi_lock);
3634 goto out;
3635 }
3636
3637 /* Set access bits in stateid */
3638 old_access_bmap = stp->st_access_bmap;
3639 set_access(open->op_share_access, stp);
3640
3641 /* Set new deny mask */
3642 old_deny_bmap = stp->st_deny_bmap;
3643 set_deny(open->op_share_deny, stp);
3644 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3645
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003646 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003647 spin_unlock(&fp->fi_lock);
3648 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003649 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003650 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003651 spin_lock(&fp->fi_lock);
3652 if (!fp->fi_fds[oflag]) {
3653 fp->fi_fds[oflag] = filp;
3654 filp = NULL;
3655 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003656 }
Trond Myklebustde186432014-07-10 14:07:26 -04003657 spin_unlock(&fp->fi_lock);
3658 if (filp)
3659 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003660
3661 status = nfsd4_truncate(rqstp, cur_fh, open);
3662 if (status)
3663 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003664out:
3665 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003666out_put_access:
3667 stp->st_access_bmap = old_access_bmap;
3668 nfs4_file_put_access(fp, open->op_share_access);
3669 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3670 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003671}
3672
Al Virob37ad282006-10-19 23:28:59 -07003673static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003674nfs4_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 -07003675{
Al Virob37ad282006-10-19 23:28:59 -07003676 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003677 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003679 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003680 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003681
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003682 /* test and set deny mode */
3683 spin_lock(&fp->fi_lock);
3684 status = nfs4_file_check_deny(fp, open->op_share_deny);
3685 if (status == nfs_ok) {
3686 old_deny_bmap = stp->st_deny_bmap;
3687 set_deny(open->op_share_deny, stp);
3688 fp->fi_share_deny |=
3689 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3690 }
3691 spin_unlock(&fp->fi_lock);
3692
3693 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003696 status = nfsd4_truncate(rqstp, cur_fh, open);
3697 if (status != nfs_ok)
3698 reset_union_bmap_deny(old_deny_bmap, stp);
3699 return status;
3700}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003703nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003705 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706}
3707
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003708/* Should we give out recallable state?: */
3709static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3710{
3711 if (clp->cl_cb_state == NFSD4_CB_UP)
3712 return true;
3713 /*
3714 * In the sessions case, since we don't have to establish a
3715 * separate connection for callbacks, we assume it's OK
3716 * until we hear otherwise:
3717 */
3718 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3719}
3720
Jeff Laytond564fbe2014-07-16 10:31:58 -04003721static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003722{
3723 struct file_lock *fl;
3724
3725 fl = locks_alloc_lock();
3726 if (!fl)
3727 return NULL;
3728 locks_init_lock(fl);
3729 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003730 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003731 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3732 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003733 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003734 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003735 return fl;
3736}
3737
J. Bruce Fields99c41512013-05-21 16:21:25 -04003738static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003739{
Trond Myklebust11b91642014-07-29 21:34:08 -04003740 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003741 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003742 struct file *filp;
3743 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003744
Jeff Laytond564fbe2014-07-16 10:31:58 -04003745 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003746 if (!fl)
3747 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003748 filp = find_readable_file(fp);
3749 if (!filp) {
3750 /* We should always have a readable file here */
3751 WARN_ON_ONCE(1);
3752 return -EBADF;
3753 }
3754 fl->fl_file = filp;
3755 status = vfs_setlease(filp, fl->fl_type, &fl);
3756 if (status) {
3757 locks_free_lock(fl);
3758 goto out_fput;
3759 }
Benny Halevycdc97502014-05-30 09:09:30 -04003760 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003761 spin_lock(&fp->fi_lock);
3762 /* Did the lease get broken before we took the lock? */
3763 status = -EAGAIN;
3764 if (fp->fi_had_conflict)
3765 goto out_unlock;
3766 /* Race breaker */
3767 if (fp->fi_lease) {
3768 status = 0;
3769 atomic_inc(&fp->fi_delegees);
3770 hash_delegation_locked(dp, fp);
3771 goto out_unlock;
3772 }
3773 fp->fi_lease = fl;
3774 fp->fi_deleg_file = filp;
3775 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003776 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003777 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003778 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003779 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003780out_unlock:
3781 spin_unlock(&fp->fi_lock);
3782 spin_unlock(&state_lock);
3783out_fput:
3784 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003785 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003786}
3787
Jeff Layton0b266932014-07-25 07:34:25 -04003788static struct nfs4_delegation *
3789nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3790 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003791{
Jeff Layton0b266932014-07-25 07:34:25 -04003792 int status;
3793 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003794
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003795 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003796 return ERR_PTR(-EAGAIN);
3797
3798 dp = alloc_init_deleg(clp, fh);
3799 if (!dp)
3800 return ERR_PTR(-ENOMEM);
3801
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003802 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003803 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003804 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003805 dp->dl_stid.sc_file = fp;
Jeff Layton417c6622014-07-21 09:34:57 -04003806 if (!fp->fi_lease) {
3807 spin_unlock(&fp->fi_lock);
3808 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003809 status = nfs4_setlease(dp);
3810 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003811 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003812 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003813 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003814 status = -EAGAIN;
3815 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003816 }
Benny Halevy931ee562014-05-30 09:09:27 -04003817 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003818 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003819out_unlock:
3820 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003821 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003822out:
3823 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003824 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003825 return ERR_PTR(status);
3826 }
3827 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003828}
3829
Benny Halevy4aa89132012-02-21 14:16:44 -08003830static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3831{
3832 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3833 if (status == -EAGAIN)
3834 open->op_why_no_deleg = WND4_CONTENTION;
3835 else {
3836 open->op_why_no_deleg = WND4_RESOURCE;
3837 switch (open->op_deleg_want) {
3838 case NFS4_SHARE_WANT_READ_DELEG:
3839 case NFS4_SHARE_WANT_WRITE_DELEG:
3840 case NFS4_SHARE_WANT_ANY_DELEG:
3841 break;
3842 case NFS4_SHARE_WANT_CANCEL:
3843 open->op_why_no_deleg = WND4_CANCELLED;
3844 break;
3845 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003846 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003847 }
3848 }
3849}
3850
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851/*
3852 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003853 *
3854 * Note we don't support write delegations, and won't until the vfs has
3855 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 */
3857static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003858nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3859 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860{
3861 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003862 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3863 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003864 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003865 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003867 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003868 open->op_recall = 0;
3869 switch (open->op_claim_type) {
3870 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003871 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003872 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003873 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3874 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003875 break;
3876 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003877 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003878 /*
3879 * Let's not give out any delegations till everyone's
3880 * had the chance to reclaim theirs....
3881 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003882 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003883 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003884 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003885 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003886 /*
3887 * Also, if the file was opened for write or
3888 * create, there's a good chance the client's
3889 * about to write to it, resulting in an
3890 * immediate recall (since we don't support
3891 * write delegations):
3892 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003893 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003894 goto out_no_deleg;
3895 if (open->op_create == NFS4_OPEN_CREATE)
3896 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003897 break;
3898 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003899 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003900 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003901 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003902 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003903 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003905 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003907 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003908 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003909 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003910 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003911 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003912out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003913 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3914 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003915 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003916 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003917 open->op_recall = 1;
3918 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003919
3920 /* 4.1 client asking for a delegation? */
3921 if (open->op_deleg_want)
3922 nfsd4_open_deleg_none_ext(open, status);
3923 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924}
3925
Benny Halevye27f49c2012-02-21 14:16:54 -08003926static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3927 struct nfs4_delegation *dp)
3928{
3929 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3930 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3931 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3932 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3933 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3934 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3935 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3936 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3937 }
3938 /* Otherwise the client must be confused wanting a delegation
3939 * it already has, therefore we don't return
3940 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3941 */
3942}
3943
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944/*
3945 * called with nfs4_lock_state() held.
3946 */
Al Virob37ad282006-10-19 23:28:59 -07003947__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3949{
Andy Adamson66689582009-04-03 08:28:45 +03003950 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003951 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003953 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003954 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003955 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 /*
3958 * Lookup file; if found, lookup stateid and check open request,
3959 * and check for delegations in the process of being recalled.
3960 * If not found, create the nfs4_file struct
3961 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003962 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04003963 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04003964 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003965 if (status)
3966 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003967 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04003969 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003970 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003971 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003972 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003973 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 }
3975
3976 /*
3977 * OPEN the file, or upgrade an existing OPEN.
3978 * If truncate fails, the OPEN fails.
3979 */
3980 if (stp) {
3981 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003982 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 if (status)
3984 goto out;
3985 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003986 stp = open->op_stp;
3987 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003988 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003989 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3990 if (status) {
3991 release_open_stateid(stp);
3992 goto out;
3993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003995 update_stateid(&stp->st_stid.sc_stateid);
3996 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997
Benny Halevyd24433c2012-02-16 20:57:17 +02003998 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02003999 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4000 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4001 open->op_why_no_deleg = WND4_NOT_WANTED;
4002 goto nodeleg;
4003 }
4004 }
4005
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 /*
4007 * Attempt to hand out a delegation. No error return, because the
4008 * OPEN succeeds even if we fail.
4009 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004010 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004011nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 status = nfs_ok;
4013
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004014 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004015 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004017 /* 4.1 client trying to upgrade/downgrade delegation? */
4018 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004019 open->op_deleg_want)
4020 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004021
NeilBrown13cd2182005-06-23 22:03:10 -07004022 if (fp)
4023 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004024 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05004025 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 /*
4027 * To finish the open response, we just need to set the rflags.
4028 */
4029 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004030 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004031 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004033 if (dp)
4034 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004035 if (stp)
4036 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037
4038 return status;
4039}
4040
Jeff Layton58fb12e2014-07-29 21:34:27 -04004041void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4042 struct nfsd4_open *open, __be32 status)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004043{
4044 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004045 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004046
Jeff Laytond3134b12014-07-29 21:34:32 -04004047 nfsd4_cstate_assign_replay(cstate, so);
4048 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004049 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004050 if (open->op_file)
4051 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004052 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004053 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004054}
4055
Al Virob37ad282006-10-19 23:28:59 -07004056__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004057nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4058 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059{
4060 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004061 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004062 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
4064 nfs4_lock_state();
4065 dprintk("process_renew(%08x/%08x): starting\n",
4066 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004067 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004068 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004070 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004072 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004073 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 goto out;
4075 status = nfs_ok;
4076out:
4077 nfs4_unlock_state();
4078 return status;
4079}
4080
NeilBrowna76b4312005-06-23 22:04:01 -07004081static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004082nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004083{
Jeff Layton33dcc482012-04-10 11:08:48 -04004084 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004085 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004086 return;
4087
NeilBrowna76b4312005-06-23 22:04:01 -07004088 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004089 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004090 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004091 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004092 /*
4093 * Now that every NFSv4 client has had the chance to recover and
4094 * to see the (possibly new, possibly shorter) lease time, we
4095 * can safely set the next grace time to the current lease time:
4096 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03004097 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07004098}
4099
NeilBrownfd39ca92005-06-23 22:04:03 -07004100static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004101nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102{
4103 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004104 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 struct nfs4_delegation *dp;
4106 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004107 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004108 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
4110 nfs4_lock_state();
4111
4112 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004113 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004114 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004115 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004116 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 clp = list_entry(pos, struct nfs4_client, cl_lru);
4118 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4119 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004120 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 break;
4122 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004123 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004124 dprintk("NFSD: client in use (clientid %08x)\n",
4125 clp->cl_clientid.cl_id);
4126 continue;
4127 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004128 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004129 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004130 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004131 list_for_each_safe(pos, next, &reaplist) {
4132 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 dprintk("NFSD: purging unused client (clientid %08x)\n",
4134 clp->cl_clientid.cl_id);
4135 expire_client(clp);
4136 }
Benny Halevycdc97502014-05-30 09:09:30 -04004137 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004138 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004140 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4141 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004143 t = dp->dl_time - cutoff;
4144 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 break;
4146 }
Jeff Layton42690672014-07-25 07:34:20 -04004147 unhash_delegation_locked(dp);
4148 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 }
Benny Halevycdc97502014-05-30 09:09:30 -04004150 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004151 while (!list_empty(&reaplist)) {
4152 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4153 dl_recall_lru);
4154 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004155 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004157 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004158 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
4159 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004160 t = oo->oo_time - cutoff;
4161 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 break;
4163 }
Jeff Laytond3134b12014-07-29 21:34:32 -04004164 release_last_closed_stateid(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04004166 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04004168 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169}
4170
Harvey Harrisona254b242008-02-20 12:49:00 -08004171static struct workqueue_struct *laundry_wq;
4172static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004173
4174static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004175laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176{
4177 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004178 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4179 work);
4180 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4181 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004183 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004185 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186}
4187
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004188static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004189{
Trond Myklebust11b91642014-07-29 21:34:08 -04004190 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004191 return nfserr_bad_stateid;
4192 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193}
4194
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004196access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004198 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4199 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4200 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201}
4202
4203static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004204access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004206 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4207 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208}
4209
4210static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004211__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212{
Al Virob37ad282006-10-19 23:28:59 -07004213 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
J. Bruce Fields02921912010-07-29 15:16:59 -04004215 /* For lock stateid's, we test the parent open, not the lock: */
4216 if (stp->st_openstp)
4217 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004218 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004220 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 goto out;
4222 status = nfs_ok;
4223out:
4224 return status;
4225}
4226
Al Virob37ad282006-10-19 23:28:59 -07004227static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004228check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004230 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004232 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004233 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 * conflicting state; so we must wait out the grace period. */
4235 return nfserr_grace;
4236 } else if (flags & WR_STATE)
4237 return nfs4_share_conflict(current_fh,
4238 NFS4_SHARE_DENY_WRITE);
4239 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4240 return nfs4_share_conflict(current_fh,
4241 NFS4_SHARE_DENY_READ);
4242}
4243
4244/*
4245 * Allow READ/WRITE during grace period on recovered state only for files
4246 * that are not able to provide mandatory locking.
4247 */
4248static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004249grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004251 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252}
4253
J. Bruce Fields81b82962011-08-23 11:03:29 -04004254/* Returns true iff a is later than b: */
4255static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4256{
Jim Rees1a9357f2013-05-17 17:33:00 -04004257 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004258}
4259
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004260static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004261{
Andy Adamson66689582009-04-03 08:28:45 +03004262 /*
4263 * When sessions are used the stateid generation number is ignored
4264 * when it is zero.
4265 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004266 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004267 return nfs_ok;
4268
4269 if (in->si_generation == ref->si_generation)
4270 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004271
J. Bruce Fields0836f582008-01-26 19:08:12 -05004272 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004273 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004274 return nfserr_bad_stateid;
4275 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004276 * However, we could see a stateid from the past, even from a
4277 * non-buggy client. For example, if the client sends a lock
4278 * while some IO is outstanding, the lock may bump si_generation
4279 * while the IO is still in flight. The client could avoid that
4280 * situation by waiting for responses on all the IO requests,
4281 * but better performance may result in retrying IO that
4282 * receives an old_stateid error if requests are rarely
4283 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004284 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004285 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004286}
4287
Chuck Lever7df302f2012-05-29 13:56:37 -04004288static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004289{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004290 struct nfs4_stid *s;
4291 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004292 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004293
Chuck Lever7df302f2012-05-29 13:56:37 -04004294 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004295 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004296 /* Client debugging aid. */
4297 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4298 char addr_str[INET6_ADDRSTRLEN];
4299 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4300 sizeof(addr_str));
4301 pr_warn_ratelimited("NFSD: client %s testing state ID "
4302 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004303 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004304 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004305 spin_lock(&cl->cl_lock);
4306 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004307 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004308 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004309 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004310 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004311 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004312 switch (s->sc_type) {
4313 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004314 status = nfs_ok;
4315 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004316 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004317 status = nfserr_deleg_revoked;
4318 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004319 case NFS4_OPEN_STID:
4320 case NFS4_LOCK_STID:
4321 ols = openlockstateid(s);
4322 if (ols->st_stateowner->so_is_open_owner
4323 && !(openowner(ols->st_stateowner)->oo_flags
4324 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004325 status = nfserr_bad_stateid;
4326 else
4327 status = nfs_ok;
4328 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004329 default:
4330 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004331 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004332 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004333 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004334 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004335 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004336out_unlock:
4337 spin_unlock(&cl->cl_lock);
4338 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004339}
4340
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004341static __be32
4342nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4343 stateid_t *stateid, unsigned char typemask,
4344 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004345{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004346 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004347
4348 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4349 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004350 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004351 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004352 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004353 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004354 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004355 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004356 if (status)
4357 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004358 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004359 if (!*s)
4360 return nfserr_bad_stateid;
4361 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004362}
4363
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364/*
4365* Checks for stateid operations
4366*/
Al Virob37ad282006-10-19 23:28:59 -07004367__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004368nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004369 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004371 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004372 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004374 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004376 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004377 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004378 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 if (filpp)
4381 *filpp = NULL;
4382
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004383 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 return nfserr_grace;
4385
4386 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004387 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
Trond Myklebust14bcab12014-04-18 14:44:07 -04004389 nfs4_lock_state();
4390
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004391 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004392 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004393 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004394 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004395 goto unlock_state;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004396 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4397 if (status)
4398 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004399 switch (s->sc_type) {
4400 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004401 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004402 status = nfs4_check_delegmode(dp, flags);
4403 if (status)
4404 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004405 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004406 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004407 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004408 WARN_ON_ONCE(1);
4409 status = nfserr_serverfault;
4410 goto out;
4411 }
Trond Myklebustde186432014-07-10 14:07:26 -04004412 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004413 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004414 break;
4415 case NFS4_OPEN_STID:
4416 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004417 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004418 status = nfs4_check_fh(current_fh, stp);
4419 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004421 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004422 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004424 status = nfs4_check_openmode(stp, flags);
4425 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004427 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004428 struct nfs4_file *fp = stp->st_stid.sc_file;
4429
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004430 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004431 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004432 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004433 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004434 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004435 break;
4436 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004437 status = nfserr_bad_stateid;
4438 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 }
4440 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004441 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004442 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004444 nfs4_put_stid(s);
4445unlock_state:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004446 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 return status;
4448}
4449
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004450/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004451 * Test if the stateid is valid
4452 */
4453__be32
4454nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4455 struct nfsd4_test_stateid *test_stateid)
4456{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004457 struct nfsd4_test_stateid_id *stateid;
4458 struct nfs4_client *cl = cstate->session->se_client;
4459
4460 nfs4_lock_state();
4461 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004462 stateid->ts_id_status =
4463 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004464 nfs4_unlock_state();
4465
Bryan Schumaker17456802011-07-13 10:50:48 -04004466 return nfs_ok;
4467}
4468
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004469__be32
4470nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4471 struct nfsd4_free_stateid *free_stateid)
4472{
4473 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004474 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004475 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004476 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004477 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004478 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004479
4480 nfs4_lock_state();
Jeff Layton1af71cc2014-07-29 21:34:14 -04004481 spin_lock(&cl->cl_lock);
4482 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004483 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004484 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004485 switch (s->sc_type) {
4486 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004487 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004488 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004489 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004490 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4491 if (ret)
4492 break;
4493 ret = nfserr_locks_held;
4494 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004495 case NFS4_LOCK_STID:
4496 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4497 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004498 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004499 stp = openlockstateid(s);
4500 ret = nfserr_locks_held;
4501 if (check_for_locks(stp->st_stid.sc_file,
4502 lockowner(stp->st_stateowner)))
4503 break;
4504 unhash_lock_stateid(stp);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004505 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004506 nfs4_put_stid(s);
4507 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004508 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004509 case NFS4_REVOKED_DELEG_STID:
4510 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004511 list_del_init(&dp->dl_recall_lru);
4512 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004513 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004514 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004515 goto out;
4516 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004517 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004518out_unlock:
4519 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004520out:
4521 nfs4_unlock_state();
4522 return ret;
4523}
4524
NeilBrown4c4cd222005-07-07 17:59:27 -07004525static inline int
4526setlkflg (int type)
4527{
4528 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4529 RD_STATE : WR_STATE;
4530}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004532static __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 -04004533{
4534 struct svc_fh *current_fh = &cstate->current_fh;
4535 struct nfs4_stateowner *sop = stp->st_stateowner;
4536 __be32 status;
4537
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004538 status = nfsd4_check_seqid(cstate, sop, seqid);
4539 if (status)
4540 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004541 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4542 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004543 /*
4544 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004545 * nfserr_replay_me from the previous step, and
4546 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004547 */
4548 return nfserr_bad_stateid;
4549 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4550 if (status)
4551 return status;
4552 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004553}
4554
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555/*
4556 * Checks for sequence id mutating operations.
4557 */
Al Virob37ad282006-10-19 23:28:59 -07004558static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004559nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004560 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004561 struct nfs4_ol_stateid **stpp,
4562 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004564 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004565 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004566 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004568 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4569 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004570
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004572 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004573 if (status)
4574 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004575 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004576 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004578 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004579 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004580 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004581 else
4582 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004583 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004584}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004585
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004586static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4587 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004588{
4589 __be32 status;
4590 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004591 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004593 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004594 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004595 if (status)
4596 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004597 oo = openowner(stp->st_stateowner);
4598 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4599 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004600 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004601 }
4602 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004603 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604}
4605
Al Virob37ad282006-10-19 23:28:59 -07004606__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004607nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004608 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609{
Al Virob37ad282006-10-19 23:28:59 -07004610 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004611 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004612 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004613 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614
Al Viroa6a9f182013-09-16 10:57:01 -04004615 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4616 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004618 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004619 if (status)
4620 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621
4622 nfs4_lock_state();
4623
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004624 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004625 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004626 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004627 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004628 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004629 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004630 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004631 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004632 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004633 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004634 update_stateid(&stp->st_stid.sc_stateid);
4635 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004636 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004637 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004638
Jeff Layton2a4317c2012-03-21 16:42:43 -04004639 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004640 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004641put_stateid:
4642 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004644 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004645 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 return status;
4647}
4648
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004649static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004651 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004652 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004653 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004654 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004655}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004656
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004657static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4658{
4659 switch (to_access) {
4660 case NFS4_SHARE_ACCESS_READ:
4661 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4662 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4663 break;
4664 case NFS4_SHARE_ACCESS_WRITE:
4665 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4666 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4667 break;
4668 case NFS4_SHARE_ACCESS_BOTH:
4669 break;
4670 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004671 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 }
4673}
4674
Al Virob37ad282006-10-19 23:28:59 -07004675__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004676nfsd4_open_downgrade(struct svc_rqst *rqstp,
4677 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004678 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679{
Al Virob37ad282006-10-19 23:28:59 -07004680 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004681 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004682 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683
Al Viroa6a9f182013-09-16 10:57:01 -04004684 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4685 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004687 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004688 if (od->od_deleg_want)
4689 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4690 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691
4692 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004693 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004694 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004695 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004698 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004699 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004701 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004703 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004704 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004706 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004708 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709
Jeff Laytonce0fc432012-05-11 09:45:14 -04004710 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004712 update_stateid(&stp->st_stid.sc_stateid);
4713 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004715put_stateid:
4716 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004718 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004719 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 return status;
4721}
4722
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004723static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4724{
Trond Myklebustacf92952014-06-30 11:48:37 -04004725 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytond83017f2014-07-29 21:34:42 -04004726 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004727
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004728 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04004729 spin_lock(&clp->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04004730 unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004731
Jeff Laytond83017f2014-07-29 21:34:42 -04004732 if (clp->cl_minorversion) {
4733 put_ol_stateid_locked(s, &reaplist);
4734 spin_unlock(&clp->cl_lock);
4735 free_ol_stateid_reaplist(&reaplist);
4736 } else {
4737 spin_unlock(&clp->cl_lock);
4738 free_ol_stateid_reaplist(&reaplist);
Jeff Laytond3134b12014-07-29 21:34:32 -04004739 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04004740 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004741}
4742
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743/*
4744 * nfs4_unlock_state() called after encode
4745 */
Al Virob37ad282006-10-19 23:28:59 -07004746__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004747nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004748 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749{
Al Virob37ad282006-10-19 23:28:59 -07004750 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004751 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004752 struct net *net = SVC_NET(rqstp);
4753 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754
Al Viroa6a9f182013-09-16 10:57:01 -04004755 dprintk("NFSD: nfsd4_close on file %pd\n",
4756 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757
4758 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004759 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4760 &close->cl_stateid,
4761 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004762 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004763 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004764 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004766 update_stateid(&stp->st_stid.sc_stateid);
4767 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004769 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004770
4771 /* put reference from nfs4_preprocess_seqid_op */
4772 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773out:
Jeff Layton58fb12e2014-07-29 21:34:27 -04004774 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 return status;
4776}
4777
Al Virob37ad282006-10-19 23:28:59 -07004778__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004779nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4780 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004782 struct nfs4_delegation *dp;
4783 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004784 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004785 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004786 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004788 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004789 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790
4791 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004792 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004793 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004794 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004795 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004796 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004797 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004798 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004799
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004800 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004801put_stateid:
4802 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004804 nfs4_unlock_state();
4805
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 return status;
4807}
4808
4809
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811
Benny Halevy87df4de2008-12-15 19:42:03 +02004812static inline u64
4813end_offset(u64 start, u64 len)
4814{
4815 u64 end;
4816
4817 end = start + len;
4818 return end >= start ? end: NFS4_MAX_UINT64;
4819}
4820
4821/* last octet in a range */
4822static inline u64
4823last_byte_offset(u64 start, u64 len)
4824{
4825 u64 end;
4826
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004827 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004828 end = start + len;
4829 return end > start ? end - 1: NFS4_MAX_UINT64;
4830}
4831
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832/*
4833 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4834 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4835 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4836 * locking, this prevents us from being completely protocol-compliant. The
4837 * real solution to this problem is to start using unsigned file offsets in
4838 * the VFS, but this is a very deep change!
4839 */
4840static inline void
4841nfs4_transform_lock_offset(struct file_lock *lock)
4842{
4843 if (lock->fl_start < 0)
4844 lock->fl_start = OFFSET_MAX;
4845 if (lock->fl_end < 0)
4846 lock->fl_end = OFFSET_MAX;
4847}
4848
NeilBrownd5b90262006-04-10 22:55:22 -07004849/* Hack!: For now, we're defining this just so we can use a pointer to it
4850 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004851static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004852};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853
4854static inline void
4855nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4856{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004857 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858
NeilBrownd5b90262006-04-10 22:55:22 -07004859 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004860 lo = (struct nfs4_lockowner *) fl->fl_owner;
4861 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4862 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004863 if (!deny->ld_owner.data)
4864 /* We just don't care that much */
4865 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004866 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4867 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004868 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004869nevermind:
4870 deny->ld_owner.len = 0;
4871 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004872 deny->ld_clientid.cl_boot = 0;
4873 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 }
4875 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004876 deny->ld_length = NFS4_MAX_UINT64;
4877 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4879 deny->ld_type = NFS4_READ_LT;
4880 if (fl->fl_type != F_RDLCK)
4881 deny->ld_type = NFS4_WRITE_LT;
4882}
4883
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004884static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004885find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004886 struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004888 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004889 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890
Trond Myklebustd4f04892014-07-29 21:34:36 -04004891 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4892 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004893 if (so->so_is_open_owner)
4894 continue;
Trond Myklebustd4f04892014-07-29 21:34:36 -04004895 if (!same_owner_str(so, owner))
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004896 continue;
Jeff Layton5db1c032014-07-29 21:34:28 -04004897 atomic_inc(&so->so_count);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004898 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 }
4900 return NULL;
4901}
4902
Trond Myklebustc58c6612014-07-29 21:34:35 -04004903static struct nfs4_lockowner *
4904find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004905 struct nfs4_client *clp)
Trond Myklebustc58c6612014-07-29 21:34:35 -04004906{
4907 struct nfs4_lockowner *lo;
4908
Trond Myklebustd4f04892014-07-29 21:34:36 -04004909 spin_lock(&clp->cl_lock);
4910 lo = find_lockowner_str_locked(clid, owner, clp);
4911 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004912 return lo;
4913}
4914
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004915static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4916{
Trond Myklebustc58c6612014-07-29 21:34:35 -04004917 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004918}
4919
Jeff Layton6b180f02014-07-29 21:34:26 -04004920static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4921{
4922 struct nfs4_lockowner *lo = lockowner(sop);
4923
4924 kmem_cache_free(lockowner_slab, lo);
4925}
4926
4927static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004928 .so_unhash = nfs4_unhash_lockowner,
4929 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004930};
4931
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932/*
4933 * Alloc a lock owner structure.
4934 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004935 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004937 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004939static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004940alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
4941 struct nfs4_ol_stateid *open_stp,
4942 struct nfsd4_lock *lock)
4943{
Trond Myklebustc58c6612014-07-29 21:34:35 -04004944 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004946 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4947 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004949 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4950 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04004951 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04004952 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04004953 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004954 ret = find_lockowner_str_locked(&clp->cl_clientid,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004955 &lock->lk_new_owner, clp);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004956 if (ret == NULL) {
4957 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004958 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004959 ret = lo;
4960 } else
4961 nfs4_free_lockowner(&lo->lo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04004962 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004963 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964}
4965
Jeff Layton356a95e2014-07-29 21:34:13 -04004966static void
4967init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
4968 struct nfs4_file *fp, struct inode *inode,
4969 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004971 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972
Jeff Layton356a95e2014-07-29 21:34:13 -04004973 lockdep_assert_held(&clp->cl_lock);
4974
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004975 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004976 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004977 stp->st_stateowner = &lo->lo_owner;
Trond Myklebuste4f1dd72014-07-29 21:34:30 -04004978 atomic_inc(&lo->lo_owner.so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07004979 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004980 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04004981 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004982 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004984 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004985 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04004986 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004987 spin_lock(&fp->fi_lock);
4988 list_add(&stp->st_perfile, &fp->fi_stateids);
4989 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990}
4991
Jeff Laytonc53530d2014-06-30 11:48:39 -04004992static struct nfs4_ol_stateid *
4993find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4994{
4995 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04004996 struct nfs4_client *clp = lo->lo_owner.so_client;
4997
4998 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004999
5000 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005001 if (lst->st_stid.sc_file == fp) {
5002 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005003 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005004 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005005 }
5006 return NULL;
5007}
5008
Jeff Layton356a95e2014-07-29 21:34:13 -04005009static struct nfs4_ol_stateid *
5010find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5011 struct inode *inode, struct nfs4_ol_stateid *ost,
5012 bool *new)
5013{
5014 struct nfs4_stid *ns = NULL;
5015 struct nfs4_ol_stateid *lst;
5016 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5017 struct nfs4_client *clp = oo->oo_owner.so_client;
5018
5019 spin_lock(&clp->cl_lock);
5020 lst = find_lock_stateid(lo, fi);
5021 if (lst == NULL) {
5022 spin_unlock(&clp->cl_lock);
5023 ns = nfs4_alloc_stid(clp, stateid_slab);
5024 if (ns == NULL)
5025 return NULL;
5026
5027 spin_lock(&clp->cl_lock);
5028 lst = find_lock_stateid(lo, fi);
5029 if (likely(!lst)) {
5030 lst = openlockstateid(ns);
5031 init_lock_stateid(lst, lo, fi, inode, ost);
5032 ns = NULL;
5033 *new = true;
5034 }
5035 }
5036 spin_unlock(&clp->cl_lock);
5037 if (ns)
5038 nfs4_put_stid(ns);
5039 return lst;
5040}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005041
NeilBrownfd39ca92005-06-23 22:04:03 -07005042static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043check_lock_length(u64 offset, u64 length)
5044{
Benny Halevy87df4de2008-12-15 19:42:03 +02005045 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 LOFF_OVERFLOW(offset, length)));
5047}
5048
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005049static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005050{
Trond Myklebust11b91642014-07-29 21:34:08 -04005051 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005052
Jeff Layton7214e862014-07-10 14:07:33 -04005053 lockdep_assert_held(&fp->fi_lock);
5054
Jeff Layton82c5ff12012-05-11 09:45:13 -04005055 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005056 return;
Jeff Layton12659652014-07-10 14:07:28 -04005057 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005058 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005059}
5060
Jeff Layton356a95e2014-07-29 21:34:13 -04005061static __be32
5062lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5063 struct nfs4_ol_stateid *ost,
5064 struct nfsd4_lock *lock,
5065 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005066{
Jeff Layton5db1c032014-07-29 21:34:28 -04005067 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005068 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005069 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5070 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005071 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005072 struct nfs4_lockowner *lo;
5073 unsigned int strhashval;
5074
Trond Myklebustd4f04892014-07-29 21:34:36 -04005075 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005076 if (!lo) {
Trond Myklebustd4f04892014-07-29 21:34:36 -04005077 strhashval = ownerstr_hashval(&lock->v.new.owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005078 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5079 if (lo == NULL)
5080 return nfserr_jukebox;
5081 } else {
5082 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005083 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005084 if (!cstate->minorversion &&
5085 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005086 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005087 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005088
Jeff Layton356a95e2014-07-29 21:34:13 -04005089 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005090 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005091 status = nfserr_jukebox;
5092 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005093 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005094 status = nfs_ok;
5095out:
5096 nfs4_put_stateowner(&lo->lo_owner);
5097 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005098}
5099
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100/*
5101 * LOCK operation
5102 */
Al Virob37ad282006-10-19 23:28:59 -07005103__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005104nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005105 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005107 struct nfs4_openowner *open_sop = NULL;
5108 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005109 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005110 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005111 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005112 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005113 struct file_lock *file_lock = NULL;
5114 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005115 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005116 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005117 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005118 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005119 struct net *net = SVC_NET(rqstp);
5120 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121
5122 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5123 (long long) lock->lk_offset,
5124 (long long) lock->lk_length);
5125
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 if (check_lock_length(lock->lk_offset, lock->lk_length))
5127 return nfserr_inval;
5128
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005129 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005130 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005131 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5132 return status;
5133 }
5134
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 nfs4_lock_state();
5136
5137 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005138 if (nfsd4_has_session(cstate))
5139 /* See rfc 5661 18.10.3: given clientid is ignored: */
5140 memcpy(&lock->v.new.clientid,
5141 &cstate->session->se_client->cl_clientid,
5142 sizeof(clientid_t));
5143
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005145 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005149 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 lock->lk_new_open_seqid,
5151 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005152 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005153 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005155 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005156 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005157 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005158 &lock->v.new.clientid))
5159 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005160 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005161 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005162 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005163 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005164 lock->lk_old_lock_seqid,
5165 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005166 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005167 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005168 if (status)
5169 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005170 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005172 lkflg = setlkflg(lock->lk_type);
5173 status = nfs4_check_openmode(lock_stp, lkflg);
5174 if (status)
5175 goto out;
5176
NeilBrown0dd395d2005-07-07 17:59:15 -07005177 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005178 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005179 goto out;
5180 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005181 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005182 goto out;
5183
Jeff Layton21179d82012-08-21 08:03:32 -04005184 file_lock = locks_alloc_lock();
5185 if (!file_lock) {
5186 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5187 status = nfserr_jukebox;
5188 goto out;
5189 }
5190
Trond Myklebust11b91642014-07-29 21:34:08 -04005191 fp = lock_stp->st_stid.sc_file;
Jeff Layton21179d82012-08-21 08:03:32 -04005192 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 switch (lock->lk_type) {
5194 case NFS4_READ_LT:
5195 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005196 spin_lock(&fp->fi_lock);
5197 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005198 if (filp)
5199 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005200 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005201 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005202 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 case NFS4_WRITE_LT:
5204 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005205 spin_lock(&fp->fi_lock);
5206 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005207 if (filp)
5208 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005209 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005210 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005211 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 default:
5213 status = nfserr_inval;
5214 goto out;
5215 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005216 if (!filp) {
5217 status = nfserr_openmode;
5218 goto out;
5219 }
Jeff Layton21179d82012-08-21 08:03:32 -04005220 file_lock->fl_owner = (fl_owner_t)lock_sop;
5221 file_lock->fl_pid = current->tgid;
5222 file_lock->fl_file = filp;
5223 file_lock->fl_flags = FL_POSIX;
5224 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5225 file_lock->fl_start = lock->lk_offset;
5226 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5227 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228
Jeff Layton21179d82012-08-21 08:03:32 -04005229 conflock = locks_alloc_lock();
5230 if (!conflock) {
5231 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5232 status = nfserr_jukebox;
5233 goto out;
5234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235
Jeff Layton21179d82012-08-21 08:03:32 -04005236 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005237 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005239 update_stateid(&lock_stp->st_stid.sc_stateid);
5240 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005242 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005243 break;
5244 case (EAGAIN): /* conflock holds conflicting lock */
5245 status = nfserr_denied;
5246 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005247 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005248 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 case (EDEADLK):
5250 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005251 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005252 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005253 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005254 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005255 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257out:
Trond Myklebustde186432014-07-10 14:07:26 -04005258 if (filp)
5259 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005260 if (lock_stp) {
5261 /* Bump seqid manually if the 4.0 replay owner is openowner */
5262 if (cstate->replay_owner &&
5263 cstate->replay_owner != &lock_sop->lo_owner &&
5264 seqid_mutating_err(ntohl(status)))
5265 lock_sop->lo_owner.so_seqid++;
5266
5267 /*
5268 * If this is a new, never-before-used stateid, and we are
5269 * returning an error, then just go ahead and release it.
5270 */
5271 if (status && new)
5272 release_lock_stateid(lock_stp);
5273
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005274 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005275 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005276 if (open_stp)
5277 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005278 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005279 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005280 if (file_lock)
5281 locks_free_lock(file_lock);
5282 if (conflock)
5283 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 return status;
5285}
5286
5287/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005288 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5289 * so we do a temporary open here just to get an open file to pass to
5290 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5291 * inode operation.)
5292 */
Al Viro04da6e92012-04-13 00:00:04 -04005293static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005294{
5295 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005296 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5297 if (!err) {
5298 err = nfserrno(vfs_test_lock(file, lock));
5299 nfsd_close(file);
5300 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005301 return err;
5302}
5303
5304/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 * LOCKT operation
5306 */
Al Virob37ad282006-10-19 23:28:59 -07005307__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005308nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5309 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310{
Jeff Layton21179d82012-08-21 08:03:32 -04005311 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005312 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005313 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005314 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005316 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 return nfserr_grace;
5318
5319 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5320 return nfserr_inval;
5321
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 nfs4_lock_state();
5323
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005324 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005325 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005326 if (status)
5327 goto out;
5328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005330 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332
Jeff Layton21179d82012-08-21 08:03:32 -04005333 file_lock = locks_alloc_lock();
5334 if (!file_lock) {
5335 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5336 status = nfserr_jukebox;
5337 goto out;
5338 }
5339 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 switch (lockt->lt_type) {
5341 case NFS4_READ_LT:
5342 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005343 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 break;
5345 case NFS4_WRITE_LT:
5346 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005347 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 break;
5349 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005350 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 status = nfserr_inval;
5352 goto out;
5353 }
5354
Trond Myklebustd4f04892014-07-29 21:34:36 -04005355 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5356 cstate->clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005357 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005358 file_lock->fl_owner = (fl_owner_t)lo;
5359 file_lock->fl_pid = current->tgid;
5360 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361
Jeff Layton21179d82012-08-21 08:03:32 -04005362 file_lock->fl_start = lockt->lt_offset;
5363 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364
Jeff Layton21179d82012-08-21 08:03:32 -04005365 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366
Jeff Layton21179d82012-08-21 08:03:32 -04005367 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005368 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005369 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005370
Jeff Layton21179d82012-08-21 08:03:32 -04005371 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005373 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 }
5375out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005376 if (lo)
5377 nfs4_put_stateowner(&lo->lo_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005379 if (file_lock)
5380 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 return status;
5382}
5383
Al Virob37ad282006-10-19 23:28:59 -07005384__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005385nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005386 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005388 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005390 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005391 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005392 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005393 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5394
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5396 (long long) locku->lu_offset,
5397 (long long) locku->lu_length);
5398
5399 if (check_lock_length(locku->lu_offset, locku->lu_length))
5400 return nfserr_inval;
5401
5402 nfs4_lock_state();
5403
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005404 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005405 &locku->lu_stateid, NFS4_LOCK_STID,
5406 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005407 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005409 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005410 if (!filp) {
5411 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005412 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005413 }
Jeff Layton21179d82012-08-21 08:03:32 -04005414 file_lock = locks_alloc_lock();
5415 if (!file_lock) {
5416 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5417 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005418 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005419 }
5420 locks_init_lock(file_lock);
5421 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005422 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005423 file_lock->fl_pid = current->tgid;
5424 file_lock->fl_file = filp;
5425 file_lock->fl_flags = FL_POSIX;
5426 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5427 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428
Jeff Layton21179d82012-08-21 08:03:32 -04005429 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5430 locku->lu_length);
5431 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432
Jeff Layton21179d82012-08-21 08:03:32 -04005433 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005434 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005435 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 goto out_nfserr;
5437 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005438 update_stateid(&stp->st_stid.sc_stateid);
5439 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005440fput:
5441 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005442put_stateid:
5443 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005445 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005446 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005447 if (file_lock)
5448 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 return status;
5450
5451out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005452 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005453 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454}
5455
5456/*
5457 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005458 * true: locks held by lockowner
5459 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005461static bool
5462check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463{
5464 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005465 int status = false;
5466 struct file *filp = find_any_file(fp);
5467 struct inode *inode;
5468
5469 if (!filp) {
5470 /* Any valid lock stateid should have some sort of access */
5471 WARN_ON_ONCE(1);
5472 return status;
5473 }
5474
5475 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476
Jeff Layton1c8c6012013-06-21 08:58:15 -04005477 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005479 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005480 status = true;
5481 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005484 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005485 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 return status;
5487}
5488
Al Virob37ad282006-10-19 23:28:59 -07005489__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005490nfsd4_release_lockowner(struct svc_rqst *rqstp,
5491 struct nfsd4_compound_state *cstate,
5492 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493{
5494 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005495 struct nfs4_stateowner *sop;
5496 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005497 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005499 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005500 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005501 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005502 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503
5504 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5505 clid->cl_boot, clid->cl_id);
5506
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 nfs4_lock_state();
5508
Jeff Layton4b24ca72014-06-30 11:48:44 -04005509 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005510 if (status)
5511 goto out;
5512
Trond Myklebustd4f04892014-07-29 21:34:36 -04005513 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005514 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005515 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005516 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005517 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005518
5519 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005520 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005521
Jeff Layton882e9d22014-07-29 21:34:37 -04005522 /* see if there are still any locks associated with it */
5523 lo = lockowner(sop);
5524 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5525 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5526 status = nfserr_locks_held;
5527 spin_unlock(&clp->cl_lock);
5528 goto out;
5529 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005530 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005531
5532 atomic_inc(&sop->so_count);
5533 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005534 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005535 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005536 if (lo)
5537 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538out:
5539 nfs4_unlock_state();
5540 return status;
5541}
5542
5543static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005544alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545{
NeilBrowna55370a2005-06-23 22:03:52 -07005546 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547}
5548
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005549bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005550nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005551{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005552 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005553
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005554 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005555 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005556}
5557
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558/*
5559 * failure => all reset bets are off, nfserr_no_grace...
5560 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005561struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005562nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563{
5564 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005565 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
NeilBrowna55370a2005-06-23 22:03:52 -07005567 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5568 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005569 if (crp) {
5570 strhashval = clientstr_hashval(name);
5571 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005572 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005573 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005574 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005575 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005576 }
5577 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578}
5579
Jeff Layton2a4317c2012-03-21 16:42:43 -04005580void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005581nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005582{
5583 list_del(&crp->cr_strhash);
5584 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005585 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005586}
5587
5588void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005589nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590{
5591 struct nfs4_client_reclaim *crp = NULL;
5592 int i;
5593
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005595 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5596 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005598 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 }
5600 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005601 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602}
5603
5604/*
5605 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005606struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005607nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608{
5609 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 struct nfs4_client_reclaim *crp = NULL;
5611
Jeff Layton278c9312012-11-12 15:00:52 -05005612 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613
Jeff Layton278c9312012-11-12 15:00:52 -05005614 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005615 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005616 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 return crp;
5618 }
5619 }
5620 return NULL;
5621}
5622
5623/*
5624* Called from OPEN. Look for clientid in reclaim list.
5625*/
Al Virob37ad282006-10-19 23:28:59 -07005626__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005627nfs4_check_open_reclaim(clientid_t *clid,
5628 struct nfsd4_compound_state *cstate,
5629 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005631 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005632
5633 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005634 status = lookup_clientid(clid, cstate, nn);
5635 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005636 return nfserr_reclaim_bad;
5637
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005638 if (nfsd4_client_record_check(cstate->clp))
5639 return nfserr_reclaim_bad;
5640
5641 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642}
5643
Bryan Schumaker65178db2011-11-01 13:35:21 -04005644#ifdef CONFIG_NFSD_FAULT_INJECTION
5645
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005646u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5647{
J. Bruce Fields221a6872013-04-01 22:23:49 -04005648 if (mark_client_expired(clp))
5649 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005650 expire_client(clp);
5651 return 1;
5652}
5653
Bryan Schumaker184c1842012-11-29 11:40:44 -05005654u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5655{
5656 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005657 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005658 printk(KERN_INFO "NFS Client: %s\n", buf);
5659 return 1;
5660}
5661
5662static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5663 const char *type)
5664{
5665 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005666 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005667 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5668}
5669
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005670static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5671 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005672{
5673 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005674 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005675 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005676 u64 count = 0;
5677
5678 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005679 list_for_each_entry_safe(stp, st_next,
5680 &oop->oo_owner.so_stateids, st_perstateowner) {
5681 list_for_each_entry_safe(lst, lst_next,
5682 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005683 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005684 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005685 if (++count == max)
5686 return count;
5687 }
5688 }
5689 }
5690
5691 return count;
5692}
5693
5694u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5695{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005696 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005697}
5698
Bryan Schumaker184c1842012-11-29 11:40:44 -05005699u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5700{
5701 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5702 nfsd_print_count(clp, count, "locked files");
5703 return count;
5704}
5705
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005706static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5707{
5708 struct nfs4_openowner *oop, *next;
5709 u64 count = 0;
5710
5711 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5712 if (func)
5713 func(oop);
5714 if (++count == max)
5715 break;
5716 }
5717
5718 return count;
5719}
5720
5721u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5722{
5723 return nfsd_foreach_client_open(clp, max, release_openowner);
5724}
5725
Bryan Schumaker184c1842012-11-29 11:40:44 -05005726u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5727{
5728 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5729 nfsd_print_count(clp, count, "open files");
5730 return count;
5731}
5732
Bryan Schumaker269de302012-11-29 11:40:42 -05005733static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5734 struct list_head *victims)
5735{
5736 struct nfs4_delegation *dp, *next;
5737 u64 count = 0;
5738
Benny Halevycdc97502014-05-30 09:09:30 -04005739 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005740 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005741 if (victims) {
5742 /*
5743 * It's not safe to mess with delegations that have a
5744 * non-zero dl_time. They might have already been broken
5745 * and could be processed by the laundromat outside of
5746 * the state_lock. Just leave them be.
5747 */
5748 if (dp->dl_time != 0)
5749 continue;
5750
Jeff Layton42690672014-07-25 07:34:20 -04005751 unhash_delegation_locked(dp);
5752 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005753 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005754 if (++count == max)
5755 break;
5756 }
5757 return count;
5758}
5759
5760u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5761{
5762 struct nfs4_delegation *dp, *next;
5763 LIST_HEAD(victims);
5764 u64 count;
5765
Benny Halevycdc97502014-05-30 09:09:30 -04005766 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005767 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005768 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005769
Jeff Layton2d4a5322014-07-25 07:34:21 -04005770 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5771 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005772 revoke_delegation(dp);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005773 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005774
5775 return count;
5776}
5777
5778u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5779{
Jeff Laytondff13992014-07-08 14:02:49 -04005780 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005781 LIST_HEAD(victims);
5782 u64 count;
5783
Benny Halevycdc97502014-05-30 09:09:30 -04005784 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005785 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005786 while (!list_empty(&victims)) {
5787 dp = list_first_entry(&victims, struct nfs4_delegation,
5788 dl_recall_lru);
5789 list_del_init(&dp->dl_recall_lru);
5790 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005791 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005792 }
Benny Halevycdc97502014-05-30 09:09:30 -04005793 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005794
5795 return count;
5796}
5797
Bryan Schumaker184c1842012-11-29 11:40:44 -05005798u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5799{
5800 u64 count = 0;
5801
Benny Halevycdc97502014-05-30 09:09:30 -04005802 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005803 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005804 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005805
5806 nfsd_print_count(clp, count, "delegations");
5807 return count;
5808}
5809
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005810u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005811{
5812 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005813 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005814 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005815
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005816 if (!nfsd_netns_ready(nn))
5817 return 0;
5818
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005819 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005820 count += func(clp, max - count);
5821 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005822 break;
5823 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005824
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005825 return count;
5826}
5827
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005828struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5829{
5830 struct nfs4_client *clp;
5831 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5832
5833 if (!nfsd_netns_ready(nn))
5834 return NULL;
5835
5836 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5837 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5838 return clp;
5839 }
5840 return NULL;
5841}
5842
Bryan Schumaker65178db2011-11-01 13:35:21 -04005843#endif /* CONFIG_NFSD_FAULT_INJECTION */
5844
Meelap Shahc2f1a552007-07-17 04:04:39 -07005845/*
5846 * Since the lifetime of a delegation isn't limited to that of an open, a
5847 * client may quite reasonably hang on to a delegation as long as it has
5848 * the inode cached. This becomes an obvious problem the first time a
5849 * client's inode cache approaches the size of the server's total memory.
5850 *
5851 * For now we avoid this problem by imposing a hard limit on the number
5852 * of delegations, which varies according to the server's memory size.
5853 */
5854static void
5855set_max_delegations(void)
5856{
5857 /*
5858 * Allow at most 4 delegations per megabyte of RAM. Quick
5859 * estimates suggest that in the worst case (where every delegation
5860 * is for a different inode), a delegation could take about 1.5K,
5861 * giving a worst case usage of about 6% of memory.
5862 */
5863 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5864}
5865
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005866static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005867{
5868 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5869 int i;
5870
5871 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5872 CLIENT_HASH_SIZE, GFP_KERNEL);
5873 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005874 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005875 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5876 CLIENT_HASH_SIZE, GFP_KERNEL);
5877 if (!nn->unconf_id_hashtbl)
5878 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005879 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5880 SESSION_HASH_SIZE, GFP_KERNEL);
5881 if (!nn->sessionid_hashtbl)
5882 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005883
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005884 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005885 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005886 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005887 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005888 for (i = 0; i < SESSION_HASH_SIZE; i++)
5889 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005890 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005891 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005892 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005893 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005894 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005895 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005896
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005897 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005898 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005899
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005900 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005901
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005902err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005903 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005904err_unconf_id:
5905 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005906err:
5907 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005908}
5909
5910static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005911nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005912{
5913 int i;
5914 struct nfs4_client *clp = NULL;
5915 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5916
5917 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5918 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5919 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5920 destroy_client(clp);
5921 }
5922 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005923
Kinglong Mee2b905632014-03-26 22:09:30 +08005924 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5925 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5926 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5927 destroy_client(clp);
5928 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005929 }
5930
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005931 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005932 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005933 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005934 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005935}
5936
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005937int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005938nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005939{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005940 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005941 int ret;
5942
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005943 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005944 if (ret)
5945 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005946 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005947 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005948 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005949 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005950 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005951 nn->nfsd4_grace, net);
5952 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005953 return 0;
5954}
5955
5956/* initialization to perform when the nfsd service is started: */
5957
5958int
5959nfs4_state_start(void)
5960{
5961 int ret;
5962
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005963 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005964 if (ret)
5965 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005966 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005967 if (laundry_wq == NULL) {
5968 ret = -ENOMEM;
5969 goto out_recovery;
5970 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005971 ret = nfsd4_create_callback_queue();
5972 if (ret)
5973 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005974
Meelap Shahc2f1a552007-07-17 04:04:39 -07005975 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005976
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005977 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005978
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005979out_free_laundry:
5980 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005981out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005982 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983}
5984
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005985void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005986nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005990 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005992 cancel_delayed_work_sync(&nn->laundromat_work);
5993 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005994
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005995 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04005997 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005998 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006000 unhash_delegation_locked(dp);
6001 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 }
Benny Halevycdc97502014-05-30 09:09:30 -04006003 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 list_for_each_safe(pos, next, &reaplist) {
6005 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006006 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04006007 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008 }
6009
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006010 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006011 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04006012 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013}
6014
6015void
6016nfs4_state_shutdown(void)
6017{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006018 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006019 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006021
6022static void
6023get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6024{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006025 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6026 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006027}
6028
6029static void
6030put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6031{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006032 if (cstate->minorversion) {
6033 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6034 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6035 }
6036}
6037
6038void
6039clear_current_stateid(struct nfsd4_compound_state *cstate)
6040{
6041 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006042}
6043
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006044/*
6045 * functions to set current state id
6046 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006047void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006048nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6049{
6050 put_stateid(cstate, &odp->od_stateid);
6051}
6052
6053void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006054nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6055{
6056 put_stateid(cstate, &open->op_stateid);
6057}
6058
6059void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006060nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6061{
6062 put_stateid(cstate, &close->cl_stateid);
6063}
6064
6065void
6066nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6067{
6068 put_stateid(cstate, &lock->lk_resp_stateid);
6069}
6070
6071/*
6072 * functions to consume current state id
6073 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006074
6075void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006076nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6077{
6078 get_stateid(cstate, &odp->od_stateid);
6079}
6080
6081void
6082nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6083{
6084 get_stateid(cstate, &drp->dr_stateid);
6085}
6086
6087void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006088nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6089{
6090 get_stateid(cstate, &fsp->fr_stateid);
6091}
6092
6093void
6094nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6095{
6096 get_stateid(cstate, &setattr->sa_stateid);
6097}
6098
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006099void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006100nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6101{
6102 get_stateid(cstate, &close->cl_stateid);
6103}
6104
6105void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006106nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006107{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006108 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006109}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006110
6111void
6112nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6113{
6114 get_stateid(cstate, &read->rd_stateid);
6115}
6116
6117void
6118nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6119{
6120 get_stateid(cstate, &write->wr_stateid);
6121}