blob: e7dfd4e9d94209f9825836ad073fe765b581f977 [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{
1125 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
1126
1127 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001128 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001129 oo->oo_last_closed_stid = NULL;
Jeff Laytond3134b12014-07-29 21:34:32 -04001130 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001131 }
1132}
1133
Jeff Layton2c41beb2014-07-29 21:34:41 -04001134static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001135{
1136 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001137 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001138 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001139
Jeff Layton2c41beb2014-07-29 21:34:41 -04001140 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001141
Trond Myklebustd4f04892014-07-29 21:34:36 -04001142 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001143 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001144 while (!list_empty(&oo->oo_owner.so_stateids)) {
1145 stp = list_first_entry(&oo->oo_owner.so_stateids,
1146 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytond83017f2014-07-29 21:34:42 -04001147 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001148 put_ol_stateid_locked(stp, &reaplist);
1149 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001150 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001151 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001152 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001153 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001154}
1155
Marc Eshel5282fd72009-04-03 08:27:52 +03001156static inline int
1157hash_sessionid(struct nfs4_sessionid *sessionid)
1158{
1159 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1160
1161 return sid->sequence % SESSION_HASH_SIZE;
1162}
1163
Trond Myklebust8f199b82012-03-20 15:11:17 -04001164#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001165static inline void
1166dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1167{
1168 u32 *ptr = (u32 *)(&sessionid->data[0]);
1169 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1170}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001171#else
1172static inline void
1173dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1174{
1175}
1176#endif
1177
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001178/*
1179 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1180 * won't be used for replay.
1181 */
1182void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1183{
1184 struct nfs4_stateowner *so = cstate->replay_owner;
1185
1186 if (nfserr == nfserr_replay_me)
1187 return;
1188
1189 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001190 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001191 return;
1192 }
1193 if (!so)
1194 return;
1195 if (so->so_is_open_owner)
1196 release_last_closed_stateid(openowner(so));
1197 so->so_seqid++;
1198 return;
1199}
Marc Eshel5282fd72009-04-03 08:27:52 +03001200
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001201static void
1202gen_sessionid(struct nfsd4_session *ses)
1203{
1204 struct nfs4_client *clp = ses->se_client;
1205 struct nfsd4_sessionid *sid;
1206
1207 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1208 sid->clientid = clp->cl_clientid;
1209 sid->sequence = current_sessionid++;
1210 sid->reserved = 0;
1211}
1212
1213/*
Andy Adamsona6496372009-08-28 08:45:01 -04001214 * The protocol defines ca_maxresponssize_cached to include the size of
1215 * the rpc header, but all we need to cache is the data starting after
1216 * the end of the initial SEQUENCE operation--the rest we regenerate
1217 * each time. Therefore we can advertise a ca_maxresponssize_cached
1218 * value that is the number of bytes in our cache plus a few additional
1219 * bytes. In order to stay on the safe side, and not promise more than
1220 * we can cache, those additional bytes must be the minimum possible: 24
1221 * bytes of rpc header (xid through accept state, with AUTH_NULL
1222 * verifier), 12 for the compound header (with zero-length tag), and 44
1223 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001224 */
Andy Adamsona6496372009-08-28 08:45:01 -04001225#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1226
Andy Adamson557ce262009-08-28 08:45:04 -04001227static void
1228free_session_slots(struct nfsd4_session *ses)
1229{
1230 int i;
1231
1232 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1233 kfree(ses->se_slots[i]);
1234}
1235
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001236/*
1237 * We don't actually need to cache the rpc and session headers, so we
1238 * can allocate a little less for each slot:
1239 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001240static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001241{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001242 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001243
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001244 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1245 size = 0;
1246 else
1247 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1248 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001249}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001250
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001251/*
1252 * XXX: If we run out of reserved DRC memory we could (up to a point)
1253 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001254 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001255 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001256static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001257{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001258 u32 slotsize = slot_bytes(ca);
1259 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001260 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001261
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001262 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001263 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1264 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001265 num = min_t(int, num, avail / slotsize);
1266 nfsd_drc_mem_used += num * slotsize;
1267 spin_unlock(&nfsd_drc_lock);
1268
1269 return num;
1270}
1271
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001272static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001273{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001274 int slotsize = slot_bytes(ca);
1275
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001276 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001277 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001278 spin_unlock(&nfsd_drc_lock);
1279}
1280
Kinglong Mee60810e52014-01-01 00:35:47 +08001281static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1282 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001283{
Kinglong Mee60810e52014-01-01 00:35:47 +08001284 int numslots = fattrs->maxreqs;
1285 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001286 struct nfsd4_session *new;
1287 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001288
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001289 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001290 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1291 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001292
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001293 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001294 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001295 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001296 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001297 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001298 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001299 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001300 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001301 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001302
1303 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1304 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1305
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001306 return new;
1307out_free:
1308 while (i--)
1309 kfree(new->se_slots[i]);
1310 kfree(new);
1311 return NULL;
1312}
1313
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001314static void free_conn(struct nfsd4_conn *c)
1315{
1316 svc_xprt_put(c->cn_xprt);
1317 kfree(c);
1318}
1319
1320static void nfsd4_conn_lost(struct svc_xpt_user *u)
1321{
1322 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1323 struct nfs4_client *clp = c->cn_session->se_client;
1324
1325 spin_lock(&clp->cl_lock);
1326 if (!list_empty(&c->cn_persession)) {
1327 list_del(&c->cn_persession);
1328 free_conn(c);
1329 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001330 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001331 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001332}
1333
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001334static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001335{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001336 struct nfsd4_conn *conn;
1337
1338 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1339 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001340 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001341 svc_xprt_get(rqstp->rq_xprt);
1342 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001343 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001344 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1345 return conn;
1346}
1347
J. Bruce Fields328ead22010-09-29 16:11:06 -04001348static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1349{
1350 conn->cn_session = ses;
1351 list_add(&conn->cn_persession, &ses->se_conns);
1352}
1353
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001354static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1355{
1356 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001357
1358 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001359 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001360 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001361}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001362
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001363static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001364{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001365 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001366 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001367}
1368
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001369static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001370{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001371 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001372
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001373 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001374 ret = nfsd4_register_conn(conn);
1375 if (ret)
1376 /* oops; xprt is already down: */
1377 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001378 /* We may have gained or lost a callback channel: */
1379 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001380}
1381
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001382static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001383{
1384 u32 dir = NFS4_CDFC4_FORE;
1385
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001386 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001387 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001388 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001389}
1390
1391/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001392static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001393{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001394 struct nfs4_client *clp = s->se_client;
1395 struct nfsd4_conn *c;
1396
1397 spin_lock(&clp->cl_lock);
1398 while (!list_empty(&s->se_conns)) {
1399 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1400 list_del_init(&c->cn_persession);
1401 spin_unlock(&clp->cl_lock);
1402
1403 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1404 free_conn(c);
1405
1406 spin_lock(&clp->cl_lock);
1407 }
1408 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001409}
1410
J. Bruce Fields1377b692012-09-11 21:42:40 -04001411static void __free_session(struct nfsd4_session *ses)
1412{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001413 free_session_slots(ses);
1414 kfree(ses);
1415}
1416
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001417static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001418{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001419 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001420 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001421 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001422}
Andy Adamson557ce262009-08-28 08:45:04 -04001423
Fengguang Wu135ae822012-11-10 07:20:25 -05001424static 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 -04001425{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001426 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001427 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001428
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001429 new->se_client = clp;
1430 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001431
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001432 INIT_LIST_HEAD(&new->se_conns);
1433
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001434 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001435 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001436 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001437 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001438 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001439 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001440 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001441 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001442 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001443 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001444
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001445 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001446 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001447 /*
1448 * This is a little silly; with sessions there's no real
1449 * use for the callback address. Use the peer address
1450 * as a reasonable default for now, but consider fixing
1451 * the rpc client not to require an address in the
1452 * future:
1453 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001454 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1455 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001456 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001457}
1458
Benny Halevy9089f1b2010-05-12 00:12:26 +03001459/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001460static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001461__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001462{
1463 struct nfsd4_session *elem;
1464 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001465 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001466
Trond Myklebust0a880a22014-07-30 08:27:10 -04001467 lockdep_assert_held(&nn->client_lock);
1468
Marc Eshel5282fd72009-04-03 08:27:52 +03001469 dump_sessionid(__func__, sessionid);
1470 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001471 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001472 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001473 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1474 NFS4_MAX_SESSIONID_LEN)) {
1475 return elem;
1476 }
1477 }
1478
1479 dprintk("%s: session not found\n", __func__);
1480 return NULL;
1481}
1482
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001483static struct nfsd4_session *
1484find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1485 __be32 *ret)
1486{
1487 struct nfsd4_session *session;
1488 __be32 status = nfserr_badsession;
1489
1490 session = __find_in_sessionid_hashtbl(sessionid, net);
1491 if (!session)
1492 goto out;
1493 status = nfsd4_get_session_locked(session);
1494 if (status)
1495 session = NULL;
1496out:
1497 *ret = status;
1498 return session;
1499}
1500
Benny Halevy9089f1b2010-05-12 00:12:26 +03001501/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001502static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001503unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001504{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001505 struct nfs4_client *clp = ses->se_client;
1506 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1507
1508 lockdep_assert_held(&nn->client_lock);
1509
Andy Adamson7116ed62009-04-03 08:27:43 +03001510 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001511 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001512 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001513 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001514}
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1517static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001518STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001520 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001522 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001523 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return 1;
1525}
1526
1527/*
1528 * XXX Should we use a slab cache ?
1529 * This type of memory management is somewhat inefficient, but we use it
1530 * anyway since SETCLIENTID is not a common operation.
1531 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001532static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
1534 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001535 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001537 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1538 if (clp == NULL)
1539 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001540 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001541 if (clp->cl_name.data == NULL)
1542 goto err_no_name;
1543 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1544 OWNER_HASH_SIZE, GFP_KERNEL);
1545 if (!clp->cl_ownerstr_hashtbl)
1546 goto err_no_hashtbl;
1547 for (i = 0; i < OWNER_HASH_SIZE; i++)
1548 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001549 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001550 INIT_LIST_HEAD(&clp->cl_sessions);
1551 idr_init(&clp->cl_stateids);
1552 atomic_set(&clp->cl_refcount, 0);
1553 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1554 INIT_LIST_HEAD(&clp->cl_idhash);
1555 INIT_LIST_HEAD(&clp->cl_openowners);
1556 INIT_LIST_HEAD(&clp->cl_delegations);
1557 INIT_LIST_HEAD(&clp->cl_lru);
1558 INIT_LIST_HEAD(&clp->cl_callbacks);
1559 INIT_LIST_HEAD(&clp->cl_revoked);
1560 spin_lock_init(&clp->cl_lock);
1561 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001563err_no_hashtbl:
1564 kfree(clp->cl_name.data);
1565err_no_name:
1566 kfree(clp);
1567 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001570static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571free_client(struct nfs4_client *clp)
1572{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001573 while (!list_empty(&clp->cl_sessions)) {
1574 struct nfsd4_session *ses;
1575 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1576 se_perclnt);
1577 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001578 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1579 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001580 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001581 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001582 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001583 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001585 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 kfree(clp);
1587}
1588
Benny Halevy84d38ac2010-05-12 00:13:16 +03001589/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001590static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001591unhash_client_locked(struct nfs4_client *clp)
1592{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001593 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001594 struct nfsd4_session *ses;
1595
Trond Myklebust0a880a22014-07-30 08:27:10 -04001596 lockdep_assert_held(&nn->client_lock);
1597
Trond Myklebust4beb3452014-07-30 08:27:02 -04001598 /* Mark the client as expired! */
1599 clp->cl_time = 0;
1600 /* Make it invisible */
1601 if (!list_empty(&clp->cl_idhash)) {
1602 list_del_init(&clp->cl_idhash);
1603 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1604 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1605 else
1606 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1607 }
1608 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001609 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001610 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1611 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001612 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001613}
1614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001616unhash_client(struct nfs4_client *clp)
1617{
1618 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1619
1620 spin_lock(&nn->client_lock);
1621 unhash_client_locked(clp);
1622 spin_unlock(&nn->client_lock);
1623}
1624
1625static void
1626__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001628 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 struct list_head reaplist;
1631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001633 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001634 while (!list_empty(&clp->cl_delegations)) {
1635 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001636 unhash_delegation_locked(dp);
1637 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
Benny Halevycdc97502014-05-30 09:09:30 -04001639 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 while (!list_empty(&reaplist)) {
1641 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001642 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001643 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001645 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001646 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001647 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001648 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001649 }
NeilBrownea1da632005-06-23 22:04:17 -07001650 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001651 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Jeff Laytond3134b12014-07-29 21:34:32 -04001652 atomic_inc(&oo->oo_owner.so_count);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001653 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001655 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001656 if (clp->cl_cb_conn.cb_xprt)
1657 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001658 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
1660
Trond Myklebust4beb3452014-07-30 08:27:02 -04001661static void
1662destroy_client(struct nfs4_client *clp)
1663{
1664 unhash_client(clp);
1665 __destroy_client(clp);
1666}
1667
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001668static void expire_client(struct nfs4_client *clp)
1669{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001670 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001671 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001672 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001673}
1674
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001675static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1676{
1677 memcpy(target->cl_verifier.data, source->data,
1678 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
1680
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001681static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1682{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1684 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1685}
1686
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001687static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001688{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001689 if (source->cr_principal) {
1690 target->cr_principal =
1691 kstrdup(source->cr_principal, GFP_KERNEL);
1692 if (target->cr_principal == NULL)
1693 return -ENOMEM;
1694 } else
1695 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001696 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 target->cr_uid = source->cr_uid;
1698 target->cr_gid = source->cr_gid;
1699 target->cr_group_info = source->cr_group_info;
1700 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001701 target->cr_gss_mech = source->cr_gss_mech;
1702 if (source->cr_gss_mech)
1703 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001704 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705}
1706
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001707static long long
1708compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1709{
1710 long long res;
1711
1712 res = o1->len - o2->len;
1713 if (res)
1714 return res;
1715 return (long long)memcmp(o1->data, o2->data, o1->len);
1716}
1717
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001718static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001719{
NeilBrowna55370a2005-06-23 22:03:52 -07001720 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721}
1722
1723static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001724same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1725{
1726 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728
1729static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001730same_clid(clientid_t *cl1, clientid_t *cl2)
1731{
1732 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
1734
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001735static bool groups_equal(struct group_info *g1, struct group_info *g2)
1736{
1737 int i;
1738
1739 if (g1->ngroups != g2->ngroups)
1740 return false;
1741 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001742 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001743 return false;
1744 return true;
1745}
1746
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001747/*
1748 * RFC 3530 language requires clid_inuse be returned when the
1749 * "principal" associated with a requests differs from that previously
1750 * used. We use uid, gid's, and gss principal string as our best
1751 * approximation. We also don't want to allow non-gss use of a client
1752 * established using gss: in theory cr_principal should catch that
1753 * change, but in practice cr_principal can be null even in the gss case
1754 * since gssd doesn't always pass down a principal string.
1755 */
1756static bool is_gss_cred(struct svc_cred *cr)
1757{
1758 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1759 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1760}
1761
1762
Vivek Trivedi5559b502012-07-24 21:18:20 +05301763static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001764same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1765{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001766 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001767 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1768 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001769 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1770 return false;
1771 if (cr1->cr_principal == cr2->cr_principal)
1772 return true;
1773 if (!cr1->cr_principal || !cr2->cr_principal)
1774 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301775 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776}
1777
J. Bruce Fields57266a62013-04-13 14:27:29 -04001778static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1779{
1780 struct svc_cred *cr = &rqstp->rq_cred;
1781 u32 service;
1782
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001783 if (!cr->cr_gss_mech)
1784 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001785 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1786 return service == RPC_GSS_SVC_INTEGRITY ||
1787 service == RPC_GSS_SVC_PRIVACY;
1788}
1789
1790static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1791{
1792 struct svc_cred *cr = &rqstp->rq_cred;
1793
1794 if (!cl->cl_mach_cred)
1795 return true;
1796 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1797 return false;
1798 if (!svc_rqst_integrity_protected(rqstp))
1799 return false;
1800 if (!cr->cr_principal)
1801 return false;
1802 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1803}
1804
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001805static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001806{
1807 static u32 current_clientid = 1;
1808
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001809 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 clp->cl_clientid.cl_id = current_clientid++;
1811}
1812
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001813static void gen_confirm(struct nfs4_client *clp)
1814{
Chuck Leverab4684d2012-03-02 17:13:50 -05001815 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001816 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Jeff Laytonf4199922014-06-17 07:44:11 -04001818 /*
1819 * This is opaque to client, so no need to byte-swap. Use
1820 * __force to keep sparse happy
1821 */
1822 verf[0] = (__force __be32)get_seconds();
1823 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001824 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
Jeff Layton4770d722014-07-29 21:34:10 -04001827static struct nfs4_stid *
1828find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001829{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001830 struct nfs4_stid *ret;
1831
1832 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1833 if (!ret || !ret->sc_type)
1834 return NULL;
1835 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001836}
1837
Jeff Layton4770d722014-07-29 21:34:10 -04001838static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001839find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001840{
1841 struct nfs4_stid *s;
1842
Jeff Layton4770d722014-07-29 21:34:10 -04001843 spin_lock(&cl->cl_lock);
1844 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001845 if (s != NULL) {
1846 if (typemask & s->sc_type)
1847 atomic_inc(&s->sc_count);
1848 else
1849 s = NULL;
1850 }
Jeff Layton4770d722014-07-29 21:34:10 -04001851 spin_unlock(&cl->cl_lock);
1852 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001853}
1854
Jeff Layton2216d442012-11-12 15:00:57 -05001855static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001856 struct svc_rqst *rqstp, nfs4_verifier *verf)
1857{
1858 struct nfs4_client *clp;
1859 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001860 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001861 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001862
1863 clp = alloc_client(name);
1864 if (clp == NULL)
1865 return NULL;
1866
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001867 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1868 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001869 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001870 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001871 }
Jeff Layton02e12152014-07-16 10:31:57 -04001872 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001873 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001874 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001875 copy_verf(clp, verf);
1876 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001877 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001878 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001879 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001880 return clp;
1881}
1882
NeilBrownfd39ca92005-06-23 22:04:03 -07001883static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001884add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1885{
1886 struct rb_node **new = &(root->rb_node), *parent = NULL;
1887 struct nfs4_client *clp;
1888
1889 while (*new) {
1890 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1891 parent = *new;
1892
1893 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1894 new = &((*new)->rb_left);
1895 else
1896 new = &((*new)->rb_right);
1897 }
1898
1899 rb_link_node(&new_clp->cl_namenode, parent, new);
1900 rb_insert_color(&new_clp->cl_namenode, root);
1901}
1902
1903static struct nfs4_client *
1904find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1905{
1906 long long cmp;
1907 struct rb_node *node = root->rb_node;
1908 struct nfs4_client *clp;
1909
1910 while (node) {
1911 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1912 cmp = compare_blob(&clp->cl_name, name);
1913 if (cmp > 0)
1914 node = node->rb_left;
1915 else if (cmp < 0)
1916 node = node->rb_right;
1917 else
1918 return clp;
1919 }
1920 return NULL;
1921}
1922
1923static void
1924add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
1926 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001927 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Trond Myklebust0a880a22014-07-30 08:27:10 -04001929 lockdep_assert_held(&nn->client_lock);
1930
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001931 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001932 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001934 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001935 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936}
1937
NeilBrownfd39ca92005-06-23 22:04:03 -07001938static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939move_to_confirmed(struct nfs4_client *clp)
1940{
1941 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001942 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Trond Myklebust0a880a22014-07-30 08:27:10 -04001944 lockdep_assert_held(&nn->client_lock);
1945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001947 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001948 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001949 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001950 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001951 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952}
1953
1954static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001955find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
1957 struct nfs4_client *clp;
1958 unsigned int idhashval = clientid_hashval(clid->cl_id);
1959
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001960 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001961 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001962 if ((bool)clp->cl_minorversion != sessions)
1963 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001964 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
1968 return NULL;
1969}
1970
1971static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001972find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1973{
1974 struct list_head *tbl = nn->conf_id_hashtbl;
1975
Trond Myklebust0a880a22014-07-30 08:27:10 -04001976 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001977 return find_client_in_id_table(tbl, clid, sessions);
1978}
1979
1980static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001981find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001983 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Trond Myklebust0a880a22014-07-30 08:27:10 -04001985 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001986 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987}
1988
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001989static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001990{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001991 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001992}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001993
NeilBrown28ce6052005-06-23 22:03:56 -07001994static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001995find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001996{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001997 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001998 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001999}
2000
2001static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002002find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002003{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002004 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002005 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002006}
2007
NeilBrownfd39ca92005-06-23 22:04:03 -07002008static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002009gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002011 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002012 struct sockaddr *sa = svc_addr(rqstp);
2013 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002014 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Jeff Layton7077ecb2009-08-14 12:57:58 -04002016 /* Currently, we only support tcp and tcp6 for the callback channel */
2017 if (se->se_callback_netid_len == 3 &&
2018 !memcmp(se->se_callback_netid_val, "tcp", 3))
2019 expected_family = AF_INET;
2020 else if (se->se_callback_netid_len == 4 &&
2021 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2022 expected_family = AF_INET6;
2023 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 goto out_err;
2025
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002026 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002027 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002028 (struct sockaddr *)&conn->cb_addr,
2029 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002030
J. Bruce Fields07263f12010-05-31 19:09:40 -04002031 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002033
J. Bruce Fields07263f12010-05-31 19:09:40 -04002034 if (conn->cb_addr.ss_family == AF_INET6)
2035 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002036
J. Bruce Fields07263f12010-05-31 19:09:40 -04002037 conn->cb_prog = se->se_callback_prog;
2038 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002039 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 return;
2041out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002042 conn->cb_addr.ss_family = AF_UNSPEC;
2043 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002044 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 "will not receive delegations\n",
2046 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 return;
2049}
2050
Andy Adamson074fe892009-04-03 08:28:15 +03002051/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002052 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002053 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002054static void
Andy Adamson074fe892009-04-03 08:28:15 +03002055nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2056{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002057 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002058 struct nfsd4_slot *slot = resp->cstate.slot;
2059 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002060
Andy Adamson557ce262009-08-28 08:45:04 -04002061 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002062
Andy Adamson557ce262009-08-28 08:45:04 -04002063 slot->sl_opcnt = resp->opcnt;
2064 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002065
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002066 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002067 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002068 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002069 return;
2070 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002071 base = resp->cstate.data_offset;
2072 slot->sl_datalen = buf->len - base;
2073 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04002074 WARN("%s: sessions DRC could not cache compound\n", __func__);
2075 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002076}
2077
2078/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002079 * Encode the replay sequence operation from the slot values.
2080 * If cachethis is FALSE encode the uncached rep error on the next
2081 * operation which sets resp->p and increments resp->opcnt for
2082 * nfs4svc_encode_compoundres.
2083 *
Andy Adamson074fe892009-04-03 08:28:15 +03002084 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002085static __be32
2086nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2087 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002088{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002089 struct nfsd4_op *op;
2090 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002091
Andy Adamsonabfabf82009-07-23 19:02:18 -04002092 /* Encode the replayed sequence operation */
2093 op = &args->ops[resp->opcnt - 1];
2094 nfsd4_encode_operation(resp, op);
2095
2096 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002097 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002098 op = &args->ops[resp->opcnt++];
2099 op->status = nfserr_retry_uncached_rep;
2100 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002101 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002102 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002103}
2104
2105/*
Andy Adamson557ce262009-08-28 08:45:04 -04002106 * The sequence operation is not cached because we can use the slot and
2107 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002108 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002109static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002110nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2111 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002112{
Andy Adamson557ce262009-08-28 08:45:04 -04002113 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002114 struct xdr_stream *xdr = &resp->xdr;
2115 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002116 __be32 status;
2117
Andy Adamson557ce262009-08-28 08:45:04 -04002118 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002119
Andy Adamsonabfabf82009-07-23 19:02:18 -04002120 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002121 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002122 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002123
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002124 p = xdr_reserve_space(xdr, slot->sl_datalen);
2125 if (!p) {
2126 WARN_ON_ONCE(1);
2127 return nfserr_serverfault;
2128 }
2129 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2130 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002131
Andy Adamson557ce262009-08-28 08:45:04 -04002132 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002133 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002134}
2135
Andy Adamson0733d212009-04-03 08:28:01 +03002136/*
2137 * Set the exchange_id flags returned by the server.
2138 */
2139static void
2140nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2141{
2142 /* pNFS is not supported */
2143 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2144
2145 /* Referrals are supported, Migration is not. */
2146 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2147
2148 /* set the wire flags to return to client. */
2149 clid->flags = new->cl_exchange_flags;
2150}
2151
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002152static bool client_has_state(struct nfs4_client *clp)
2153{
2154 /*
2155 * Note clp->cl_openowners check isn't quite right: there's no
2156 * need to count owners without stateid's.
2157 *
2158 * Also note we should probably be using this in 4.0 case too.
2159 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002160 return !list_empty(&clp->cl_openowners)
2161 || !list_empty(&clp->cl_delegations)
2162 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002163}
2164
Al Virob37ad282006-10-19 23:28:59 -07002165__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002166nfsd4_exchange_id(struct svc_rqst *rqstp,
2167 struct nfsd4_compound_state *cstate,
2168 struct nfsd4_exchange_id *exid)
2169{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002170 struct nfs4_client *conf, *new;
2171 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002172 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002173 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002174 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002175 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002176 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002177 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002178
Jeff Layton363168b2009-08-14 12:57:56 -04002179 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002180 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002181 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002182 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002183 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002184
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002185 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002186 return nfserr_inval;
2187
Andy Adamson0733d212009-04-03 08:28:01 +03002188 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002189 case SP4_MACH_CRED:
2190 if (!svc_rqst_integrity_protected(rqstp))
2191 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002192 case SP4_NONE:
2193 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002194 default: /* checked by xdr code */
2195 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002196 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002197 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002198 }
2199
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002200 new = create_client(exid->clname, rqstp, &verf);
2201 if (new == NULL)
2202 return nfserr_jukebox;
2203
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002204 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03002205 nfs4_lock_state();
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002206 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002207 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002208 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002209 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2210 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2211
J. Bruce Fields136e6582012-05-12 20:37:23 -04002212 if (update) {
2213 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002214 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002215 goto out;
2216 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002217 if (!mach_creds_match(conf, rqstp)) {
2218 status = nfserr_wrong_cred;
2219 goto out;
2220 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002221 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002222 status = nfserr_perm;
2223 goto out;
2224 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002225 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002226 status = nfserr_not_same;
2227 goto out;
2228 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002229 /* case 6 */
2230 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002231 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002232 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002233 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002234 if (client_has_state(conf)) {
2235 status = nfserr_clid_inuse;
2236 goto out;
2237 }
Andy Adamson0733d212009-04-03 08:28:01 +03002238 goto out_new;
2239 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002240 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002241 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002242 goto out_copy;
2243 }
2244 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002245 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002246 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002247 }
2248
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002249 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002250 status = nfserr_noent;
2251 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002252 }
2253
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002254 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002255 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002256 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002257
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002258 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002259out_new:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002260 if (conf)
2261 unhash_client_locked(conf);
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002262 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002263 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002264
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002265 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002266 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002267 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002268out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002269 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2270 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002271
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002272 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2273 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002274
2275 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002276 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002277 status = nfs_ok;
2278
2279out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002280 spin_unlock(&nn->client_lock);
Andy Adamson0733d212009-04-03 08:28:01 +03002281 nfs4_unlock_state();
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002282 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002283 expire_client(new);
2284 if (unconf)
2285 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002286 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002287}
2288
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002289static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002290check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002291{
Andy Adamson88e588d2009-07-23 19:02:15 -04002292 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2293 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002294
2295 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002296 if (slot_inuse) {
2297 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002298 return nfserr_jukebox;
2299 else
2300 return nfserr_seq_misordered;
2301 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002302 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002303 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002304 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002305 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002306 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002307 return nfserr_seq_misordered;
2308}
2309
Andy Adamson49557cc2009-07-23 19:02:16 -04002310/*
2311 * Cache the create session result into the create session single DRC
2312 * slot cache by saving the xdr structure. sl_seqid has been set.
2313 * Do this for solo or embedded create session operations.
2314 */
2315static void
2316nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002317 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002318{
2319 slot->sl_status = nfserr;
2320 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2321}
2322
2323static __be32
2324nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2325 struct nfsd4_clid_slot *slot)
2326{
2327 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2328 return slot->sl_status;
2329}
2330
Mi Jinlong1b74c252011-07-14 14:50:17 +08002331#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2332 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2333 1 + /* MIN tag is length with zero, only length */ \
2334 3 + /* version, opcount, opcode */ \
2335 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2336 /* seqid, slotID, slotID, cache */ \
2337 4 ) * sizeof(__be32))
2338
2339#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2340 2 + /* verifier: AUTH_NULL, length 0 */\
2341 1 + /* status */ \
2342 1 + /* MIN tag is length with zero, only length */ \
2343 3 + /* opcount, opcode, opstatus*/ \
2344 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2345 /* seqid, slotID, slotID, slotID, status */ \
2346 5 ) * sizeof(__be32))
2347
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002348static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002349{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002350 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2351
J. Bruce Fields373cd402013-04-08 15:42:12 -04002352 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2353 return nfserr_toosmall;
2354 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2355 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002356 ca->headerpadsz = 0;
2357 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2358 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2359 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2360 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2361 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2362 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2363 /*
2364 * Note decreasing slot size below client's request may make it
2365 * difficult for client to function correctly, whereas
2366 * decreasing the number of slots will (just?) affect
2367 * performance. When short on memory we therefore prefer to
2368 * decrease number of slots instead of their size. Clients that
2369 * request larger slots than they need will get poor results:
2370 */
2371 ca->maxreqs = nfsd4_get_drc_mem(ca);
2372 if (!ca->maxreqs)
2373 return nfserr_jukebox;
2374
J. Bruce Fields373cd402013-04-08 15:42:12 -04002375 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002376}
2377
Kinglong Mee8a891632013-12-23 18:11:02 +08002378#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2379 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2380#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2381 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2382
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002383static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2384{
2385 ca->headerpadsz = 0;
2386
2387 /*
2388 * These RPC_MAX_HEADER macros are overkill, especially since we
2389 * don't even do gss on the backchannel yet. But this is still
2390 * less than 1k. Tighten up this estimate in the unlikely event
2391 * it turns out to be a problem for some client:
2392 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002393 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002394 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002395 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002396 return nfserr_toosmall;
2397 ca->maxresp_cached = 0;
2398 if (ca->maxops < 2)
2399 return nfserr_toosmall;
2400
2401 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002402}
2403
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002404static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2405{
2406 switch (cbs->flavor) {
2407 case RPC_AUTH_NULL:
2408 case RPC_AUTH_UNIX:
2409 return nfs_ok;
2410 default:
2411 /*
2412 * GSS case: the spec doesn't allow us to return this
2413 * error. But it also doesn't allow us not to support
2414 * GSS.
2415 * I'd rather this fail hard than return some error the
2416 * client might think it can already handle:
2417 */
2418 return nfserr_encr_alg_unsupp;
2419 }
2420}
2421
Andy Adamson069b6ad2009-04-03 08:27:58 +03002422__be32
2423nfsd4_create_session(struct svc_rqst *rqstp,
2424 struct nfsd4_compound_state *cstate,
2425 struct nfsd4_create_session *cr_ses)
2426{
Jeff Layton363168b2009-08-14 12:57:56 -04002427 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002428 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002429 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002430 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002431 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002432 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002433 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002434 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002435
Mi Jinlonga62573d2011-03-23 17:57:07 +08002436 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2437 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002438 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2439 if (status)
2440 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002441 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002442 if (status)
2443 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002444 status = check_backchannel_attrs(&cr_ses->back_channel);
2445 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002446 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002447 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002448 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002449 if (!new)
2450 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002451 conn = alloc_conn_from_crses(rqstp, cr_ses);
2452 if (!conn)
2453 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002454
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002455 nfs4_lock_state();
Jeff Laytond20c11d2014-07-30 08:27:07 -04002456 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002457 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002458 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002459 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002460
2461 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002462 status = nfserr_wrong_cred;
2463 if (!mach_creds_match(conf, rqstp))
2464 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002465 cs_slot = &conf->cl_cs_slot;
2466 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002467 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002468 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002469 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002470 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002471 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002472 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002473 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002474 } else if (unconf) {
2475 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002476 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002477 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002478 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002479 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002480 status = nfserr_wrong_cred;
2481 if (!mach_creds_match(unconf, rqstp))
2482 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002483 cs_slot = &unconf->cl_cs_slot;
2484 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002485 if (status) {
2486 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002487 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002488 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002489 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002490 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002491 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002492 status = mark_client_expired_locked(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002493 if (status)
2494 goto out_free_conn;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002495 unhash_client_locked(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002496 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002497 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002498 conf = unconf;
2499 } else {
2500 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002501 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002502 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002503 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002504 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002505 * We do not support RDMA or persistent sessions
2506 */
2507 cr_ses->flags &= ~SESSION4_PERSIST;
2508 cr_ses->flags &= ~SESSION4_RDMA;
2509
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002510 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002511 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002512
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002513 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002514 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002515 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002516 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002517
Jeff Laytond20c11d2014-07-30 08:27:07 -04002518 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002519 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002520 spin_unlock(&nn->client_lock);
2521 /* init connection and backchannel */
2522 nfsd4_init_conn(rqstp, conn, new);
2523 nfsd4_put_session(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002524 nfs4_unlock_state();
Jeff Laytond20c11d2014-07-30 08:27:07 -04002525 if (old)
2526 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002527 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002528out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002529 spin_unlock(&nn->client_lock);
Yanchuan Nian266533c2012-12-24 18:11:45 +08002530 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002531 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002532 if (old)
2533 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002534out_free_session:
2535 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002536out_release_drc_mem:
2537 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002538 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002539}
2540
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002541static __be32 nfsd4_map_bcts_dir(u32 *dir)
2542{
2543 switch (*dir) {
2544 case NFS4_CDFC4_FORE:
2545 case NFS4_CDFC4_BACK:
2546 return nfs_ok;
2547 case NFS4_CDFC4_FORE_OR_BOTH:
2548 case NFS4_CDFC4_BACK_OR_BOTH:
2549 *dir = NFS4_CDFC4_BOTH;
2550 return nfs_ok;
2551 };
2552 return nfserr_inval;
2553}
2554
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002555__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2556{
2557 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002558 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002559 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002560
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002561 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2562 if (status)
2563 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002564 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002565 session->se_cb_prog = bc->bc_cb_program;
2566 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002567 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002568
2569 nfsd4_probe_callback(session->se_client);
2570
2571 return nfs_ok;
2572}
2573
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002574__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2575 struct nfsd4_compound_state *cstate,
2576 struct nfsd4_bind_conn_to_session *bcts)
2577{
2578 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002579 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002580 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002581 struct net *net = SVC_NET(rqstp);
2582 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002583
2584 if (!nfsd4_last_compound_op(rqstp))
2585 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002586 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002587 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002588 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002589 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002590 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002591 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002592 status = nfserr_wrong_cred;
2593 if (!mach_creds_match(session->se_client, rqstp))
2594 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002595 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002596 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002597 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002598 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002599 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002600 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002601 goto out;
2602 nfsd4_init_conn(rqstp, conn, session);
2603 status = nfs_ok;
2604out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002605 nfsd4_put_session(session);
2606out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002607 nfs4_unlock_state();
2608 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002609}
2610
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002611static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2612{
2613 if (!session)
2614 return 0;
2615 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2616}
2617
Andy Adamson069b6ad2009-04-03 08:27:58 +03002618__be32
2619nfsd4_destroy_session(struct svc_rqst *r,
2620 struct nfsd4_compound_state *cstate,
2621 struct nfsd4_destroy_session *sessionid)
2622{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002623 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002624 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002625 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002626 struct net *net = SVC_NET(r);
2627 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002628
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002629 nfs4_lock_state();
2630 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002631 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002632 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002633 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002634 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002635 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002636 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002637 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002638 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002639 if (!ses)
2640 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002641 status = nfserr_wrong_cred;
2642 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002643 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002644 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002645 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002646 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002647 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002648 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002649
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002650 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002651
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002652 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002653 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002654out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002655 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002656out_client_lock:
2657 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002658out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002659 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002660 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002661}
2662
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002663static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002664{
2665 struct nfsd4_conn *c;
2666
2667 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002668 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002669 return c;
2670 }
2671 }
2672 return NULL;
2673}
2674
J. Bruce Fields57266a62013-04-13 14:27:29 -04002675static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002676{
2677 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002678 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002679 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002680 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002681
2682 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002683 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002684 if (c)
2685 goto out_free;
2686 status = nfserr_conn_not_bound_to_session;
2687 if (clp->cl_mach_cred)
2688 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002689 __nfsd4_hash_conn(new, ses);
2690 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002691 ret = nfsd4_register_conn(new);
2692 if (ret)
2693 /* oops; xprt is already down: */
2694 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002695 return nfs_ok;
2696out_free:
2697 spin_unlock(&clp->cl_lock);
2698 free_conn(new);
2699 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002700}
2701
Mi Jinlong868b89c2011-04-27 09:09:58 +08002702static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2703{
2704 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2705
2706 return args->opcnt > session->se_fchannel.maxops;
2707}
2708
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002709static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2710 struct nfsd4_session *session)
2711{
2712 struct xdr_buf *xb = &rqstp->rq_arg;
2713
2714 return xb->len > session->se_fchannel.maxreq_sz;
2715}
2716
Andy Adamson069b6ad2009-04-03 08:27:58 +03002717__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002718nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002719 struct nfsd4_compound_state *cstate,
2720 struct nfsd4_sequence *seq)
2721{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002722 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002723 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002724 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002725 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002726 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002727 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002728 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002729 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002730 struct net *net = SVC_NET(rqstp);
2731 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002732
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002733 if (resp->opcnt != 1)
2734 return nfserr_sequence_pos;
2735
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002736 /*
2737 * Will be either used or freed by nfsd4_sequence_check_conn
2738 * below.
2739 */
2740 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2741 if (!conn)
2742 return nfserr_jukebox;
2743
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002744 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002745 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002746 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002747 goto out_no_session;
2748 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002749
Mi Jinlong868b89c2011-04-27 09:09:58 +08002750 status = nfserr_too_many_ops;
2751 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002752 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002753
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002754 status = nfserr_req_too_big;
2755 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002756 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002757
Benny Halevyb85d4c02009-04-03 08:28:08 +03002758 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002759 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002760 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002761
Andy Adamson557ce262009-08-28 08:45:04 -04002762 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002763 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2764
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002765 /* We do not negotiate the number of slots yet, so set the
2766 * maxslots to the session maxreqs which is used to encode
2767 * sr_highest_slotid and the sr_target_slot id to maxslots */
2768 seq->maxslots = session->se_fchannel.maxreqs;
2769
J. Bruce Fields73e79482012-02-13 16:39:00 -05002770 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2771 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002772 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002773 status = nfserr_seq_misordered;
2774 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002775 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002776 cstate->slot = slot;
2777 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002778 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002779 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002780 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002781 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002782 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002783 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002784 }
2785 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002786 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002787
J. Bruce Fields57266a62013-04-13 14:27:29 -04002788 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002789 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002790 if (status)
2791 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002792
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002793 buflen = (seq->cachethis) ?
2794 session->se_fchannel.maxresp_cached :
2795 session->se_fchannel.maxresp_sz;
2796 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2797 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002798 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002799 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002800 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002801
2802 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002803 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002804 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002805 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002806 if (seq->cachethis)
2807 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002808 else
2809 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002810
2811 cstate->slot = slot;
2812 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002813 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002814
Benny Halevyb85d4c02009-04-03 08:28:08 +03002815out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002816 switch (clp->cl_cb_state) {
2817 case NFSD4_CB_DOWN:
2818 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2819 break;
2820 case NFSD4_CB_FAULT:
2821 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2822 break;
2823 default:
2824 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002825 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002826 if (!list_empty(&clp->cl_revoked))
2827 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002828out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002829 if (conn)
2830 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002831 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002832 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002833out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002834 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002835 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002836}
2837
Trond Myklebustb6076642014-06-30 11:48:35 -04002838void
2839nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2840{
2841 struct nfsd4_compound_state *cs = &resp->cstate;
2842
2843 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002844 if (cs->status != nfserr_replay_cache) {
2845 nfsd4_store_cache_entry(resp);
2846 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2847 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002848 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002849 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002850 } else if (cs->clp)
2851 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002852}
2853
Mi Jinlong345c2842011-10-20 17:51:39 +08002854__be32
2855nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2856{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002857 struct nfs4_client *conf, *unconf;
2858 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002859 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002860 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002861
2862 nfs4_lock_state();
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002863 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002864 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002865 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002866 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002867
2868 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002869 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002870 status = nfserr_clientid_busy;
2871 goto out;
2872 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002873 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08002874 } else if (unconf)
2875 clp = unconf;
2876 else {
2877 status = nfserr_stale_clientid;
2878 goto out;
2879 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002880 if (!mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002881 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002882 status = nfserr_wrong_cred;
2883 goto out;
2884 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002885 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002886out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002887 spin_unlock(&nn->client_lock);
Mi Jinlong345c2842011-10-20 17:51:39 +08002888 nfs4_unlock_state();
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002889 if (clp)
2890 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002891 return status;
2892}
2893
Andy Adamson069b6ad2009-04-03 08:27:58 +03002894__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002895nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2896{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002897 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002898
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002899 if (rc->rca_one_fs) {
2900 if (!cstate->current_fh.fh_dentry)
2901 return nfserr_nofilehandle;
2902 /*
2903 * We don't take advantage of the rca_one_fs case.
2904 * That's OK, it's optional, we can safely ignore it.
2905 */
2906 return nfs_ok;
2907 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002908
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002909 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002910 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002911 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2912 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002913 goto out;
2914
2915 status = nfserr_stale_clientid;
2916 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002917 /*
2918 * The following error isn't really legal.
2919 * But we only get here if the client just explicitly
2920 * destroyed the client. Surely it no longer cares what
2921 * error it gets back on an operation for the dead
2922 * client.
2923 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002924 goto out;
2925
2926 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002927 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002928out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002929 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002930 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002931}
2932
2933__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002934nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2935 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002937 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002939 struct nfs4_client *conf, *new;
2940 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002941 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002942 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2943
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002944 new = create_client(clname, rqstp, &clverifier);
2945 if (new == NULL)
2946 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002947 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 nfs4_lock_state();
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002949 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002950 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002951 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002952 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002954 if (clp_used_exchangeid(conf))
2955 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002956 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002957 char addr_str[INET6_ADDRSTRLEN];
2958 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2959 sizeof(addr_str));
2960 dprintk("NFSD: setclientid: string in use by client "
2961 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 goto out;
2963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002965 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002966 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002967 unhash_client_locked(unconf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002968 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002969 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002971 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002972 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002973 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002974 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002975 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2977 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2978 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002979 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 status = nfs_ok;
2981out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002982 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 nfs4_unlock_state();
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002984 if (new)
2985 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002986 if (unconf)
2987 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 return status;
2989}
2990
2991
Al Virob37ad282006-10-19 23:28:59 -07002992__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002993nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2994 struct nfsd4_compound_state *cstate,
2995 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996{
NeilBrown21ab45a2005-06-23 22:04:14 -07002997 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002998 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3000 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003001 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003002 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003004 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07003007
Jeff Laytond20c11d2014-07-30 08:27:07 -04003008 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003009 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003010 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003011 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003012 * We try hard to give out unique clientid's, so if we get an
3013 * attempt to confirm the same clientid with a different cred,
3014 * there's a bug somewhere. Let's charitably assume it's our
3015 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003016 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04003017 status = nfserr_serverfault;
3018 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3019 goto out;
3020 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3021 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003022 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003023 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3024 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003026 else /* case 4: client hasn't noticed we rebooted yet? */
3027 status = nfserr_stale_clientid;
3028 goto out;
3029 }
3030 status = nfs_ok;
3031 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003032 old = unconf;
3033 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003034 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003035 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003036 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3037 if (old) {
3038 status = mark_client_expired_locked(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003039 if (status)
3040 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003041 unhash_client_locked(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003042 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003043 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003044 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003045 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003046 get_client_locked(conf);
3047 spin_unlock(&nn->client_lock);
3048 nfsd4_probe_callback(conf);
3049 spin_lock(&nn->client_lock);
3050 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003052 spin_unlock(&nn->client_lock);
3053 if (old)
3054 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 nfs4_unlock_state();
3056 return status;
3057}
3058
J. Bruce Fields32513b42011-10-13 16:00:16 -04003059static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003061 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3062}
3063
3064/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003065static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003066{
Trond Myklebustca943212014-07-23 16:17:39 -04003067 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
Trond Myklebust950e0112014-06-30 11:48:31 -04003069 lockdep_assert_held(&state_lock);
3070
J. Bruce Fields32513b42011-10-13 16:00:16 -04003071 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003072 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003073 INIT_LIST_HEAD(&fp->fi_stateids);
3074 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003075 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003076 fp->fi_had_conflict = false;
3077 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003078 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003079 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3080 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04003081 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082}
3083
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003084void
NeilBrowne60d4392005-06-23 22:03:01 -07003085nfsd4_free_slabs(void)
3086{
Christoph Hellwigabf11352014-05-21 07:43:03 -07003087 kmem_cache_destroy(openowner_slab);
3088 kmem_cache_destroy(lockowner_slab);
3089 kmem_cache_destroy(file_slab);
3090 kmem_cache_destroy(stateid_slab);
3091 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003092}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Bryan Schumaker72083392011-11-01 15:24:59 -04003094int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095nfsd4_init_slabs(void)
3096{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003097 openowner_slab = kmem_cache_create("nfsd4_openowners",
3098 sizeof(struct nfs4_openowner), 0, 0, NULL);
3099 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003100 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003101 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003102 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003103 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003104 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003105 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003106 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003107 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003108 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003109 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003110 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003111 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003112 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003113 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003114 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003115 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003116 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003118
3119out_free_stateid_slab:
3120 kmem_cache_destroy(stateid_slab);
3121out_free_file_slab:
3122 kmem_cache_destroy(file_slab);
3123out_free_lockowner_slab:
3124 kmem_cache_destroy(lockowner_slab);
3125out_free_openowner_slab:
3126 kmem_cache_destroy(openowner_slab);
3127out:
NeilBrowne60d4392005-06-23 22:03:01 -07003128 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3129 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130}
3131
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003132static void init_nfs4_replay(struct nfs4_replay *rp)
3133{
3134 rp->rp_status = nfserr_serverfault;
3135 rp->rp_buflen = 0;
3136 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003137 mutex_init(&rp->rp_mutex);
3138}
3139
3140static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3141 struct nfs4_stateowner *so)
3142{
3143 if (!nfsd4_has_session(cstate)) {
3144 mutex_lock(&so->so_replay.rp_mutex);
3145 cstate->replay_owner = so;
3146 atomic_inc(&so->so_count);
3147 }
3148}
3149
3150void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3151{
3152 struct nfs4_stateowner *so = cstate->replay_owner;
3153
3154 if (so != NULL) {
3155 cstate->replay_owner = NULL;
3156 mutex_unlock(&so->so_replay.rp_mutex);
3157 nfs4_put_stateowner(so);
3158 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003159}
3160
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003161static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162{
3163 struct nfs4_stateowner *sop;
3164
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003165 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003166 if (!sop)
3167 return NULL;
3168
3169 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3170 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003171 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003172 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003174 sop->so_owner.len = owner->len;
3175
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003176 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003177 sop->so_client = clp;
3178 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003179 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003180 return sop;
3181}
3182
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003183static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003184{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003185 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003186
Trond Myklebustd4f04892014-07-29 21:34:36 -04003187 list_add(&oo->oo_owner.so_strhash,
3188 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003189 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190}
3191
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003192static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3193{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003194 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003195}
3196
Jeff Layton6b180f02014-07-29 21:34:26 -04003197static void nfs4_free_openowner(struct nfs4_stateowner *so)
3198{
3199 struct nfs4_openowner *oo = openowner(so);
3200
3201 kmem_cache_free(openowner_slab, oo);
3202}
3203
3204static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003205 .so_unhash = nfs4_unhash_openowner,
3206 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003207};
3208
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003209static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003210alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003211 struct nfsd4_compound_state *cstate)
3212{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003213 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003214 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003216 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3217 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003219 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003220 oo->oo_owner.so_is_open_owner = 1;
3221 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003222 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003223 if (nfsd4_has_session(cstate))
3224 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003225 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003226 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003227 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003228 spin_lock(&clp->cl_lock);
3229 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003230 if (ret == NULL) {
3231 hash_openowner(oo, clp, strhashval);
3232 ret = oo;
3233 } else
3234 nfs4_free_openowner(&oo->oo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003235 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003236 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237}
3238
J. Bruce Fields996e0932011-10-17 11:14:48 -04003239static 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 -04003240 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003242 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003243 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003244 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003245 stp->st_stateowner = &oo->oo_owner;
Jeff Laytond3134b12014-07-29 21:34:32 -04003246 atomic_inc(&stp->st_stateowner->so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07003247 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003248 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 stp->st_access_bmap = 0;
3250 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003251 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003252 spin_lock(&oo->oo_owner.so_client->cl_lock);
3253 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003254 spin_lock(&fp->fi_lock);
3255 list_add(&stp->st_perfile, &fp->fi_stateids);
3256 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003257 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258}
3259
Jeff Laytond3134b12014-07-29 21:34:32 -04003260/*
3261 * In the 4.0 case we need to keep the owners around a little while to handle
3262 * CLOSE replay. We still do need to release any file access that is held by
3263 * them before returning however.
3264 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003266move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267{
Jeff Laytond3134b12014-07-29 21:34:32 -04003268 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3269 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3270 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003271
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003272 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
Jeff Laytonb401be222014-07-29 21:34:33 -04003274 /*
3275 * We know that we hold one reference via nfsd4_close, and another
3276 * "persistent" reference for the client. If the refcount is higher
3277 * than 2, then there are still calls in progress that are using this
3278 * stateid. We can't put the sc_file reference until they are finished.
3279 * Wait for the refcount to drop to 2. Since it has been unhashed,
3280 * there should be no danger of the refcount going back up again at
3281 * this point.
3282 */
3283 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3284
Jeff Laytond3134b12014-07-29 21:34:32 -04003285 release_all_access(s);
3286 if (s->st_stid.sc_file) {
3287 put_nfs4_file(s->st_stid.sc_file);
3288 s->st_stid.sc_file = NULL;
3289 }
3290 release_last_closed_stateid(oo);
3291 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003292 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003293 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294}
3295
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296/* search file_hashtbl[] for file */
3297static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003298find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299{
Trond Myklebustca943212014-07-23 16:17:39 -04003300 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 struct nfs4_file *fp;
3302
Trond Myklebust950e0112014-06-30 11:48:31 -04003303 lockdep_assert_held(&state_lock);
3304
Jeff Layton89876f82013-04-02 09:01:59 -04003305 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003306 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003307 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 }
3311 return NULL;
3312}
3313
Trond Myklebust950e0112014-06-30 11:48:31 -04003314static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003315find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003316{
3317 struct nfs4_file *fp;
3318
3319 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003320 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003321 spin_unlock(&state_lock);
3322 return fp;
3323}
3324
3325static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003326find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003327{
3328 struct nfs4_file *fp;
3329
3330 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003331 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003332 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003333 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003334 fp = new;
3335 }
3336 spin_unlock(&state_lock);
3337
3338 return fp;
3339}
3340
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003341/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 * Called to check deny when READ with all zero stateid or
3343 * WRITE with all zero or all one stateid
3344 */
Al Virob37ad282006-10-19 23:28:59 -07003345static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3347{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003349 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350
Trond Myklebustca943212014-07-23 16:17:39 -04003351 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003352 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003353 return ret;
3354 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003355 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003356 if (fp->fi_share_deny & deny_type)
3357 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003358 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003359 put_nfs4_file(fp);
3360 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361}
3362
Jeff Layton02e12152014-07-16 10:31:57 -04003363void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364{
Trond Myklebust11b91642014-07-29 21:34:08 -04003365 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3366 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003367
Trond Myklebust11b91642014-07-29 21:34:08 -04003368 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003369
Jeff Layton02e12152014-07-16 10:31:57 -04003370 /*
3371 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003372 * already holding inode->i_lock.
3373 *
Jeff Laytondff13992014-07-08 14:02:49 -04003374 * If the dl_time != 0, then we know that it has already been
3375 * queued for a lease break. Don't queue it again.
3376 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003377 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003378 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003379 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003380 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003381 }
Jeff Layton02e12152014-07-16 10:31:57 -04003382 spin_unlock(&state_lock);
3383}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384
Jeff Layton02e12152014-07-16 10:31:57 -04003385static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3386{
3387 /*
3388 * We're assuming the state code never drops its reference
3389 * without first removing the lease. Since we're in this lease
3390 * callback (and since the lease code is serialized by the kernel
3391 * lock) we know the server hasn't removed the lease yet, we know
3392 * it's safe to take a reference.
3393 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003394 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003395 nfsd4_cb_recall(dp);
3396}
3397
Jeff Layton1c8c6012013-06-21 08:58:15 -04003398/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003399static void nfsd_break_deleg_cb(struct file_lock *fl)
3400{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003401 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3402 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003403
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003404 if (!fp) {
3405 WARN(1, "(%p)->fl_owner NULL\n", fl);
3406 return;
3407 }
3408 if (fp->fi_had_conflict) {
3409 WARN(1, "duplicate break on %p\n", fp);
3410 return;
3411 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003412 /*
3413 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003414 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003415 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003416 */
3417 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
Jeff Layton02e12152014-07-16 10:31:57 -04003419 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003420 fp->fi_had_conflict = true;
3421 /*
3422 * If there are no delegations on the list, then we can't count on this
3423 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3424 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3425 * true should keep any new delegations from being hashed.
3426 */
3427 if (list_empty(&fp->fi_delegations))
3428 fl->fl_break_time = jiffies;
3429 else
3430 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3431 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003432 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433}
3434
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435static
3436int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3437{
3438 if (arg & F_UNLCK)
3439 return lease_modify(onlist, arg);
3440 else
3441 return -EAGAIN;
3442}
3443
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003444static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003445 .lm_break = nfsd_break_deleg_cb,
3446 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447};
3448
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003449static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3450{
3451 if (nfsd4_has_session(cstate))
3452 return nfs_ok;
3453 if (seqid == so->so_seqid - 1)
3454 return nfserr_replay_me;
3455 if (seqid == so->so_seqid)
3456 return nfs_ok;
3457 return nfserr_bad_seqid;
3458}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
Jeff Layton4b24ca72014-06-30 11:48:44 -04003460static __be32 lookup_clientid(clientid_t *clid,
3461 struct nfsd4_compound_state *cstate,
3462 struct nfsd_net *nn)
3463{
3464 struct nfs4_client *found;
3465
3466 if (cstate->clp) {
3467 found = cstate->clp;
3468 if (!same_clid(&found->cl_clientid, clid))
3469 return nfserr_stale_clientid;
3470 return nfs_ok;
3471 }
3472
3473 if (STALE_CLIENTID(clid, nn))
3474 return nfserr_stale_clientid;
3475
3476 /*
3477 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3478 * cached already then we know this is for is for v4.0 and "sessions"
3479 * will be false.
3480 */
3481 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003482 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003483 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003484 if (!found) {
3485 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003486 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003487 }
3488 atomic_inc(&found->cl_refcount);
3489 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003490
3491 /* Cache the nfs4_client in cstate! */
3492 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003493 return nfs_ok;
3494}
3495
Al Virob37ad282006-10-19 23:28:59 -07003496__be32
Andy Adamson66689582009-04-03 08:28:45 +03003497nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003498 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 clientid_t *clientid = &open->op_clientid;
3501 struct nfs4_client *clp = NULL;
3502 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003503 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003504 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003506 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003508 /*
3509 * In case we need it later, after we've already created the
3510 * file and don't want to risk a further failure:
3511 */
3512 open->op_file = nfsd4_alloc_file();
3513 if (open->op_file == NULL)
3514 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
Trond Myklebust2d91e892014-06-30 11:48:46 -04003516 status = lookup_clientid(clientid, cstate, nn);
3517 if (status)
3518 return status;
3519 clp = cstate->clp;
3520
Trond Myklebustd4f04892014-07-29 21:34:36 -04003521 strhashval = ownerstr_hashval(&open->op_owner);
3522 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003523 open->op_openowner = oo;
3524 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003525 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003527 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003528 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003529 release_openowner(oo);
3530 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003531 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003532 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003533 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3534 if (status)
3535 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003536 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003537new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003538 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003539 if (oo == NULL)
3540 return nfserr_jukebox;
3541 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003542alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003543 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003544 if (!open->op_stp)
3545 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003546 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547}
3548
Al Virob37ad282006-10-19 23:28:59 -07003549static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003550nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3551{
3552 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3553 return nfserr_openmode;
3554 else
3555 return nfs_ok;
3556}
3557
Daniel Mackc47d8322011-05-16 16:38:14 +02003558static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003559{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003560 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3561}
3562
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003563static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003564{
3565 struct nfs4_stid *ret;
3566
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003567 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003568 if (!ret)
3569 return NULL;
3570 return delegstateid(ret);
3571}
3572
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003573static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3574{
3575 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3576 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3577}
3578
Al Virob37ad282006-10-19 23:28:59 -07003579static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003580nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003581 struct nfs4_delegation **dp)
3582{
3583 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003584 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003585 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003586
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003587 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3588 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003589 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003590 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003591 status = nfs4_check_delegmode(deleg, flags);
3592 if (status) {
3593 nfs4_put_stid(&deleg->dl_stid);
3594 goto out;
3595 }
3596 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003597out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003598 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003599 return nfs_ok;
3600 if (status)
3601 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003602 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003603 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003604}
3605
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003606static struct nfs4_ol_stateid *
3607nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003609 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003610 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611
Trond Myklebust1d31a252014-07-10 14:07:25 -04003612 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003613 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 /* ignore lock owners */
3615 if (local->st_stateowner->so_is_open_owner == 0)
3616 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003617 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003618 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003619 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003620 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003623 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003624 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625}
3626
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003627static inline int nfs4_access_to_access(u32 nfs4_access)
3628{
3629 int flags = 0;
3630
3631 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3632 flags |= NFSD_MAY_READ;
3633 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3634 flags |= NFSD_MAY_WRITE;
3635 return flags;
3636}
3637
Al Virob37ad282006-10-19 23:28:59 -07003638static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3640 struct nfsd4_open *open)
3641{
3642 struct iattr iattr = {
3643 .ia_valid = ATTR_SIZE,
3644 .ia_size = 0,
3645 };
3646 if (!open->op_truncate)
3647 return 0;
3648 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003649 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3651}
3652
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003653static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003654 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3655 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003656{
Trond Myklebustde186432014-07-10 14:07:26 -04003657 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003658 __be32 status;
3659 int oflag = nfs4_access_to_omode(open->op_share_access);
3660 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003661 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003662
Trond Myklebustde186432014-07-10 14:07:26 -04003663 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003664
3665 /*
3666 * Are we trying to set a deny mode that would conflict with
3667 * current access?
3668 */
3669 status = nfs4_file_check_deny(fp, open->op_share_deny);
3670 if (status != nfs_ok) {
3671 spin_unlock(&fp->fi_lock);
3672 goto out;
3673 }
3674
3675 /* set access to the file */
3676 status = nfs4_file_get_access(fp, open->op_share_access);
3677 if (status != nfs_ok) {
3678 spin_unlock(&fp->fi_lock);
3679 goto out;
3680 }
3681
3682 /* Set access bits in stateid */
3683 old_access_bmap = stp->st_access_bmap;
3684 set_access(open->op_share_access, stp);
3685
3686 /* Set new deny mask */
3687 old_deny_bmap = stp->st_deny_bmap;
3688 set_deny(open->op_share_deny, stp);
3689 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3690
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003691 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003692 spin_unlock(&fp->fi_lock);
3693 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003694 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003695 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003696 spin_lock(&fp->fi_lock);
3697 if (!fp->fi_fds[oflag]) {
3698 fp->fi_fds[oflag] = filp;
3699 filp = NULL;
3700 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003701 }
Trond Myklebustde186432014-07-10 14:07:26 -04003702 spin_unlock(&fp->fi_lock);
3703 if (filp)
3704 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003705
3706 status = nfsd4_truncate(rqstp, cur_fh, open);
3707 if (status)
3708 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003709out:
3710 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003711out_put_access:
3712 stp->st_access_bmap = old_access_bmap;
3713 nfs4_file_put_access(fp, open->op_share_access);
3714 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3715 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003716}
3717
Al Virob37ad282006-10-19 23:28:59 -07003718static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003719nfs4_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 -07003720{
Al Virob37ad282006-10-19 23:28:59 -07003721 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003722 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003724 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003725 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003726
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003727 /* test and set deny mode */
3728 spin_lock(&fp->fi_lock);
3729 status = nfs4_file_check_deny(fp, open->op_share_deny);
3730 if (status == nfs_ok) {
3731 old_deny_bmap = stp->st_deny_bmap;
3732 set_deny(open->op_share_deny, stp);
3733 fp->fi_share_deny |=
3734 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3735 }
3736 spin_unlock(&fp->fi_lock);
3737
3738 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003741 status = nfsd4_truncate(rqstp, cur_fh, open);
3742 if (status != nfs_ok)
3743 reset_union_bmap_deny(old_deny_bmap, stp);
3744 return status;
3745}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003748nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003750 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751}
3752
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003753/* Should we give out recallable state?: */
3754static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3755{
3756 if (clp->cl_cb_state == NFSD4_CB_UP)
3757 return true;
3758 /*
3759 * In the sessions case, since we don't have to establish a
3760 * separate connection for callbacks, we assume it's OK
3761 * until we hear otherwise:
3762 */
3763 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3764}
3765
Jeff Laytond564fbe2014-07-16 10:31:58 -04003766static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003767{
3768 struct file_lock *fl;
3769
3770 fl = locks_alloc_lock();
3771 if (!fl)
3772 return NULL;
3773 locks_init_lock(fl);
3774 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003775 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003776 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3777 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003778 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003779 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003780 return fl;
3781}
3782
J. Bruce Fields99c41512013-05-21 16:21:25 -04003783static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003784{
Trond Myklebust11b91642014-07-29 21:34:08 -04003785 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003786 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003787 struct file *filp;
3788 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003789
Jeff Laytond564fbe2014-07-16 10:31:58 -04003790 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003791 if (!fl)
3792 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003793 filp = find_readable_file(fp);
3794 if (!filp) {
3795 /* We should always have a readable file here */
3796 WARN_ON_ONCE(1);
3797 return -EBADF;
3798 }
3799 fl->fl_file = filp;
3800 status = vfs_setlease(filp, fl->fl_type, &fl);
3801 if (status) {
3802 locks_free_lock(fl);
3803 goto out_fput;
3804 }
Benny Halevycdc97502014-05-30 09:09:30 -04003805 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003806 spin_lock(&fp->fi_lock);
3807 /* Did the lease get broken before we took the lock? */
3808 status = -EAGAIN;
3809 if (fp->fi_had_conflict)
3810 goto out_unlock;
3811 /* Race breaker */
3812 if (fp->fi_lease) {
3813 status = 0;
3814 atomic_inc(&fp->fi_delegees);
3815 hash_delegation_locked(dp, fp);
3816 goto out_unlock;
3817 }
3818 fp->fi_lease = fl;
3819 fp->fi_deleg_file = filp;
3820 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003821 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003822 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003823 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003824 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003825out_unlock:
3826 spin_unlock(&fp->fi_lock);
3827 spin_unlock(&state_lock);
3828out_fput:
3829 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003830 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003831}
3832
Jeff Layton0b266932014-07-25 07:34:25 -04003833static struct nfs4_delegation *
3834nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3835 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003836{
Jeff Layton0b266932014-07-25 07:34:25 -04003837 int status;
3838 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003839
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003840 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003841 return ERR_PTR(-EAGAIN);
3842
3843 dp = alloc_init_deleg(clp, fh);
3844 if (!dp)
3845 return ERR_PTR(-ENOMEM);
3846
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003847 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003848 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003849 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003850 dp->dl_stid.sc_file = fp;
Jeff Layton417c6622014-07-21 09:34:57 -04003851 if (!fp->fi_lease) {
3852 spin_unlock(&fp->fi_lock);
3853 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003854 status = nfs4_setlease(dp);
3855 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003856 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003857 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003858 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003859 status = -EAGAIN;
3860 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003861 }
Benny Halevy931ee562014-05-30 09:09:27 -04003862 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003863 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003864out_unlock:
3865 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003866 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003867out:
3868 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003869 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003870 return ERR_PTR(status);
3871 }
3872 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003873}
3874
Benny Halevy4aa89132012-02-21 14:16:44 -08003875static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3876{
3877 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3878 if (status == -EAGAIN)
3879 open->op_why_no_deleg = WND4_CONTENTION;
3880 else {
3881 open->op_why_no_deleg = WND4_RESOURCE;
3882 switch (open->op_deleg_want) {
3883 case NFS4_SHARE_WANT_READ_DELEG:
3884 case NFS4_SHARE_WANT_WRITE_DELEG:
3885 case NFS4_SHARE_WANT_ANY_DELEG:
3886 break;
3887 case NFS4_SHARE_WANT_CANCEL:
3888 open->op_why_no_deleg = WND4_CANCELLED;
3889 break;
3890 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003891 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003892 }
3893 }
3894}
3895
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896/*
3897 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003898 *
3899 * Note we don't support write delegations, and won't until the vfs has
3900 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 */
3902static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003903nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3904 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905{
3906 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003907 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3908 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003909 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003910 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003912 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003913 open->op_recall = 0;
3914 switch (open->op_claim_type) {
3915 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003916 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003917 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003918 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3919 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003920 break;
3921 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003922 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003923 /*
3924 * Let's not give out any delegations till everyone's
3925 * had the chance to reclaim theirs....
3926 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003927 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003928 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003929 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003930 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003931 /*
3932 * Also, if the file was opened for write or
3933 * create, there's a good chance the client's
3934 * about to write to it, resulting in an
3935 * immediate recall (since we don't support
3936 * write delegations):
3937 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003938 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003939 goto out_no_deleg;
3940 if (open->op_create == NFS4_OPEN_CREATE)
3941 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003942 break;
3943 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003944 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003945 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003946 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003947 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003948 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003950 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003952 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003953 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003954 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003955 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003956 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003957out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003958 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3959 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003960 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003961 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003962 open->op_recall = 1;
3963 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003964
3965 /* 4.1 client asking for a delegation? */
3966 if (open->op_deleg_want)
3967 nfsd4_open_deleg_none_ext(open, status);
3968 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969}
3970
Benny Halevye27f49c2012-02-21 14:16:54 -08003971static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3972 struct nfs4_delegation *dp)
3973{
3974 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3975 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3976 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3977 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3978 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3979 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3980 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3981 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3982 }
3983 /* Otherwise the client must be confused wanting a delegation
3984 * it already has, therefore we don't return
3985 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3986 */
3987}
3988
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989/*
3990 * called with nfs4_lock_state() held.
3991 */
Al Virob37ad282006-10-19 23:28:59 -07003992__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3994{
Andy Adamson66689582009-04-03 08:28:45 +03003995 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003996 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003998 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003999 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004000 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 /*
4003 * Lookup file; if found, lookup stateid and check open request,
4004 * and check for delegations in the process of being recalled.
4005 * If not found, create the nfs4_file struct
4006 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004007 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004008 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004009 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004010 if (status)
4011 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04004012 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004014 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004015 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004016 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004017 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004018 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 }
4020
4021 /*
4022 * OPEN the file, or upgrade an existing OPEN.
4023 * If truncate fails, the OPEN fails.
4024 */
4025 if (stp) {
4026 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004027 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 if (status)
4029 goto out;
4030 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004031 stp = open->op_stp;
4032 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04004033 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004034 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4035 if (status) {
4036 release_open_stateid(stp);
4037 goto out;
4038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004040 update_stateid(&stp->st_stid.sc_stateid);
4041 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
Benny Halevyd24433c2012-02-16 20:57:17 +02004043 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004044 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4045 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4046 open->op_why_no_deleg = WND4_NOT_WANTED;
4047 goto nodeleg;
4048 }
4049 }
4050
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 /*
4052 * Attempt to hand out a delegation. No error return, because the
4053 * OPEN succeeds even if we fail.
4054 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004055 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004056nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 status = nfs_ok;
4058
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004059 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004060 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004062 /* 4.1 client trying to upgrade/downgrade delegation? */
4063 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004064 open->op_deleg_want)
4065 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004066
NeilBrown13cd2182005-06-23 22:03:10 -07004067 if (fp)
4068 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004069 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05004070 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 /*
4072 * To finish the open response, we just need to set the rflags.
4073 */
4074 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004075 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004076 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004078 if (dp)
4079 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004080 if (stp)
4081 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082
4083 return status;
4084}
4085
Jeff Layton58fb12e2014-07-29 21:34:27 -04004086void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4087 struct nfsd4_open *open, __be32 status)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004088{
4089 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004090 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004091
Jeff Laytond3134b12014-07-29 21:34:32 -04004092 nfsd4_cstate_assign_replay(cstate, so);
4093 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004094 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004095 if (open->op_file)
4096 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004097 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004098 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004099}
4100
Al Virob37ad282006-10-19 23:28:59 -07004101__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004102nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4103 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104{
4105 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004106 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004107 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
4109 nfs4_lock_state();
4110 dprintk("process_renew(%08x/%08x): starting\n",
4111 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004112 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004113 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004115 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004117 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004118 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 goto out;
4120 status = nfs_ok;
4121out:
4122 nfs4_unlock_state();
4123 return status;
4124}
4125
NeilBrowna76b4312005-06-23 22:04:01 -07004126static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004127nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004128{
Jeff Layton33dcc482012-04-10 11:08:48 -04004129 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004130 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004131 return;
4132
NeilBrowna76b4312005-06-23 22:04:01 -07004133 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004134 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004135 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004136 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004137 /*
4138 * Now that every NFSv4 client has had the chance to recover and
4139 * to see the (possibly new, possibly shorter) lease time, we
4140 * can safely set the next grace time to the current lease time:
4141 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03004142 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07004143}
4144
NeilBrownfd39ca92005-06-23 22:04:03 -07004145static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004146nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147{
4148 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004149 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 struct nfs4_delegation *dp;
4151 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004152 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004153 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154
4155 nfs4_lock_state();
4156
4157 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004158 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004159 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004160 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004161 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 clp = list_entry(pos, struct nfs4_client, cl_lru);
4163 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4164 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004165 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 break;
4167 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004168 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004169 dprintk("NFSD: client in use (clientid %08x)\n",
4170 clp->cl_clientid.cl_id);
4171 continue;
4172 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004173 unhash_client_locked(clp);
4174 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004175 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004176 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004177 list_for_each_safe(pos, next, &reaplist) {
4178 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 dprintk("NFSD: purging unused client (clientid %08x)\n",
4180 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004181 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 expire_client(clp);
4183 }
Benny Halevycdc97502014-05-30 09:09:30 -04004184 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004185 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004187 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4188 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004190 t = dp->dl_time - cutoff;
4191 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 break;
4193 }
Jeff Layton42690672014-07-25 07:34:20 -04004194 unhash_delegation_locked(dp);
4195 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 }
Benny Halevycdc97502014-05-30 09:09:30 -04004197 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004198 while (!list_empty(&reaplist)) {
4199 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4200 dl_recall_lru);
4201 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004202 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004204 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004205 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
4206 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004207 t = oo->oo_time - cutoff;
4208 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 break;
4210 }
Jeff Laytond3134b12014-07-29 21:34:32 -04004211 release_last_closed_stateid(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04004213 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04004215 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216}
4217
Harvey Harrisona254b242008-02-20 12:49:00 -08004218static struct workqueue_struct *laundry_wq;
4219static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004220
4221static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004222laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223{
4224 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004225 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4226 work);
4227 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4228 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004230 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004232 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233}
4234
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004235static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004236{
Trond Myklebust11b91642014-07-29 21:34:08 -04004237 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004238 return nfserr_bad_stateid;
4239 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240}
4241
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004243access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004245 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4246 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4247 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248}
4249
4250static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004251access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004253 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4254 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255}
4256
4257static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004258__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259{
Al Virob37ad282006-10-19 23:28:59 -07004260 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
J. Bruce Fields02921912010-07-29 15:16:59 -04004262 /* For lock stateid's, we test the parent open, not the lock: */
4263 if (stp->st_openstp)
4264 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004265 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004267 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 goto out;
4269 status = nfs_ok;
4270out:
4271 return status;
4272}
4273
Al Virob37ad282006-10-19 23:28:59 -07004274static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004275check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004277 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004279 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004280 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 * conflicting state; so we must wait out the grace period. */
4282 return nfserr_grace;
4283 } else if (flags & WR_STATE)
4284 return nfs4_share_conflict(current_fh,
4285 NFS4_SHARE_DENY_WRITE);
4286 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4287 return nfs4_share_conflict(current_fh,
4288 NFS4_SHARE_DENY_READ);
4289}
4290
4291/*
4292 * Allow READ/WRITE during grace period on recovered state only for files
4293 * that are not able to provide mandatory locking.
4294 */
4295static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004296grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004298 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299}
4300
J. Bruce Fields81b82962011-08-23 11:03:29 -04004301/* Returns true iff a is later than b: */
4302static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4303{
Jim Rees1a9357f2013-05-17 17:33:00 -04004304 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004305}
4306
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004307static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004308{
Andy Adamson66689582009-04-03 08:28:45 +03004309 /*
4310 * When sessions are used the stateid generation number is ignored
4311 * when it is zero.
4312 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004313 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004314 return nfs_ok;
4315
4316 if (in->si_generation == ref->si_generation)
4317 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004318
J. Bruce Fields0836f582008-01-26 19:08:12 -05004319 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004320 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004321 return nfserr_bad_stateid;
4322 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004323 * However, we could see a stateid from the past, even from a
4324 * non-buggy client. For example, if the client sends a lock
4325 * while some IO is outstanding, the lock may bump si_generation
4326 * while the IO is still in flight. The client could avoid that
4327 * situation by waiting for responses on all the IO requests,
4328 * but better performance may result in retrying IO that
4329 * receives an old_stateid error if requests are rarely
4330 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004331 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004332 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004333}
4334
Chuck Lever7df302f2012-05-29 13:56:37 -04004335static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004336{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004337 struct nfs4_stid *s;
4338 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004339 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004340
Chuck Lever7df302f2012-05-29 13:56:37 -04004341 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004342 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004343 /* Client debugging aid. */
4344 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4345 char addr_str[INET6_ADDRSTRLEN];
4346 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4347 sizeof(addr_str));
4348 pr_warn_ratelimited("NFSD: client %s testing state ID "
4349 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004350 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004351 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004352 spin_lock(&cl->cl_lock);
4353 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004354 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004355 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004356 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004357 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004358 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004359 switch (s->sc_type) {
4360 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004361 status = nfs_ok;
4362 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004363 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004364 status = nfserr_deleg_revoked;
4365 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004366 case NFS4_OPEN_STID:
4367 case NFS4_LOCK_STID:
4368 ols = openlockstateid(s);
4369 if (ols->st_stateowner->so_is_open_owner
4370 && !(openowner(ols->st_stateowner)->oo_flags
4371 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004372 status = nfserr_bad_stateid;
4373 else
4374 status = nfs_ok;
4375 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004376 default:
4377 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004378 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004379 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004380 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004381 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004382 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004383out_unlock:
4384 spin_unlock(&cl->cl_lock);
4385 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004386}
4387
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004388static __be32
4389nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4390 stateid_t *stateid, unsigned char typemask,
4391 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004392{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004393 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004394
4395 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4396 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004397 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004398 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004399 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004400 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004401 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004402 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004403 if (status)
4404 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004405 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004406 if (!*s)
4407 return nfserr_bad_stateid;
4408 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004409}
4410
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411/*
4412* Checks for stateid operations
4413*/
Al Virob37ad282006-10-19 23:28:59 -07004414__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004415nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004416 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004418 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004419 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004421 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004423 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004424 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004425 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 if (filpp)
4428 *filpp = NULL;
4429
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004430 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 return nfserr_grace;
4432
4433 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004434 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435
Trond Myklebust14bcab12014-04-18 14:44:07 -04004436 nfs4_lock_state();
4437
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004438 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004439 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004440 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004441 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004442 goto unlock_state;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004443 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4444 if (status)
4445 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004446 switch (s->sc_type) {
4447 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004448 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004449 status = nfs4_check_delegmode(dp, flags);
4450 if (status)
4451 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004452 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004453 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004454 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004455 WARN_ON_ONCE(1);
4456 status = nfserr_serverfault;
4457 goto out;
4458 }
Trond Myklebustde186432014-07-10 14:07:26 -04004459 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004460 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004461 break;
4462 case NFS4_OPEN_STID:
4463 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004464 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004465 status = nfs4_check_fh(current_fh, stp);
4466 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004468 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004469 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004471 status = nfs4_check_openmode(stp, flags);
4472 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004474 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004475 struct nfs4_file *fp = stp->st_stid.sc_file;
4476
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004477 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004478 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004479 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004480 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004481 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004482 break;
4483 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004484 status = nfserr_bad_stateid;
4485 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 }
4487 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004488 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004489 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004491 nfs4_put_stid(s);
4492unlock_state:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004493 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 return status;
4495}
4496
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004497/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004498 * Test if the stateid is valid
4499 */
4500__be32
4501nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4502 struct nfsd4_test_stateid *test_stateid)
4503{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004504 struct nfsd4_test_stateid_id *stateid;
4505 struct nfs4_client *cl = cstate->session->se_client;
4506
4507 nfs4_lock_state();
4508 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004509 stateid->ts_id_status =
4510 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004511 nfs4_unlock_state();
4512
Bryan Schumaker17456802011-07-13 10:50:48 -04004513 return nfs_ok;
4514}
4515
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004516__be32
4517nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4518 struct nfsd4_free_stateid *free_stateid)
4519{
4520 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004521 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004522 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004523 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004524 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004525 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004526
4527 nfs4_lock_state();
Jeff Layton1af71cc2014-07-29 21:34:14 -04004528 spin_lock(&cl->cl_lock);
4529 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004530 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004531 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004532 switch (s->sc_type) {
4533 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004534 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004535 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004536 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004537 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4538 if (ret)
4539 break;
4540 ret = nfserr_locks_held;
4541 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004542 case NFS4_LOCK_STID:
4543 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4544 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004545 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004546 stp = openlockstateid(s);
4547 ret = nfserr_locks_held;
4548 if (check_for_locks(stp->st_stid.sc_file,
4549 lockowner(stp->st_stateowner)))
4550 break;
4551 unhash_lock_stateid(stp);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004552 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004553 nfs4_put_stid(s);
4554 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004555 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004556 case NFS4_REVOKED_DELEG_STID:
4557 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004558 list_del_init(&dp->dl_recall_lru);
4559 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004560 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004561 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004562 goto out;
4563 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004564 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004565out_unlock:
4566 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004567out:
4568 nfs4_unlock_state();
4569 return ret;
4570}
4571
NeilBrown4c4cd222005-07-07 17:59:27 -07004572static inline int
4573setlkflg (int type)
4574{
4575 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4576 RD_STATE : WR_STATE;
4577}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004579static __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 -04004580{
4581 struct svc_fh *current_fh = &cstate->current_fh;
4582 struct nfs4_stateowner *sop = stp->st_stateowner;
4583 __be32 status;
4584
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004585 status = nfsd4_check_seqid(cstate, sop, seqid);
4586 if (status)
4587 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004588 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4589 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004590 /*
4591 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004592 * nfserr_replay_me from the previous step, and
4593 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004594 */
4595 return nfserr_bad_stateid;
4596 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4597 if (status)
4598 return status;
4599 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004600}
4601
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602/*
4603 * Checks for sequence id mutating operations.
4604 */
Al Virob37ad282006-10-19 23:28:59 -07004605static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004606nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004607 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004608 struct nfs4_ol_stateid **stpp,
4609 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004611 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004612 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004613 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004615 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4616 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004617
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004619 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004620 if (status)
4621 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004622 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004623 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004625 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004626 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004627 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004628 else
4629 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004630 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004631}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004632
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004633static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4634 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004635{
4636 __be32 status;
4637 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004638 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004640 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004641 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004642 if (status)
4643 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004644 oo = openowner(stp->st_stateowner);
4645 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4646 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004647 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004648 }
4649 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004650 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651}
4652
Al Virob37ad282006-10-19 23:28:59 -07004653__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004654nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004655 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656{
Al Virob37ad282006-10-19 23:28:59 -07004657 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004658 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004659 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004660 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
Al Viroa6a9f182013-09-16 10:57:01 -04004662 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4663 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004665 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004666 if (status)
4667 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
4669 nfs4_lock_state();
4670
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004671 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004672 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004673 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004674 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004675 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004676 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004677 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004678 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004679 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004680 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004681 update_stateid(&stp->st_stid.sc_stateid);
4682 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004683 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004684 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004685
Jeff Layton2a4317c2012-03-21 16:42:43 -04004686 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004687 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004688put_stateid:
4689 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004691 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004692 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 return status;
4694}
4695
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004696static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004698 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004699 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004700 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004701 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004702}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004703
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004704static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4705{
4706 switch (to_access) {
4707 case NFS4_SHARE_ACCESS_READ:
4708 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4709 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4710 break;
4711 case NFS4_SHARE_ACCESS_WRITE:
4712 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4713 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4714 break;
4715 case NFS4_SHARE_ACCESS_BOTH:
4716 break;
4717 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004718 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 }
4720}
4721
Al Virob37ad282006-10-19 23:28:59 -07004722__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004723nfsd4_open_downgrade(struct svc_rqst *rqstp,
4724 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004725 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726{
Al Virob37ad282006-10-19 23:28:59 -07004727 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004728 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004729 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730
Al Viroa6a9f182013-09-16 10:57:01 -04004731 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4732 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004734 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004735 if (od->od_deleg_want)
4736 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4737 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738
4739 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004740 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004741 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004742 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004745 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004746 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004748 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004750 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004751 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004753 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004755 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756
Jeff Laytonce0fc432012-05-11 09:45:14 -04004757 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004759 update_stateid(&stp->st_stid.sc_stateid);
4760 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004762put_stateid:
4763 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004765 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004766 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 return status;
4768}
4769
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004770static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4771{
Trond Myklebustacf92952014-06-30 11:48:37 -04004772 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytond83017f2014-07-29 21:34:42 -04004773 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004774
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004775 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04004776 spin_lock(&clp->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04004777 unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004778
Jeff Laytond83017f2014-07-29 21:34:42 -04004779 if (clp->cl_minorversion) {
4780 put_ol_stateid_locked(s, &reaplist);
4781 spin_unlock(&clp->cl_lock);
4782 free_ol_stateid_reaplist(&reaplist);
4783 } else {
4784 spin_unlock(&clp->cl_lock);
4785 free_ol_stateid_reaplist(&reaplist);
Jeff Laytond3134b12014-07-29 21:34:32 -04004786 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04004787 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004788}
4789
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790/*
4791 * nfs4_unlock_state() called after encode
4792 */
Al Virob37ad282006-10-19 23:28:59 -07004793__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004794nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004795 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796{
Al Virob37ad282006-10-19 23:28:59 -07004797 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004798 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004799 struct net *net = SVC_NET(rqstp);
4800 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801
Al Viroa6a9f182013-09-16 10:57:01 -04004802 dprintk("NFSD: nfsd4_close on file %pd\n",
4803 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
4805 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004806 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4807 &close->cl_stateid,
4808 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004809 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004810 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004811 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004813 update_stateid(&stp->st_stid.sc_stateid);
4814 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004816 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004817
4818 /* put reference from nfs4_preprocess_seqid_op */
4819 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820out:
Jeff Layton58fb12e2014-07-29 21:34:27 -04004821 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 return status;
4823}
4824
Al Virob37ad282006-10-19 23:28:59 -07004825__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004826nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4827 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004829 struct nfs4_delegation *dp;
4830 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004831 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004832 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004833 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004835 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004836 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837
4838 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004839 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004840 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004841 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004842 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004843 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004844 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004845 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004846
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004847 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004848put_stateid:
4849 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004851 nfs4_unlock_state();
4852
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 return status;
4854}
4855
4856
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858
Benny Halevy87df4de2008-12-15 19:42:03 +02004859static inline u64
4860end_offset(u64 start, u64 len)
4861{
4862 u64 end;
4863
4864 end = start + len;
4865 return end >= start ? end: NFS4_MAX_UINT64;
4866}
4867
4868/* last octet in a range */
4869static inline u64
4870last_byte_offset(u64 start, u64 len)
4871{
4872 u64 end;
4873
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004874 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004875 end = start + len;
4876 return end > start ? end - 1: NFS4_MAX_UINT64;
4877}
4878
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879/*
4880 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4881 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4882 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4883 * locking, this prevents us from being completely protocol-compliant. The
4884 * real solution to this problem is to start using unsigned file offsets in
4885 * the VFS, but this is a very deep change!
4886 */
4887static inline void
4888nfs4_transform_lock_offset(struct file_lock *lock)
4889{
4890 if (lock->fl_start < 0)
4891 lock->fl_start = OFFSET_MAX;
4892 if (lock->fl_end < 0)
4893 lock->fl_end = OFFSET_MAX;
4894}
4895
NeilBrownd5b90262006-04-10 22:55:22 -07004896/* Hack!: For now, we're defining this just so we can use a pointer to it
4897 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004898static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004899};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900
4901static inline void
4902nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4903{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004904 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905
NeilBrownd5b90262006-04-10 22:55:22 -07004906 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004907 lo = (struct nfs4_lockowner *) fl->fl_owner;
4908 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4909 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004910 if (!deny->ld_owner.data)
4911 /* We just don't care that much */
4912 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004913 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4914 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004915 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004916nevermind:
4917 deny->ld_owner.len = 0;
4918 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004919 deny->ld_clientid.cl_boot = 0;
4920 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 }
4922 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004923 deny->ld_length = NFS4_MAX_UINT64;
4924 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4926 deny->ld_type = NFS4_READ_LT;
4927 if (fl->fl_type != F_RDLCK)
4928 deny->ld_type = NFS4_WRITE_LT;
4929}
4930
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004931static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004932find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004933 struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004935 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004936 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937
Trond Myklebust0a880a22014-07-30 08:27:10 -04004938 lockdep_assert_held(&clp->cl_lock);
4939
Trond Myklebustd4f04892014-07-29 21:34:36 -04004940 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4941 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004942 if (so->so_is_open_owner)
4943 continue;
Trond Myklebustd4f04892014-07-29 21:34:36 -04004944 if (!same_owner_str(so, owner))
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004945 continue;
Jeff Layton5db1c032014-07-29 21:34:28 -04004946 atomic_inc(&so->so_count);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004947 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 }
4949 return NULL;
4950}
4951
Trond Myklebustc58c6612014-07-29 21:34:35 -04004952static struct nfs4_lockowner *
4953find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004954 struct nfs4_client *clp)
Trond Myklebustc58c6612014-07-29 21:34:35 -04004955{
4956 struct nfs4_lockowner *lo;
4957
Trond Myklebustd4f04892014-07-29 21:34:36 -04004958 spin_lock(&clp->cl_lock);
4959 lo = find_lockowner_str_locked(clid, owner, clp);
4960 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004961 return lo;
4962}
4963
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004964static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4965{
Trond Myklebustc58c6612014-07-29 21:34:35 -04004966 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004967}
4968
Jeff Layton6b180f02014-07-29 21:34:26 -04004969static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4970{
4971 struct nfs4_lockowner *lo = lockowner(sop);
4972
4973 kmem_cache_free(lockowner_slab, lo);
4974}
4975
4976static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004977 .so_unhash = nfs4_unhash_lockowner,
4978 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004979};
4980
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981/*
4982 * Alloc a lock owner structure.
4983 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004984 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004986 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004988static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004989alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
4990 struct nfs4_ol_stateid *open_stp,
4991 struct nfsd4_lock *lock)
4992{
Trond Myklebustc58c6612014-07-29 21:34:35 -04004993 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004995 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4996 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004998 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4999 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005000 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005001 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005002 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005003 ret = find_lockowner_str_locked(&clp->cl_clientid,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005004 &lock->lk_new_owner, clp);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005005 if (ret == NULL) {
5006 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005007 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005008 ret = lo;
5009 } else
5010 nfs4_free_lockowner(&lo->lo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005011 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005012 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013}
5014
Jeff Layton356a95e2014-07-29 21:34:13 -04005015static void
5016init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5017 struct nfs4_file *fp, struct inode *inode,
5018 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005020 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021
Jeff Layton356a95e2014-07-29 21:34:13 -04005022 lockdep_assert_held(&clp->cl_lock);
5023
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005024 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005025 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005026 stp->st_stateowner = &lo->lo_owner;
Trond Myklebuste4f1dd72014-07-29 21:34:30 -04005027 atomic_inc(&lo->lo_owner.so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07005028 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005029 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04005030 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005031 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005033 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005034 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005035 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005036 spin_lock(&fp->fi_lock);
5037 list_add(&stp->st_perfile, &fp->fi_stateids);
5038 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039}
5040
Jeff Laytonc53530d2014-06-30 11:48:39 -04005041static struct nfs4_ol_stateid *
5042find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5043{
5044 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005045 struct nfs4_client *clp = lo->lo_owner.so_client;
5046
5047 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005048
5049 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005050 if (lst->st_stid.sc_file == fp) {
5051 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005052 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005053 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005054 }
5055 return NULL;
5056}
5057
Jeff Layton356a95e2014-07-29 21:34:13 -04005058static struct nfs4_ol_stateid *
5059find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5060 struct inode *inode, struct nfs4_ol_stateid *ost,
5061 bool *new)
5062{
5063 struct nfs4_stid *ns = NULL;
5064 struct nfs4_ol_stateid *lst;
5065 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5066 struct nfs4_client *clp = oo->oo_owner.so_client;
5067
5068 spin_lock(&clp->cl_lock);
5069 lst = find_lock_stateid(lo, fi);
5070 if (lst == NULL) {
5071 spin_unlock(&clp->cl_lock);
5072 ns = nfs4_alloc_stid(clp, stateid_slab);
5073 if (ns == NULL)
5074 return NULL;
5075
5076 spin_lock(&clp->cl_lock);
5077 lst = find_lock_stateid(lo, fi);
5078 if (likely(!lst)) {
5079 lst = openlockstateid(ns);
5080 init_lock_stateid(lst, lo, fi, inode, ost);
5081 ns = NULL;
5082 *new = true;
5083 }
5084 }
5085 spin_unlock(&clp->cl_lock);
5086 if (ns)
5087 nfs4_put_stid(ns);
5088 return lst;
5089}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005090
NeilBrownfd39ca92005-06-23 22:04:03 -07005091static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092check_lock_length(u64 offset, u64 length)
5093{
Benny Halevy87df4de2008-12-15 19:42:03 +02005094 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 LOFF_OVERFLOW(offset, length)));
5096}
5097
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005098static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005099{
Trond Myklebust11b91642014-07-29 21:34:08 -04005100 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005101
Jeff Layton7214e862014-07-10 14:07:33 -04005102 lockdep_assert_held(&fp->fi_lock);
5103
Jeff Layton82c5ff12012-05-11 09:45:13 -04005104 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005105 return;
Jeff Layton12659652014-07-10 14:07:28 -04005106 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005107 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005108}
5109
Jeff Layton356a95e2014-07-29 21:34:13 -04005110static __be32
5111lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5112 struct nfs4_ol_stateid *ost,
5113 struct nfsd4_lock *lock,
5114 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005115{
Jeff Layton5db1c032014-07-29 21:34:28 -04005116 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005117 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005118 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5119 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005120 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005121 struct nfs4_lockowner *lo;
5122 unsigned int strhashval;
5123
Trond Myklebustd4f04892014-07-29 21:34:36 -04005124 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005125 if (!lo) {
Trond Myklebustd4f04892014-07-29 21:34:36 -04005126 strhashval = ownerstr_hashval(&lock->v.new.owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005127 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5128 if (lo == NULL)
5129 return nfserr_jukebox;
5130 } else {
5131 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005132 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005133 if (!cstate->minorversion &&
5134 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005135 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005136 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005137
Jeff Layton356a95e2014-07-29 21:34:13 -04005138 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005139 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005140 status = nfserr_jukebox;
5141 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005142 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005143 status = nfs_ok;
5144out:
5145 nfs4_put_stateowner(&lo->lo_owner);
5146 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005147}
5148
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149/*
5150 * LOCK operation
5151 */
Al Virob37ad282006-10-19 23:28:59 -07005152__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005153nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005154 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005156 struct nfs4_openowner *open_sop = NULL;
5157 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005158 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005159 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005160 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005161 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005162 struct file_lock *file_lock = NULL;
5163 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005164 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005165 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005166 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005167 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005168 struct net *net = SVC_NET(rqstp);
5169 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170
5171 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5172 (long long) lock->lk_offset,
5173 (long long) lock->lk_length);
5174
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 if (check_lock_length(lock->lk_offset, lock->lk_length))
5176 return nfserr_inval;
5177
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005178 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005179 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005180 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5181 return status;
5182 }
5183
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 nfs4_lock_state();
5185
5186 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005187 if (nfsd4_has_session(cstate))
5188 /* See rfc 5661 18.10.3: given clientid is ignored: */
5189 memcpy(&lock->v.new.clientid,
5190 &cstate->session->se_client->cl_clientid,
5191 sizeof(clientid_t));
5192
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005194 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005198 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 lock->lk_new_open_seqid,
5200 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005201 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005202 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005204 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005205 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005206 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005207 &lock->v.new.clientid))
5208 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005209 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005210 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005211 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005212 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005213 lock->lk_old_lock_seqid,
5214 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005215 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005216 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005217 if (status)
5218 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005219 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005221 lkflg = setlkflg(lock->lk_type);
5222 status = nfs4_check_openmode(lock_stp, lkflg);
5223 if (status)
5224 goto out;
5225
NeilBrown0dd395d2005-07-07 17:59:15 -07005226 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005227 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005228 goto out;
5229 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005230 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005231 goto out;
5232
Jeff Layton21179d82012-08-21 08:03:32 -04005233 file_lock = locks_alloc_lock();
5234 if (!file_lock) {
5235 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5236 status = nfserr_jukebox;
5237 goto out;
5238 }
5239
Trond Myklebust11b91642014-07-29 21:34:08 -04005240 fp = lock_stp->st_stid.sc_file;
Jeff Layton21179d82012-08-21 08:03:32 -04005241 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 switch (lock->lk_type) {
5243 case NFS4_READ_LT:
5244 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005245 spin_lock(&fp->fi_lock);
5246 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005247 if (filp)
5248 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005249 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005250 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005251 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 case NFS4_WRITE_LT:
5253 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005254 spin_lock(&fp->fi_lock);
5255 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005256 if (filp)
5257 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005258 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005259 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005260 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 default:
5262 status = nfserr_inval;
5263 goto out;
5264 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005265 if (!filp) {
5266 status = nfserr_openmode;
5267 goto out;
5268 }
Jeff Layton21179d82012-08-21 08:03:32 -04005269 file_lock->fl_owner = (fl_owner_t)lock_sop;
5270 file_lock->fl_pid = current->tgid;
5271 file_lock->fl_file = filp;
5272 file_lock->fl_flags = FL_POSIX;
5273 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5274 file_lock->fl_start = lock->lk_offset;
5275 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5276 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277
Jeff Layton21179d82012-08-21 08:03:32 -04005278 conflock = locks_alloc_lock();
5279 if (!conflock) {
5280 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5281 status = nfserr_jukebox;
5282 goto out;
5283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284
Jeff Layton21179d82012-08-21 08:03:32 -04005285 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005286 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005288 update_stateid(&lock_stp->st_stid.sc_stateid);
5289 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005291 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005292 break;
5293 case (EAGAIN): /* conflock holds conflicting lock */
5294 status = nfserr_denied;
5295 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005296 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005297 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 case (EDEADLK):
5299 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005300 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005301 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005302 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005303 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005304 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306out:
Trond Myklebustde186432014-07-10 14:07:26 -04005307 if (filp)
5308 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005309 if (lock_stp) {
5310 /* Bump seqid manually if the 4.0 replay owner is openowner */
5311 if (cstate->replay_owner &&
5312 cstate->replay_owner != &lock_sop->lo_owner &&
5313 seqid_mutating_err(ntohl(status)))
5314 lock_sop->lo_owner.so_seqid++;
5315
5316 /*
5317 * If this is a new, never-before-used stateid, and we are
5318 * returning an error, then just go ahead and release it.
5319 */
5320 if (status && new)
5321 release_lock_stateid(lock_stp);
5322
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005323 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005324 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005325 if (open_stp)
5326 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005327 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005328 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005329 if (file_lock)
5330 locks_free_lock(file_lock);
5331 if (conflock)
5332 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 return status;
5334}
5335
5336/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005337 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5338 * so we do a temporary open here just to get an open file to pass to
5339 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5340 * inode operation.)
5341 */
Al Viro04da6e92012-04-13 00:00:04 -04005342static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005343{
5344 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005345 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5346 if (!err) {
5347 err = nfserrno(vfs_test_lock(file, lock));
5348 nfsd_close(file);
5349 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005350 return err;
5351}
5352
5353/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 * LOCKT operation
5355 */
Al Virob37ad282006-10-19 23:28:59 -07005356__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005357nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5358 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359{
Jeff Layton21179d82012-08-21 08:03:32 -04005360 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005361 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005362 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005363 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005365 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 return nfserr_grace;
5367
5368 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5369 return nfserr_inval;
5370
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 nfs4_lock_state();
5372
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005373 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005374 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005375 if (status)
5376 goto out;
5377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005379 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381
Jeff Layton21179d82012-08-21 08:03:32 -04005382 file_lock = locks_alloc_lock();
5383 if (!file_lock) {
5384 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5385 status = nfserr_jukebox;
5386 goto out;
5387 }
5388 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 switch (lockt->lt_type) {
5390 case NFS4_READ_LT:
5391 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005392 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 break;
5394 case NFS4_WRITE_LT:
5395 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005396 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 break;
5398 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005399 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 status = nfserr_inval;
5401 goto out;
5402 }
5403
Trond Myklebustd4f04892014-07-29 21:34:36 -04005404 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5405 cstate->clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005406 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005407 file_lock->fl_owner = (fl_owner_t)lo;
5408 file_lock->fl_pid = current->tgid;
5409 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410
Jeff Layton21179d82012-08-21 08:03:32 -04005411 file_lock->fl_start = lockt->lt_offset;
5412 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413
Jeff Layton21179d82012-08-21 08:03:32 -04005414 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415
Jeff Layton21179d82012-08-21 08:03:32 -04005416 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005417 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005418 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005419
Jeff Layton21179d82012-08-21 08:03:32 -04005420 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005422 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 }
5424out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005425 if (lo)
5426 nfs4_put_stateowner(&lo->lo_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005428 if (file_lock)
5429 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 return status;
5431}
5432
Al Virob37ad282006-10-19 23:28:59 -07005433__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005434nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005435 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005437 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005439 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005440 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005441 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005442 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5443
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5445 (long long) locku->lu_offset,
5446 (long long) locku->lu_length);
5447
5448 if (check_lock_length(locku->lu_offset, locku->lu_length))
5449 return nfserr_inval;
5450
5451 nfs4_lock_state();
5452
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005453 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005454 &locku->lu_stateid, NFS4_LOCK_STID,
5455 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005456 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005458 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005459 if (!filp) {
5460 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005461 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005462 }
Jeff Layton21179d82012-08-21 08:03:32 -04005463 file_lock = locks_alloc_lock();
5464 if (!file_lock) {
5465 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5466 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005467 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005468 }
5469 locks_init_lock(file_lock);
5470 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005471 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005472 file_lock->fl_pid = current->tgid;
5473 file_lock->fl_file = filp;
5474 file_lock->fl_flags = FL_POSIX;
5475 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5476 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477
Jeff Layton21179d82012-08-21 08:03:32 -04005478 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5479 locku->lu_length);
5480 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481
Jeff Layton21179d82012-08-21 08:03:32 -04005482 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005483 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005484 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 goto out_nfserr;
5486 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005487 update_stateid(&stp->st_stid.sc_stateid);
5488 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005489fput:
5490 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005491put_stateid:
5492 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005494 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005495 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005496 if (file_lock)
5497 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 return status;
5499
5500out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005501 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005502 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503}
5504
5505/*
5506 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005507 * true: locks held by lockowner
5508 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005510static bool
5511check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512{
5513 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005514 int status = false;
5515 struct file *filp = find_any_file(fp);
5516 struct inode *inode;
5517
5518 if (!filp) {
5519 /* Any valid lock stateid should have some sort of access */
5520 WARN_ON_ONCE(1);
5521 return status;
5522 }
5523
5524 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525
Jeff Layton1c8c6012013-06-21 08:58:15 -04005526 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005528 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005529 status = true;
5530 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005533 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005534 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 return status;
5536}
5537
Al Virob37ad282006-10-19 23:28:59 -07005538__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005539nfsd4_release_lockowner(struct svc_rqst *rqstp,
5540 struct nfsd4_compound_state *cstate,
5541 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542{
5543 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005544 struct nfs4_stateowner *sop;
5545 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005546 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005548 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005549 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005550 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005551 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552
5553 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5554 clid->cl_boot, clid->cl_id);
5555
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 nfs4_lock_state();
5557
Jeff Layton4b24ca72014-06-30 11:48:44 -04005558 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005559 if (status)
5560 goto out;
5561
Trond Myklebustd4f04892014-07-29 21:34:36 -04005562 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005563 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005564 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005565 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005566 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005567
5568 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005569 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005570
Jeff Layton882e9d22014-07-29 21:34:37 -04005571 /* see if there are still any locks associated with it */
5572 lo = lockowner(sop);
5573 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5574 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5575 status = nfserr_locks_held;
5576 spin_unlock(&clp->cl_lock);
5577 goto out;
5578 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005579 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005580
5581 atomic_inc(&sop->so_count);
5582 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005583 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005584 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005585 if (lo)
5586 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587out:
5588 nfs4_unlock_state();
5589 return status;
5590}
5591
5592static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005593alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594{
NeilBrowna55370a2005-06-23 22:03:52 -07005595 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596}
5597
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005598bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005599nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005600{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005601 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005602
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005603 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005604 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005605}
5606
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607/*
5608 * failure => all reset bets are off, nfserr_no_grace...
5609 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005610struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005611nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612{
5613 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005614 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615
NeilBrowna55370a2005-06-23 22:03:52 -07005616 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5617 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005618 if (crp) {
5619 strhashval = clientstr_hashval(name);
5620 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005621 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005622 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005623 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005624 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005625 }
5626 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627}
5628
Jeff Layton2a4317c2012-03-21 16:42:43 -04005629void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005630nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005631{
5632 list_del(&crp->cr_strhash);
5633 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005634 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005635}
5636
5637void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005638nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639{
5640 struct nfs4_client_reclaim *crp = NULL;
5641 int i;
5642
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005644 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5645 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005647 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 }
5649 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005650 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651}
5652
5653/*
5654 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005655struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005656nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657{
5658 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 struct nfs4_client_reclaim *crp = NULL;
5660
Jeff Layton278c9312012-11-12 15:00:52 -05005661 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662
Jeff Layton278c9312012-11-12 15:00:52 -05005663 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005664 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005665 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 return crp;
5667 }
5668 }
5669 return NULL;
5670}
5671
5672/*
5673* Called from OPEN. Look for clientid in reclaim list.
5674*/
Al Virob37ad282006-10-19 23:28:59 -07005675__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005676nfs4_check_open_reclaim(clientid_t *clid,
5677 struct nfsd4_compound_state *cstate,
5678 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005680 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005681
5682 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005683 status = lookup_clientid(clid, cstate, nn);
5684 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005685 return nfserr_reclaim_bad;
5686
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005687 if (nfsd4_client_record_check(cstate->clp))
5688 return nfserr_reclaim_bad;
5689
5690 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691}
5692
Bryan Schumaker65178db2011-11-01 13:35:21 -04005693#ifdef CONFIG_NFSD_FAULT_INJECTION
5694
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005695u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5696{
Jeff Laytond20c11d2014-07-30 08:27:07 -04005697 __be32 ret;
5698 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
5699
5700 spin_lock(&nn->client_lock);
5701 ret = mark_client_expired_locked(clp);
5702 spin_unlock(&nn->client_lock);
5703 if (ret != nfs_ok)
J. Bruce Fields221a6872013-04-01 22:23:49 -04005704 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005705 expire_client(clp);
5706 return 1;
5707}
5708
Bryan Schumaker184c1842012-11-29 11:40:44 -05005709u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5710{
5711 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005712 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005713 printk(KERN_INFO "NFS Client: %s\n", buf);
5714 return 1;
5715}
5716
5717static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5718 const char *type)
5719{
5720 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005721 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005722 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5723}
5724
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005725static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5726 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005727{
5728 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005729 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005730 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005731 u64 count = 0;
5732
5733 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005734 list_for_each_entry_safe(stp, st_next,
5735 &oop->oo_owner.so_stateids, st_perstateowner) {
5736 list_for_each_entry_safe(lst, lst_next,
5737 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005738 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005739 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005740 if (++count == max)
5741 return count;
5742 }
5743 }
5744 }
5745
5746 return count;
5747}
5748
5749u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5750{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005751 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005752}
5753
Bryan Schumaker184c1842012-11-29 11:40:44 -05005754u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5755{
5756 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5757 nfsd_print_count(clp, count, "locked files");
5758 return count;
5759}
5760
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005761static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5762{
5763 struct nfs4_openowner *oop, *next;
5764 u64 count = 0;
5765
5766 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5767 if (func)
5768 func(oop);
5769 if (++count == max)
5770 break;
5771 }
5772
5773 return count;
5774}
5775
5776u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5777{
5778 return nfsd_foreach_client_open(clp, max, release_openowner);
5779}
5780
Bryan Schumaker184c1842012-11-29 11:40:44 -05005781u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5782{
5783 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5784 nfsd_print_count(clp, count, "open files");
5785 return count;
5786}
5787
Bryan Schumaker269de302012-11-29 11:40:42 -05005788static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5789 struct list_head *victims)
5790{
5791 struct nfs4_delegation *dp, *next;
5792 u64 count = 0;
5793
Benny Halevycdc97502014-05-30 09:09:30 -04005794 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005795 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005796 if (victims) {
5797 /*
5798 * It's not safe to mess with delegations that have a
5799 * non-zero dl_time. They might have already been broken
5800 * and could be processed by the laundromat outside of
5801 * the state_lock. Just leave them be.
5802 */
5803 if (dp->dl_time != 0)
5804 continue;
5805
Jeff Layton42690672014-07-25 07:34:20 -04005806 unhash_delegation_locked(dp);
5807 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005808 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005809 if (++count == max)
5810 break;
5811 }
5812 return count;
5813}
5814
5815u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5816{
5817 struct nfs4_delegation *dp, *next;
5818 LIST_HEAD(victims);
5819 u64 count;
5820
Benny Halevycdc97502014-05-30 09:09:30 -04005821 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005822 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005823 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005824
Jeff Layton2d4a5322014-07-25 07:34:21 -04005825 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5826 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005827 revoke_delegation(dp);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005828 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005829
5830 return count;
5831}
5832
5833u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5834{
Jeff Laytondff13992014-07-08 14:02:49 -04005835 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005836 LIST_HEAD(victims);
5837 u64 count;
5838
Benny Halevycdc97502014-05-30 09:09:30 -04005839 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005840 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005841 while (!list_empty(&victims)) {
5842 dp = list_first_entry(&victims, struct nfs4_delegation,
5843 dl_recall_lru);
5844 list_del_init(&dp->dl_recall_lru);
5845 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005846 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005847 }
Benny Halevycdc97502014-05-30 09:09:30 -04005848 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005849
5850 return count;
5851}
5852
Bryan Schumaker184c1842012-11-29 11:40:44 -05005853u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5854{
5855 u64 count = 0;
5856
Benny Halevycdc97502014-05-30 09:09:30 -04005857 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005858 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005859 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005860
5861 nfsd_print_count(clp, count, "delegations");
5862 return count;
5863}
5864
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005865u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005866{
5867 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005868 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005869 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005870
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005871 if (!nfsd_netns_ready(nn))
5872 return 0;
5873
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005874 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005875 count += func(clp, max - count);
5876 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005877 break;
5878 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005879
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005880 return count;
5881}
5882
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005883struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5884{
5885 struct nfs4_client *clp;
5886 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5887
5888 if (!nfsd_netns_ready(nn))
5889 return NULL;
5890
5891 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5892 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5893 return clp;
5894 }
5895 return NULL;
5896}
5897
Bryan Schumaker65178db2011-11-01 13:35:21 -04005898#endif /* CONFIG_NFSD_FAULT_INJECTION */
5899
Meelap Shahc2f1a552007-07-17 04:04:39 -07005900/*
5901 * Since the lifetime of a delegation isn't limited to that of an open, a
5902 * client may quite reasonably hang on to a delegation as long as it has
5903 * the inode cached. This becomes an obvious problem the first time a
5904 * client's inode cache approaches the size of the server's total memory.
5905 *
5906 * For now we avoid this problem by imposing a hard limit on the number
5907 * of delegations, which varies according to the server's memory size.
5908 */
5909static void
5910set_max_delegations(void)
5911{
5912 /*
5913 * Allow at most 4 delegations per megabyte of RAM. Quick
5914 * estimates suggest that in the worst case (where every delegation
5915 * is for a different inode), a delegation could take about 1.5K,
5916 * giving a worst case usage of about 6% of memory.
5917 */
5918 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5919}
5920
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005921static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005922{
5923 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5924 int i;
5925
5926 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5927 CLIENT_HASH_SIZE, GFP_KERNEL);
5928 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005929 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005930 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5931 CLIENT_HASH_SIZE, GFP_KERNEL);
5932 if (!nn->unconf_id_hashtbl)
5933 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005934 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5935 SESSION_HASH_SIZE, GFP_KERNEL);
5936 if (!nn->sessionid_hashtbl)
5937 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005938
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005939 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005940 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005941 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005942 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005943 for (i = 0; i < SESSION_HASH_SIZE; i++)
5944 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005945 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005946 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005947 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005948 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005949 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005950 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005951
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005952 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005953 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005954
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005955 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005956
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005957err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005958 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005959err_unconf_id:
5960 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005961err:
5962 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005963}
5964
5965static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005966nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005967{
5968 int i;
5969 struct nfs4_client *clp = NULL;
5970 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5971
5972 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5973 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5974 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5975 destroy_client(clp);
5976 }
5977 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005978
Kinglong Mee2b905632014-03-26 22:09:30 +08005979 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5980 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5981 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5982 destroy_client(clp);
5983 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005984 }
5985
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005986 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005987 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005988 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005989 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005990}
5991
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005992int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005993nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005994{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005995 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005996 int ret;
5997
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005998 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005999 if (ret)
6000 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006001 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006002 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006003 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04006004 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006005 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03006006 nn->nfsd4_grace, net);
6007 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006008 return 0;
6009}
6010
6011/* initialization to perform when the nfsd service is started: */
6012
6013int
6014nfs4_state_start(void)
6015{
6016 int ret;
6017
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006018 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006019 if (ret)
6020 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07006021 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05006022 if (laundry_wq == NULL) {
6023 ret = -ENOMEM;
6024 goto out_recovery;
6025 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006026 ret = nfsd4_create_callback_queue();
6027 if (ret)
6028 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006029
Meelap Shahc2f1a552007-07-17 04:04:39 -07006030 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006031
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006032 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006033
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006034out_free_laundry:
6035 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05006036out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006037 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038}
6039
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006040void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006041nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006045 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006047 cancel_delayed_work_sync(&nn->laundromat_work);
6048 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05006049
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04006050 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04006052 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006053 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006055 unhash_delegation_locked(dp);
6056 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 }
Benny Halevycdc97502014-05-30 09:09:30 -04006058 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 list_for_each_safe(pos, next, &reaplist) {
6060 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006061 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04006062 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 }
6064
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006065 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006066 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04006067 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068}
6069
6070void
6071nfs4_state_shutdown(void)
6072{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006073 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006074 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006076
6077static void
6078get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6079{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006080 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6081 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006082}
6083
6084static void
6085put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6086{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006087 if (cstate->minorversion) {
6088 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6089 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6090 }
6091}
6092
6093void
6094clear_current_stateid(struct nfsd4_compound_state *cstate)
6095{
6096 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006097}
6098
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006099/*
6100 * functions to set current state id
6101 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006102void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006103nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6104{
6105 put_stateid(cstate, &odp->od_stateid);
6106}
6107
6108void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006109nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6110{
6111 put_stateid(cstate, &open->op_stateid);
6112}
6113
6114void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006115nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6116{
6117 put_stateid(cstate, &close->cl_stateid);
6118}
6119
6120void
6121nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6122{
6123 put_stateid(cstate, &lock->lk_resp_stateid);
6124}
6125
6126/*
6127 * functions to consume current state id
6128 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006129
6130void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006131nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6132{
6133 get_stateid(cstate, &odp->od_stateid);
6134}
6135
6136void
6137nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6138{
6139 get_stateid(cstate, &drp->dr_stateid);
6140}
6141
6142void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006143nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6144{
6145 get_stateid(cstate, &fsp->fr_stateid);
6146}
6147
6148void
6149nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6150{
6151 get_stateid(cstate, &setattr->sa_stateid);
6152}
6153
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006154void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006155nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6156{
6157 get_stateid(cstate, &close->cl_stateid);
6158}
6159
6160void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006161nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006162{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006163 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006164}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006165
6166void
6167nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6168{
6169 get_stateid(cstate, &read->rd_stateid);
6170}
6171
6172void
6173nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6174{
6175 get_stateid(cstate, &write->wr_stateid);
6176}