blob: 818480035453cbb3dc6b9843ab1e2346348a4e26 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
Dave Hansenaceaf782008-02-15 14:37:31 -080035#include <linux/file.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020036#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070038#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070039#include <linux/swap.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
Chuck Lever7df302f2012-05-29 13:56:37 -040041#include <linux/ratelimit.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050042#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton59766872013-02-04 12:50:00 -050043#include <linux/sunrpc/addr.h>
NeilBrown6282cd52014-06-04 17:39:26 +100044#include <linux/hash.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020045#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040046#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050047#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040048#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040050#include "netns.h"
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define NFSDDBG_FACILITY NFSDDBG_PROC
53
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050054#define all_ones {{~0,~0},~0}
55static const stateid_t one_stateid = {
56 .si_generation = ~0,
57 .si_opaque = all_ones,
58};
59static const stateid_t zero_stateid = {
60 /* all fields zero */
61};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010062static const stateid_t currentstateid = {
63 .si_generation = 1,
64};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050065
Andy Adamsonec6b5d72009-04-03 08:28:28 +030066static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070067
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050068#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
69#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010070#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040073static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040074static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
J. Bruce Fields8b671b82009-02-22 14:51:34 -080076/* Locking: */
77
78/* Currently used for almost all code touching nfsv4 state: */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080079static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
J. Bruce Fields8b671b82009-02-22 14:51:34 -080081/*
82 * Currently used for the del_recall_lru and file hash table. In an
83 * effort to decrease the scope of the client_mutex, this spinlock may
84 * eventually cover more:
85 */
Benny Halevycdc97502014-05-30 09:09:30 -040086static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080087
Jeff Laytonb401be222014-07-29 21:34:33 -040088/*
89 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
90 * the refcount on the open stateid to drop.
91 */
92static DECLARE_WAIT_QUEUE_HEAD(close_wq);
93
Christoph Hellwigabf11352014-05-21 07:43:03 -070094static struct kmem_cache *openowner_slab;
95static struct kmem_cache *lockowner_slab;
96static struct kmem_cache *file_slab;
97static struct kmem_cache *stateid_slab;
98static struct kmem_cache *deleg_slab;
NeilBrowne60d4392005-06-23 22:03:01 -070099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100void
101nfs4_lock_state(void)
102{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800103 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400106static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800107
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400108static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800109{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400110 return ses->se_flags & NFS4_SESSION_DEAD;
111}
112
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400113static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
114{
115 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400116 return nfserr_jukebox;
117 ses->se_flags |= NFS4_SESSION_DEAD;
118 return nfs_ok;
119}
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121void
122nfs4_unlock_state(void)
123{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800124 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
J. Bruce Fields221a6872013-04-01 22:23:49 -0400127static bool is_client_expired(struct nfs4_client *clp)
128{
129 return clp->cl_time == 0;
130}
131
132static __be32 mark_client_expired_locked(struct nfs4_client *clp)
133{
134 if (atomic_read(&clp->cl_refcount))
135 return nfserr_jukebox;
136 clp->cl_time = 0;
137 return nfs_ok;
138}
139
J. Bruce Fields221a6872013-04-01 22:23:49 -0400140static __be32 get_client_locked(struct nfs4_client *clp)
141{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400142 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
143
144 lockdep_assert_held(&nn->client_lock);
145
J. Bruce Fields221a6872013-04-01 22:23:49 -0400146 if (is_client_expired(clp))
147 return nfserr_expired;
148 atomic_inc(&clp->cl_refcount);
149 return nfs_ok;
150}
151
152/* must be called under the client_lock */
153static inline void
154renew_client_locked(struct nfs4_client *clp)
155{
156 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
157
158 if (is_client_expired(clp)) {
159 WARN_ON(1);
160 printk("%s: client (clientid %08x/%08x) already expired\n",
161 __func__,
162 clp->cl_clientid.cl_boot,
163 clp->cl_clientid.cl_id);
164 return;
165 }
166
167 dprintk("renewing client (clientid %08x/%08x)\n",
168 clp->cl_clientid.cl_boot,
169 clp->cl_clientid.cl_id);
170 list_move_tail(&clp->cl_lru, &nn->client_lru);
171 clp->cl_time = get_seconds();
172}
173
174static inline void
175renew_client(struct nfs4_client *clp)
176{
177 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
178
179 spin_lock(&nn->client_lock);
180 renew_client_locked(clp);
181 spin_unlock(&nn->client_lock);
182}
183
Fengguang Wuba138432013-04-16 22:14:15 -0400184static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400185{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400186 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
187
188 lockdep_assert_held(&nn->client_lock);
189
J. Bruce Fields221a6872013-04-01 22:23:49 -0400190 if (!atomic_dec_and_test(&clp->cl_refcount))
191 return;
192 if (!is_client_expired(clp))
193 renew_client_locked(clp);
194}
195
Jeff Layton4b24ca72014-06-30 11:48:44 -0400196static void put_client_renew(struct nfs4_client *clp)
197{
198 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
199
Jeff Laytond6c249b2014-07-08 14:02:50 -0400200 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
201 return;
202 if (!is_client_expired(clp))
203 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400204 spin_unlock(&nn->client_lock);
205}
206
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400207static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
208{
209 __be32 status;
210
211 if (is_session_dead(ses))
212 return nfserr_badsession;
213 status = get_client_locked(ses->se_client);
214 if (status)
215 return status;
216 atomic_inc(&ses->se_ref);
217 return nfs_ok;
218}
219
220static void nfsd4_put_session_locked(struct nfsd4_session *ses)
221{
222 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400223 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
224
225 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400226
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{
Jeff Layton217526e2014-07-30 08:27:11 -04001125 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1126 nfsd_net_id);
1127 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001128
Jeff Layton217526e2014-07-30 08:27:11 -04001129 spin_lock(&nn->client_lock);
1130 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001131 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001132 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001133 oo->oo_last_closed_stid = NULL;
1134 }
Jeff Layton217526e2014-07-30 08:27:11 -04001135 spin_unlock(&nn->client_lock);
1136 if (s)
1137 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001138}
1139
Jeff Layton2c41beb2014-07-29 21:34:41 -04001140static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001141{
1142 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001143 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001144 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001145
Jeff Layton2c41beb2014-07-29 21:34:41 -04001146 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001147
Trond Myklebustd4f04892014-07-29 21:34:36 -04001148 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001149 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001150 while (!list_empty(&oo->oo_owner.so_stateids)) {
1151 stp = list_first_entry(&oo->oo_owner.so_stateids,
1152 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytond83017f2014-07-29 21:34:42 -04001153 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001154 put_ol_stateid_locked(stp, &reaplist);
1155 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001156 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001157 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001158 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001159 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001160}
1161
Marc Eshel5282fd72009-04-03 08:27:52 +03001162static inline int
1163hash_sessionid(struct nfs4_sessionid *sessionid)
1164{
1165 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1166
1167 return sid->sequence % SESSION_HASH_SIZE;
1168}
1169
Trond Myklebust8f199b82012-03-20 15:11:17 -04001170#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001171static inline void
1172dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1173{
1174 u32 *ptr = (u32 *)(&sessionid->data[0]);
1175 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1176}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001177#else
1178static inline void
1179dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1180{
1181}
1182#endif
1183
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001184/*
1185 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1186 * won't be used for replay.
1187 */
1188void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1189{
1190 struct nfs4_stateowner *so = cstate->replay_owner;
1191
1192 if (nfserr == nfserr_replay_me)
1193 return;
1194
1195 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001196 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001197 return;
1198 }
1199 if (!so)
1200 return;
1201 if (so->so_is_open_owner)
1202 release_last_closed_stateid(openowner(so));
1203 so->so_seqid++;
1204 return;
1205}
Marc Eshel5282fd72009-04-03 08:27:52 +03001206
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001207static void
1208gen_sessionid(struct nfsd4_session *ses)
1209{
1210 struct nfs4_client *clp = ses->se_client;
1211 struct nfsd4_sessionid *sid;
1212
1213 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1214 sid->clientid = clp->cl_clientid;
1215 sid->sequence = current_sessionid++;
1216 sid->reserved = 0;
1217}
1218
1219/*
Andy Adamsona6496372009-08-28 08:45:01 -04001220 * The protocol defines ca_maxresponssize_cached to include the size of
1221 * the rpc header, but all we need to cache is the data starting after
1222 * the end of the initial SEQUENCE operation--the rest we regenerate
1223 * each time. Therefore we can advertise a ca_maxresponssize_cached
1224 * value that is the number of bytes in our cache plus a few additional
1225 * bytes. In order to stay on the safe side, and not promise more than
1226 * we can cache, those additional bytes must be the minimum possible: 24
1227 * bytes of rpc header (xid through accept state, with AUTH_NULL
1228 * verifier), 12 for the compound header (with zero-length tag), and 44
1229 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001230 */
Andy Adamsona6496372009-08-28 08:45:01 -04001231#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1232
Andy Adamson557ce262009-08-28 08:45:04 -04001233static void
1234free_session_slots(struct nfsd4_session *ses)
1235{
1236 int i;
1237
1238 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1239 kfree(ses->se_slots[i]);
1240}
1241
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001242/*
1243 * We don't actually need to cache the rpc and session headers, so we
1244 * can allocate a little less for each slot:
1245 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001246static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001247{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001248 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001249
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001250 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1251 size = 0;
1252 else
1253 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1254 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001255}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001256
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001257/*
1258 * XXX: If we run out of reserved DRC memory we could (up to a point)
1259 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001260 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001261 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001262static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001263{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001264 u32 slotsize = slot_bytes(ca);
1265 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001266 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001267
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001268 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001269 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1270 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001271 num = min_t(int, num, avail / slotsize);
1272 nfsd_drc_mem_used += num * slotsize;
1273 spin_unlock(&nfsd_drc_lock);
1274
1275 return num;
1276}
1277
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001278static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001279{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001280 int slotsize = slot_bytes(ca);
1281
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001282 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001283 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001284 spin_unlock(&nfsd_drc_lock);
1285}
1286
Kinglong Mee60810e52014-01-01 00:35:47 +08001287static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1288 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001289{
Kinglong Mee60810e52014-01-01 00:35:47 +08001290 int numslots = fattrs->maxreqs;
1291 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001292 struct nfsd4_session *new;
1293 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001294
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001295 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001296 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1297 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001298
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001299 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001300 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001301 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001302 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001303 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001304 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001305 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001306 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001307 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001308
1309 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1310 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1311
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001312 return new;
1313out_free:
1314 while (i--)
1315 kfree(new->se_slots[i]);
1316 kfree(new);
1317 return NULL;
1318}
1319
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001320static void free_conn(struct nfsd4_conn *c)
1321{
1322 svc_xprt_put(c->cn_xprt);
1323 kfree(c);
1324}
1325
1326static void nfsd4_conn_lost(struct svc_xpt_user *u)
1327{
1328 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1329 struct nfs4_client *clp = c->cn_session->se_client;
1330
1331 spin_lock(&clp->cl_lock);
1332 if (!list_empty(&c->cn_persession)) {
1333 list_del(&c->cn_persession);
1334 free_conn(c);
1335 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001336 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001337 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001338}
1339
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001340static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001341{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001342 struct nfsd4_conn *conn;
1343
1344 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1345 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001346 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001347 svc_xprt_get(rqstp->rq_xprt);
1348 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001349 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001350 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1351 return conn;
1352}
1353
J. Bruce Fields328ead22010-09-29 16:11:06 -04001354static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1355{
1356 conn->cn_session = ses;
1357 list_add(&conn->cn_persession, &ses->se_conns);
1358}
1359
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001360static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1361{
1362 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001363
1364 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001365 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001366 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001367}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001368
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001369static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001370{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001371 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001372 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001373}
1374
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001375static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001376{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001377 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001378
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001379 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001380 ret = nfsd4_register_conn(conn);
1381 if (ret)
1382 /* oops; xprt is already down: */
1383 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001384 /* We may have gained or lost a callback channel: */
1385 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001386}
1387
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001388static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001389{
1390 u32 dir = NFS4_CDFC4_FORE;
1391
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001392 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001393 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001394 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001395}
1396
1397/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001398static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001399{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001400 struct nfs4_client *clp = s->se_client;
1401 struct nfsd4_conn *c;
1402
1403 spin_lock(&clp->cl_lock);
1404 while (!list_empty(&s->se_conns)) {
1405 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1406 list_del_init(&c->cn_persession);
1407 spin_unlock(&clp->cl_lock);
1408
1409 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1410 free_conn(c);
1411
1412 spin_lock(&clp->cl_lock);
1413 }
1414 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001415}
1416
J. Bruce Fields1377b692012-09-11 21:42:40 -04001417static void __free_session(struct nfsd4_session *ses)
1418{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001419 free_session_slots(ses);
1420 kfree(ses);
1421}
1422
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001423static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001424{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001425 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001426 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001427 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001428}
Andy Adamson557ce262009-08-28 08:45:04 -04001429
Fengguang Wu135ae822012-11-10 07:20:25 -05001430static 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 -04001431{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001432 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001433 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001434
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001435 new->se_client = clp;
1436 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001437
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001438 INIT_LIST_HEAD(&new->se_conns);
1439
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001440 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001441 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001442 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001443 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001444 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001445 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001446 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001447 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001448 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001449 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001450
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001451 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001452 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001453 /*
1454 * This is a little silly; with sessions there's no real
1455 * use for the callback address. Use the peer address
1456 * as a reasonable default for now, but consider fixing
1457 * the rpc client not to require an address in the
1458 * future:
1459 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001460 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1461 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001462 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001463}
1464
Benny Halevy9089f1b2010-05-12 00:12:26 +03001465/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001466static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001467__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001468{
1469 struct nfsd4_session *elem;
1470 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001471 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001472
Trond Myklebust0a880a22014-07-30 08:27:10 -04001473 lockdep_assert_held(&nn->client_lock);
1474
Marc Eshel5282fd72009-04-03 08:27:52 +03001475 dump_sessionid(__func__, sessionid);
1476 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001477 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001478 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001479 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1480 NFS4_MAX_SESSIONID_LEN)) {
1481 return elem;
1482 }
1483 }
1484
1485 dprintk("%s: session not found\n", __func__);
1486 return NULL;
1487}
1488
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001489static struct nfsd4_session *
1490find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1491 __be32 *ret)
1492{
1493 struct nfsd4_session *session;
1494 __be32 status = nfserr_badsession;
1495
1496 session = __find_in_sessionid_hashtbl(sessionid, net);
1497 if (!session)
1498 goto out;
1499 status = nfsd4_get_session_locked(session);
1500 if (status)
1501 session = NULL;
1502out:
1503 *ret = status;
1504 return session;
1505}
1506
Benny Halevy9089f1b2010-05-12 00:12:26 +03001507/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001508static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001509unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001510{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001511 struct nfs4_client *clp = ses->se_client;
1512 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1513
1514 lockdep_assert_held(&nn->client_lock);
1515
Andy Adamson7116ed62009-04-03 08:27:43 +03001516 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001517 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001518 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001519 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001520}
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1523static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001524STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001526 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001528 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001529 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 return 1;
1531}
1532
1533/*
1534 * XXX Should we use a slab cache ?
1535 * This type of memory management is somewhat inefficient, but we use it
1536 * anyway since SETCLIENTID is not a common operation.
1537 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001538static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
1540 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001541 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001543 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1544 if (clp == NULL)
1545 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001546 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001547 if (clp->cl_name.data == NULL)
1548 goto err_no_name;
1549 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1550 OWNER_HASH_SIZE, GFP_KERNEL);
1551 if (!clp->cl_ownerstr_hashtbl)
1552 goto err_no_hashtbl;
1553 for (i = 0; i < OWNER_HASH_SIZE; i++)
1554 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001555 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001556 INIT_LIST_HEAD(&clp->cl_sessions);
1557 idr_init(&clp->cl_stateids);
1558 atomic_set(&clp->cl_refcount, 0);
1559 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1560 INIT_LIST_HEAD(&clp->cl_idhash);
1561 INIT_LIST_HEAD(&clp->cl_openowners);
1562 INIT_LIST_HEAD(&clp->cl_delegations);
1563 INIT_LIST_HEAD(&clp->cl_lru);
1564 INIT_LIST_HEAD(&clp->cl_callbacks);
1565 INIT_LIST_HEAD(&clp->cl_revoked);
1566 spin_lock_init(&clp->cl_lock);
1567 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001569err_no_hashtbl:
1570 kfree(clp->cl_name.data);
1571err_no_name:
1572 kfree(clp);
1573 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001576static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577free_client(struct nfs4_client *clp)
1578{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001579 while (!list_empty(&clp->cl_sessions)) {
1580 struct nfsd4_session *ses;
1581 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1582 se_perclnt);
1583 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001584 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1585 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001586 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001587 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001588 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001589 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001591 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 kfree(clp);
1593}
1594
Benny Halevy84d38ac2010-05-12 00:13:16 +03001595/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001596static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001597unhash_client_locked(struct nfs4_client *clp)
1598{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001599 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001600 struct nfsd4_session *ses;
1601
Trond Myklebust0a880a22014-07-30 08:27:10 -04001602 lockdep_assert_held(&nn->client_lock);
1603
Trond Myklebust4beb3452014-07-30 08:27:02 -04001604 /* Mark the client as expired! */
1605 clp->cl_time = 0;
1606 /* Make it invisible */
1607 if (!list_empty(&clp->cl_idhash)) {
1608 list_del_init(&clp->cl_idhash);
1609 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1610 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1611 else
1612 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1613 }
1614 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001615 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001616 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1617 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001618 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001619}
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001622unhash_client(struct nfs4_client *clp)
1623{
1624 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1625
1626 spin_lock(&nn->client_lock);
1627 unhash_client_locked(clp);
1628 spin_unlock(&nn->client_lock);
1629}
1630
1631static void
1632__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001634 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 struct list_head reaplist;
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001639 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001640 while (!list_empty(&clp->cl_delegations)) {
1641 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001642 unhash_delegation_locked(dp);
1643 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 }
Benny Halevycdc97502014-05-30 09:09:30 -04001645 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 while (!list_empty(&reaplist)) {
1647 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001648 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001649 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001651 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001652 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001653 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001654 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001655 }
NeilBrownea1da632005-06-23 22:04:17 -07001656 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001657 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Jeff Laytond3134b12014-07-29 21:34:32 -04001658 atomic_inc(&oo->oo_owner.so_count);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001659 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001661 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001662 if (clp->cl_cb_conn.cb_xprt)
1663 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001664 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
1666
Trond Myklebust4beb3452014-07-30 08:27:02 -04001667static void
1668destroy_client(struct nfs4_client *clp)
1669{
1670 unhash_client(clp);
1671 __destroy_client(clp);
1672}
1673
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001674static void expire_client(struct nfs4_client *clp)
1675{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001676 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001677 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001678 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001679}
1680
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001681static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1682{
1683 memcpy(target->cl_verifier.data, source->data,
1684 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001687static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1688{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1690 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1691}
1692
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001693static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001694{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001695 if (source->cr_principal) {
1696 target->cr_principal =
1697 kstrdup(source->cr_principal, GFP_KERNEL);
1698 if (target->cr_principal == NULL)
1699 return -ENOMEM;
1700 } else
1701 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001702 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 target->cr_uid = source->cr_uid;
1704 target->cr_gid = source->cr_gid;
1705 target->cr_group_info = source->cr_group_info;
1706 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001707 target->cr_gss_mech = source->cr_gss_mech;
1708 if (source->cr_gss_mech)
1709 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001710 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711}
1712
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001713static long long
1714compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1715{
1716 long long res;
1717
1718 res = o1->len - o2->len;
1719 if (res)
1720 return res;
1721 return (long long)memcmp(o1->data, o2->data, o1->len);
1722}
1723
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001724static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001725{
NeilBrowna55370a2005-06-23 22:03:52 -07001726 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728
1729static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001730same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1731{
1732 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
1734
1735static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001736same_clid(clientid_t *cl1, clientid_t *cl2)
1737{
1738 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739}
1740
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001741static bool groups_equal(struct group_info *g1, struct group_info *g2)
1742{
1743 int i;
1744
1745 if (g1->ngroups != g2->ngroups)
1746 return false;
1747 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001748 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001749 return false;
1750 return true;
1751}
1752
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001753/*
1754 * RFC 3530 language requires clid_inuse be returned when the
1755 * "principal" associated with a requests differs from that previously
1756 * used. We use uid, gid's, and gss principal string as our best
1757 * approximation. We also don't want to allow non-gss use of a client
1758 * established using gss: in theory cr_principal should catch that
1759 * change, but in practice cr_principal can be null even in the gss case
1760 * since gssd doesn't always pass down a principal string.
1761 */
1762static bool is_gss_cred(struct svc_cred *cr)
1763{
1764 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1765 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1766}
1767
1768
Vivek Trivedi5559b502012-07-24 21:18:20 +05301769static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001770same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1771{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001772 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001773 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1774 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001775 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1776 return false;
1777 if (cr1->cr_principal == cr2->cr_principal)
1778 return true;
1779 if (!cr1->cr_principal || !cr2->cr_principal)
1780 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301781 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782}
1783
J. Bruce Fields57266a62013-04-13 14:27:29 -04001784static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1785{
1786 struct svc_cred *cr = &rqstp->rq_cred;
1787 u32 service;
1788
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001789 if (!cr->cr_gss_mech)
1790 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001791 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1792 return service == RPC_GSS_SVC_INTEGRITY ||
1793 service == RPC_GSS_SVC_PRIVACY;
1794}
1795
1796static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1797{
1798 struct svc_cred *cr = &rqstp->rq_cred;
1799
1800 if (!cl->cl_mach_cred)
1801 return true;
1802 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1803 return false;
1804 if (!svc_rqst_integrity_protected(rqstp))
1805 return false;
1806 if (!cr->cr_principal)
1807 return false;
1808 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1809}
1810
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001811static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001812{
1813 static u32 current_clientid = 1;
1814
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001815 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 clp->cl_clientid.cl_id = current_clientid++;
1817}
1818
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001819static void gen_confirm(struct nfs4_client *clp)
1820{
Chuck Leverab4684d2012-03-02 17:13:50 -05001821 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001822 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Jeff Laytonf4199922014-06-17 07:44:11 -04001824 /*
1825 * This is opaque to client, so no need to byte-swap. Use
1826 * __force to keep sparse happy
1827 */
1828 verf[0] = (__force __be32)get_seconds();
1829 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001830 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831}
1832
Jeff Layton4770d722014-07-29 21:34:10 -04001833static struct nfs4_stid *
1834find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001835{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001836 struct nfs4_stid *ret;
1837
1838 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1839 if (!ret || !ret->sc_type)
1840 return NULL;
1841 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001842}
1843
Jeff Layton4770d722014-07-29 21:34:10 -04001844static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001845find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001846{
1847 struct nfs4_stid *s;
1848
Jeff Layton4770d722014-07-29 21:34:10 -04001849 spin_lock(&cl->cl_lock);
1850 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001851 if (s != NULL) {
1852 if (typemask & s->sc_type)
1853 atomic_inc(&s->sc_count);
1854 else
1855 s = NULL;
1856 }
Jeff Layton4770d722014-07-29 21:34:10 -04001857 spin_unlock(&cl->cl_lock);
1858 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001859}
1860
Jeff Layton2216d442012-11-12 15:00:57 -05001861static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001862 struct svc_rqst *rqstp, nfs4_verifier *verf)
1863{
1864 struct nfs4_client *clp;
1865 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001866 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001867 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001868
1869 clp = alloc_client(name);
1870 if (clp == NULL)
1871 return NULL;
1872
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001873 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1874 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001875 free_client(clp);
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
Trond Myklebust0a880a22014-07-30 08:27:10 -04001935 lockdep_assert_held(&nn->client_lock);
1936
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001937 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001938 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001940 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001941 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942}
1943
NeilBrownfd39ca92005-06-23 22:04:03 -07001944static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945move_to_confirmed(struct nfs4_client *clp)
1946{
1947 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001948 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Trond Myklebust0a880a22014-07-30 08:27:10 -04001950 lockdep_assert_held(&nn->client_lock);
1951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001953 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001954 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001955 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001956 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001957 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958}
1959
1960static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001961find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
1963 struct nfs4_client *clp;
1964 unsigned int idhashval = clientid_hashval(clid->cl_id);
1965
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001966 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001967 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001968 if ((bool)clp->cl_minorversion != sessions)
1969 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001970 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 }
1974 return NULL;
1975}
1976
1977static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001978find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1979{
1980 struct list_head *tbl = nn->conf_id_hashtbl;
1981
Trond Myklebust0a880a22014-07-30 08:27:10 -04001982 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001983 return find_client_in_id_table(tbl, clid, sessions);
1984}
1985
1986static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001987find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001989 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Trond Myklebust0a880a22014-07-30 08:27:10 -04001991 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001992 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001995static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001996{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001997 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001998}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001999
NeilBrown28ce6052005-06-23 22:03:56 -07002000static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002001find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002002{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002003 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002004 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002005}
2006
2007static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002008find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002009{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002010 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002011 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002012}
2013
NeilBrownfd39ca92005-06-23 22:04:03 -07002014static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002015gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002017 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002018 struct sockaddr *sa = svc_addr(rqstp);
2019 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002020 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Jeff Layton7077ecb2009-08-14 12:57:58 -04002022 /* Currently, we only support tcp and tcp6 for the callback channel */
2023 if (se->se_callback_netid_len == 3 &&
2024 !memcmp(se->se_callback_netid_val, "tcp", 3))
2025 expected_family = AF_INET;
2026 else if (se->se_callback_netid_len == 4 &&
2027 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2028 expected_family = AF_INET6;
2029 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 goto out_err;
2031
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002032 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002033 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002034 (struct sockaddr *)&conn->cb_addr,
2035 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002036
J. Bruce Fields07263f12010-05-31 19:09:40 -04002037 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002039
J. Bruce Fields07263f12010-05-31 19:09:40 -04002040 if (conn->cb_addr.ss_family == AF_INET6)
2041 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002042
J. Bruce Fields07263f12010-05-31 19:09:40 -04002043 conn->cb_prog = se->se_callback_prog;
2044 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002045 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 return;
2047out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002048 conn->cb_addr.ss_family = AF_UNSPEC;
2049 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002050 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 "will not receive delegations\n",
2052 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 return;
2055}
2056
Andy Adamson074fe892009-04-03 08:28:15 +03002057/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002058 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002059 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002060static void
Andy Adamson074fe892009-04-03 08:28:15 +03002061nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2062{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002063 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002064 struct nfsd4_slot *slot = resp->cstate.slot;
2065 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002066
Andy Adamson557ce262009-08-28 08:45:04 -04002067 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002068
Andy Adamson557ce262009-08-28 08:45:04 -04002069 slot->sl_opcnt = resp->opcnt;
2070 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002071
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002072 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002073 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002074 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002075 return;
2076 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002077 base = resp->cstate.data_offset;
2078 slot->sl_datalen = buf->len - base;
2079 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04002080 WARN("%s: sessions DRC could not cache compound\n", __func__);
2081 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002082}
2083
2084/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002085 * Encode the replay sequence operation from the slot values.
2086 * If cachethis is FALSE encode the uncached rep error on the next
2087 * operation which sets resp->p and increments resp->opcnt for
2088 * nfs4svc_encode_compoundres.
2089 *
Andy Adamson074fe892009-04-03 08:28:15 +03002090 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002091static __be32
2092nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2093 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002094{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002095 struct nfsd4_op *op;
2096 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002097
Andy Adamsonabfabf82009-07-23 19:02:18 -04002098 /* Encode the replayed sequence operation */
2099 op = &args->ops[resp->opcnt - 1];
2100 nfsd4_encode_operation(resp, op);
2101
2102 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002103 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002104 op = &args->ops[resp->opcnt++];
2105 op->status = nfserr_retry_uncached_rep;
2106 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002107 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002108 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002109}
2110
2111/*
Andy Adamson557ce262009-08-28 08:45:04 -04002112 * The sequence operation is not cached because we can use the slot and
2113 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002114 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002115static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002116nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2117 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002118{
Andy Adamson557ce262009-08-28 08:45:04 -04002119 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002120 struct xdr_stream *xdr = &resp->xdr;
2121 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002122 __be32 status;
2123
Andy Adamson557ce262009-08-28 08:45:04 -04002124 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002125
Andy Adamsonabfabf82009-07-23 19:02:18 -04002126 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002127 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002128 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002129
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002130 p = xdr_reserve_space(xdr, slot->sl_datalen);
2131 if (!p) {
2132 WARN_ON_ONCE(1);
2133 return nfserr_serverfault;
2134 }
2135 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2136 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002137
Andy Adamson557ce262009-08-28 08:45:04 -04002138 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002139 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002140}
2141
Andy Adamson0733d212009-04-03 08:28:01 +03002142/*
2143 * Set the exchange_id flags returned by the server.
2144 */
2145static void
2146nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2147{
2148 /* pNFS is not supported */
2149 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2150
2151 /* Referrals are supported, Migration is not. */
2152 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2153
2154 /* set the wire flags to return to client. */
2155 clid->flags = new->cl_exchange_flags;
2156}
2157
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002158static bool client_has_state(struct nfs4_client *clp)
2159{
2160 /*
2161 * Note clp->cl_openowners check isn't quite right: there's no
2162 * need to count owners without stateid's.
2163 *
2164 * Also note we should probably be using this in 4.0 case too.
2165 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002166 return !list_empty(&clp->cl_openowners)
2167 || !list_empty(&clp->cl_delegations)
2168 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002169}
2170
Al Virob37ad282006-10-19 23:28:59 -07002171__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002172nfsd4_exchange_id(struct svc_rqst *rqstp,
2173 struct nfsd4_compound_state *cstate,
2174 struct nfsd4_exchange_id *exid)
2175{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002176 struct nfs4_client *conf, *new;
2177 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002178 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002179 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002180 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002181 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002182 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002183 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002184
Jeff Layton363168b2009-08-14 12:57:56 -04002185 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002186 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002187 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002188 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002189 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002190
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002191 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002192 return nfserr_inval;
2193
Andy Adamson0733d212009-04-03 08:28:01 +03002194 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002195 case SP4_MACH_CRED:
2196 if (!svc_rqst_integrity_protected(rqstp))
2197 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002198 case SP4_NONE:
2199 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002200 default: /* checked by xdr code */
2201 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002202 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002203 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002204 }
2205
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002206 new = create_client(exid->clname, rqstp, &verf);
2207 if (new == NULL)
2208 return nfserr_jukebox;
2209
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002210 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03002211 nfs4_lock_state();
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002212 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002213 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002214 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002215 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2216 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2217
J. Bruce Fields136e6582012-05-12 20:37:23 -04002218 if (update) {
2219 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002220 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002221 goto out;
2222 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002223 if (!mach_creds_match(conf, rqstp)) {
2224 status = nfserr_wrong_cred;
2225 goto out;
2226 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002227 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002228 status = nfserr_perm;
2229 goto out;
2230 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002231 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002232 status = nfserr_not_same;
2233 goto out;
2234 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002235 /* case 6 */
2236 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002237 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002238 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002239 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002240 if (client_has_state(conf)) {
2241 status = nfserr_clid_inuse;
2242 goto out;
2243 }
Andy Adamson0733d212009-04-03 08:28:01 +03002244 goto out_new;
2245 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002246 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002247 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002248 goto out_copy;
2249 }
2250 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002251 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002252 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002253 }
2254
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002255 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002256 status = nfserr_noent;
2257 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002258 }
2259
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002260 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002261 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002262 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002263
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002264 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002265out_new:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002266 if (conf)
2267 unhash_client_locked(conf);
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002268 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002269 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002270
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002271 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002272 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002273 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002274out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002275 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2276 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002277
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002278 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2279 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002280
2281 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002282 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002283 status = nfs_ok;
2284
2285out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002286 spin_unlock(&nn->client_lock);
Andy Adamson0733d212009-04-03 08:28:01 +03002287 nfs4_unlock_state();
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002288 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002289 expire_client(new);
2290 if (unconf)
2291 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002292 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002293}
2294
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002295static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002296check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002297{
Andy Adamson88e588d2009-07-23 19:02:15 -04002298 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2299 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002300
2301 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002302 if (slot_inuse) {
2303 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002304 return nfserr_jukebox;
2305 else
2306 return nfserr_seq_misordered;
2307 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002308 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002309 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002310 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002311 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002312 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002313 return nfserr_seq_misordered;
2314}
2315
Andy Adamson49557cc2009-07-23 19:02:16 -04002316/*
2317 * Cache the create session result into the create session single DRC
2318 * slot cache by saving the xdr structure. sl_seqid has been set.
2319 * Do this for solo or embedded create session operations.
2320 */
2321static void
2322nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002323 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002324{
2325 slot->sl_status = nfserr;
2326 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2327}
2328
2329static __be32
2330nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2331 struct nfsd4_clid_slot *slot)
2332{
2333 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2334 return slot->sl_status;
2335}
2336
Mi Jinlong1b74c252011-07-14 14:50:17 +08002337#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2338 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2339 1 + /* MIN tag is length with zero, only length */ \
2340 3 + /* version, opcount, opcode */ \
2341 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2342 /* seqid, slotID, slotID, cache */ \
2343 4 ) * sizeof(__be32))
2344
2345#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2346 2 + /* verifier: AUTH_NULL, length 0 */\
2347 1 + /* status */ \
2348 1 + /* MIN tag is length with zero, only length */ \
2349 3 + /* opcount, opcode, opstatus*/ \
2350 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2351 /* seqid, slotID, slotID, slotID, status */ \
2352 5 ) * sizeof(__be32))
2353
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002354static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002355{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002356 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2357
J. Bruce Fields373cd402013-04-08 15:42:12 -04002358 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2359 return nfserr_toosmall;
2360 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2361 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002362 ca->headerpadsz = 0;
2363 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2364 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2365 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2366 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2367 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2368 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2369 /*
2370 * Note decreasing slot size below client's request may make it
2371 * difficult for client to function correctly, whereas
2372 * decreasing the number of slots will (just?) affect
2373 * performance. When short on memory we therefore prefer to
2374 * decrease number of slots instead of their size. Clients that
2375 * request larger slots than they need will get poor results:
2376 */
2377 ca->maxreqs = nfsd4_get_drc_mem(ca);
2378 if (!ca->maxreqs)
2379 return nfserr_jukebox;
2380
J. Bruce Fields373cd402013-04-08 15:42:12 -04002381 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002382}
2383
Kinglong Mee8a891632013-12-23 18:11:02 +08002384#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2385 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2386#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2387 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2388
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002389static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2390{
2391 ca->headerpadsz = 0;
2392
2393 /*
2394 * These RPC_MAX_HEADER macros are overkill, especially since we
2395 * don't even do gss on the backchannel yet. But this is still
2396 * less than 1k. Tighten up this estimate in the unlikely event
2397 * it turns out to be a problem for some client:
2398 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002399 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002400 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002401 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002402 return nfserr_toosmall;
2403 ca->maxresp_cached = 0;
2404 if (ca->maxops < 2)
2405 return nfserr_toosmall;
2406
2407 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002408}
2409
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002410static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2411{
2412 switch (cbs->flavor) {
2413 case RPC_AUTH_NULL:
2414 case RPC_AUTH_UNIX:
2415 return nfs_ok;
2416 default:
2417 /*
2418 * GSS case: the spec doesn't allow us to return this
2419 * error. But it also doesn't allow us not to support
2420 * GSS.
2421 * I'd rather this fail hard than return some error the
2422 * client might think it can already handle:
2423 */
2424 return nfserr_encr_alg_unsupp;
2425 }
2426}
2427
Andy Adamson069b6ad2009-04-03 08:27:58 +03002428__be32
2429nfsd4_create_session(struct svc_rqst *rqstp,
2430 struct nfsd4_compound_state *cstate,
2431 struct nfsd4_create_session *cr_ses)
2432{
Jeff Layton363168b2009-08-14 12:57:56 -04002433 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002434 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002435 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002436 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002437 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002438 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002439 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002440 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002441
Mi Jinlonga62573d2011-03-23 17:57:07 +08002442 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2443 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002444 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2445 if (status)
2446 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002447 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002448 if (status)
2449 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002450 status = check_backchannel_attrs(&cr_ses->back_channel);
2451 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002452 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002453 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002454 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002455 if (!new)
2456 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002457 conn = alloc_conn_from_crses(rqstp, cr_ses);
2458 if (!conn)
2459 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002460
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002461 nfs4_lock_state();
Jeff Laytond20c11d2014-07-30 08:27:07 -04002462 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002463 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002464 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002465 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002466
2467 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002468 status = nfserr_wrong_cred;
2469 if (!mach_creds_match(conf, rqstp))
2470 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002471 cs_slot = &conf->cl_cs_slot;
2472 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002473 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002474 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002475 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002476 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
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 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002480 } else if (unconf) {
2481 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002482 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002483 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002484 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002485 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002486 status = nfserr_wrong_cred;
2487 if (!mach_creds_match(unconf, rqstp))
2488 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002489 cs_slot = &unconf->cl_cs_slot;
2490 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002491 if (status) {
2492 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002493 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002494 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002495 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002496 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002497 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002498 status = mark_client_expired_locked(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002499 if (status)
2500 goto out_free_conn;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002501 unhash_client_locked(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002502 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002503 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002504 conf = unconf;
2505 } else {
2506 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002507 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002508 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002509 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002510 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002511 * We do not support RDMA or persistent sessions
2512 */
2513 cr_ses->flags &= ~SESSION4_PERSIST;
2514 cr_ses->flags &= ~SESSION4_RDMA;
2515
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002516 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002517 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002518
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002519 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002520 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002521 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002522 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002523
Jeff Laytond20c11d2014-07-30 08:27:07 -04002524 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002525 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002526 spin_unlock(&nn->client_lock);
2527 /* init connection and backchannel */
2528 nfsd4_init_conn(rqstp, conn, new);
2529 nfsd4_put_session(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002530 nfs4_unlock_state();
Jeff Laytond20c11d2014-07-30 08:27:07 -04002531 if (old)
2532 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002533 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002534out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002535 spin_unlock(&nn->client_lock);
Yanchuan Nian266533c2012-12-24 18:11:45 +08002536 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002537 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002538 if (old)
2539 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002540out_free_session:
2541 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002542out_release_drc_mem:
2543 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002544 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002545}
2546
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002547static __be32 nfsd4_map_bcts_dir(u32 *dir)
2548{
2549 switch (*dir) {
2550 case NFS4_CDFC4_FORE:
2551 case NFS4_CDFC4_BACK:
2552 return nfs_ok;
2553 case NFS4_CDFC4_FORE_OR_BOTH:
2554 case NFS4_CDFC4_BACK_OR_BOTH:
2555 *dir = NFS4_CDFC4_BOTH;
2556 return nfs_ok;
2557 };
2558 return nfserr_inval;
2559}
2560
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002561__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2562{
2563 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002564 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002565 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002566
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002567 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2568 if (status)
2569 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002570 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002571 session->se_cb_prog = bc->bc_cb_program;
2572 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002573 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002574
2575 nfsd4_probe_callback(session->se_client);
2576
2577 return nfs_ok;
2578}
2579
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002580__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2581 struct nfsd4_compound_state *cstate,
2582 struct nfsd4_bind_conn_to_session *bcts)
2583{
2584 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002585 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002586 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002587 struct net *net = SVC_NET(rqstp);
2588 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002589
2590 if (!nfsd4_last_compound_op(rqstp))
2591 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002592 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002593 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002594 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002595 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002596 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002597 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002598 status = nfserr_wrong_cred;
2599 if (!mach_creds_match(session->se_client, rqstp))
2600 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002601 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002602 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002603 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002604 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002605 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002606 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002607 goto out;
2608 nfsd4_init_conn(rqstp, conn, session);
2609 status = nfs_ok;
2610out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002611 nfsd4_put_session(session);
2612out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002613 nfs4_unlock_state();
2614 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002615}
2616
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002617static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2618{
2619 if (!session)
2620 return 0;
2621 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2622}
2623
Andy Adamson069b6ad2009-04-03 08:27:58 +03002624__be32
2625nfsd4_destroy_session(struct svc_rqst *r,
2626 struct nfsd4_compound_state *cstate,
2627 struct nfsd4_destroy_session *sessionid)
2628{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002629 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002630 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002631 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002632 struct net *net = SVC_NET(r);
2633 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002634
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002635 nfs4_lock_state();
2636 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002637 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002638 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002639 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002640 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002641 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002642 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002643 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002644 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002645 if (!ses)
2646 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002647 status = nfserr_wrong_cred;
2648 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002649 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002650 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002651 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002652 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002653 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002654 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002655
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002656 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002657
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002658 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002659 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002660out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002661 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002662out_client_lock:
2663 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002664out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002665 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002666 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002667}
2668
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002669static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002670{
2671 struct nfsd4_conn *c;
2672
2673 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002674 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002675 return c;
2676 }
2677 }
2678 return NULL;
2679}
2680
J. Bruce Fields57266a62013-04-13 14:27:29 -04002681static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002682{
2683 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002684 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002685 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002686 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002687
2688 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002689 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002690 if (c)
2691 goto out_free;
2692 status = nfserr_conn_not_bound_to_session;
2693 if (clp->cl_mach_cred)
2694 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002695 __nfsd4_hash_conn(new, ses);
2696 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002697 ret = nfsd4_register_conn(new);
2698 if (ret)
2699 /* oops; xprt is already down: */
2700 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002701 return nfs_ok;
2702out_free:
2703 spin_unlock(&clp->cl_lock);
2704 free_conn(new);
2705 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002706}
2707
Mi Jinlong868b89c2011-04-27 09:09:58 +08002708static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2709{
2710 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2711
2712 return args->opcnt > session->se_fchannel.maxops;
2713}
2714
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002715static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2716 struct nfsd4_session *session)
2717{
2718 struct xdr_buf *xb = &rqstp->rq_arg;
2719
2720 return xb->len > session->se_fchannel.maxreq_sz;
2721}
2722
Andy Adamson069b6ad2009-04-03 08:27:58 +03002723__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002724nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002725 struct nfsd4_compound_state *cstate,
2726 struct nfsd4_sequence *seq)
2727{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002728 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002729 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002730 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002731 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002732 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002733 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002734 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002735 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002736 struct net *net = SVC_NET(rqstp);
2737 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002738
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002739 if (resp->opcnt != 1)
2740 return nfserr_sequence_pos;
2741
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002742 /*
2743 * Will be either used or freed by nfsd4_sequence_check_conn
2744 * below.
2745 */
2746 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2747 if (!conn)
2748 return nfserr_jukebox;
2749
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002750 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002751 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002752 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002753 goto out_no_session;
2754 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002755
Mi Jinlong868b89c2011-04-27 09:09:58 +08002756 status = nfserr_too_many_ops;
2757 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002758 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002759
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002760 status = nfserr_req_too_big;
2761 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002762 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002763
Benny Halevyb85d4c02009-04-03 08:28:08 +03002764 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002765 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002766 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002767
Andy Adamson557ce262009-08-28 08:45:04 -04002768 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002769 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2770
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002771 /* We do not negotiate the number of slots yet, so set the
2772 * maxslots to the session maxreqs which is used to encode
2773 * sr_highest_slotid and the sr_target_slot id to maxslots */
2774 seq->maxslots = session->se_fchannel.maxreqs;
2775
J. Bruce Fields73e79482012-02-13 16:39:00 -05002776 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2777 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002778 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002779 status = nfserr_seq_misordered;
2780 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002781 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002782 cstate->slot = slot;
2783 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002784 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002785 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002786 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002787 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002788 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002789 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002790 }
2791 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002792 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002793
J. Bruce Fields57266a62013-04-13 14:27:29 -04002794 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002795 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002796 if (status)
2797 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002798
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002799 buflen = (seq->cachethis) ?
2800 session->se_fchannel.maxresp_cached :
2801 session->se_fchannel.maxresp_sz;
2802 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2803 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002804 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002805 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002806 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002807
2808 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002809 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002810 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002811 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002812 if (seq->cachethis)
2813 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002814 else
2815 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002816
2817 cstate->slot = slot;
2818 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002819 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002820
Benny Halevyb85d4c02009-04-03 08:28:08 +03002821out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002822 switch (clp->cl_cb_state) {
2823 case NFSD4_CB_DOWN:
2824 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2825 break;
2826 case NFSD4_CB_FAULT:
2827 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2828 break;
2829 default:
2830 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002831 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002832 if (!list_empty(&clp->cl_revoked))
2833 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002834out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002835 if (conn)
2836 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002837 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002838 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002839out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002840 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002841 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002842}
2843
Trond Myklebustb6076642014-06-30 11:48:35 -04002844void
2845nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2846{
2847 struct nfsd4_compound_state *cs = &resp->cstate;
2848
2849 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002850 if (cs->status != nfserr_replay_cache) {
2851 nfsd4_store_cache_entry(resp);
2852 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2853 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002854 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002855 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002856 } else if (cs->clp)
2857 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002858}
2859
Mi Jinlong345c2842011-10-20 17:51:39 +08002860__be32
2861nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2862{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002863 struct nfs4_client *conf, *unconf;
2864 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002865 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002866 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002867
2868 nfs4_lock_state();
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002869 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002870 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002871 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002872 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002873
2874 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002875 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002876 status = nfserr_clientid_busy;
2877 goto out;
2878 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002879 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08002880 } else if (unconf)
2881 clp = unconf;
2882 else {
2883 status = nfserr_stale_clientid;
2884 goto out;
2885 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002886 if (!mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002887 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002888 status = nfserr_wrong_cred;
2889 goto out;
2890 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002891 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002892out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002893 spin_unlock(&nn->client_lock);
Mi Jinlong345c2842011-10-20 17:51:39 +08002894 nfs4_unlock_state();
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002895 if (clp)
2896 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002897 return status;
2898}
2899
Andy Adamson069b6ad2009-04-03 08:27:58 +03002900__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002901nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2902{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002903 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002904
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002905 if (rc->rca_one_fs) {
2906 if (!cstate->current_fh.fh_dentry)
2907 return nfserr_nofilehandle;
2908 /*
2909 * We don't take advantage of the rca_one_fs case.
2910 * That's OK, it's optional, we can safely ignore it.
2911 */
2912 return nfs_ok;
2913 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002914
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002915 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002916 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002917 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2918 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002919 goto out;
2920
2921 status = nfserr_stale_clientid;
2922 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002923 /*
2924 * The following error isn't really legal.
2925 * But we only get here if the client just explicitly
2926 * destroyed the client. Surely it no longer cares what
2927 * error it gets back on an operation for the dead
2928 * client.
2929 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002930 goto out;
2931
2932 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002933 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002934out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002935 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002936 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002937}
2938
2939__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002940nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2941 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002943 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002945 struct nfs4_client *conf, *new;
2946 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002947 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002948 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2949
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002950 new = create_client(clname, rqstp, &clverifier);
2951 if (new == NULL)
2952 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002953 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 nfs4_lock_state();
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002955 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002956 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002957 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002958 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002960 if (clp_used_exchangeid(conf))
2961 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002962 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002963 char addr_str[INET6_ADDRSTRLEN];
2964 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2965 sizeof(addr_str));
2966 dprintk("NFSD: setclientid: string in use by client "
2967 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 goto out;
2969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002971 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002972 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002973 unhash_client_locked(unconf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002974 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002975 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002977 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002978 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002979 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002980 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002981 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2983 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2984 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002985 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 status = nfs_ok;
2987out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002988 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 nfs4_unlock_state();
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002990 if (new)
2991 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002992 if (unconf)
2993 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 return status;
2995}
2996
2997
Al Virob37ad282006-10-19 23:28:59 -07002998__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002999nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3000 struct nfsd4_compound_state *cstate,
3001 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002{
NeilBrown21ab45a2005-06-23 22:04:14 -07003003 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003004 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3006 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003007 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003008 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003010 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07003013
Jeff Laytond20c11d2014-07-30 08:27:07 -04003014 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003015 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003016 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003017 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003018 * We try hard to give out unique clientid's, so if we get an
3019 * attempt to confirm the same clientid with a different cred,
3020 * there's a bug somewhere. Let's charitably assume it's our
3021 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003022 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04003023 status = nfserr_serverfault;
3024 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3025 goto out;
3026 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3027 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003028 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003029 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3030 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003032 else /* case 4: client hasn't noticed we rebooted yet? */
3033 status = nfserr_stale_clientid;
3034 goto out;
3035 }
3036 status = nfs_ok;
3037 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003038 old = unconf;
3039 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003040 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003041 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003042 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3043 if (old) {
3044 status = mark_client_expired_locked(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003045 if (status)
3046 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003047 unhash_client_locked(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003048 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003049 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003050 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003051 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003052 get_client_locked(conf);
3053 spin_unlock(&nn->client_lock);
3054 nfsd4_probe_callback(conf);
3055 spin_lock(&nn->client_lock);
3056 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003058 spin_unlock(&nn->client_lock);
3059 if (old)
3060 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 nfs4_unlock_state();
3062 return status;
3063}
3064
J. Bruce Fields32513b42011-10-13 16:00:16 -04003065static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003067 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3068}
3069
3070/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003071static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003072{
Trond Myklebustca943212014-07-23 16:17:39 -04003073 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Trond Myklebust950e0112014-06-30 11:48:31 -04003075 lockdep_assert_held(&state_lock);
3076
J. Bruce Fields32513b42011-10-13 16:00:16 -04003077 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003078 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003079 INIT_LIST_HEAD(&fp->fi_stateids);
3080 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003081 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003082 fp->fi_had_conflict = false;
3083 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003084 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003085 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3086 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04003087 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088}
3089
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003090void
NeilBrowne60d4392005-06-23 22:03:01 -07003091nfsd4_free_slabs(void)
3092{
Christoph Hellwigabf11352014-05-21 07:43:03 -07003093 kmem_cache_destroy(openowner_slab);
3094 kmem_cache_destroy(lockowner_slab);
3095 kmem_cache_destroy(file_slab);
3096 kmem_cache_destroy(stateid_slab);
3097 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003098}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Bryan Schumaker72083392011-11-01 15:24:59 -04003100int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101nfsd4_init_slabs(void)
3102{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003103 openowner_slab = kmem_cache_create("nfsd4_openowners",
3104 sizeof(struct nfs4_openowner), 0, 0, NULL);
3105 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003106 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003107 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003108 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003109 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003110 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003111 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003112 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003113 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003114 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003115 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003116 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003117 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003118 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003119 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003120 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003121 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003122 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003124
3125out_free_stateid_slab:
3126 kmem_cache_destroy(stateid_slab);
3127out_free_file_slab:
3128 kmem_cache_destroy(file_slab);
3129out_free_lockowner_slab:
3130 kmem_cache_destroy(lockowner_slab);
3131out_free_openowner_slab:
3132 kmem_cache_destroy(openowner_slab);
3133out:
NeilBrowne60d4392005-06-23 22:03:01 -07003134 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3135 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136}
3137
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003138static void init_nfs4_replay(struct nfs4_replay *rp)
3139{
3140 rp->rp_status = nfserr_serverfault;
3141 rp->rp_buflen = 0;
3142 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003143 mutex_init(&rp->rp_mutex);
3144}
3145
3146static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3147 struct nfs4_stateowner *so)
3148{
3149 if (!nfsd4_has_session(cstate)) {
3150 mutex_lock(&so->so_replay.rp_mutex);
3151 cstate->replay_owner = so;
3152 atomic_inc(&so->so_count);
3153 }
3154}
3155
3156void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3157{
3158 struct nfs4_stateowner *so = cstate->replay_owner;
3159
3160 if (so != NULL) {
3161 cstate->replay_owner = NULL;
3162 mutex_unlock(&so->so_replay.rp_mutex);
3163 nfs4_put_stateowner(so);
3164 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003165}
3166
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003167static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168{
3169 struct nfs4_stateowner *sop;
3170
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003171 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003172 if (!sop)
3173 return NULL;
3174
3175 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3176 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003177 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003178 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003180 sop->so_owner.len = owner->len;
3181
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003182 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003183 sop->so_client = clp;
3184 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003185 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003186 return sop;
3187}
3188
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003189static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003190{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003191 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003192
Trond Myklebustd4f04892014-07-29 21:34:36 -04003193 list_add(&oo->oo_owner.so_strhash,
3194 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003195 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196}
3197
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003198static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3199{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003200 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003201}
3202
Jeff Layton6b180f02014-07-29 21:34:26 -04003203static void nfs4_free_openowner(struct nfs4_stateowner *so)
3204{
3205 struct nfs4_openowner *oo = openowner(so);
3206
3207 kmem_cache_free(openowner_slab, oo);
3208}
3209
3210static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003211 .so_unhash = nfs4_unhash_openowner,
3212 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003213};
3214
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003215static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003216alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003217 struct nfsd4_compound_state *cstate)
3218{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003219 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003220 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003222 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3223 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003225 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003226 oo->oo_owner.so_is_open_owner = 1;
3227 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003228 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003229 if (nfsd4_has_session(cstate))
3230 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003231 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003232 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003233 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003234 spin_lock(&clp->cl_lock);
3235 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003236 if (ret == NULL) {
3237 hash_openowner(oo, clp, strhashval);
3238 ret = oo;
3239 } else
3240 nfs4_free_openowner(&oo->oo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003241 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003242 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243}
3244
J. Bruce Fields996e0932011-10-17 11:14:48 -04003245static 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 -04003246 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003248 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003249 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003250 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003251 stp->st_stateowner = &oo->oo_owner;
Jeff Laytond3134b12014-07-29 21:34:32 -04003252 atomic_inc(&stp->st_stateowner->so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07003253 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003254 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 stp->st_access_bmap = 0;
3256 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003257 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003258 spin_lock(&oo->oo_owner.so_client->cl_lock);
3259 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003260 spin_lock(&fp->fi_lock);
3261 list_add(&stp->st_perfile, &fp->fi_stateids);
3262 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003263 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264}
3265
Jeff Laytond3134b12014-07-29 21:34:32 -04003266/*
3267 * In the 4.0 case we need to keep the owners around a little while to handle
3268 * CLOSE replay. We still do need to release any file access that is held by
3269 * them before returning however.
3270 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003272move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273{
Jeff Layton217526e2014-07-30 08:27:11 -04003274 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003275 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3276 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3277 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003278
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003279 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
Jeff Laytonb401be222014-07-29 21:34:33 -04003281 /*
3282 * We know that we hold one reference via nfsd4_close, and another
3283 * "persistent" reference for the client. If the refcount is higher
3284 * than 2, then there are still calls in progress that are using this
3285 * stateid. We can't put the sc_file reference until they are finished.
3286 * Wait for the refcount to drop to 2. Since it has been unhashed,
3287 * there should be no danger of the refcount going back up again at
3288 * this point.
3289 */
3290 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3291
Jeff Laytond3134b12014-07-29 21:34:32 -04003292 release_all_access(s);
3293 if (s->st_stid.sc_file) {
3294 put_nfs4_file(s->st_stid.sc_file);
3295 s->st_stid.sc_file = NULL;
3296 }
Jeff Layton217526e2014-07-30 08:27:11 -04003297
3298 spin_lock(&nn->client_lock);
3299 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003300 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003301 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003302 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003303 spin_unlock(&nn->client_lock);
3304 if (last)
3305 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306}
3307
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308/* search file_hashtbl[] for file */
3309static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003310find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311{
Trond Myklebustca943212014-07-23 16:17:39 -04003312 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 struct nfs4_file *fp;
3314
Trond Myklebust950e0112014-06-30 11:48:31 -04003315 lockdep_assert_held(&state_lock);
3316
Jeff Layton89876f82013-04-02 09:01:59 -04003317 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003318 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003319 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 }
3323 return NULL;
3324}
3325
Trond Myklebust950e0112014-06-30 11:48:31 -04003326static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003327find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003328{
3329 struct nfs4_file *fp;
3330
3331 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003332 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003333 spin_unlock(&state_lock);
3334 return fp;
3335}
3336
3337static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003338find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003339{
3340 struct nfs4_file *fp;
3341
3342 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003343 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003344 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003345 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003346 fp = new;
3347 }
3348 spin_unlock(&state_lock);
3349
3350 return fp;
3351}
3352
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003353/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 * Called to check deny when READ with all zero stateid or
3355 * WRITE with all zero or all one stateid
3356 */
Al Virob37ad282006-10-19 23:28:59 -07003357static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3359{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003361 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
Trond Myklebustca943212014-07-23 16:17:39 -04003363 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003364 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003365 return ret;
3366 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003367 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003368 if (fp->fi_share_deny & deny_type)
3369 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003370 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003371 put_nfs4_file(fp);
3372 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373}
3374
Jeff Layton02e12152014-07-16 10:31:57 -04003375void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376{
Trond Myklebust11b91642014-07-29 21:34:08 -04003377 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3378 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003379
Trond Myklebust11b91642014-07-29 21:34:08 -04003380 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003381
Jeff Layton02e12152014-07-16 10:31:57 -04003382 /*
3383 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003384 * already holding inode->i_lock.
3385 *
Jeff Laytondff13992014-07-08 14:02:49 -04003386 * If the dl_time != 0, then we know that it has already been
3387 * queued for a lease break. Don't queue it again.
3388 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003389 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003390 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003391 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003392 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003393 }
Jeff Layton02e12152014-07-16 10:31:57 -04003394 spin_unlock(&state_lock);
3395}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
Jeff Layton02e12152014-07-16 10:31:57 -04003397static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3398{
3399 /*
3400 * We're assuming the state code never drops its reference
3401 * without first removing the lease. Since we're in this lease
3402 * callback (and since the lease code is serialized by the kernel
3403 * lock) we know the server hasn't removed the lease yet, we know
3404 * it's safe to take a reference.
3405 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003406 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003407 nfsd4_cb_recall(dp);
3408}
3409
Jeff Layton1c8c6012013-06-21 08:58:15 -04003410/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003411static void nfsd_break_deleg_cb(struct file_lock *fl)
3412{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003413 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3414 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003415
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003416 if (!fp) {
3417 WARN(1, "(%p)->fl_owner NULL\n", fl);
3418 return;
3419 }
3420 if (fp->fi_had_conflict) {
3421 WARN(1, "duplicate break on %p\n", fp);
3422 return;
3423 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003424 /*
3425 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003426 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003427 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003428 */
3429 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
Jeff Layton02e12152014-07-16 10:31:57 -04003431 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003432 fp->fi_had_conflict = true;
3433 /*
3434 * If there are no delegations on the list, then we can't count on this
3435 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3436 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3437 * true should keep any new delegations from being hashed.
3438 */
3439 if (list_empty(&fp->fi_delegations))
3440 fl->fl_break_time = jiffies;
3441 else
3442 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3443 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003444 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445}
3446
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447static
3448int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3449{
3450 if (arg & F_UNLCK)
3451 return lease_modify(onlist, arg);
3452 else
3453 return -EAGAIN;
3454}
3455
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003456static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003457 .lm_break = nfsd_break_deleg_cb,
3458 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459};
3460
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003461static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3462{
3463 if (nfsd4_has_session(cstate))
3464 return nfs_ok;
3465 if (seqid == so->so_seqid - 1)
3466 return nfserr_replay_me;
3467 if (seqid == so->so_seqid)
3468 return nfs_ok;
3469 return nfserr_bad_seqid;
3470}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
Jeff Layton4b24ca72014-06-30 11:48:44 -04003472static __be32 lookup_clientid(clientid_t *clid,
3473 struct nfsd4_compound_state *cstate,
3474 struct nfsd_net *nn)
3475{
3476 struct nfs4_client *found;
3477
3478 if (cstate->clp) {
3479 found = cstate->clp;
3480 if (!same_clid(&found->cl_clientid, clid))
3481 return nfserr_stale_clientid;
3482 return nfs_ok;
3483 }
3484
3485 if (STALE_CLIENTID(clid, nn))
3486 return nfserr_stale_clientid;
3487
3488 /*
3489 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3490 * cached already then we know this is for is for v4.0 and "sessions"
3491 * will be false.
3492 */
3493 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003494 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003495 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003496 if (!found) {
3497 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003498 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003499 }
3500 atomic_inc(&found->cl_refcount);
3501 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003502
3503 /* Cache the nfs4_client in cstate! */
3504 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003505 return nfs_ok;
3506}
3507
Al Virob37ad282006-10-19 23:28:59 -07003508__be32
Andy Adamson66689582009-04-03 08:28:45 +03003509nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003510 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 clientid_t *clientid = &open->op_clientid;
3513 struct nfs4_client *clp = NULL;
3514 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003515 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003516 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003518 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003520 /*
3521 * In case we need it later, after we've already created the
3522 * file and don't want to risk a further failure:
3523 */
3524 open->op_file = nfsd4_alloc_file();
3525 if (open->op_file == NULL)
3526 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527
Trond Myklebust2d91e892014-06-30 11:48:46 -04003528 status = lookup_clientid(clientid, cstate, nn);
3529 if (status)
3530 return status;
3531 clp = cstate->clp;
3532
Trond Myklebustd4f04892014-07-29 21:34:36 -04003533 strhashval = ownerstr_hashval(&open->op_owner);
3534 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003535 open->op_openowner = oo;
3536 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003537 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003539 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003540 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003541 release_openowner(oo);
3542 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003543 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003544 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003545 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3546 if (status)
3547 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003548 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003549new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003550 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003551 if (oo == NULL)
3552 return nfserr_jukebox;
3553 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003554alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003555 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003556 if (!open->op_stp)
3557 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003558 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559}
3560
Al Virob37ad282006-10-19 23:28:59 -07003561static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003562nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3563{
3564 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3565 return nfserr_openmode;
3566 else
3567 return nfs_ok;
3568}
3569
Daniel Mackc47d8322011-05-16 16:38:14 +02003570static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003571{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003572 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3573}
3574
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003575static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003576{
3577 struct nfs4_stid *ret;
3578
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003579 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003580 if (!ret)
3581 return NULL;
3582 return delegstateid(ret);
3583}
3584
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003585static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3586{
3587 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3588 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3589}
3590
Al Virob37ad282006-10-19 23:28:59 -07003591static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003592nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003593 struct nfs4_delegation **dp)
3594{
3595 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003596 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003597 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003598
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003599 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3600 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003601 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003602 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003603 status = nfs4_check_delegmode(deleg, flags);
3604 if (status) {
3605 nfs4_put_stid(&deleg->dl_stid);
3606 goto out;
3607 }
3608 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003609out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003610 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003611 return nfs_ok;
3612 if (status)
3613 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003614 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003615 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003616}
3617
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003618static struct nfs4_ol_stateid *
3619nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003621 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003622 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623
Trond Myklebust1d31a252014-07-10 14:07:25 -04003624 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003625 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 /* ignore lock owners */
3627 if (local->st_stateowner->so_is_open_owner == 0)
3628 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003629 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003630 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003631 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003632 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003635 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003636 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637}
3638
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003639static inline int nfs4_access_to_access(u32 nfs4_access)
3640{
3641 int flags = 0;
3642
3643 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3644 flags |= NFSD_MAY_READ;
3645 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3646 flags |= NFSD_MAY_WRITE;
3647 return flags;
3648}
3649
Al Virob37ad282006-10-19 23:28:59 -07003650static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3652 struct nfsd4_open *open)
3653{
3654 struct iattr iattr = {
3655 .ia_valid = ATTR_SIZE,
3656 .ia_size = 0,
3657 };
3658 if (!open->op_truncate)
3659 return 0;
3660 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003661 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3663}
3664
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003665static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003666 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3667 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003668{
Trond Myklebustde186432014-07-10 14:07:26 -04003669 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003670 __be32 status;
3671 int oflag = nfs4_access_to_omode(open->op_share_access);
3672 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003673 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003674
Trond Myklebustde186432014-07-10 14:07:26 -04003675 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003676
3677 /*
3678 * Are we trying to set a deny mode that would conflict with
3679 * current access?
3680 */
3681 status = nfs4_file_check_deny(fp, open->op_share_deny);
3682 if (status != nfs_ok) {
3683 spin_unlock(&fp->fi_lock);
3684 goto out;
3685 }
3686
3687 /* set access to the file */
3688 status = nfs4_file_get_access(fp, open->op_share_access);
3689 if (status != nfs_ok) {
3690 spin_unlock(&fp->fi_lock);
3691 goto out;
3692 }
3693
3694 /* Set access bits in stateid */
3695 old_access_bmap = stp->st_access_bmap;
3696 set_access(open->op_share_access, stp);
3697
3698 /* Set new deny mask */
3699 old_deny_bmap = stp->st_deny_bmap;
3700 set_deny(open->op_share_deny, stp);
3701 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3702
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003703 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003704 spin_unlock(&fp->fi_lock);
3705 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003706 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003707 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003708 spin_lock(&fp->fi_lock);
3709 if (!fp->fi_fds[oflag]) {
3710 fp->fi_fds[oflag] = filp;
3711 filp = NULL;
3712 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003713 }
Trond Myklebustde186432014-07-10 14:07:26 -04003714 spin_unlock(&fp->fi_lock);
3715 if (filp)
3716 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003717
3718 status = nfsd4_truncate(rqstp, cur_fh, open);
3719 if (status)
3720 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003721out:
3722 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003723out_put_access:
3724 stp->st_access_bmap = old_access_bmap;
3725 nfs4_file_put_access(fp, open->op_share_access);
3726 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3727 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003728}
3729
Al Virob37ad282006-10-19 23:28:59 -07003730static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003731nfs4_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 -07003732{
Al Virob37ad282006-10-19 23:28:59 -07003733 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003734 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003736 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003737 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003738
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003739 /* test and set deny mode */
3740 spin_lock(&fp->fi_lock);
3741 status = nfs4_file_check_deny(fp, open->op_share_deny);
3742 if (status == nfs_ok) {
3743 old_deny_bmap = stp->st_deny_bmap;
3744 set_deny(open->op_share_deny, stp);
3745 fp->fi_share_deny |=
3746 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3747 }
3748 spin_unlock(&fp->fi_lock);
3749
3750 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003753 status = nfsd4_truncate(rqstp, cur_fh, open);
3754 if (status != nfs_ok)
3755 reset_union_bmap_deny(old_deny_bmap, stp);
3756 return status;
3757}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003760nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003762 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763}
3764
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003765/* Should we give out recallable state?: */
3766static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3767{
3768 if (clp->cl_cb_state == NFSD4_CB_UP)
3769 return true;
3770 /*
3771 * In the sessions case, since we don't have to establish a
3772 * separate connection for callbacks, we assume it's OK
3773 * until we hear otherwise:
3774 */
3775 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3776}
3777
Jeff Laytond564fbe2014-07-16 10:31:58 -04003778static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003779{
3780 struct file_lock *fl;
3781
3782 fl = locks_alloc_lock();
3783 if (!fl)
3784 return NULL;
3785 locks_init_lock(fl);
3786 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003787 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003788 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3789 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003790 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003791 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003792 return fl;
3793}
3794
J. Bruce Fields99c41512013-05-21 16:21:25 -04003795static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003796{
Trond Myklebust11b91642014-07-29 21:34:08 -04003797 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003798 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003799 struct file *filp;
3800 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003801
Jeff Laytond564fbe2014-07-16 10:31:58 -04003802 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003803 if (!fl)
3804 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003805 filp = find_readable_file(fp);
3806 if (!filp) {
3807 /* We should always have a readable file here */
3808 WARN_ON_ONCE(1);
3809 return -EBADF;
3810 }
3811 fl->fl_file = filp;
3812 status = vfs_setlease(filp, fl->fl_type, &fl);
3813 if (status) {
3814 locks_free_lock(fl);
3815 goto out_fput;
3816 }
Benny Halevycdc97502014-05-30 09:09:30 -04003817 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003818 spin_lock(&fp->fi_lock);
3819 /* Did the lease get broken before we took the lock? */
3820 status = -EAGAIN;
3821 if (fp->fi_had_conflict)
3822 goto out_unlock;
3823 /* Race breaker */
3824 if (fp->fi_lease) {
3825 status = 0;
3826 atomic_inc(&fp->fi_delegees);
3827 hash_delegation_locked(dp, fp);
3828 goto out_unlock;
3829 }
3830 fp->fi_lease = fl;
3831 fp->fi_deleg_file = filp;
3832 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003833 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003834 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003835 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003836 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003837out_unlock:
3838 spin_unlock(&fp->fi_lock);
3839 spin_unlock(&state_lock);
3840out_fput:
3841 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003842 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003843}
3844
Jeff Layton0b266932014-07-25 07:34:25 -04003845static struct nfs4_delegation *
3846nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3847 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003848{
Jeff Layton0b266932014-07-25 07:34:25 -04003849 int status;
3850 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003851
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003852 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003853 return ERR_PTR(-EAGAIN);
3854
3855 dp = alloc_init_deleg(clp, fh);
3856 if (!dp)
3857 return ERR_PTR(-ENOMEM);
3858
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003859 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003860 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003861 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003862 dp->dl_stid.sc_file = fp;
Jeff Layton417c6622014-07-21 09:34:57 -04003863 if (!fp->fi_lease) {
3864 spin_unlock(&fp->fi_lock);
3865 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003866 status = nfs4_setlease(dp);
3867 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003868 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003869 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003870 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003871 status = -EAGAIN;
3872 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003873 }
Benny Halevy931ee562014-05-30 09:09:27 -04003874 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003875 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003876out_unlock:
3877 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003878 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003879out:
3880 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003881 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003882 return ERR_PTR(status);
3883 }
3884 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003885}
3886
Benny Halevy4aa89132012-02-21 14:16:44 -08003887static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3888{
3889 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3890 if (status == -EAGAIN)
3891 open->op_why_no_deleg = WND4_CONTENTION;
3892 else {
3893 open->op_why_no_deleg = WND4_RESOURCE;
3894 switch (open->op_deleg_want) {
3895 case NFS4_SHARE_WANT_READ_DELEG:
3896 case NFS4_SHARE_WANT_WRITE_DELEG:
3897 case NFS4_SHARE_WANT_ANY_DELEG:
3898 break;
3899 case NFS4_SHARE_WANT_CANCEL:
3900 open->op_why_no_deleg = WND4_CANCELLED;
3901 break;
3902 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003903 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003904 }
3905 }
3906}
3907
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908/*
3909 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003910 *
3911 * Note we don't support write delegations, and won't until the vfs has
3912 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 */
3914static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003915nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3916 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917{
3918 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003919 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3920 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003921 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003922 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003924 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003925 open->op_recall = 0;
3926 switch (open->op_claim_type) {
3927 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003928 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003929 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003930 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3931 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003932 break;
3933 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003934 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003935 /*
3936 * Let's not give out any delegations till everyone's
3937 * had the chance to reclaim theirs....
3938 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003939 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003940 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003941 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003942 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003943 /*
3944 * Also, if the file was opened for write or
3945 * create, there's a good chance the client's
3946 * about to write to it, resulting in an
3947 * immediate recall (since we don't support
3948 * write delegations):
3949 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003950 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003951 goto out_no_deleg;
3952 if (open->op_create == NFS4_OPEN_CREATE)
3953 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003954 break;
3955 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003956 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003957 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003958 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003959 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003960 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003962 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003964 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003965 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003966 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003967 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003968 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003969out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003970 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3971 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003972 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003973 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003974 open->op_recall = 1;
3975 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003976
3977 /* 4.1 client asking for a delegation? */
3978 if (open->op_deleg_want)
3979 nfsd4_open_deleg_none_ext(open, status);
3980 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981}
3982
Benny Halevye27f49c2012-02-21 14:16:54 -08003983static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3984 struct nfs4_delegation *dp)
3985{
3986 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3987 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3988 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3989 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3990 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3991 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3992 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3993 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3994 }
3995 /* Otherwise the client must be confused wanting a delegation
3996 * it already has, therefore we don't return
3997 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3998 */
3999}
4000
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001/*
4002 * called with nfs4_lock_state() held.
4003 */
Al Virob37ad282006-10-19 23:28:59 -07004004__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4006{
Andy Adamson66689582009-04-03 08:28:45 +03004007 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004008 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004010 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004011 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004012 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 /*
4015 * Lookup file; if found, lookup stateid and check open request,
4016 * and check for delegations in the process of being recalled.
4017 * If not found, create the nfs4_file struct
4018 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004019 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004020 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004021 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004022 if (status)
4023 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04004024 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004026 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004027 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004028 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004029 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004030 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 }
4032
4033 /*
4034 * OPEN the file, or upgrade an existing OPEN.
4035 * If truncate fails, the OPEN fails.
4036 */
4037 if (stp) {
4038 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004039 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 if (status)
4041 goto out;
4042 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004043 stp = open->op_stp;
4044 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04004045 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004046 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4047 if (status) {
4048 release_open_stateid(stp);
4049 goto out;
4050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004052 update_stateid(&stp->st_stid.sc_stateid);
4053 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054
Benny Halevyd24433c2012-02-16 20:57:17 +02004055 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004056 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4057 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4058 open->op_why_no_deleg = WND4_NOT_WANTED;
4059 goto nodeleg;
4060 }
4061 }
4062
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 /*
4064 * Attempt to hand out a delegation. No error return, because the
4065 * OPEN succeeds even if we fail.
4066 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004067 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004068nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 status = nfs_ok;
4070
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004071 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004072 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004074 /* 4.1 client trying to upgrade/downgrade delegation? */
4075 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004076 open->op_deleg_want)
4077 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004078
NeilBrown13cd2182005-06-23 22:03:10 -07004079 if (fp)
4080 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004081 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05004082 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 /*
4084 * To finish the open response, we just need to set the rflags.
4085 */
4086 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004087 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004088 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004090 if (dp)
4091 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004092 if (stp)
4093 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094
4095 return status;
4096}
4097
Jeff Layton58fb12e2014-07-29 21:34:27 -04004098void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4099 struct nfsd4_open *open, __be32 status)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004100{
4101 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004102 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004103
Jeff Laytond3134b12014-07-29 21:34:32 -04004104 nfsd4_cstate_assign_replay(cstate, so);
4105 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004106 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004107 if (open->op_file)
4108 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004109 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004110 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004111}
4112
Al Virob37ad282006-10-19 23:28:59 -07004113__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004114nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4115 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116{
4117 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004118 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004119 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120
4121 nfs4_lock_state();
4122 dprintk("process_renew(%08x/%08x): starting\n",
4123 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004124 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004125 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004127 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004129 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004130 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 goto out;
4132 status = nfs_ok;
4133out:
4134 nfs4_unlock_state();
4135 return status;
4136}
4137
NeilBrowna76b4312005-06-23 22:04:01 -07004138static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004139nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004140{
Jeff Layton33dcc482012-04-10 11:08:48 -04004141 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004142 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004143 return;
4144
NeilBrowna76b4312005-06-23 22:04:01 -07004145 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004146 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004147 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004148 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004149 /*
4150 * Now that every NFSv4 client has had the chance to recover and
4151 * to see the (possibly new, possibly shorter) lease time, we
4152 * can safely set the next grace time to the current lease time:
4153 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03004154 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07004155}
4156
NeilBrownfd39ca92005-06-23 22:04:03 -07004157static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004158nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159{
4160 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004161 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004163 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004165 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004166 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167
4168 nfs4_lock_state();
4169
4170 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004171 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004172 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004173 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004174 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 clp = list_entry(pos, struct nfs4_client, cl_lru);
4176 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4177 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004178 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 break;
4180 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004181 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004182 dprintk("NFSD: client in use (clientid %08x)\n",
4183 clp->cl_clientid.cl_id);
4184 continue;
4185 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004186 unhash_client_locked(clp);
4187 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004188 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004189 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004190 list_for_each_safe(pos, next, &reaplist) {
4191 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 dprintk("NFSD: purging unused client (clientid %08x)\n",
4193 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004194 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 expire_client(clp);
4196 }
Benny Halevycdc97502014-05-30 09:09:30 -04004197 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004198 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004200 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4201 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004203 t = dp->dl_time - cutoff;
4204 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 break;
4206 }
Jeff Layton42690672014-07-25 07:34:20 -04004207 unhash_delegation_locked(dp);
4208 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 }
Benny Halevycdc97502014-05-30 09:09:30 -04004210 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004211 while (!list_empty(&reaplist)) {
4212 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4213 dl_recall_lru);
4214 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004215 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 }
Jeff Layton217526e2014-07-30 08:27:11 -04004217
4218 spin_lock(&nn->client_lock);
4219 while (!list_empty(&nn->close_lru)) {
4220 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4221 oo_close_lru);
4222 if (time_after((unsigned long)oo->oo_time,
4223 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004224 t = oo->oo_time - cutoff;
4225 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 break;
4227 }
Jeff Layton217526e2014-07-30 08:27:11 -04004228 list_del_init(&oo->oo_close_lru);
4229 stp = oo->oo_last_closed_stid;
4230 oo->oo_last_closed_stid = NULL;
4231 spin_unlock(&nn->client_lock);
4232 nfs4_put_stid(&stp->st_stid);
4233 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 }
Jeff Layton217526e2014-07-30 08:27:11 -04004235 spin_unlock(&nn->client_lock);
4236
Jeff Laytona832e7a2014-05-30 09:09:26 -04004237 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04004239 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240}
4241
Harvey Harrisona254b242008-02-20 12:49:00 -08004242static struct workqueue_struct *laundry_wq;
4243static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004244
4245static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004246laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247{
4248 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004249 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4250 work);
4251 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4252 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004254 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004256 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257}
4258
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004259static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004260{
Trond Myklebust11b91642014-07-29 21:34:08 -04004261 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004262 return nfserr_bad_stateid;
4263 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264}
4265
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004267access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004269 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4270 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4271 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272}
4273
4274static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004275access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004277 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4278 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279}
4280
4281static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004282__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283{
Al Virob37ad282006-10-19 23:28:59 -07004284 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285
J. Bruce Fields02921912010-07-29 15:16:59 -04004286 /* For lock stateid's, we test the parent open, not the lock: */
4287 if (stp->st_openstp)
4288 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004289 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004291 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 goto out;
4293 status = nfs_ok;
4294out:
4295 return status;
4296}
4297
Al Virob37ad282006-10-19 23:28:59 -07004298static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004299check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004301 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004303 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004304 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 * conflicting state; so we must wait out the grace period. */
4306 return nfserr_grace;
4307 } else if (flags & WR_STATE)
4308 return nfs4_share_conflict(current_fh,
4309 NFS4_SHARE_DENY_WRITE);
4310 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4311 return nfs4_share_conflict(current_fh,
4312 NFS4_SHARE_DENY_READ);
4313}
4314
4315/*
4316 * Allow READ/WRITE during grace period on recovered state only for files
4317 * that are not able to provide mandatory locking.
4318 */
4319static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004320grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004322 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323}
4324
J. Bruce Fields81b82962011-08-23 11:03:29 -04004325/* Returns true iff a is later than b: */
4326static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4327{
Jim Rees1a9357f2013-05-17 17:33:00 -04004328 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004329}
4330
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004331static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004332{
Andy Adamson66689582009-04-03 08:28:45 +03004333 /*
4334 * When sessions are used the stateid generation number is ignored
4335 * when it is zero.
4336 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004337 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004338 return nfs_ok;
4339
4340 if (in->si_generation == ref->si_generation)
4341 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004342
J. Bruce Fields0836f582008-01-26 19:08:12 -05004343 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004344 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004345 return nfserr_bad_stateid;
4346 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004347 * However, we could see a stateid from the past, even from a
4348 * non-buggy client. For example, if the client sends a lock
4349 * while some IO is outstanding, the lock may bump si_generation
4350 * while the IO is still in flight. The client could avoid that
4351 * situation by waiting for responses on all the IO requests,
4352 * but better performance may result in retrying IO that
4353 * receives an old_stateid error if requests are rarely
4354 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004355 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004356 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004357}
4358
Chuck Lever7df302f2012-05-29 13:56:37 -04004359static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004360{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004361 struct nfs4_stid *s;
4362 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004363 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004364
Chuck Lever7df302f2012-05-29 13:56:37 -04004365 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004366 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004367 /* Client debugging aid. */
4368 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4369 char addr_str[INET6_ADDRSTRLEN];
4370 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4371 sizeof(addr_str));
4372 pr_warn_ratelimited("NFSD: client %s testing state ID "
4373 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004374 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004375 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004376 spin_lock(&cl->cl_lock);
4377 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004378 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004379 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004380 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004381 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004382 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004383 switch (s->sc_type) {
4384 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004385 status = nfs_ok;
4386 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004387 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004388 status = nfserr_deleg_revoked;
4389 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004390 case NFS4_OPEN_STID:
4391 case NFS4_LOCK_STID:
4392 ols = openlockstateid(s);
4393 if (ols->st_stateowner->so_is_open_owner
4394 && !(openowner(ols->st_stateowner)->oo_flags
4395 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004396 status = nfserr_bad_stateid;
4397 else
4398 status = nfs_ok;
4399 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004400 default:
4401 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004402 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004403 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004404 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004405 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004406 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004407out_unlock:
4408 spin_unlock(&cl->cl_lock);
4409 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004410}
4411
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004412static __be32
4413nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4414 stateid_t *stateid, unsigned char typemask,
4415 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004416{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004417 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004418
4419 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4420 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004421 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004422 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004423 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004424 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004425 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004426 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004427 if (status)
4428 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004429 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004430 if (!*s)
4431 return nfserr_bad_stateid;
4432 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004433}
4434
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435/*
4436* Checks for stateid operations
4437*/
Al Virob37ad282006-10-19 23:28:59 -07004438__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004439nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004440 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004442 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004443 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004445 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004447 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004448 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004449 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 if (filpp)
4452 *filpp = NULL;
4453
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004454 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 return nfserr_grace;
4456
4457 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004458 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459
Trond Myklebust14bcab12014-04-18 14:44:07 -04004460 nfs4_lock_state();
4461
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004462 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004463 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004464 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004465 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004466 goto unlock_state;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004467 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4468 if (status)
4469 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004470 switch (s->sc_type) {
4471 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004472 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004473 status = nfs4_check_delegmode(dp, flags);
4474 if (status)
4475 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004476 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004477 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004478 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004479 WARN_ON_ONCE(1);
4480 status = nfserr_serverfault;
4481 goto out;
4482 }
Trond Myklebustde186432014-07-10 14:07:26 -04004483 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004484 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004485 break;
4486 case NFS4_OPEN_STID:
4487 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004488 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004489 status = nfs4_check_fh(current_fh, stp);
4490 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004492 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004493 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004495 status = nfs4_check_openmode(stp, flags);
4496 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004498 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004499 struct nfs4_file *fp = stp->st_stid.sc_file;
4500
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004501 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004502 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004503 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004504 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004505 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004506 break;
4507 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004508 status = nfserr_bad_stateid;
4509 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 }
4511 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004512 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004513 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004515 nfs4_put_stid(s);
4516unlock_state:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004517 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 return status;
4519}
4520
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004521/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004522 * Test if the stateid is valid
4523 */
4524__be32
4525nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4526 struct nfsd4_test_stateid *test_stateid)
4527{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004528 struct nfsd4_test_stateid_id *stateid;
4529 struct nfs4_client *cl = cstate->session->se_client;
4530
4531 nfs4_lock_state();
4532 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004533 stateid->ts_id_status =
4534 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004535 nfs4_unlock_state();
4536
Bryan Schumaker17456802011-07-13 10:50:48 -04004537 return nfs_ok;
4538}
4539
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004540__be32
4541nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4542 struct nfsd4_free_stateid *free_stateid)
4543{
4544 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004545 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004546 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004547 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004548 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004549 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004550
4551 nfs4_lock_state();
Jeff Layton1af71cc2014-07-29 21:34:14 -04004552 spin_lock(&cl->cl_lock);
4553 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004554 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004555 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004556 switch (s->sc_type) {
4557 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004558 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004559 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004560 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004561 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4562 if (ret)
4563 break;
4564 ret = nfserr_locks_held;
4565 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004566 case NFS4_LOCK_STID:
4567 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4568 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004569 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004570 stp = openlockstateid(s);
4571 ret = nfserr_locks_held;
4572 if (check_for_locks(stp->st_stid.sc_file,
4573 lockowner(stp->st_stateowner)))
4574 break;
4575 unhash_lock_stateid(stp);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004576 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004577 nfs4_put_stid(s);
4578 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004579 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004580 case NFS4_REVOKED_DELEG_STID:
4581 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004582 list_del_init(&dp->dl_recall_lru);
4583 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004584 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004585 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004586 goto out;
4587 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004588 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004589out_unlock:
4590 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004591out:
4592 nfs4_unlock_state();
4593 return ret;
4594}
4595
NeilBrown4c4cd222005-07-07 17:59:27 -07004596static inline int
4597setlkflg (int type)
4598{
4599 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4600 RD_STATE : WR_STATE;
4601}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004603static __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 -04004604{
4605 struct svc_fh *current_fh = &cstate->current_fh;
4606 struct nfs4_stateowner *sop = stp->st_stateowner;
4607 __be32 status;
4608
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004609 status = nfsd4_check_seqid(cstate, sop, seqid);
4610 if (status)
4611 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004612 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4613 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004614 /*
4615 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004616 * nfserr_replay_me from the previous step, and
4617 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004618 */
4619 return nfserr_bad_stateid;
4620 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4621 if (status)
4622 return status;
4623 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004624}
4625
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626/*
4627 * Checks for sequence id mutating operations.
4628 */
Al Virob37ad282006-10-19 23:28:59 -07004629static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004630nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004631 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004632 struct nfs4_ol_stateid **stpp,
4633 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004635 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004636 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004637 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004639 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4640 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004641
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004643 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004644 if (status)
4645 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004646 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004647 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004649 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004650 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004651 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004652 else
4653 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004654 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004655}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004656
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004657static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4658 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004659{
4660 __be32 status;
4661 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004662 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004664 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004665 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004666 if (status)
4667 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004668 oo = openowner(stp->st_stateowner);
4669 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4670 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004671 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004672 }
4673 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004674 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675}
4676
Al Virob37ad282006-10-19 23:28:59 -07004677__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004678nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004679 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680{
Al Virob37ad282006-10-19 23:28:59 -07004681 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004682 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004683 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004684 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
Al Viroa6a9f182013-09-16 10:57:01 -04004686 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4687 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004689 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004690 if (status)
4691 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692
4693 nfs4_lock_state();
4694
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004695 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004696 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004697 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004698 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004699 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004700 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004701 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004702 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004703 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004704 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004705 update_stateid(&stp->st_stid.sc_stateid);
4706 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004707 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004708 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004709
Jeff Layton2a4317c2012-03-21 16:42:43 -04004710 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004711 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004712put_stateid:
4713 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004715 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004716 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 return status;
4718}
4719
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004720static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004722 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004723 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004724 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004725 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004726}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004727
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004728static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4729{
4730 switch (to_access) {
4731 case NFS4_SHARE_ACCESS_READ:
4732 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4733 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4734 break;
4735 case NFS4_SHARE_ACCESS_WRITE:
4736 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4737 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4738 break;
4739 case NFS4_SHARE_ACCESS_BOTH:
4740 break;
4741 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004742 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 }
4744}
4745
Al Virob37ad282006-10-19 23:28:59 -07004746__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004747nfsd4_open_downgrade(struct svc_rqst *rqstp,
4748 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004749 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750{
Al Virob37ad282006-10-19 23:28:59 -07004751 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004752 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004753 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754
Al Viroa6a9f182013-09-16 10:57:01 -04004755 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4756 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004758 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004759 if (od->od_deleg_want)
4760 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4761 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762
4763 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004764 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004765 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004766 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004769 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004770 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004772 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004774 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004775 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004777 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004779 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780
Jeff Laytonce0fc432012-05-11 09:45:14 -04004781 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004783 update_stateid(&stp->st_stid.sc_stateid);
4784 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004786put_stateid:
4787 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004789 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004790 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 return status;
4792}
4793
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004794static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4795{
Trond Myklebustacf92952014-06-30 11:48:37 -04004796 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytond83017f2014-07-29 21:34:42 -04004797 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004798
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004799 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04004800 spin_lock(&clp->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04004801 unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004802
Jeff Laytond83017f2014-07-29 21:34:42 -04004803 if (clp->cl_minorversion) {
4804 put_ol_stateid_locked(s, &reaplist);
4805 spin_unlock(&clp->cl_lock);
4806 free_ol_stateid_reaplist(&reaplist);
4807 } else {
4808 spin_unlock(&clp->cl_lock);
4809 free_ol_stateid_reaplist(&reaplist);
Jeff Laytond3134b12014-07-29 21:34:32 -04004810 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04004811 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004812}
4813
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814/*
4815 * nfs4_unlock_state() called after encode
4816 */
Al Virob37ad282006-10-19 23:28:59 -07004817__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004818nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004819 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820{
Al Virob37ad282006-10-19 23:28:59 -07004821 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004822 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004823 struct net *net = SVC_NET(rqstp);
4824 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
Al Viroa6a9f182013-09-16 10:57:01 -04004826 dprintk("NFSD: nfsd4_close on file %pd\n",
4827 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828
4829 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004830 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4831 &close->cl_stateid,
4832 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004833 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004834 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004835 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004837 update_stateid(&stp->st_stid.sc_stateid);
4838 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004840 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004841
4842 /* put reference from nfs4_preprocess_seqid_op */
4843 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844out:
Jeff Layton58fb12e2014-07-29 21:34:27 -04004845 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 return status;
4847}
4848
Al Virob37ad282006-10-19 23:28:59 -07004849__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004850nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4851 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004853 struct nfs4_delegation *dp;
4854 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004855 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004856 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004857 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004859 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004860 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861
4862 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004863 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004864 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004865 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004866 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004867 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004868 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004869 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004870
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004871 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004872put_stateid:
4873 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004875 nfs4_unlock_state();
4876
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 return status;
4878}
4879
4880
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882
Benny Halevy87df4de2008-12-15 19:42:03 +02004883static inline u64
4884end_offset(u64 start, u64 len)
4885{
4886 u64 end;
4887
4888 end = start + len;
4889 return end >= start ? end: NFS4_MAX_UINT64;
4890}
4891
4892/* last octet in a range */
4893static inline u64
4894last_byte_offset(u64 start, u64 len)
4895{
4896 u64 end;
4897
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004898 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004899 end = start + len;
4900 return end > start ? end - 1: NFS4_MAX_UINT64;
4901}
4902
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903/*
4904 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4905 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4906 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4907 * locking, this prevents us from being completely protocol-compliant. The
4908 * real solution to this problem is to start using unsigned file offsets in
4909 * the VFS, but this is a very deep change!
4910 */
4911static inline void
4912nfs4_transform_lock_offset(struct file_lock *lock)
4913{
4914 if (lock->fl_start < 0)
4915 lock->fl_start = OFFSET_MAX;
4916 if (lock->fl_end < 0)
4917 lock->fl_end = OFFSET_MAX;
4918}
4919
NeilBrownd5b90262006-04-10 22:55:22 -07004920/* Hack!: For now, we're defining this just so we can use a pointer to it
4921 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004922static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004923};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924
4925static inline void
4926nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4927{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004928 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929
NeilBrownd5b90262006-04-10 22:55:22 -07004930 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004931 lo = (struct nfs4_lockowner *) fl->fl_owner;
4932 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4933 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004934 if (!deny->ld_owner.data)
4935 /* We just don't care that much */
4936 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004937 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4938 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004939 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004940nevermind:
4941 deny->ld_owner.len = 0;
4942 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004943 deny->ld_clientid.cl_boot = 0;
4944 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 }
4946 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004947 deny->ld_length = NFS4_MAX_UINT64;
4948 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4950 deny->ld_type = NFS4_READ_LT;
4951 if (fl->fl_type != F_RDLCK)
4952 deny->ld_type = NFS4_WRITE_LT;
4953}
4954
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004955static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004956find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004957 struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004959 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004960 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961
Trond Myklebust0a880a22014-07-30 08:27:10 -04004962 lockdep_assert_held(&clp->cl_lock);
4963
Trond Myklebustd4f04892014-07-29 21:34:36 -04004964 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4965 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004966 if (so->so_is_open_owner)
4967 continue;
Trond Myklebustd4f04892014-07-29 21:34:36 -04004968 if (!same_owner_str(so, owner))
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004969 continue;
Jeff Layton5db1c032014-07-29 21:34:28 -04004970 atomic_inc(&so->so_count);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004971 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 }
4973 return NULL;
4974}
4975
Trond Myklebustc58c6612014-07-29 21:34:35 -04004976static struct nfs4_lockowner *
4977find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004978 struct nfs4_client *clp)
Trond Myklebustc58c6612014-07-29 21:34:35 -04004979{
4980 struct nfs4_lockowner *lo;
4981
Trond Myklebustd4f04892014-07-29 21:34:36 -04004982 spin_lock(&clp->cl_lock);
4983 lo = find_lockowner_str_locked(clid, owner, clp);
4984 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004985 return lo;
4986}
4987
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004988static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4989{
Trond Myklebustc58c6612014-07-29 21:34:35 -04004990 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004991}
4992
Jeff Layton6b180f02014-07-29 21:34:26 -04004993static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4994{
4995 struct nfs4_lockowner *lo = lockowner(sop);
4996
4997 kmem_cache_free(lockowner_slab, lo);
4998}
4999
5000static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005001 .so_unhash = nfs4_unhash_lockowner,
5002 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005003};
5004
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005/*
5006 * Alloc a lock owner structure.
5007 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005008 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005010 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005012static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005013alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5014 struct nfs4_ol_stateid *open_stp,
5015 struct nfsd4_lock *lock)
5016{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005017 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005019 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5020 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005022 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5023 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005024 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005025 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005026 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005027 ret = find_lockowner_str_locked(&clp->cl_clientid,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005028 &lock->lk_new_owner, clp);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005029 if (ret == NULL) {
5030 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005031 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005032 ret = lo;
5033 } else
5034 nfs4_free_lockowner(&lo->lo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005035 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005036 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037}
5038
Jeff Layton356a95e2014-07-29 21:34:13 -04005039static void
5040init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5041 struct nfs4_file *fp, struct inode *inode,
5042 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005044 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045
Jeff Layton356a95e2014-07-29 21:34:13 -04005046 lockdep_assert_held(&clp->cl_lock);
5047
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005048 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005049 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005050 stp->st_stateowner = &lo->lo_owner;
Trond Myklebuste4f1dd72014-07-29 21:34:30 -04005051 atomic_inc(&lo->lo_owner.so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07005052 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005053 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04005054 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005055 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005057 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005058 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005059 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005060 spin_lock(&fp->fi_lock);
5061 list_add(&stp->st_perfile, &fp->fi_stateids);
5062 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063}
5064
Jeff Laytonc53530d2014-06-30 11:48:39 -04005065static struct nfs4_ol_stateid *
5066find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5067{
5068 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005069 struct nfs4_client *clp = lo->lo_owner.so_client;
5070
5071 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005072
5073 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005074 if (lst->st_stid.sc_file == fp) {
5075 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005076 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005077 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005078 }
5079 return NULL;
5080}
5081
Jeff Layton356a95e2014-07-29 21:34:13 -04005082static struct nfs4_ol_stateid *
5083find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5084 struct inode *inode, struct nfs4_ol_stateid *ost,
5085 bool *new)
5086{
5087 struct nfs4_stid *ns = NULL;
5088 struct nfs4_ol_stateid *lst;
5089 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5090 struct nfs4_client *clp = oo->oo_owner.so_client;
5091
5092 spin_lock(&clp->cl_lock);
5093 lst = find_lock_stateid(lo, fi);
5094 if (lst == NULL) {
5095 spin_unlock(&clp->cl_lock);
5096 ns = nfs4_alloc_stid(clp, stateid_slab);
5097 if (ns == NULL)
5098 return NULL;
5099
5100 spin_lock(&clp->cl_lock);
5101 lst = find_lock_stateid(lo, fi);
5102 if (likely(!lst)) {
5103 lst = openlockstateid(ns);
5104 init_lock_stateid(lst, lo, fi, inode, ost);
5105 ns = NULL;
5106 *new = true;
5107 }
5108 }
5109 spin_unlock(&clp->cl_lock);
5110 if (ns)
5111 nfs4_put_stid(ns);
5112 return lst;
5113}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005114
NeilBrownfd39ca92005-06-23 22:04:03 -07005115static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116check_lock_length(u64 offset, u64 length)
5117{
Benny Halevy87df4de2008-12-15 19:42:03 +02005118 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 LOFF_OVERFLOW(offset, length)));
5120}
5121
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005122static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005123{
Trond Myklebust11b91642014-07-29 21:34:08 -04005124 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005125
Jeff Layton7214e862014-07-10 14:07:33 -04005126 lockdep_assert_held(&fp->fi_lock);
5127
Jeff Layton82c5ff12012-05-11 09:45:13 -04005128 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005129 return;
Jeff Layton12659652014-07-10 14:07:28 -04005130 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005131 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005132}
5133
Jeff Layton356a95e2014-07-29 21:34:13 -04005134static __be32
5135lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5136 struct nfs4_ol_stateid *ost,
5137 struct nfsd4_lock *lock,
5138 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005139{
Jeff Layton5db1c032014-07-29 21:34:28 -04005140 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005141 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005142 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5143 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005144 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005145 struct nfs4_lockowner *lo;
5146 unsigned int strhashval;
5147
Trond Myklebustd4f04892014-07-29 21:34:36 -04005148 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005149 if (!lo) {
Trond Myklebustd4f04892014-07-29 21:34:36 -04005150 strhashval = ownerstr_hashval(&lock->v.new.owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005151 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5152 if (lo == NULL)
5153 return nfserr_jukebox;
5154 } else {
5155 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005156 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005157 if (!cstate->minorversion &&
5158 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005159 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005160 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005161
Jeff Layton356a95e2014-07-29 21:34:13 -04005162 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005163 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005164 status = nfserr_jukebox;
5165 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005166 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005167 status = nfs_ok;
5168out:
5169 nfs4_put_stateowner(&lo->lo_owner);
5170 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005171}
5172
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173/*
5174 * LOCK operation
5175 */
Al Virob37ad282006-10-19 23:28:59 -07005176__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005177nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005178 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005180 struct nfs4_openowner *open_sop = NULL;
5181 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005182 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005183 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005184 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005185 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005186 struct file_lock *file_lock = NULL;
5187 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005188 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005189 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005190 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005191 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005192 struct net *net = SVC_NET(rqstp);
5193 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194
5195 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5196 (long long) lock->lk_offset,
5197 (long long) lock->lk_length);
5198
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 if (check_lock_length(lock->lk_offset, lock->lk_length))
5200 return nfserr_inval;
5201
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005202 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005203 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005204 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5205 return status;
5206 }
5207
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 nfs4_lock_state();
5209
5210 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005211 if (nfsd4_has_session(cstate))
5212 /* See rfc 5661 18.10.3: given clientid is ignored: */
5213 memcpy(&lock->v.new.clientid,
5214 &cstate->session->se_client->cl_clientid,
5215 sizeof(clientid_t));
5216
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005218 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005222 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 lock->lk_new_open_seqid,
5224 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005225 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005226 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005228 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005229 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005230 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005231 &lock->v.new.clientid))
5232 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005233 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005234 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005235 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005236 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005237 lock->lk_old_lock_seqid,
5238 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005239 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005240 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005241 if (status)
5242 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005243 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005245 lkflg = setlkflg(lock->lk_type);
5246 status = nfs4_check_openmode(lock_stp, lkflg);
5247 if (status)
5248 goto out;
5249
NeilBrown0dd395d2005-07-07 17:59:15 -07005250 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005251 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005252 goto out;
5253 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005254 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005255 goto out;
5256
Jeff Layton21179d82012-08-21 08:03:32 -04005257 file_lock = locks_alloc_lock();
5258 if (!file_lock) {
5259 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5260 status = nfserr_jukebox;
5261 goto out;
5262 }
5263
Trond Myklebust11b91642014-07-29 21:34:08 -04005264 fp = lock_stp->st_stid.sc_file;
Jeff Layton21179d82012-08-21 08:03:32 -04005265 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 switch (lock->lk_type) {
5267 case NFS4_READ_LT:
5268 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005269 spin_lock(&fp->fi_lock);
5270 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005271 if (filp)
5272 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005273 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005274 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005275 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 case NFS4_WRITE_LT:
5277 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005278 spin_lock(&fp->fi_lock);
5279 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005280 if (filp)
5281 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005282 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005283 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005284 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 default:
5286 status = nfserr_inval;
5287 goto out;
5288 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005289 if (!filp) {
5290 status = nfserr_openmode;
5291 goto out;
5292 }
Jeff Layton21179d82012-08-21 08:03:32 -04005293 file_lock->fl_owner = (fl_owner_t)lock_sop;
5294 file_lock->fl_pid = current->tgid;
5295 file_lock->fl_file = filp;
5296 file_lock->fl_flags = FL_POSIX;
5297 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5298 file_lock->fl_start = lock->lk_offset;
5299 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5300 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301
Jeff Layton21179d82012-08-21 08:03:32 -04005302 conflock = locks_alloc_lock();
5303 if (!conflock) {
5304 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5305 status = nfserr_jukebox;
5306 goto out;
5307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308
Jeff Layton21179d82012-08-21 08:03:32 -04005309 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005310 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005312 update_stateid(&lock_stp->st_stid.sc_stateid);
5313 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005315 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005316 break;
5317 case (EAGAIN): /* conflock holds conflicting lock */
5318 status = nfserr_denied;
5319 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005320 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005321 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 case (EDEADLK):
5323 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005324 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005325 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005326 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005327 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005328 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330out:
Trond Myklebustde186432014-07-10 14:07:26 -04005331 if (filp)
5332 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005333 if (lock_stp) {
5334 /* Bump seqid manually if the 4.0 replay owner is openowner */
5335 if (cstate->replay_owner &&
5336 cstate->replay_owner != &lock_sop->lo_owner &&
5337 seqid_mutating_err(ntohl(status)))
5338 lock_sop->lo_owner.so_seqid++;
5339
5340 /*
5341 * If this is a new, never-before-used stateid, and we are
5342 * returning an error, then just go ahead and release it.
5343 */
5344 if (status && new)
5345 release_lock_stateid(lock_stp);
5346
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005347 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005348 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005349 if (open_stp)
5350 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005351 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005352 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005353 if (file_lock)
5354 locks_free_lock(file_lock);
5355 if (conflock)
5356 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 return status;
5358}
5359
5360/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005361 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5362 * so we do a temporary open here just to get an open file to pass to
5363 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5364 * inode operation.)
5365 */
Al Viro04da6e92012-04-13 00:00:04 -04005366static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005367{
5368 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005369 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5370 if (!err) {
5371 err = nfserrno(vfs_test_lock(file, lock));
5372 nfsd_close(file);
5373 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005374 return err;
5375}
5376
5377/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 * LOCKT operation
5379 */
Al Virob37ad282006-10-19 23:28:59 -07005380__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005381nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5382 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383{
Jeff Layton21179d82012-08-21 08:03:32 -04005384 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005385 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005386 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005387 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005389 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 return nfserr_grace;
5391
5392 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5393 return nfserr_inval;
5394
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 nfs4_lock_state();
5396
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005397 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005398 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005399 if (status)
5400 goto out;
5401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005403 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405
Jeff Layton21179d82012-08-21 08:03:32 -04005406 file_lock = locks_alloc_lock();
5407 if (!file_lock) {
5408 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5409 status = nfserr_jukebox;
5410 goto out;
5411 }
5412 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 switch (lockt->lt_type) {
5414 case NFS4_READ_LT:
5415 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005416 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 break;
5418 case NFS4_WRITE_LT:
5419 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005420 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 break;
5422 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005423 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 status = nfserr_inval;
5425 goto out;
5426 }
5427
Trond Myklebustd4f04892014-07-29 21:34:36 -04005428 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5429 cstate->clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005430 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005431 file_lock->fl_owner = (fl_owner_t)lo;
5432 file_lock->fl_pid = current->tgid;
5433 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434
Jeff Layton21179d82012-08-21 08:03:32 -04005435 file_lock->fl_start = lockt->lt_offset;
5436 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437
Jeff Layton21179d82012-08-21 08:03:32 -04005438 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439
Jeff Layton21179d82012-08-21 08:03:32 -04005440 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005441 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005442 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005443
Jeff Layton21179d82012-08-21 08:03:32 -04005444 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005446 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 }
5448out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005449 if (lo)
5450 nfs4_put_stateowner(&lo->lo_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005452 if (file_lock)
5453 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 return status;
5455}
5456
Al Virob37ad282006-10-19 23:28:59 -07005457__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005458nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005459 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005461 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005463 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005464 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005465 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005466 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5467
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5469 (long long) locku->lu_offset,
5470 (long long) locku->lu_length);
5471
5472 if (check_lock_length(locku->lu_offset, locku->lu_length))
5473 return nfserr_inval;
5474
5475 nfs4_lock_state();
5476
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005477 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005478 &locku->lu_stateid, NFS4_LOCK_STID,
5479 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005480 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005482 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005483 if (!filp) {
5484 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005485 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005486 }
Jeff Layton21179d82012-08-21 08:03:32 -04005487 file_lock = locks_alloc_lock();
5488 if (!file_lock) {
5489 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5490 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005491 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005492 }
5493 locks_init_lock(file_lock);
5494 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005495 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005496 file_lock->fl_pid = current->tgid;
5497 file_lock->fl_file = filp;
5498 file_lock->fl_flags = FL_POSIX;
5499 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5500 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501
Jeff Layton21179d82012-08-21 08:03:32 -04005502 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5503 locku->lu_length);
5504 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505
Jeff Layton21179d82012-08-21 08:03:32 -04005506 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005507 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005508 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 goto out_nfserr;
5510 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005511 update_stateid(&stp->st_stid.sc_stateid);
5512 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005513fput:
5514 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005515put_stateid:
5516 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005518 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005519 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005520 if (file_lock)
5521 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 return status;
5523
5524out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005525 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005526 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527}
5528
5529/*
5530 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005531 * true: locks held by lockowner
5532 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005534static bool
5535check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536{
5537 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005538 int status = false;
5539 struct file *filp = find_any_file(fp);
5540 struct inode *inode;
5541
5542 if (!filp) {
5543 /* Any valid lock stateid should have some sort of access */
5544 WARN_ON_ONCE(1);
5545 return status;
5546 }
5547
5548 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549
Jeff Layton1c8c6012013-06-21 08:58:15 -04005550 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005552 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005553 status = true;
5554 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005557 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005558 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 return status;
5560}
5561
Al Virob37ad282006-10-19 23:28:59 -07005562__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005563nfsd4_release_lockowner(struct svc_rqst *rqstp,
5564 struct nfsd4_compound_state *cstate,
5565 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566{
5567 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005568 struct nfs4_stateowner *sop;
5569 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005570 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005572 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005573 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005574 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005575 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576
5577 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5578 clid->cl_boot, clid->cl_id);
5579
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 nfs4_lock_state();
5581
Jeff Layton4b24ca72014-06-30 11:48:44 -04005582 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005583 if (status)
5584 goto out;
5585
Trond Myklebustd4f04892014-07-29 21:34:36 -04005586 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005587 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005588 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005589 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005590 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005591
5592 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005593 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005594
Jeff Layton882e9d22014-07-29 21:34:37 -04005595 /* see if there are still any locks associated with it */
5596 lo = lockowner(sop);
5597 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5598 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5599 status = nfserr_locks_held;
5600 spin_unlock(&clp->cl_lock);
5601 goto out;
5602 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005603 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005604
5605 atomic_inc(&sop->so_count);
5606 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005607 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005608 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005609 if (lo)
5610 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611out:
5612 nfs4_unlock_state();
5613 return status;
5614}
5615
5616static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005617alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618{
NeilBrowna55370a2005-06-23 22:03:52 -07005619 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620}
5621
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005622bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005623nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005624{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005625 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005626
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005627 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005628 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005629}
5630
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631/*
5632 * failure => all reset bets are off, nfserr_no_grace...
5633 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005634struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005635nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636{
5637 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005638 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639
NeilBrowna55370a2005-06-23 22:03:52 -07005640 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5641 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005642 if (crp) {
5643 strhashval = clientstr_hashval(name);
5644 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005645 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005646 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005647 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005648 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005649 }
5650 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651}
5652
Jeff Layton2a4317c2012-03-21 16:42:43 -04005653void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005654nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005655{
5656 list_del(&crp->cr_strhash);
5657 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005658 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005659}
5660
5661void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005662nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663{
5664 struct nfs4_client_reclaim *crp = NULL;
5665 int i;
5666
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005668 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5669 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005671 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 }
5673 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005674 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675}
5676
5677/*
5678 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005679struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005680nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681{
5682 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 struct nfs4_client_reclaim *crp = NULL;
5684
Jeff Layton278c9312012-11-12 15:00:52 -05005685 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686
Jeff Layton278c9312012-11-12 15:00:52 -05005687 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005688 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005689 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 return crp;
5691 }
5692 }
5693 return NULL;
5694}
5695
5696/*
5697* Called from OPEN. Look for clientid in reclaim list.
5698*/
Al Virob37ad282006-10-19 23:28:59 -07005699__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005700nfs4_check_open_reclaim(clientid_t *clid,
5701 struct nfsd4_compound_state *cstate,
5702 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005704 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005705
5706 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005707 status = lookup_clientid(clid, cstate, nn);
5708 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005709 return nfserr_reclaim_bad;
5710
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005711 if (nfsd4_client_record_check(cstate->clp))
5712 return nfserr_reclaim_bad;
5713
5714 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715}
5716
Bryan Schumaker65178db2011-11-01 13:35:21 -04005717#ifdef CONFIG_NFSD_FAULT_INJECTION
5718
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005719u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5720{
Jeff Laytond20c11d2014-07-30 08:27:07 -04005721 __be32 ret;
5722 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
5723
5724 spin_lock(&nn->client_lock);
5725 ret = mark_client_expired_locked(clp);
5726 spin_unlock(&nn->client_lock);
5727 if (ret != nfs_ok)
J. Bruce Fields221a6872013-04-01 22:23:49 -04005728 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005729 expire_client(clp);
5730 return 1;
5731}
5732
Bryan Schumaker184c1842012-11-29 11:40:44 -05005733u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5734{
5735 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005736 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005737 printk(KERN_INFO "NFS Client: %s\n", buf);
5738 return 1;
5739}
5740
5741static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5742 const char *type)
5743{
5744 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005745 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005746 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5747}
5748
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005749static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5750 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005751{
5752 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005753 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005754 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005755 u64 count = 0;
5756
5757 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005758 list_for_each_entry_safe(stp, st_next,
5759 &oop->oo_owner.so_stateids, st_perstateowner) {
5760 list_for_each_entry_safe(lst, lst_next,
5761 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005762 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005763 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005764 if (++count == max)
5765 return count;
5766 }
5767 }
5768 }
5769
5770 return count;
5771}
5772
5773u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5774{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005775 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005776}
5777
Bryan Schumaker184c1842012-11-29 11:40:44 -05005778u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5779{
5780 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5781 nfsd_print_count(clp, count, "locked files");
5782 return count;
5783}
5784
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005785static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5786{
5787 struct nfs4_openowner *oop, *next;
5788 u64 count = 0;
5789
5790 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5791 if (func)
5792 func(oop);
5793 if (++count == max)
5794 break;
5795 }
5796
5797 return count;
5798}
5799
5800u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5801{
5802 return nfsd_foreach_client_open(clp, max, release_openowner);
5803}
5804
Bryan Schumaker184c1842012-11-29 11:40:44 -05005805u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5806{
5807 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5808 nfsd_print_count(clp, count, "open files");
5809 return count;
5810}
5811
Bryan Schumaker269de302012-11-29 11:40:42 -05005812static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5813 struct list_head *victims)
5814{
5815 struct nfs4_delegation *dp, *next;
5816 u64 count = 0;
5817
Benny Halevycdc97502014-05-30 09:09:30 -04005818 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005819 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005820 if (victims) {
5821 /*
5822 * It's not safe to mess with delegations that have a
5823 * non-zero dl_time. They might have already been broken
5824 * and could be processed by the laundromat outside of
5825 * the state_lock. Just leave them be.
5826 */
5827 if (dp->dl_time != 0)
5828 continue;
5829
Jeff Layton42690672014-07-25 07:34:20 -04005830 unhash_delegation_locked(dp);
5831 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005832 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005833 if (++count == max)
5834 break;
5835 }
5836 return count;
5837}
5838
5839u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5840{
5841 struct nfs4_delegation *dp, *next;
5842 LIST_HEAD(victims);
5843 u64 count;
5844
Benny Halevycdc97502014-05-30 09:09:30 -04005845 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005846 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005847 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005848
Jeff Layton2d4a5322014-07-25 07:34:21 -04005849 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5850 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005851 revoke_delegation(dp);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005852 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005853
5854 return count;
5855}
5856
5857u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5858{
Jeff Laytondff13992014-07-08 14:02:49 -04005859 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005860 LIST_HEAD(victims);
5861 u64 count;
5862
Benny Halevycdc97502014-05-30 09:09:30 -04005863 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005864 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005865 while (!list_empty(&victims)) {
5866 dp = list_first_entry(&victims, struct nfs4_delegation,
5867 dl_recall_lru);
5868 list_del_init(&dp->dl_recall_lru);
5869 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005870 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005871 }
Benny Halevycdc97502014-05-30 09:09:30 -04005872 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005873
5874 return count;
5875}
5876
Bryan Schumaker184c1842012-11-29 11:40:44 -05005877u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5878{
5879 u64 count = 0;
5880
Benny Halevycdc97502014-05-30 09:09:30 -04005881 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005882 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005883 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005884
5885 nfsd_print_count(clp, count, "delegations");
5886 return count;
5887}
5888
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005889u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005890{
5891 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005892 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005893 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005894
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005895 if (!nfsd_netns_ready(nn))
5896 return 0;
5897
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005898 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005899 count += func(clp, max - count);
5900 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005901 break;
5902 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005903
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005904 return count;
5905}
5906
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005907struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5908{
5909 struct nfs4_client *clp;
5910 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5911
5912 if (!nfsd_netns_ready(nn))
5913 return NULL;
5914
5915 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5916 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5917 return clp;
5918 }
5919 return NULL;
5920}
5921
Bryan Schumaker65178db2011-11-01 13:35:21 -04005922#endif /* CONFIG_NFSD_FAULT_INJECTION */
5923
Meelap Shahc2f1a552007-07-17 04:04:39 -07005924/*
5925 * Since the lifetime of a delegation isn't limited to that of an open, a
5926 * client may quite reasonably hang on to a delegation as long as it has
5927 * the inode cached. This becomes an obvious problem the first time a
5928 * client's inode cache approaches the size of the server's total memory.
5929 *
5930 * For now we avoid this problem by imposing a hard limit on the number
5931 * of delegations, which varies according to the server's memory size.
5932 */
5933static void
5934set_max_delegations(void)
5935{
5936 /*
5937 * Allow at most 4 delegations per megabyte of RAM. Quick
5938 * estimates suggest that in the worst case (where every delegation
5939 * is for a different inode), a delegation could take about 1.5K,
5940 * giving a worst case usage of about 6% of memory.
5941 */
5942 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5943}
5944
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005945static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005946{
5947 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5948 int i;
5949
5950 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5951 CLIENT_HASH_SIZE, GFP_KERNEL);
5952 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005953 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005954 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5955 CLIENT_HASH_SIZE, GFP_KERNEL);
5956 if (!nn->unconf_id_hashtbl)
5957 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005958 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5959 SESSION_HASH_SIZE, GFP_KERNEL);
5960 if (!nn->sessionid_hashtbl)
5961 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005962
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005963 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005964 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005965 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005966 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005967 for (i = 0; i < SESSION_HASH_SIZE; i++)
5968 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005969 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005970 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005971 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005972 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005973 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005974 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005975
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005976 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005977 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005978
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005979 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005980
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005981err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005982 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005983err_unconf_id:
5984 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005985err:
5986 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005987}
5988
5989static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005990nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005991{
5992 int i;
5993 struct nfs4_client *clp = NULL;
5994 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5995
5996 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5997 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5998 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5999 destroy_client(clp);
6000 }
6001 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006002
Kinglong Mee2b905632014-03-26 22:09:30 +08006003 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6004 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6005 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6006 destroy_client(clp);
6007 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006008 }
6009
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006010 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006011 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006012 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006013 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006014}
6015
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006016int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006017nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07006018{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006019 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006020 int ret;
6021
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006022 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006023 if (ret)
6024 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006025 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006026 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006027 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04006028 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006029 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03006030 nn->nfsd4_grace, net);
6031 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006032 return 0;
6033}
6034
6035/* initialization to perform when the nfsd service is started: */
6036
6037int
6038nfs4_state_start(void)
6039{
6040 int ret;
6041
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006042 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006043 if (ret)
6044 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07006045 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05006046 if (laundry_wq == NULL) {
6047 ret = -ENOMEM;
6048 goto out_recovery;
6049 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006050 ret = nfsd4_create_callback_queue();
6051 if (ret)
6052 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006053
Meelap Shahc2f1a552007-07-17 04:04:39 -07006054 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006055
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006056 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006057
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006058out_free_laundry:
6059 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05006060out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006061 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062}
6063
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006064void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006065nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006069 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006071 cancel_delayed_work_sync(&nn->laundromat_work);
6072 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05006073
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04006074 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04006076 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006077 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006079 unhash_delegation_locked(dp);
6080 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 }
Benny Halevycdc97502014-05-30 09:09:30 -04006082 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 list_for_each_safe(pos, next, &reaplist) {
6084 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006085 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04006086 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 }
6088
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006089 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006090 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04006091 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092}
6093
6094void
6095nfs4_state_shutdown(void)
6096{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006097 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006098 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006100
6101static void
6102get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6103{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006104 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6105 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006106}
6107
6108static void
6109put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6110{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006111 if (cstate->minorversion) {
6112 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6113 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6114 }
6115}
6116
6117void
6118clear_current_stateid(struct nfsd4_compound_state *cstate)
6119{
6120 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006121}
6122
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006123/*
6124 * functions to set current state id
6125 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006126void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006127nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6128{
6129 put_stateid(cstate, &odp->od_stateid);
6130}
6131
6132void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006133nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6134{
6135 put_stateid(cstate, &open->op_stateid);
6136}
6137
6138void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006139nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6140{
6141 put_stateid(cstate, &close->cl_stateid);
6142}
6143
6144void
6145nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6146{
6147 put_stateid(cstate, &lock->lk_resp_stateid);
6148}
6149
6150/*
6151 * functions to consume current state id
6152 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006153
6154void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006155nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6156{
6157 get_stateid(cstate, &odp->od_stateid);
6158}
6159
6160void
6161nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6162{
6163 get_stateid(cstate, &drp->dr_stateid);
6164}
6165
6166void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006167nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6168{
6169 get_stateid(cstate, &fsp->fr_stateid);
6170}
6171
6172void
6173nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6174{
6175 get_stateid(cstate, &setattr->sa_stateid);
6176}
6177
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006178void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006179nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6180{
6181 get_stateid(cstate, &close->cl_stateid);
6182}
6183
6184void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006185nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006186{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006187 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006188}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006189
6190void
6191nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6192{
6193 get_stateid(cstate, &read->rd_stateid);
6194}
6195
6196void
6197nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6198{
6199 get_stateid(cstate, &write->wr_stateid);
6200}