blob: 2e80a59e7e912181430aeb568979e4ba02a6ee16 [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
J. Bruce Fields8b671b82009-02-22 14:51:34 -080078/*
79 * Currently used for the del_recall_lru and file hash table. In an
80 * effort to decrease the scope of the client_mutex, this spinlock may
81 * eventually cover more:
82 */
Benny Halevycdc97502014-05-30 09:09:30 -040083static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080084
Jeff Laytonb401be222014-07-29 21:34:33 -040085/*
86 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
87 * the refcount on the open stateid to drop.
88 */
89static DECLARE_WAIT_QUEUE_HEAD(close_wq);
90
Christoph Hellwigabf11352014-05-21 07:43:03 -070091static struct kmem_cache *openowner_slab;
92static struct kmem_cache *lockowner_slab;
93static struct kmem_cache *file_slab;
94static struct kmem_cache *stateid_slab;
95static struct kmem_cache *deleg_slab;
NeilBrowne60d4392005-06-23 22:03:01 -070096
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -040097static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -080098
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -040099static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800100{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400101 return ses->se_flags & NFS4_SESSION_DEAD;
102}
103
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400104static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
105{
106 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400107 return nfserr_jukebox;
108 ses->se_flags |= NFS4_SESSION_DEAD;
109 return nfs_ok;
110}
111
J. Bruce Fields221a6872013-04-01 22:23:49 -0400112static bool is_client_expired(struct nfs4_client *clp)
113{
114 return clp->cl_time == 0;
115}
116
J. Bruce Fields221a6872013-04-01 22:23:49 -0400117static __be32 get_client_locked(struct nfs4_client *clp)
118{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400119 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
120
121 lockdep_assert_held(&nn->client_lock);
122
J. Bruce Fields221a6872013-04-01 22:23:49 -0400123 if (is_client_expired(clp))
124 return nfserr_expired;
125 atomic_inc(&clp->cl_refcount);
126 return nfs_ok;
127}
128
129/* must be called under the client_lock */
130static inline void
131renew_client_locked(struct nfs4_client *clp)
132{
133 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
134
135 if (is_client_expired(clp)) {
136 WARN_ON(1);
137 printk("%s: client (clientid %08x/%08x) already expired\n",
138 __func__,
139 clp->cl_clientid.cl_boot,
140 clp->cl_clientid.cl_id);
141 return;
142 }
143
144 dprintk("renewing client (clientid %08x/%08x)\n",
145 clp->cl_clientid.cl_boot,
146 clp->cl_clientid.cl_id);
147 list_move_tail(&clp->cl_lru, &nn->client_lru);
148 clp->cl_time = get_seconds();
149}
150
151static inline void
152renew_client(struct nfs4_client *clp)
153{
154 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
155
156 spin_lock(&nn->client_lock);
157 renew_client_locked(clp);
158 spin_unlock(&nn->client_lock);
159}
160
Fengguang Wuba138432013-04-16 22:14:15 -0400161static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400162{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400163 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
164
165 lockdep_assert_held(&nn->client_lock);
166
J. Bruce Fields221a6872013-04-01 22:23:49 -0400167 if (!atomic_dec_and_test(&clp->cl_refcount))
168 return;
169 if (!is_client_expired(clp))
170 renew_client_locked(clp);
171}
172
Jeff Layton4b24ca72014-06-30 11:48:44 -0400173static void put_client_renew(struct nfs4_client *clp)
174{
175 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
176
Jeff Laytond6c249b2014-07-08 14:02:50 -0400177 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
178 return;
179 if (!is_client_expired(clp))
180 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400181 spin_unlock(&nn->client_lock);
182}
183
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400184static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
185{
186 __be32 status;
187
188 if (is_session_dead(ses))
189 return nfserr_badsession;
190 status = get_client_locked(ses->se_client);
191 if (status)
192 return status;
193 atomic_inc(&ses->se_ref);
194 return nfs_ok;
195}
196
197static void nfsd4_put_session_locked(struct nfsd4_session *ses)
198{
199 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400200 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
201
202 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400203
204 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
205 free_session(ses);
206 put_client_renew_locked(clp);
207}
208
209static void nfsd4_put_session(struct nfsd4_session *ses)
210{
211 struct nfs4_client *clp = ses->se_client;
212 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
213
214 spin_lock(&nn->client_lock);
215 nfsd4_put_session_locked(ses);
216 spin_unlock(&nn->client_lock);
217}
218
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400219static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400220same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400221{
222 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400223 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400224}
225
226static struct nfs4_openowner *
227find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400228 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400229{
230 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400231
Trond Myklebustd4f04892014-07-29 21:34:36 -0400232 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400233
Trond Myklebustd4f04892014-07-29 21:34:36 -0400234 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
235 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400236 if (!so->so_is_open_owner)
237 continue;
Trond Myklebustd4f04892014-07-29 21:34:36 -0400238 if (same_owner_str(so, &open->op_owner)) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400239 atomic_inc(&so->so_count);
Trond Myklebustd4f04892014-07-29 21:34:36 -0400240 return openowner(so);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400241 }
242 }
243 return NULL;
244}
245
246static struct nfs4_openowner *
247find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400248 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400249{
250 struct nfs4_openowner *oo;
251
Trond Myklebustd4f04892014-07-29 21:34:36 -0400252 spin_lock(&clp->cl_lock);
253 oo = find_openstateowner_str_locked(hashval, open, clp);
254 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400255 return oo;
256}
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258static inline u32
259opaque_hashval(const void *ptr, int nbytes)
260{
261 unsigned char *cptr = (unsigned char *) ptr;
262
263 u32 x = 0;
264 while (nbytes--) {
265 x *= 37;
266 x += *cptr++;
267 }
268 return x;
269}
270
J. Bruce Fields32513b42011-10-13 16:00:16 -0400271static void nfsd4_free_file(struct nfs4_file *f)
272{
273 kmem_cache_free(file_slab, f);
274}
275
NeilBrown13cd2182005-06-23 22:03:10 -0700276static inline void
277put_nfs4_file(struct nfs4_file *fi)
278{
Jeff Layton02e12152014-07-16 10:31:57 -0400279 might_lock(&state_lock);
280
Benny Halevycdc97502014-05-30 09:09:30 -0400281 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton89876f82013-04-02 09:01:59 -0400282 hlist_del(&fi->fi_hash);
Benny Halevycdc97502014-05-30 09:09:30 -0400283 spin_unlock(&state_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400284 nfsd4_free_file(fi);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800285 }
NeilBrown13cd2182005-06-23 22:03:10 -0700286}
287
288static inline void
289get_nfs4_file(struct nfs4_file *fi)
290{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800291 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700292}
293
Trond Myklebustde186432014-07-10 14:07:26 -0400294static struct file *
295__nfs4_get_fd(struct nfs4_file *f, int oflag)
296{
297 if (f->fi_fds[oflag])
298 return get_file(f->fi_fds[oflag]);
299 return NULL;
300}
301
302static struct file *
303find_writeable_file_locked(struct nfs4_file *f)
304{
305 struct file *ret;
306
307 lockdep_assert_held(&f->fi_lock);
308
309 ret = __nfs4_get_fd(f, O_WRONLY);
310 if (!ret)
311 ret = __nfs4_get_fd(f, O_RDWR);
312 return ret;
313}
314
315static struct file *
316find_writeable_file(struct nfs4_file *f)
317{
318 struct file *ret;
319
320 spin_lock(&f->fi_lock);
321 ret = find_writeable_file_locked(f);
322 spin_unlock(&f->fi_lock);
323
324 return ret;
325}
326
327static struct file *find_readable_file_locked(struct nfs4_file *f)
328{
329 struct file *ret;
330
331 lockdep_assert_held(&f->fi_lock);
332
333 ret = __nfs4_get_fd(f, O_RDONLY);
334 if (!ret)
335 ret = __nfs4_get_fd(f, O_RDWR);
336 return ret;
337}
338
339static struct file *
340find_readable_file(struct nfs4_file *f)
341{
342 struct file *ret;
343
344 spin_lock(&f->fi_lock);
345 ret = find_readable_file_locked(f);
346 spin_unlock(&f->fi_lock);
347
348 return ret;
349}
350
351static struct file *
352find_any_file(struct nfs4_file *f)
353{
354 struct file *ret;
355
356 spin_lock(&f->fi_lock);
357 ret = __nfs4_get_fd(f, O_RDWR);
358 if (!ret) {
359 ret = __nfs4_get_fd(f, O_WRONLY);
360 if (!ret)
361 ret = __nfs4_get_fd(f, O_RDONLY);
362 }
363 spin_unlock(&f->fi_lock);
364 return ret;
365}
366
Trond Myklebust02a35082014-07-25 07:34:22 -0400367static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800368unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700369
370/*
371 * Open owner state (share locks)
372 */
373
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500374/* hash tables for lock and open owners */
375#define OWNER_HASH_BITS 8
376#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
377#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700378
Trond Myklebustd4f04892014-07-29 21:34:36 -0400379static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400380{
381 unsigned int ret;
382
383 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500384 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400385}
NeilBrownef0f3392006-04-10 22:55:41 -0700386
NeilBrownef0f3392006-04-10 22:55:41 -0700387/* hash table for nfs4_file */
388#define FILE_HASH_BITS 8
389#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800390
Trond Myklebustca943212014-07-23 16:17:39 -0400391static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400392{
Trond Myklebustca943212014-07-23 16:17:39 -0400393 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
394}
395
396static unsigned int file_hashval(struct knfsd_fh *fh)
397{
398 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
399}
400
401static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
402{
403 return fh1->fh_size == fh2->fh_size &&
404 !memcmp(fh1->fh_base.fh_pad,
405 fh2->fh_base.fh_pad,
406 fh1->fh_size);
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400407}
408
Jeff Layton89876f82013-04-02 09:01:59 -0400409static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700410
Jeff Layton12659652014-07-10 14:07:28 -0400411static void
412__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400413{
Jeff Layton7214e862014-07-10 14:07:33 -0400414 lockdep_assert_held(&fp->fi_lock);
415
Jeff Layton12659652014-07-10 14:07:28 -0400416 if (access & NFS4_SHARE_ACCESS_WRITE)
417 atomic_inc(&fp->fi_access[O_WRONLY]);
418 if (access & NFS4_SHARE_ACCESS_READ)
419 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400420}
421
Jeff Layton12659652014-07-10 14:07:28 -0400422static __be32
423nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400424{
Jeff Layton7214e862014-07-10 14:07:33 -0400425 lockdep_assert_held(&fp->fi_lock);
426
Jeff Layton12659652014-07-10 14:07:28 -0400427 /* Does this access mode make sense? */
428 if (access & ~NFS4_SHARE_ACCESS_BOTH)
429 return nfserr_inval;
430
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400431 /* Does it conflict with a deny mode already set? */
432 if ((access & fp->fi_share_deny) != 0)
433 return nfserr_share_denied;
434
Jeff Layton12659652014-07-10 14:07:28 -0400435 __nfs4_file_get_access(fp, access);
436 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400437}
438
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400439static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
440{
441 /* Common case is that there is no deny mode. */
442 if (deny) {
443 /* Does this deny mode make sense? */
444 if (deny & ~NFS4_SHARE_DENY_BOTH)
445 return nfserr_inval;
446
447 if ((deny & NFS4_SHARE_DENY_READ) &&
448 atomic_read(&fp->fi_access[O_RDONLY]))
449 return nfserr_share_denied;
450
451 if ((deny & NFS4_SHARE_DENY_WRITE) &&
452 atomic_read(&fp->fi_access[O_WRONLY]))
453 return nfserr_share_denied;
454 }
455 return nfs_ok;
456}
457
J. Bruce Fields998db522010-08-07 09:21:41 -0400458static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400459{
Trond Myklebustde186432014-07-10 14:07:26 -0400460 might_lock(&fp->fi_lock);
461
462 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
463 struct file *f1 = NULL;
464 struct file *f2 = NULL;
465
Jeff Layton6d338b52014-07-10 14:07:29 -0400466 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400467 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400468 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400469 spin_unlock(&fp->fi_lock);
470 if (f1)
471 fput(f1);
472 if (f2)
473 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400474 }
475}
476
Jeff Layton12659652014-07-10 14:07:28 -0400477static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400478{
Jeff Layton12659652014-07-10 14:07:28 -0400479 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
480
481 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400482 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400483 if (access & NFS4_SHARE_ACCESS_READ)
484 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400485}
486
Trond Myklebust60116952014-07-29 21:34:06 -0400487static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
488 struct kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400489{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500490 struct nfs4_stid *stid;
491 int new_id;
492
Trond Myklebustf8338832014-07-25 07:34:19 -0400493 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500494 if (!stid)
495 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400496
Jeff Layton4770d722014-07-29 21:34:10 -0400497 idr_preload(GFP_KERNEL);
498 spin_lock(&cl->cl_lock);
499 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
500 spin_unlock(&cl->cl_lock);
501 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700502 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500503 goto out_free;
504 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500505 stid->sc_stateid.si_opaque.so_id = new_id;
506 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
507 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400508 atomic_set(&stid->sc_count, 1);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500509
J. Bruce Fields996e0932011-10-17 11:14:48 -0400510 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500511 * It shouldn't be a problem to reuse an opaque stateid value.
512 * I don't think it is for 4.1. But with 4.0 I worry that, for
513 * example, a stray write retransmission could be accepted by
514 * the server when it should have been rejected. Therefore,
515 * adopt a trick from the sctp code to attempt to maximize the
516 * amount of time until an id is reused, by ensuring they always
517 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400518 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500519 return stid;
520out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800521 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500522 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400523}
524
Jeff Laytonb49e0842014-07-29 21:34:11 -0400525static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400526{
Trond Myklebust60116952014-07-29 21:34:06 -0400527 struct nfs4_stid *stid;
528 struct nfs4_ol_stateid *stp;
529
530 stid = nfs4_alloc_stid(clp, stateid_slab);
531 if (!stid)
532 return NULL;
533
534 stp = openlockstateid(stid);
535 stp->st_stid.sc_free = nfs4_free_ol_stateid;
536 return stp;
537}
538
539static void nfs4_free_deleg(struct nfs4_stid *stid)
540{
Trond Myklebust60116952014-07-29 21:34:06 -0400541 kmem_cache_free(deleg_slab, stid);
542 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400543}
544
NeilBrown6282cd52014-06-04 17:39:26 +1000545/*
546 * When we recall a delegation, we should be careful not to hand it
547 * out again straight away.
548 * To ensure this we keep a pair of bloom filters ('new' and 'old')
549 * in which the filehandles of recalled delegations are "stored".
550 * If a filehandle appear in either filter, a delegation is blocked.
551 * When a delegation is recalled, the filehandle is stored in the "new"
552 * filter.
553 * Every 30 seconds we swap the filters and clear the "new" one,
554 * unless both are empty of course.
555 *
556 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
557 * low 3 bytes as hash-table indices.
558 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400559 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000560 * is used to manage concurrent access. Testing does not need the lock
561 * except when swapping the two filters.
562 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400563static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000564static struct bloom_pair {
565 int entries, old_entries;
566 time_t swap_time;
567 int new; /* index into 'set' */
568 DECLARE_BITMAP(set[2], 256);
569} blocked_delegations;
570
571static int delegation_blocked(struct knfsd_fh *fh)
572{
573 u32 hash;
574 struct bloom_pair *bd = &blocked_delegations;
575
576 if (bd->entries == 0)
577 return 0;
578 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400579 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000580 if (seconds_since_boot() - bd->swap_time > 30) {
581 bd->entries -= bd->old_entries;
582 bd->old_entries = bd->entries;
583 memset(bd->set[bd->new], 0,
584 sizeof(bd->set[0]));
585 bd->new = 1-bd->new;
586 bd->swap_time = seconds_since_boot();
587 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400588 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000589 }
590 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
591 if (test_bit(hash&255, bd->set[0]) &&
592 test_bit((hash>>8)&255, bd->set[0]) &&
593 test_bit((hash>>16)&255, bd->set[0]))
594 return 1;
595
596 if (test_bit(hash&255, bd->set[1]) &&
597 test_bit((hash>>8)&255, bd->set[1]) &&
598 test_bit((hash>>16)&255, bd->set[1]))
599 return 1;
600
601 return 0;
602}
603
604static void block_delegations(struct knfsd_fh *fh)
605{
606 u32 hash;
607 struct bloom_pair *bd = &blocked_delegations;
608
609 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
610
Jeff Laytonf54fe962014-07-25 07:34:26 -0400611 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000612 __set_bit(hash&255, bd->set[bd->new]);
613 __set_bit((hash>>8)&255, bd->set[bd->new]);
614 __set_bit((hash>>16)&255, bd->set[bd->new]);
615 if (bd->entries == 0)
616 bd->swap_time = seconds_since_boot();
617 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400618 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000619}
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621static struct nfs4_delegation *
Jeff Laytonf9416e22014-07-25 07:34:23 -0400622alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400625 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400628 n = atomic_long_inc_return(&num_delegations);
629 if (n < 0 || n > max_delegations)
630 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000631 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400632 goto out_dec;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400633 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700634 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400635 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400636
637 dp->dl_stid.sc_free = nfs4_free_deleg;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400638 /*
639 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400640 * meaning of seqid 0 isn't meaningful, really, but let's avoid
641 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400642 */
643 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700644 INIT_LIST_HEAD(&dp->dl_perfile);
645 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 INIT_LIST_HEAD(&dp->dl_recall_lru);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400647 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Jeff Layton02e12152014-07-16 10:31:57 -0400648 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400650out_dec:
651 atomic_long_dec(&num_delegations);
652 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
655void
Trond Myklebust60116952014-07-29 21:34:06 -0400656nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Trond Myklebust11b91642014-07-29 21:34:08 -0400658 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400659 struct nfs4_client *clp = s->sc_client;
660
Jeff Layton4770d722014-07-29 21:34:10 -0400661 might_lock(&clp->cl_lock);
662
Jeff Laytonb401be222014-07-29 21:34:33 -0400663 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
664 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400665 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400666 }
Trond Myklebust60116952014-07-29 21:34:06 -0400667 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400668 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400669 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400670 if (fp)
671 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500674static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Jeff Layton417c6622014-07-21 09:34:57 -0400676 lockdep_assert_held(&state_lock);
677
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -0500678 if (!fp->fi_lease)
679 return;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500680 if (atomic_dec_and_test(&fp->fi_delegees)) {
681 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
682 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400683 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500684 fp->fi_deleg_file = NULL;
685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400688static void unhash_stid(struct nfs4_stid *s)
689{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500690 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400691}
692
Benny Halevy931ee562014-05-30 09:09:27 -0400693static void
694hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
695{
Benny Halevycdc97502014-05-30 09:09:30 -0400696 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400697 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400698
Trond Myklebust67cb1272014-07-29 21:34:17 -0400699 atomic_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400700 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400701 list_add(&dp->dl_perfile, &fp->fi_delegations);
702 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
703}
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705static void
Jeff Layton42690672014-07-25 07:34:20 -0400706unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Trond Myklebust11b91642014-07-29 21:34:08 -0400708 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400709
Jeff Layton42690672014-07-25 07:34:20 -0400710 lockdep_assert_held(&state_lock);
711
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400712 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400713 /* Ensure that deleg break won't try to requeue it */
714 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400715 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400716 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400718 list_del_init(&dp->dl_perfile);
719 spin_unlock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400720 if (fp)
Trond Myklebustf8338832014-07-25 07:34:19 -0400721 nfs4_put_deleg_lease(fp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400722}
723
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400724static void destroy_delegation(struct nfs4_delegation *dp)
725{
Jeff Layton42690672014-07-25 07:34:20 -0400726 spin_lock(&state_lock);
727 unhash_delegation_locked(dp);
728 spin_unlock(&state_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400729 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400730}
731
732static void revoke_delegation(struct nfs4_delegation *dp)
733{
734 struct nfs4_client *clp = dp->dl_stid.sc_client;
735
Jeff Layton2d4a5322014-07-25 07:34:21 -0400736 WARN_ON(!list_empty(&dp->dl_recall_lru));
737
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400738 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400739 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400740 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400741 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400742 spin_lock(&clp->cl_lock);
743 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
744 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400745 }
746}
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748/*
749 * SETCLIENTID state
750 */
751
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400752static unsigned int clientid_hashval(u32 id)
753{
754 return id & CLIENT_HASH_MASK;
755}
756
757static unsigned int clientstr_hashval(const char *name)
758{
759 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
760}
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400763 * We store the NONE, READ, WRITE, and BOTH bits separately in the
764 * st_{access,deny}_bmap field of the stateid, in order to track not
765 * only what share bits are currently in force, but also what
766 * combinations of share bits previous opens have used. This allows us
767 * to enforce the recommendation of rfc 3530 14.2.19 that the server
768 * return an error if the client attempt to downgrade to a combination
769 * of share bits not explicable by closing some of its previous opens.
770 *
771 * XXX: This enforcement is actually incomplete, since we don't keep
772 * track of access/deny bit combinations; so, e.g., we allow:
773 *
774 * OPEN allow read, deny write
775 * OPEN allow both, deny none
776 * DOWNGRADE allow read, deny none
777 *
778 * which we should reject.
779 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400780static unsigned int
781bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400782 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400783 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400784
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400785 for (i = 1; i < 4; i++) {
786 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400787 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400788 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400789 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400790}
791
Jeff Layton82c5ff12012-05-11 09:45:13 -0400792/* set share access for a given stateid */
793static inline void
794set_access(u32 access, struct nfs4_ol_stateid *stp)
795{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400796 unsigned char mask = 1 << access;
797
798 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
799 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400800}
801
802/* clear share access for a given stateid */
803static inline void
804clear_access(u32 access, struct nfs4_ol_stateid *stp)
805{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400806 unsigned char mask = 1 << access;
807
808 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
809 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400810}
811
812/* test whether a given stateid has access */
813static inline bool
814test_access(u32 access, struct nfs4_ol_stateid *stp)
815{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400816 unsigned char mask = 1 << access;
817
818 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400819}
820
Jeff Laytonce0fc432012-05-11 09:45:14 -0400821/* set share deny for a given stateid */
822static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400823set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400824{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400825 unsigned char mask = 1 << deny;
826
827 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
828 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400829}
830
831/* clear share deny for a given stateid */
832static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400833clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400834{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400835 unsigned char mask = 1 << deny;
836
837 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
838 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400839}
840
841/* test whether a given stateid is denying specific access */
842static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400843test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400844{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400845 unsigned char mask = 1 << deny;
846
847 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400848}
849
850static int nfs4_access_to_omode(u32 access)
851{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400852 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400853 case NFS4_SHARE_ACCESS_READ:
854 return O_RDONLY;
855 case NFS4_SHARE_ACCESS_WRITE:
856 return O_WRONLY;
857 case NFS4_SHARE_ACCESS_BOTH:
858 return O_RDWR;
859 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500860 WARN_ON_ONCE(1);
861 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400862}
863
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400864/*
865 * A stateid that had a deny mode associated with it is being released
866 * or downgraded. Recalculate the deny mode on the file.
867 */
868static void
869recalculate_deny_mode(struct nfs4_file *fp)
870{
871 struct nfs4_ol_stateid *stp;
872
873 spin_lock(&fp->fi_lock);
874 fp->fi_share_deny = 0;
875 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
876 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
877 spin_unlock(&fp->fi_lock);
878}
879
880static void
881reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
882{
883 int i;
884 bool change = false;
885
886 for (i = 1; i < 4; i++) {
887 if ((i & deny) != i) {
888 change = true;
889 clear_deny(i, stp);
890 }
891 }
892
893 /* Recalculate per-file deny mode if there was a change */
894 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -0400895 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400896}
897
Jeff Layton82c5ff12012-05-11 09:45:13 -0400898/* release all access and file references for a given stateid */
899static void
900release_all_access(struct nfs4_ol_stateid *stp)
901{
902 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -0400903 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400904
905 if (fp && stp->st_deny_bmap != 0)
906 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400907
908 for (i = 1; i < 4; i++) {
909 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -0400910 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400911 clear_access(i, stp);
912 }
913}
914
Jeff Layton6b180f02014-07-29 21:34:26 -0400915static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
916{
Jeff Laytona819ecc2014-07-29 21:34:38 -0400917 struct nfs4_client *clp = sop->so_client;
918
919 might_lock(&clp->cl_lock);
920
921 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -0400922 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -0400923 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -0400924 spin_unlock(&clp->cl_lock);
Jeff Layton6b180f02014-07-29 21:34:26 -0400925 kfree(sop->so_owner.data);
926 sop->so_ops->so_free(sop);
927}
928
Jeff Layton4ae098d2014-07-29 21:34:43 -0400929static void unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500930{
Trond Myklebust11b91642014-07-29 21:34:08 -0400931 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -0400932
Jeff Layton1c755dc2014-07-29 21:34:12 -0400933 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
934
Trond Myklebust1d31a252014-07-10 14:07:25 -0400935 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500936 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400937 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500938 list_del(&stp->st_perstateowner);
939}
940
Trond Myklebust60116952014-07-29 21:34:06 -0400941static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500942{
Trond Myklebust60116952014-07-29 21:34:06 -0400943 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400944
Trond Myklebust60116952014-07-29 21:34:06 -0400945 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -0400946 if (stp->st_stateowner)
947 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -0400948 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500949}
950
Jeff Laytonb49e0842014-07-29 21:34:11 -0400951static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500952{
Jeff Laytonb49e0842014-07-29 21:34:11 -0400953 struct nfs4_ol_stateid *stp = openlockstateid(stid);
954 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500955 struct file *file;
956
Jeff Laytonb49e0842014-07-29 21:34:11 -0400957 file = find_any_file(stp->st_stid.sc_file);
958 if (file)
959 filp_close(file, (fl_owner_t)lo);
960 nfs4_free_ol_stateid(stid);
961}
962
Jeff Layton2c41beb2014-07-29 21:34:41 -0400963/*
964 * Put the persistent reference to an already unhashed generic stateid, while
965 * holding the cl_lock. If it's the last reference, then put it onto the
966 * reaplist for later destruction.
967 */
968static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
969 struct list_head *reaplist)
970{
971 struct nfs4_stid *s = &stp->st_stid;
972 struct nfs4_client *clp = s->sc_client;
973
974 lockdep_assert_held(&clp->cl_lock);
975
976 WARN_ON_ONCE(!list_empty(&stp->st_locks));
977
978 if (!atomic_dec_and_test(&s->sc_count)) {
979 wake_up_all(&close_wq);
980 return;
981 }
982
983 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
984 list_add(&stp->st_locks, reaplist);
985}
986
Jeff Layton3c1c9952014-07-29 21:34:39 -0400987static void unhash_lock_stateid(struct nfs4_ol_stateid *stp)
988{
989 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
990
991 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
992
993 list_del_init(&stp->st_locks);
Jeff Layton4ae098d2014-07-29 21:34:43 -0400994 unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -0400995 unhash_stid(&stp->st_stid);
996}
997
Jeff Layton5adfd882014-07-29 21:34:31 -0400998static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -0400999{
Jeff Layton1c755dc2014-07-29 21:34:12 -04001000 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1001
1002 spin_lock(&oo->oo_owner.so_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001003 unhash_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001004 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04001005 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001006}
1007
Trond Myklebustc58c6612014-07-29 21:34:35 -04001008static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001009{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001010 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001011
Trond Myklebustd4f04892014-07-29 21:34:36 -04001012 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001013
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001014 list_del_init(&lo->lo_owner.so_strhash);
1015}
1016
Jeff Layton2c41beb2014-07-29 21:34:41 -04001017/*
1018 * Free a list of generic stateids that were collected earlier after being
1019 * fully unhashed.
1020 */
1021static void
1022free_ol_stateid_reaplist(struct list_head *reaplist)
1023{
1024 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001025 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001026
1027 might_sleep();
1028
1029 while (!list_empty(reaplist)) {
1030 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1031 st_locks);
1032 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001033 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001034 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001035 if (fp)
1036 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001037 }
1038}
1039
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001040static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001041{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001042 struct nfs4_client *clp = lo->lo_owner.so_client;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001043 struct nfs4_ol_stateid *stp;
1044 struct list_head reaplist;
1045
1046 INIT_LIST_HEAD(&reaplist);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001047
Trond Myklebustd4f04892014-07-29 21:34:36 -04001048 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001049 unhash_lockowner_locked(lo);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001050 while (!list_empty(&lo->lo_owner.so_stateids)) {
1051 stp = list_first_entry(&lo->lo_owner.so_stateids,
1052 struct nfs4_ol_stateid, st_perstateowner);
1053 unhash_lock_stateid(stp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001054 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001055 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001056 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001057 free_ol_stateid_reaplist(&reaplist);
Jeff Layton6b180f02014-07-29 21:34:26 -04001058 nfs4_put_stateowner(&lo->lo_owner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001059}
1060
Jeff Laytond83017f2014-07-29 21:34:42 -04001061static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1062 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001063{
1064 struct nfs4_ol_stateid *stp;
1065
1066 while (!list_empty(&open_stp->st_locks)) {
1067 stp = list_entry(open_stp->st_locks.next,
1068 struct nfs4_ol_stateid, st_locks);
Jeff Laytond83017f2014-07-29 21:34:42 -04001069 unhash_lock_stateid(stp);
1070 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001071 }
1072}
1073
Jeff Laytond83017f2014-07-29 21:34:42 -04001074static void unhash_open_stateid(struct nfs4_ol_stateid *stp,
1075 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001076{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001077 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1078
Jeff Layton4ae098d2014-07-29 21:34:43 -04001079 unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001080 release_open_stateid_locks(stp, reaplist);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001081}
1082
1083static void release_open_stateid(struct nfs4_ol_stateid *stp)
1084{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001085 LIST_HEAD(reaplist);
1086
1087 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04001088 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001089 put_ol_stateid_locked(stp, &reaplist);
1090 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1091 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001092}
1093
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001094static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001095{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001096 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001097
Trond Myklebustd4f04892014-07-29 21:34:36 -04001098 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001099
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001100 list_del_init(&oo->oo_owner.so_strhash);
1101 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001102}
1103
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001104static void release_last_closed_stateid(struct nfs4_openowner *oo)
1105{
Jeff Layton217526e2014-07-30 08:27:11 -04001106 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1107 nfsd_net_id);
1108 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001109
Jeff Layton217526e2014-07-30 08:27:11 -04001110 spin_lock(&nn->client_lock);
1111 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001112 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001113 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001114 oo->oo_last_closed_stid = NULL;
1115 }
Jeff Layton217526e2014-07-30 08:27:11 -04001116 spin_unlock(&nn->client_lock);
1117 if (s)
1118 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001119}
1120
Jeff Layton2c41beb2014-07-29 21:34:41 -04001121static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001122{
1123 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001124 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001125 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001126
Jeff Layton2c41beb2014-07-29 21:34:41 -04001127 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001128
Trond Myklebustd4f04892014-07-29 21:34:36 -04001129 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001130 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001131 while (!list_empty(&oo->oo_owner.so_stateids)) {
1132 stp = list_first_entry(&oo->oo_owner.so_stateids,
1133 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytond83017f2014-07-29 21:34:42 -04001134 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001135 put_ol_stateid_locked(stp, &reaplist);
1136 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001137 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001138 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001139 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001140 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001141}
1142
Marc Eshel5282fd72009-04-03 08:27:52 +03001143static inline int
1144hash_sessionid(struct nfs4_sessionid *sessionid)
1145{
1146 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1147
1148 return sid->sequence % SESSION_HASH_SIZE;
1149}
1150
Trond Myklebust8f199b82012-03-20 15:11:17 -04001151#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001152static inline void
1153dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1154{
1155 u32 *ptr = (u32 *)(&sessionid->data[0]);
1156 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1157}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001158#else
1159static inline void
1160dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1161{
1162}
1163#endif
1164
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001165/*
1166 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1167 * won't be used for replay.
1168 */
1169void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1170{
1171 struct nfs4_stateowner *so = cstate->replay_owner;
1172
1173 if (nfserr == nfserr_replay_me)
1174 return;
1175
1176 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001177 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001178 return;
1179 }
1180 if (!so)
1181 return;
1182 if (so->so_is_open_owner)
1183 release_last_closed_stateid(openowner(so));
1184 so->so_seqid++;
1185 return;
1186}
Marc Eshel5282fd72009-04-03 08:27:52 +03001187
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001188static void
1189gen_sessionid(struct nfsd4_session *ses)
1190{
1191 struct nfs4_client *clp = ses->se_client;
1192 struct nfsd4_sessionid *sid;
1193
1194 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1195 sid->clientid = clp->cl_clientid;
1196 sid->sequence = current_sessionid++;
1197 sid->reserved = 0;
1198}
1199
1200/*
Andy Adamsona6496372009-08-28 08:45:01 -04001201 * The protocol defines ca_maxresponssize_cached to include the size of
1202 * the rpc header, but all we need to cache is the data starting after
1203 * the end of the initial SEQUENCE operation--the rest we regenerate
1204 * each time. Therefore we can advertise a ca_maxresponssize_cached
1205 * value that is the number of bytes in our cache plus a few additional
1206 * bytes. In order to stay on the safe side, and not promise more than
1207 * we can cache, those additional bytes must be the minimum possible: 24
1208 * bytes of rpc header (xid through accept state, with AUTH_NULL
1209 * verifier), 12 for the compound header (with zero-length tag), and 44
1210 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001211 */
Andy Adamsona6496372009-08-28 08:45:01 -04001212#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1213
Andy Adamson557ce262009-08-28 08:45:04 -04001214static void
1215free_session_slots(struct nfsd4_session *ses)
1216{
1217 int i;
1218
1219 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1220 kfree(ses->se_slots[i]);
1221}
1222
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001223/*
1224 * We don't actually need to cache the rpc and session headers, so we
1225 * can allocate a little less for each slot:
1226 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001227static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001228{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001229 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001230
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001231 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1232 size = 0;
1233 else
1234 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1235 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001236}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001237
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001238/*
1239 * XXX: If we run out of reserved DRC memory we could (up to a point)
1240 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001241 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001242 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001243static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001244{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001245 u32 slotsize = slot_bytes(ca);
1246 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001247 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001248
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001249 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001250 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1251 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001252 num = min_t(int, num, avail / slotsize);
1253 nfsd_drc_mem_used += num * slotsize;
1254 spin_unlock(&nfsd_drc_lock);
1255
1256 return num;
1257}
1258
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001259static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001260{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001261 int slotsize = slot_bytes(ca);
1262
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001263 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001264 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001265 spin_unlock(&nfsd_drc_lock);
1266}
1267
Kinglong Mee60810e52014-01-01 00:35:47 +08001268static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1269 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001270{
Kinglong Mee60810e52014-01-01 00:35:47 +08001271 int numslots = fattrs->maxreqs;
1272 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001273 struct nfsd4_session *new;
1274 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001275
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001276 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001277 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1278 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001279
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001280 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001281 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001282 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001283 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001284 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001285 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001286 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001287 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001288 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001289
1290 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1291 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1292
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001293 return new;
1294out_free:
1295 while (i--)
1296 kfree(new->se_slots[i]);
1297 kfree(new);
1298 return NULL;
1299}
1300
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001301static void free_conn(struct nfsd4_conn *c)
1302{
1303 svc_xprt_put(c->cn_xprt);
1304 kfree(c);
1305}
1306
1307static void nfsd4_conn_lost(struct svc_xpt_user *u)
1308{
1309 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1310 struct nfs4_client *clp = c->cn_session->se_client;
1311
1312 spin_lock(&clp->cl_lock);
1313 if (!list_empty(&c->cn_persession)) {
1314 list_del(&c->cn_persession);
1315 free_conn(c);
1316 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001317 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001318 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001319}
1320
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001321static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001322{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001323 struct nfsd4_conn *conn;
1324
1325 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1326 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001327 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001328 svc_xprt_get(rqstp->rq_xprt);
1329 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001330 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001331 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1332 return conn;
1333}
1334
J. Bruce Fields328ead22010-09-29 16:11:06 -04001335static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1336{
1337 conn->cn_session = ses;
1338 list_add(&conn->cn_persession, &ses->se_conns);
1339}
1340
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001341static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1342{
1343 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001344
1345 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001346 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001347 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001348}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001349
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001350static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001351{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001352 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001353 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001354}
1355
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001356static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001357{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001358 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001359
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001360 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001361 ret = nfsd4_register_conn(conn);
1362 if (ret)
1363 /* oops; xprt is already down: */
1364 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001365 /* We may have gained or lost a callback channel: */
1366 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001367}
1368
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001369static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001370{
1371 u32 dir = NFS4_CDFC4_FORE;
1372
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001373 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001374 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001375 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001376}
1377
1378/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001379static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001380{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001381 struct nfs4_client *clp = s->se_client;
1382 struct nfsd4_conn *c;
1383
1384 spin_lock(&clp->cl_lock);
1385 while (!list_empty(&s->se_conns)) {
1386 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1387 list_del_init(&c->cn_persession);
1388 spin_unlock(&clp->cl_lock);
1389
1390 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1391 free_conn(c);
1392
1393 spin_lock(&clp->cl_lock);
1394 }
1395 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001396}
1397
J. Bruce Fields1377b692012-09-11 21:42:40 -04001398static void __free_session(struct nfsd4_session *ses)
1399{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001400 free_session_slots(ses);
1401 kfree(ses);
1402}
1403
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001404static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001405{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001406 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001407 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001408 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001409}
Andy Adamson557ce262009-08-28 08:45:04 -04001410
Fengguang Wu135ae822012-11-10 07:20:25 -05001411static 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 -04001412{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001413 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001414 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001415
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001416 new->se_client = clp;
1417 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001418
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001419 INIT_LIST_HEAD(&new->se_conns);
1420
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001421 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001422 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001423 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001424 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001425 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001426 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001427 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001428 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001429 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001430 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001431
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001432 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001433 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001434 /*
1435 * This is a little silly; with sessions there's no real
1436 * use for the callback address. Use the peer address
1437 * as a reasonable default for now, but consider fixing
1438 * the rpc client not to require an address in the
1439 * future:
1440 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001441 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1442 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001443 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001444}
1445
Benny Halevy9089f1b2010-05-12 00:12:26 +03001446/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001447static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001448__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001449{
1450 struct nfsd4_session *elem;
1451 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001452 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001453
Trond Myklebust0a880a22014-07-30 08:27:10 -04001454 lockdep_assert_held(&nn->client_lock);
1455
Marc Eshel5282fd72009-04-03 08:27:52 +03001456 dump_sessionid(__func__, sessionid);
1457 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001458 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001459 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001460 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1461 NFS4_MAX_SESSIONID_LEN)) {
1462 return elem;
1463 }
1464 }
1465
1466 dprintk("%s: session not found\n", __func__);
1467 return NULL;
1468}
1469
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001470static struct nfsd4_session *
1471find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1472 __be32 *ret)
1473{
1474 struct nfsd4_session *session;
1475 __be32 status = nfserr_badsession;
1476
1477 session = __find_in_sessionid_hashtbl(sessionid, net);
1478 if (!session)
1479 goto out;
1480 status = nfsd4_get_session_locked(session);
1481 if (status)
1482 session = NULL;
1483out:
1484 *ret = status;
1485 return session;
1486}
1487
Benny Halevy9089f1b2010-05-12 00:12:26 +03001488/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001489static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001490unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001491{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001492 struct nfs4_client *clp = ses->se_client;
1493 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1494
1495 lockdep_assert_held(&nn->client_lock);
1496
Andy Adamson7116ed62009-04-03 08:27:43 +03001497 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001498 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001499 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001500 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001501}
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1504static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001505STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001507 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001509 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001510 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 return 1;
1512}
1513
1514/*
1515 * XXX Should we use a slab cache ?
1516 * This type of memory management is somewhat inefficient, but we use it
1517 * anyway since SETCLIENTID is not a common operation.
1518 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001519static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
1521 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001522 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001524 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1525 if (clp == NULL)
1526 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001527 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001528 if (clp->cl_name.data == NULL)
1529 goto err_no_name;
1530 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1531 OWNER_HASH_SIZE, GFP_KERNEL);
1532 if (!clp->cl_ownerstr_hashtbl)
1533 goto err_no_hashtbl;
1534 for (i = 0; i < OWNER_HASH_SIZE; i++)
1535 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001536 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001537 INIT_LIST_HEAD(&clp->cl_sessions);
1538 idr_init(&clp->cl_stateids);
1539 atomic_set(&clp->cl_refcount, 0);
1540 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1541 INIT_LIST_HEAD(&clp->cl_idhash);
1542 INIT_LIST_HEAD(&clp->cl_openowners);
1543 INIT_LIST_HEAD(&clp->cl_delegations);
1544 INIT_LIST_HEAD(&clp->cl_lru);
1545 INIT_LIST_HEAD(&clp->cl_callbacks);
1546 INIT_LIST_HEAD(&clp->cl_revoked);
1547 spin_lock_init(&clp->cl_lock);
1548 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001550err_no_hashtbl:
1551 kfree(clp->cl_name.data);
1552err_no_name:
1553 kfree(clp);
1554 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
1556
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001557static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558free_client(struct nfs4_client *clp)
1559{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001560 while (!list_empty(&clp->cl_sessions)) {
1561 struct nfsd4_session *ses;
1562 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1563 se_perclnt);
1564 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001565 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1566 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001567 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001568 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001569 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001570 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001572 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 kfree(clp);
1574}
1575
Benny Halevy84d38ac2010-05-12 00:13:16 +03001576/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001577static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001578unhash_client_locked(struct nfs4_client *clp)
1579{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001580 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001581 struct nfsd4_session *ses;
1582
Trond Myklebust0a880a22014-07-30 08:27:10 -04001583 lockdep_assert_held(&nn->client_lock);
1584
Trond Myklebust4beb3452014-07-30 08:27:02 -04001585 /* Mark the client as expired! */
1586 clp->cl_time = 0;
1587 /* Make it invisible */
1588 if (!list_empty(&clp->cl_idhash)) {
1589 list_del_init(&clp->cl_idhash);
1590 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1591 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1592 else
1593 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1594 }
1595 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001596 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001597 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1598 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001599 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001600}
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001603unhash_client(struct nfs4_client *clp)
1604{
1605 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1606
1607 spin_lock(&nn->client_lock);
1608 unhash_client_locked(clp);
1609 spin_unlock(&nn->client_lock);
1610}
1611
Jeff Layton97403d92014-07-30 08:27:12 -04001612static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1613{
1614 if (atomic_read(&clp->cl_refcount))
1615 return nfserr_jukebox;
1616 unhash_client_locked(clp);
1617 return nfs_ok;
1618}
1619
Trond Myklebust4beb3452014-07-30 08:27:02 -04001620static void
1621__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001623 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 struct list_head reaplist;
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001628 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001629 while (!list_empty(&clp->cl_delegations)) {
1630 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001631 unhash_delegation_locked(dp);
1632 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 }
Benny Halevycdc97502014-05-30 09:09:30 -04001634 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 while (!list_empty(&reaplist)) {
1636 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001637 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001638 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001640 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001641 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001642 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001643 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001644 }
NeilBrownea1da632005-06-23 22:04:17 -07001645 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001646 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Jeff Laytond3134b12014-07-29 21:34:32 -04001647 atomic_inc(&oo->oo_owner.so_count);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001648 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001650 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001651 if (clp->cl_cb_conn.cb_xprt)
1652 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001653 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654}
1655
Trond Myklebust4beb3452014-07-30 08:27:02 -04001656static void
1657destroy_client(struct nfs4_client *clp)
1658{
1659 unhash_client(clp);
1660 __destroy_client(clp);
1661}
1662
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001663static void expire_client(struct nfs4_client *clp)
1664{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001665 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001666 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001667 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001668}
1669
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001670static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1671{
1672 memcpy(target->cl_verifier.data, source->data,
1673 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674}
1675
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001676static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1677{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1679 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1680}
1681
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001682static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001683{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001684 if (source->cr_principal) {
1685 target->cr_principal =
1686 kstrdup(source->cr_principal, GFP_KERNEL);
1687 if (target->cr_principal == NULL)
1688 return -ENOMEM;
1689 } else
1690 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001691 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 target->cr_uid = source->cr_uid;
1693 target->cr_gid = source->cr_gid;
1694 target->cr_group_info = source->cr_group_info;
1695 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001696 target->cr_gss_mech = source->cr_gss_mech;
1697 if (source->cr_gss_mech)
1698 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001699 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
1701
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001702static long long
1703compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1704{
1705 long long res;
1706
1707 res = o1->len - o2->len;
1708 if (res)
1709 return res;
1710 return (long long)memcmp(o1->data, o2->data, o1->len);
1711}
1712
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001713static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001714{
NeilBrowna55370a2005-06-23 22:03:52 -07001715 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716}
1717
1718static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001719same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1720{
1721 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
1724static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001725same_clid(clientid_t *cl1, clientid_t *cl2)
1726{
1727 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001730static bool groups_equal(struct group_info *g1, struct group_info *g2)
1731{
1732 int i;
1733
1734 if (g1->ngroups != g2->ngroups)
1735 return false;
1736 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001737 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001738 return false;
1739 return true;
1740}
1741
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001742/*
1743 * RFC 3530 language requires clid_inuse be returned when the
1744 * "principal" associated with a requests differs from that previously
1745 * used. We use uid, gid's, and gss principal string as our best
1746 * approximation. We also don't want to allow non-gss use of a client
1747 * established using gss: in theory cr_principal should catch that
1748 * change, but in practice cr_principal can be null even in the gss case
1749 * since gssd doesn't always pass down a principal string.
1750 */
1751static bool is_gss_cred(struct svc_cred *cr)
1752{
1753 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1754 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1755}
1756
1757
Vivek Trivedi5559b502012-07-24 21:18:20 +05301758static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001759same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1760{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001761 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001762 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1763 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001764 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1765 return false;
1766 if (cr1->cr_principal == cr2->cr_principal)
1767 return true;
1768 if (!cr1->cr_principal || !cr2->cr_principal)
1769 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301770 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
1772
J. Bruce Fields57266a62013-04-13 14:27:29 -04001773static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1774{
1775 struct svc_cred *cr = &rqstp->rq_cred;
1776 u32 service;
1777
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001778 if (!cr->cr_gss_mech)
1779 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001780 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1781 return service == RPC_GSS_SVC_INTEGRITY ||
1782 service == RPC_GSS_SVC_PRIVACY;
1783}
1784
1785static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1786{
1787 struct svc_cred *cr = &rqstp->rq_cred;
1788
1789 if (!cl->cl_mach_cred)
1790 return true;
1791 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1792 return false;
1793 if (!svc_rqst_integrity_protected(rqstp))
1794 return false;
1795 if (!cr->cr_principal)
1796 return false;
1797 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1798}
1799
Jeff Layton294ac322014-07-30 08:27:15 -04001800static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001801{
Chuck Leverab4684d2012-03-02 17:13:50 -05001802 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Jeff Laytonf4199922014-06-17 07:44:11 -04001804 /*
1805 * This is opaque to client, so no need to byte-swap. Use
1806 * __force to keep sparse happy
1807 */
1808 verf[0] = (__force __be32)get_seconds();
Jeff Layton294ac322014-07-30 08:27:15 -04001809 verf[1] = (__force __be32)nn->clientid_counter;
Chuck Leverab4684d2012-03-02 17:13:50 -05001810 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
Jeff Layton294ac322014-07-30 08:27:15 -04001813static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1814{
1815 clp->cl_clientid.cl_boot = nn->boot_time;
1816 clp->cl_clientid.cl_id = nn->clientid_counter++;
1817 gen_confirm(clp, nn);
1818}
1819
Jeff Layton4770d722014-07-29 21:34:10 -04001820static struct nfs4_stid *
1821find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001822{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001823 struct nfs4_stid *ret;
1824
1825 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1826 if (!ret || !ret->sc_type)
1827 return NULL;
1828 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001829}
1830
Jeff Layton4770d722014-07-29 21:34:10 -04001831static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001832find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001833{
1834 struct nfs4_stid *s;
1835
Jeff Layton4770d722014-07-29 21:34:10 -04001836 spin_lock(&cl->cl_lock);
1837 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001838 if (s != NULL) {
1839 if (typemask & s->sc_type)
1840 atomic_inc(&s->sc_count);
1841 else
1842 s = NULL;
1843 }
Jeff Layton4770d722014-07-29 21:34:10 -04001844 spin_unlock(&cl->cl_lock);
1845 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001846}
1847
Jeff Layton2216d442012-11-12 15:00:57 -05001848static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001849 struct svc_rqst *rqstp, nfs4_verifier *verf)
1850{
1851 struct nfs4_client *clp;
1852 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001853 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001854 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001855
1856 clp = alloc_client(name);
1857 if (clp == NULL)
1858 return NULL;
1859
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001860 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1861 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001862 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001863 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001864 }
Jeff Layton02e12152014-07-16 10:31:57 -04001865 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001866 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001867 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001868 copy_verf(clp, verf);
1869 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001870 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001871 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001872 return clp;
1873}
1874
NeilBrownfd39ca92005-06-23 22:04:03 -07001875static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001876add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1877{
1878 struct rb_node **new = &(root->rb_node), *parent = NULL;
1879 struct nfs4_client *clp;
1880
1881 while (*new) {
1882 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1883 parent = *new;
1884
1885 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1886 new = &((*new)->rb_left);
1887 else
1888 new = &((*new)->rb_right);
1889 }
1890
1891 rb_link_node(&new_clp->cl_namenode, parent, new);
1892 rb_insert_color(&new_clp->cl_namenode, root);
1893}
1894
1895static struct nfs4_client *
1896find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1897{
1898 long long cmp;
1899 struct rb_node *node = root->rb_node;
1900 struct nfs4_client *clp;
1901
1902 while (node) {
1903 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1904 cmp = compare_blob(&clp->cl_name, name);
1905 if (cmp > 0)
1906 node = node->rb_left;
1907 else if (cmp < 0)
1908 node = node->rb_right;
1909 else
1910 return clp;
1911 }
1912 return NULL;
1913}
1914
1915static void
1916add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
1918 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001919 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Trond Myklebust0a880a22014-07-30 08:27:10 -04001921 lockdep_assert_held(&nn->client_lock);
1922
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001923 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001924 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001926 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001927 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928}
1929
NeilBrownfd39ca92005-06-23 22:04:03 -07001930static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931move_to_confirmed(struct nfs4_client *clp)
1932{
1933 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001934 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Trond Myklebust0a880a22014-07-30 08:27:10 -04001936 lockdep_assert_held(&nn->client_lock);
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001939 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001940 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001941 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001942 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001943 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944}
1945
1946static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001947find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
1949 struct nfs4_client *clp;
1950 unsigned int idhashval = clientid_hashval(clid->cl_id);
1951
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001952 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001953 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001954 if ((bool)clp->cl_minorversion != sessions)
1955 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001956 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 }
1960 return NULL;
1961}
1962
1963static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001964find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1965{
1966 struct list_head *tbl = nn->conf_id_hashtbl;
1967
Trond Myklebust0a880a22014-07-30 08:27:10 -04001968 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001969 return find_client_in_id_table(tbl, clid, sessions);
1970}
1971
1972static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001973find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001975 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Trond Myklebust0a880a22014-07-30 08:27:10 -04001977 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001978 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979}
1980
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001981static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001982{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001983 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001984}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001985
NeilBrown28ce6052005-06-23 22:03:56 -07001986static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001987find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001988{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001989 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001990 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001991}
1992
1993static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001994find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001995{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001996 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001997 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001998}
1999
NeilBrownfd39ca92005-06-23 22:04:03 -07002000static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002001gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002003 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002004 struct sockaddr *sa = svc_addr(rqstp);
2005 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002006 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Jeff Layton7077ecb2009-08-14 12:57:58 -04002008 /* Currently, we only support tcp and tcp6 for the callback channel */
2009 if (se->se_callback_netid_len == 3 &&
2010 !memcmp(se->se_callback_netid_val, "tcp", 3))
2011 expected_family = AF_INET;
2012 else if (se->se_callback_netid_len == 4 &&
2013 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2014 expected_family = AF_INET6;
2015 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 goto out_err;
2017
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002018 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002019 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002020 (struct sockaddr *)&conn->cb_addr,
2021 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002022
J. Bruce Fields07263f12010-05-31 19:09:40 -04002023 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002025
J. Bruce Fields07263f12010-05-31 19:09:40 -04002026 if (conn->cb_addr.ss_family == AF_INET6)
2027 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002028
J. Bruce Fields07263f12010-05-31 19:09:40 -04002029 conn->cb_prog = se->se_callback_prog;
2030 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002031 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 return;
2033out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002034 conn->cb_addr.ss_family = AF_UNSPEC;
2035 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002036 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 "will not receive delegations\n",
2038 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 return;
2041}
2042
Andy Adamson074fe892009-04-03 08:28:15 +03002043/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002044 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002045 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002046static void
Andy Adamson074fe892009-04-03 08:28:15 +03002047nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2048{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002049 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002050 struct nfsd4_slot *slot = resp->cstate.slot;
2051 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002052
Andy Adamson557ce262009-08-28 08:45:04 -04002053 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002054
Andy Adamson557ce262009-08-28 08:45:04 -04002055 slot->sl_opcnt = resp->opcnt;
2056 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002057
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002058 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002059 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002060 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002061 return;
2062 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002063 base = resp->cstate.data_offset;
2064 slot->sl_datalen = buf->len - base;
2065 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04002066 WARN("%s: sessions DRC could not cache compound\n", __func__);
2067 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002068}
2069
2070/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002071 * Encode the replay sequence operation from the slot values.
2072 * If cachethis is FALSE encode the uncached rep error on the next
2073 * operation which sets resp->p and increments resp->opcnt for
2074 * nfs4svc_encode_compoundres.
2075 *
Andy Adamson074fe892009-04-03 08:28:15 +03002076 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002077static __be32
2078nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2079 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002080{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002081 struct nfsd4_op *op;
2082 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002083
Andy Adamsonabfabf82009-07-23 19:02:18 -04002084 /* Encode the replayed sequence operation */
2085 op = &args->ops[resp->opcnt - 1];
2086 nfsd4_encode_operation(resp, op);
2087
2088 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002089 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002090 op = &args->ops[resp->opcnt++];
2091 op->status = nfserr_retry_uncached_rep;
2092 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002093 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002094 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002095}
2096
2097/*
Andy Adamson557ce262009-08-28 08:45:04 -04002098 * The sequence operation is not cached because we can use the slot and
2099 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002100 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002101static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002102nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2103 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002104{
Andy Adamson557ce262009-08-28 08:45:04 -04002105 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002106 struct xdr_stream *xdr = &resp->xdr;
2107 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002108 __be32 status;
2109
Andy Adamson557ce262009-08-28 08:45:04 -04002110 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002111
Andy Adamsonabfabf82009-07-23 19:02:18 -04002112 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002113 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002114 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002115
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002116 p = xdr_reserve_space(xdr, slot->sl_datalen);
2117 if (!p) {
2118 WARN_ON_ONCE(1);
2119 return nfserr_serverfault;
2120 }
2121 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2122 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002123
Andy Adamson557ce262009-08-28 08:45:04 -04002124 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002125 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002126}
2127
Andy Adamson0733d212009-04-03 08:28:01 +03002128/*
2129 * Set the exchange_id flags returned by the server.
2130 */
2131static void
2132nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2133{
2134 /* pNFS is not supported */
2135 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2136
2137 /* Referrals are supported, Migration is not. */
2138 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2139
2140 /* set the wire flags to return to client. */
2141 clid->flags = new->cl_exchange_flags;
2142}
2143
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002144static bool client_has_state(struct nfs4_client *clp)
2145{
2146 /*
2147 * Note clp->cl_openowners check isn't quite right: there's no
2148 * need to count owners without stateid's.
2149 *
2150 * Also note we should probably be using this in 4.0 case too.
2151 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002152 return !list_empty(&clp->cl_openowners)
2153 || !list_empty(&clp->cl_delegations)
2154 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002155}
2156
Al Virob37ad282006-10-19 23:28:59 -07002157__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002158nfsd4_exchange_id(struct svc_rqst *rqstp,
2159 struct nfsd4_compound_state *cstate,
2160 struct nfsd4_exchange_id *exid)
2161{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002162 struct nfs4_client *conf, *new;
2163 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002164 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002165 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002166 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002167 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002168 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002169 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002170
Jeff Layton363168b2009-08-14 12:57:56 -04002171 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002172 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002173 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002174 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002175 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002176
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002177 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002178 return nfserr_inval;
2179
Andy Adamson0733d212009-04-03 08:28:01 +03002180 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002181 case SP4_MACH_CRED:
2182 if (!svc_rqst_integrity_protected(rqstp))
2183 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002184 case SP4_NONE:
2185 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002186 default: /* checked by xdr code */
2187 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002188 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002189 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002190 }
2191
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002192 new = create_client(exid->clname, rqstp, &verf);
2193 if (new == NULL)
2194 return nfserr_jukebox;
2195
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002196 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002197 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002198 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002199 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002200 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2201 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2202
J. Bruce Fields136e6582012-05-12 20:37:23 -04002203 if (update) {
2204 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002205 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002206 goto out;
2207 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002208 if (!mach_creds_match(conf, rqstp)) {
2209 status = nfserr_wrong_cred;
2210 goto out;
2211 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002212 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002213 status = nfserr_perm;
2214 goto out;
2215 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002216 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002217 status = nfserr_not_same;
2218 goto out;
2219 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002220 /* case 6 */
2221 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002222 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002223 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002224 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002225 if (client_has_state(conf)) {
2226 status = nfserr_clid_inuse;
2227 goto out;
2228 }
Andy Adamson0733d212009-04-03 08:28:01 +03002229 goto out_new;
2230 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002231 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002232 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002233 goto out_copy;
2234 }
2235 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002236 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002237 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002238 }
2239
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002240 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002241 status = nfserr_noent;
2242 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002243 }
2244
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002245 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002246 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002247 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002248
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002249 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002250out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002251 if (conf) {
2252 status = mark_client_expired_locked(conf);
2253 if (status)
2254 goto out;
2255 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002256 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002257 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002258
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002259 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002260 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002261 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002262out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002263 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2264 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002265
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002266 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2267 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002268
2269 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002270 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002271 status = nfs_ok;
2272
2273out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002274 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002275 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002276 expire_client(new);
2277 if (unconf)
2278 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002279 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002280}
2281
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002282static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002283check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002284{
Andy Adamson88e588d2009-07-23 19:02:15 -04002285 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2286 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002287
2288 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002289 if (slot_inuse) {
2290 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002291 return nfserr_jukebox;
2292 else
2293 return nfserr_seq_misordered;
2294 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002295 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002296 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002297 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002298 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002299 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002300 return nfserr_seq_misordered;
2301}
2302
Andy Adamson49557cc2009-07-23 19:02:16 -04002303/*
2304 * Cache the create session result into the create session single DRC
2305 * slot cache by saving the xdr structure. sl_seqid has been set.
2306 * Do this for solo or embedded create session operations.
2307 */
2308static void
2309nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002310 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002311{
2312 slot->sl_status = nfserr;
2313 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2314}
2315
2316static __be32
2317nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2318 struct nfsd4_clid_slot *slot)
2319{
2320 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2321 return slot->sl_status;
2322}
2323
Mi Jinlong1b74c252011-07-14 14:50:17 +08002324#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2325 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2326 1 + /* MIN tag is length with zero, only length */ \
2327 3 + /* version, opcount, opcode */ \
2328 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2329 /* seqid, slotID, slotID, cache */ \
2330 4 ) * sizeof(__be32))
2331
2332#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2333 2 + /* verifier: AUTH_NULL, length 0 */\
2334 1 + /* status */ \
2335 1 + /* MIN tag is length with zero, only length */ \
2336 3 + /* opcount, opcode, opstatus*/ \
2337 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2338 /* seqid, slotID, slotID, slotID, status */ \
2339 5 ) * sizeof(__be32))
2340
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002341static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002342{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002343 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2344
J. Bruce Fields373cd402013-04-08 15:42:12 -04002345 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2346 return nfserr_toosmall;
2347 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2348 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002349 ca->headerpadsz = 0;
2350 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2351 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2352 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2353 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2354 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2355 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2356 /*
2357 * Note decreasing slot size below client's request may make it
2358 * difficult for client to function correctly, whereas
2359 * decreasing the number of slots will (just?) affect
2360 * performance. When short on memory we therefore prefer to
2361 * decrease number of slots instead of their size. Clients that
2362 * request larger slots than they need will get poor results:
2363 */
2364 ca->maxreqs = nfsd4_get_drc_mem(ca);
2365 if (!ca->maxreqs)
2366 return nfserr_jukebox;
2367
J. Bruce Fields373cd402013-04-08 15:42:12 -04002368 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002369}
2370
Kinglong Mee8a891632013-12-23 18:11:02 +08002371#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2372 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2373#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2374 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2375
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002376static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2377{
2378 ca->headerpadsz = 0;
2379
2380 /*
2381 * These RPC_MAX_HEADER macros are overkill, especially since we
2382 * don't even do gss on the backchannel yet. But this is still
2383 * less than 1k. Tighten up this estimate in the unlikely event
2384 * it turns out to be a problem for some client:
2385 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002386 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002387 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002388 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002389 return nfserr_toosmall;
2390 ca->maxresp_cached = 0;
2391 if (ca->maxops < 2)
2392 return nfserr_toosmall;
2393
2394 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002395}
2396
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002397static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2398{
2399 switch (cbs->flavor) {
2400 case RPC_AUTH_NULL:
2401 case RPC_AUTH_UNIX:
2402 return nfs_ok;
2403 default:
2404 /*
2405 * GSS case: the spec doesn't allow us to return this
2406 * error. But it also doesn't allow us not to support
2407 * GSS.
2408 * I'd rather this fail hard than return some error the
2409 * client might think it can already handle:
2410 */
2411 return nfserr_encr_alg_unsupp;
2412 }
2413}
2414
Andy Adamson069b6ad2009-04-03 08:27:58 +03002415__be32
2416nfsd4_create_session(struct svc_rqst *rqstp,
2417 struct nfsd4_compound_state *cstate,
2418 struct nfsd4_create_session *cr_ses)
2419{
Jeff Layton363168b2009-08-14 12:57:56 -04002420 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002421 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002422 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002423 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002424 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002425 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002426 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002427 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002428
Mi Jinlonga62573d2011-03-23 17:57:07 +08002429 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2430 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002431 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2432 if (status)
2433 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002434 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002435 if (status)
2436 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002437 status = check_backchannel_attrs(&cr_ses->back_channel);
2438 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002439 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002440 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002441 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002442 if (!new)
2443 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002444 conn = alloc_conn_from_crses(rqstp, cr_ses);
2445 if (!conn)
2446 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002447
Jeff Laytond20c11d2014-07-30 08:27:07 -04002448 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002449 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002450 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002451 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002452
2453 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002454 status = nfserr_wrong_cred;
2455 if (!mach_creds_match(conf, rqstp))
2456 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002457 cs_slot = &conf->cl_cs_slot;
2458 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002459 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002460 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002461 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002462 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002463 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002464 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002465 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002466 } else if (unconf) {
2467 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002468 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002469 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002470 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002471 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002472 status = nfserr_wrong_cred;
2473 if (!mach_creds_match(unconf, rqstp))
2474 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002475 cs_slot = &unconf->cl_cs_slot;
2476 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002477 if (status) {
2478 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002479 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002480 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002481 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002482 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002483 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002484 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002485 if (status) {
2486 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002487 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002488 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002489 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002490 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002491 conf = unconf;
2492 } else {
2493 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002494 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002495 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002496 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002497 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002498 * We do not support RDMA or persistent sessions
2499 */
2500 cr_ses->flags &= ~SESSION4_PERSIST;
2501 cr_ses->flags &= ~SESSION4_RDMA;
2502
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002503 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002504 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002505
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002506 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002507 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002508 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002509 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002510
Jeff Laytond20c11d2014-07-30 08:27:07 -04002511 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002512 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002513 spin_unlock(&nn->client_lock);
2514 /* init connection and backchannel */
2515 nfsd4_init_conn(rqstp, conn, new);
2516 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002517 if (old)
2518 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002519 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002520out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002521 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002522 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002523 if (old)
2524 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002525out_free_session:
2526 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002527out_release_drc_mem:
2528 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002529 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002530}
2531
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002532static __be32 nfsd4_map_bcts_dir(u32 *dir)
2533{
2534 switch (*dir) {
2535 case NFS4_CDFC4_FORE:
2536 case NFS4_CDFC4_BACK:
2537 return nfs_ok;
2538 case NFS4_CDFC4_FORE_OR_BOTH:
2539 case NFS4_CDFC4_BACK_OR_BOTH:
2540 *dir = NFS4_CDFC4_BOTH;
2541 return nfs_ok;
2542 };
2543 return nfserr_inval;
2544}
2545
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002546__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2547{
2548 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002549 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002550 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002551
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002552 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2553 if (status)
2554 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002555 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002556 session->se_cb_prog = bc->bc_cb_program;
2557 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002558 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002559
2560 nfsd4_probe_callback(session->se_client);
2561
2562 return nfs_ok;
2563}
2564
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002565__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2566 struct nfsd4_compound_state *cstate,
2567 struct nfsd4_bind_conn_to_session *bcts)
2568{
2569 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002570 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002571 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002572 struct net *net = SVC_NET(rqstp);
2573 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002574
2575 if (!nfsd4_last_compound_op(rqstp))
2576 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002577 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002578 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002579 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002580 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002581 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002582 status = nfserr_wrong_cred;
2583 if (!mach_creds_match(session->se_client, rqstp))
2584 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002585 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002586 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002587 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002588 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002589 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002590 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002591 goto out;
2592 nfsd4_init_conn(rqstp, conn, session);
2593 status = nfs_ok;
2594out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002595 nfsd4_put_session(session);
2596out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002597 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002598}
2599
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002600static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2601{
2602 if (!session)
2603 return 0;
2604 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2605}
2606
Andy Adamson069b6ad2009-04-03 08:27:58 +03002607__be32
2608nfsd4_destroy_session(struct svc_rqst *r,
2609 struct nfsd4_compound_state *cstate,
2610 struct nfsd4_destroy_session *sessionid)
2611{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002612 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002613 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002614 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002615 struct net *net = SVC_NET(r);
2616 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002617
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002618 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002619 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002620 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002621 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002622 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002623 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002624 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002625 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002626 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002627 if (!ses)
2628 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002629 status = nfserr_wrong_cred;
2630 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002631 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002632 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002633 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002634 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002635 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002636 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002637
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002638 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002639
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002640 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002641 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002642out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002643 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002644out_client_lock:
2645 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002646out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03002647 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002648}
2649
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002650static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002651{
2652 struct nfsd4_conn *c;
2653
2654 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002655 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002656 return c;
2657 }
2658 }
2659 return NULL;
2660}
2661
J. Bruce Fields57266a62013-04-13 14:27:29 -04002662static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002663{
2664 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002665 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002666 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002667 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002668
2669 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002670 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002671 if (c)
2672 goto out_free;
2673 status = nfserr_conn_not_bound_to_session;
2674 if (clp->cl_mach_cred)
2675 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002676 __nfsd4_hash_conn(new, ses);
2677 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002678 ret = nfsd4_register_conn(new);
2679 if (ret)
2680 /* oops; xprt is already down: */
2681 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002682 return nfs_ok;
2683out_free:
2684 spin_unlock(&clp->cl_lock);
2685 free_conn(new);
2686 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002687}
2688
Mi Jinlong868b89c2011-04-27 09:09:58 +08002689static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2690{
2691 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2692
2693 return args->opcnt > session->se_fchannel.maxops;
2694}
2695
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002696static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2697 struct nfsd4_session *session)
2698{
2699 struct xdr_buf *xb = &rqstp->rq_arg;
2700
2701 return xb->len > session->se_fchannel.maxreq_sz;
2702}
2703
Andy Adamson069b6ad2009-04-03 08:27:58 +03002704__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002705nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002706 struct nfsd4_compound_state *cstate,
2707 struct nfsd4_sequence *seq)
2708{
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002709 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002710 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002711 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002712 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002713 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002714 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002715 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002716 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002717 struct net *net = SVC_NET(rqstp);
2718 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002719
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002720 if (resp->opcnt != 1)
2721 return nfserr_sequence_pos;
2722
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002723 /*
2724 * Will be either used or freed by nfsd4_sequence_check_conn
2725 * below.
2726 */
2727 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2728 if (!conn)
2729 return nfserr_jukebox;
2730
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002731 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002732 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002733 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002734 goto out_no_session;
2735 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002736
Mi Jinlong868b89c2011-04-27 09:09:58 +08002737 status = nfserr_too_many_ops;
2738 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002739 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002740
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002741 status = nfserr_req_too_big;
2742 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002743 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002744
Benny Halevyb85d4c02009-04-03 08:28:08 +03002745 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002746 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002747 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002748
Andy Adamson557ce262009-08-28 08:45:04 -04002749 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002750 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2751
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002752 /* We do not negotiate the number of slots yet, so set the
2753 * maxslots to the session maxreqs which is used to encode
2754 * sr_highest_slotid and the sr_target_slot id to maxslots */
2755 seq->maxslots = session->se_fchannel.maxreqs;
2756
J. Bruce Fields73e79482012-02-13 16:39:00 -05002757 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2758 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002759 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002760 status = nfserr_seq_misordered;
2761 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002762 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002763 cstate->slot = slot;
2764 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002765 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002766 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002767 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002768 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002769 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002770 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002771 }
2772 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002773 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002774
J. Bruce Fields57266a62013-04-13 14:27:29 -04002775 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002776 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002777 if (status)
2778 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002779
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002780 buflen = (seq->cachethis) ?
2781 session->se_fchannel.maxresp_cached :
2782 session->se_fchannel.maxresp_sz;
2783 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2784 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002785 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002786 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002787 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002788
2789 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002790 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002791 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002792 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002793 if (seq->cachethis)
2794 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002795 else
2796 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002797
2798 cstate->slot = slot;
2799 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002800 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002801
Benny Halevyb85d4c02009-04-03 08:28:08 +03002802out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002803 switch (clp->cl_cb_state) {
2804 case NFSD4_CB_DOWN:
2805 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2806 break;
2807 case NFSD4_CB_FAULT:
2808 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2809 break;
2810 default:
2811 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002812 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002813 if (!list_empty(&clp->cl_revoked))
2814 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002815out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002816 if (conn)
2817 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002818 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002819 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002820out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002821 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002822 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002823}
2824
Trond Myklebustb6076642014-06-30 11:48:35 -04002825void
2826nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2827{
2828 struct nfsd4_compound_state *cs = &resp->cstate;
2829
2830 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002831 if (cs->status != nfserr_replay_cache) {
2832 nfsd4_store_cache_entry(resp);
2833 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2834 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002835 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002836 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002837 } else if (cs->clp)
2838 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002839}
2840
Mi Jinlong345c2842011-10-20 17:51:39 +08002841__be32
2842nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2843{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002844 struct nfs4_client *conf, *unconf;
2845 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002846 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002847 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002848
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002849 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002850 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002851 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002852 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002853
2854 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002855 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002856 status = nfserr_clientid_busy;
2857 goto out;
2858 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04002859 status = mark_client_expired_locked(conf);
2860 if (status)
2861 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002862 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08002863 } else if (unconf)
2864 clp = unconf;
2865 else {
2866 status = nfserr_stale_clientid;
2867 goto out;
2868 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002869 if (!mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002870 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002871 status = nfserr_wrong_cred;
2872 goto out;
2873 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002874 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002875out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002876 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002877 if (clp)
2878 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002879 return status;
2880}
2881
Andy Adamson069b6ad2009-04-03 08:27:58 +03002882__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002883nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2884{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002885 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002886
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002887 if (rc->rca_one_fs) {
2888 if (!cstate->current_fh.fh_dentry)
2889 return nfserr_nofilehandle;
2890 /*
2891 * We don't take advantage of the rca_one_fs case.
2892 * That's OK, it's optional, we can safely ignore it.
2893 */
2894 return nfs_ok;
2895 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002896
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002897 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002898 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2899 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002900 goto out;
2901
2902 status = nfserr_stale_clientid;
2903 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002904 /*
2905 * The following error isn't really legal.
2906 * But we only get here if the client just explicitly
2907 * destroyed the client. Surely it no longer cares what
2908 * error it gets back on an operation for the dead
2909 * client.
2910 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002911 goto out;
2912
2913 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002914 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002915out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002916 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002917}
2918
2919__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002920nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2921 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002923 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002925 struct nfs4_client *conf, *new;
2926 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002927 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002928 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2929
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002930 new = create_client(clname, rqstp, &clverifier);
2931 if (new == NULL)
2932 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002933 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002934 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002935 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002936 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002937 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002939 if (clp_used_exchangeid(conf))
2940 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002941 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002942 char addr_str[INET6_ADDRSTRLEN];
2943 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2944 sizeof(addr_str));
2945 dprintk("NFSD: setclientid: string in use by client "
2946 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 goto out;
2948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002950 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002951 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002952 unhash_client_locked(unconf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002953 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002954 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002956 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002957 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002958 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002959 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002960 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2962 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2963 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002964 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 status = nfs_ok;
2966out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002967 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002968 if (new)
2969 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002970 if (unconf)
2971 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 return status;
2973}
2974
2975
Al Virob37ad282006-10-19 23:28:59 -07002976__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002977nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2978 struct nfsd4_compound_state *cstate,
2979 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980{
NeilBrown21ab45a2005-06-23 22:04:14 -07002981 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002982 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2984 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002985 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002986 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002988 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07002990
Jeff Laytond20c11d2014-07-30 08:27:07 -04002991 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002992 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002993 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002994 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002995 * We try hard to give out unique clientid's, so if we get an
2996 * attempt to confirm the same clientid with a different cred,
2997 * there's a bug somewhere. Let's charitably assume it's our
2998 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002999 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04003000 status = nfserr_serverfault;
3001 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3002 goto out;
3003 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3004 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003005 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003006 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3007 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003009 else /* case 4: client hasn't noticed we rebooted yet? */
3010 status = nfserr_stale_clientid;
3011 goto out;
3012 }
3013 status = nfs_ok;
3014 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003015 old = unconf;
3016 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003017 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003018 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003019 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3020 if (old) {
3021 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003022 if (status) {
3023 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003024 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003025 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003026 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003027 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003028 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003029 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003030 get_client_locked(conf);
3031 spin_unlock(&nn->client_lock);
3032 nfsd4_probe_callback(conf);
3033 spin_lock(&nn->client_lock);
3034 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003036 spin_unlock(&nn->client_lock);
3037 if (old)
3038 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 return status;
3040}
3041
J. Bruce Fields32513b42011-10-13 16:00:16 -04003042static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003044 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3045}
3046
3047/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003048static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003049{
Trond Myklebustca943212014-07-23 16:17:39 -04003050 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Trond Myklebust950e0112014-06-30 11:48:31 -04003052 lockdep_assert_held(&state_lock);
3053
J. Bruce Fields32513b42011-10-13 16:00:16 -04003054 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003055 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003056 INIT_LIST_HEAD(&fp->fi_stateids);
3057 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003058 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003059 fp->fi_had_conflict = false;
3060 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003061 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003062 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3063 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04003064 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065}
3066
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003067void
NeilBrowne60d4392005-06-23 22:03:01 -07003068nfsd4_free_slabs(void)
3069{
Christoph Hellwigabf11352014-05-21 07:43:03 -07003070 kmem_cache_destroy(openowner_slab);
3071 kmem_cache_destroy(lockowner_slab);
3072 kmem_cache_destroy(file_slab);
3073 kmem_cache_destroy(stateid_slab);
3074 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003075}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Bryan Schumaker72083392011-11-01 15:24:59 -04003077int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078nfsd4_init_slabs(void)
3079{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003080 openowner_slab = kmem_cache_create("nfsd4_openowners",
3081 sizeof(struct nfs4_openowner), 0, 0, NULL);
3082 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003083 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003084 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003085 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003086 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003087 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003088 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003089 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003090 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003091 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003092 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003093 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003094 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003095 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003096 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003097 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003098 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003099 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003101
3102out_free_stateid_slab:
3103 kmem_cache_destroy(stateid_slab);
3104out_free_file_slab:
3105 kmem_cache_destroy(file_slab);
3106out_free_lockowner_slab:
3107 kmem_cache_destroy(lockowner_slab);
3108out_free_openowner_slab:
3109 kmem_cache_destroy(openowner_slab);
3110out:
NeilBrowne60d4392005-06-23 22:03:01 -07003111 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3112 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113}
3114
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003115static void init_nfs4_replay(struct nfs4_replay *rp)
3116{
3117 rp->rp_status = nfserr_serverfault;
3118 rp->rp_buflen = 0;
3119 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003120 mutex_init(&rp->rp_mutex);
3121}
3122
3123static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3124 struct nfs4_stateowner *so)
3125{
3126 if (!nfsd4_has_session(cstate)) {
3127 mutex_lock(&so->so_replay.rp_mutex);
3128 cstate->replay_owner = so;
3129 atomic_inc(&so->so_count);
3130 }
3131}
3132
3133void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3134{
3135 struct nfs4_stateowner *so = cstate->replay_owner;
3136
3137 if (so != NULL) {
3138 cstate->replay_owner = NULL;
3139 mutex_unlock(&so->so_replay.rp_mutex);
3140 nfs4_put_stateowner(so);
3141 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003142}
3143
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003144static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145{
3146 struct nfs4_stateowner *sop;
3147
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003148 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003149 if (!sop)
3150 return NULL;
3151
3152 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3153 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003154 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003155 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003157 sop->so_owner.len = owner->len;
3158
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003159 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003160 sop->so_client = clp;
3161 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003162 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003163 return sop;
3164}
3165
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003166static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003167{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003168 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003169
Trond Myklebustd4f04892014-07-29 21:34:36 -04003170 list_add(&oo->oo_owner.so_strhash,
3171 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003172 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173}
3174
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003175static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3176{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003177 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003178}
3179
Jeff Layton6b180f02014-07-29 21:34:26 -04003180static void nfs4_free_openowner(struct nfs4_stateowner *so)
3181{
3182 struct nfs4_openowner *oo = openowner(so);
3183
3184 kmem_cache_free(openowner_slab, oo);
3185}
3186
3187static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003188 .so_unhash = nfs4_unhash_openowner,
3189 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003190};
3191
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003192static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003193alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003194 struct nfsd4_compound_state *cstate)
3195{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003196 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003197 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003199 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3200 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003202 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003203 oo->oo_owner.so_is_open_owner = 1;
3204 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003205 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003206 if (nfsd4_has_session(cstate))
3207 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003208 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003209 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003210 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003211 spin_lock(&clp->cl_lock);
3212 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003213 if (ret == NULL) {
3214 hash_openowner(oo, clp, strhashval);
3215 ret = oo;
3216 } else
3217 nfs4_free_openowner(&oo->oo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003218 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003219 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220}
3221
J. Bruce Fields996e0932011-10-17 11:14:48 -04003222static 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 -04003223 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003225 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003226 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003227 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003228 stp->st_stateowner = &oo->oo_owner;
Jeff Laytond3134b12014-07-29 21:34:32 -04003229 atomic_inc(&stp->st_stateowner->so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07003230 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003231 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 stp->st_access_bmap = 0;
3233 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003234 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003235 spin_lock(&oo->oo_owner.so_client->cl_lock);
3236 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003237 spin_lock(&fp->fi_lock);
3238 list_add(&stp->st_perfile, &fp->fi_stateids);
3239 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003240 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241}
3242
Jeff Laytond3134b12014-07-29 21:34:32 -04003243/*
3244 * In the 4.0 case we need to keep the owners around a little while to handle
3245 * CLOSE replay. We still do need to release any file access that is held by
3246 * them before returning however.
3247 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003249move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
Jeff Layton217526e2014-07-30 08:27:11 -04003251 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003252 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3253 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3254 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003255
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003256 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
Jeff Laytonb401be222014-07-29 21:34:33 -04003258 /*
3259 * We know that we hold one reference via nfsd4_close, and another
3260 * "persistent" reference for the client. If the refcount is higher
3261 * than 2, then there are still calls in progress that are using this
3262 * stateid. We can't put the sc_file reference until they are finished.
3263 * Wait for the refcount to drop to 2. Since it has been unhashed,
3264 * there should be no danger of the refcount going back up again at
3265 * this point.
3266 */
3267 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3268
Jeff Laytond3134b12014-07-29 21:34:32 -04003269 release_all_access(s);
3270 if (s->st_stid.sc_file) {
3271 put_nfs4_file(s->st_stid.sc_file);
3272 s->st_stid.sc_file = NULL;
3273 }
Jeff Layton217526e2014-07-30 08:27:11 -04003274
3275 spin_lock(&nn->client_lock);
3276 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003277 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003278 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003279 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003280 spin_unlock(&nn->client_lock);
3281 if (last)
3282 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283}
3284
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285/* search file_hashtbl[] for file */
3286static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003287find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288{
Trond Myklebustca943212014-07-23 16:17:39 -04003289 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 struct nfs4_file *fp;
3291
Trond Myklebust950e0112014-06-30 11:48:31 -04003292 lockdep_assert_held(&state_lock);
3293
Jeff Layton89876f82013-04-02 09:01:59 -04003294 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003295 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003296 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 }
3300 return NULL;
3301}
3302
Trond Myklebust950e0112014-06-30 11:48:31 -04003303static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003304find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003305{
3306 struct nfs4_file *fp;
3307
3308 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003309 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003310 spin_unlock(&state_lock);
3311 return fp;
3312}
3313
3314static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003315find_or_add_file(struct nfs4_file *new, 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 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003322 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003323 fp = new;
3324 }
3325 spin_unlock(&state_lock);
3326
3327 return fp;
3328}
3329
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003330/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 * Called to check deny when READ with all zero stateid or
3332 * WRITE with all zero or all one stateid
3333 */
Al Virob37ad282006-10-19 23:28:59 -07003334static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3336{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003338 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339
Trond Myklebustca943212014-07-23 16:17:39 -04003340 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003341 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003342 return ret;
3343 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003344 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003345 if (fp->fi_share_deny & deny_type)
3346 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003347 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003348 put_nfs4_file(fp);
3349 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350}
3351
Jeff Layton02e12152014-07-16 10:31:57 -04003352void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353{
Trond Myklebust11b91642014-07-29 21:34:08 -04003354 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3355 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003356
Trond Myklebust11b91642014-07-29 21:34:08 -04003357 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003358
Jeff Layton02e12152014-07-16 10:31:57 -04003359 /*
3360 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003361 * already holding inode->i_lock.
3362 *
Jeff Laytondff13992014-07-08 14:02:49 -04003363 * If the dl_time != 0, then we know that it has already been
3364 * queued for a lease break. Don't queue it again.
3365 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003366 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003367 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003368 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003369 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003370 }
Jeff Layton02e12152014-07-16 10:31:57 -04003371 spin_unlock(&state_lock);
3372}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373
Jeff Layton02e12152014-07-16 10:31:57 -04003374static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3375{
3376 /*
3377 * We're assuming the state code never drops its reference
3378 * without first removing the lease. Since we're in this lease
3379 * callback (and since the lease code is serialized by the kernel
3380 * lock) we know the server hasn't removed the lease yet, we know
3381 * it's safe to take a reference.
3382 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003383 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003384 nfsd4_cb_recall(dp);
3385}
3386
Jeff Layton1c8c6012013-06-21 08:58:15 -04003387/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003388static void nfsd_break_deleg_cb(struct file_lock *fl)
3389{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003390 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3391 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003392
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003393 if (!fp) {
3394 WARN(1, "(%p)->fl_owner NULL\n", fl);
3395 return;
3396 }
3397 if (fp->fi_had_conflict) {
3398 WARN(1, "duplicate break on %p\n", fp);
3399 return;
3400 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003401 /*
3402 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003403 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003404 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003405 */
3406 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
Jeff Layton02e12152014-07-16 10:31:57 -04003408 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003409 fp->fi_had_conflict = true;
3410 /*
3411 * If there are no delegations on the list, then we can't count on this
3412 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3413 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3414 * true should keep any new delegations from being hashed.
3415 */
3416 if (list_empty(&fp->fi_delegations))
3417 fl->fl_break_time = jiffies;
3418 else
3419 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3420 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003421 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422}
3423
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424static
3425int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3426{
3427 if (arg & F_UNLCK)
3428 return lease_modify(onlist, arg);
3429 else
3430 return -EAGAIN;
3431}
3432
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003433static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003434 .lm_break = nfsd_break_deleg_cb,
3435 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436};
3437
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003438static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3439{
3440 if (nfsd4_has_session(cstate))
3441 return nfs_ok;
3442 if (seqid == so->so_seqid - 1)
3443 return nfserr_replay_me;
3444 if (seqid == so->so_seqid)
3445 return nfs_ok;
3446 return nfserr_bad_seqid;
3447}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
Jeff Layton4b24ca72014-06-30 11:48:44 -04003449static __be32 lookup_clientid(clientid_t *clid,
3450 struct nfsd4_compound_state *cstate,
3451 struct nfsd_net *nn)
3452{
3453 struct nfs4_client *found;
3454
3455 if (cstate->clp) {
3456 found = cstate->clp;
3457 if (!same_clid(&found->cl_clientid, clid))
3458 return nfserr_stale_clientid;
3459 return nfs_ok;
3460 }
3461
3462 if (STALE_CLIENTID(clid, nn))
3463 return nfserr_stale_clientid;
3464
3465 /*
3466 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3467 * cached already then we know this is for is for v4.0 and "sessions"
3468 * will be false.
3469 */
3470 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003471 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003472 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003473 if (!found) {
3474 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003475 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003476 }
3477 atomic_inc(&found->cl_refcount);
3478 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003479
3480 /* Cache the nfs4_client in cstate! */
3481 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003482 return nfs_ok;
3483}
3484
Al Virob37ad282006-10-19 23:28:59 -07003485__be32
Andy Adamson66689582009-04-03 08:28:45 +03003486nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003487 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 clientid_t *clientid = &open->op_clientid;
3490 struct nfs4_client *clp = NULL;
3491 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003492 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003493 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003495 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003497 /*
3498 * In case we need it later, after we've already created the
3499 * file and don't want to risk a further failure:
3500 */
3501 open->op_file = nfsd4_alloc_file();
3502 if (open->op_file == NULL)
3503 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
Trond Myklebust2d91e892014-06-30 11:48:46 -04003505 status = lookup_clientid(clientid, cstate, nn);
3506 if (status)
3507 return status;
3508 clp = cstate->clp;
3509
Trond Myklebustd4f04892014-07-29 21:34:36 -04003510 strhashval = ownerstr_hashval(&open->op_owner);
3511 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003512 open->op_openowner = oo;
3513 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003514 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003516 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003517 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003518 release_openowner(oo);
3519 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003520 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003521 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003522 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3523 if (status)
3524 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003525 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003526new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003527 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003528 if (oo == NULL)
3529 return nfserr_jukebox;
3530 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003531alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003532 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003533 if (!open->op_stp)
3534 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003535 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536}
3537
Al Virob37ad282006-10-19 23:28:59 -07003538static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003539nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3540{
3541 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3542 return nfserr_openmode;
3543 else
3544 return nfs_ok;
3545}
3546
Daniel Mackc47d8322011-05-16 16:38:14 +02003547static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003548{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003549 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3550}
3551
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003552static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003553{
3554 struct nfs4_stid *ret;
3555
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003556 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003557 if (!ret)
3558 return NULL;
3559 return delegstateid(ret);
3560}
3561
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003562static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3563{
3564 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3565 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3566}
3567
Al Virob37ad282006-10-19 23:28:59 -07003568static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003569nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003570 struct nfs4_delegation **dp)
3571{
3572 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003573 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003574 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003575
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003576 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3577 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003578 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003579 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003580 status = nfs4_check_delegmode(deleg, flags);
3581 if (status) {
3582 nfs4_put_stid(&deleg->dl_stid);
3583 goto out;
3584 }
3585 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003586out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003587 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003588 return nfs_ok;
3589 if (status)
3590 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003591 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003592 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003593}
3594
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003595static struct nfs4_ol_stateid *
3596nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003598 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003599 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600
Trond Myklebust1d31a252014-07-10 14:07:25 -04003601 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003602 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 /* ignore lock owners */
3604 if (local->st_stateowner->so_is_open_owner == 0)
3605 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003606 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003607 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003608 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003609 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003612 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003613 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614}
3615
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003616static inline int nfs4_access_to_access(u32 nfs4_access)
3617{
3618 int flags = 0;
3619
3620 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3621 flags |= NFSD_MAY_READ;
3622 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3623 flags |= NFSD_MAY_WRITE;
3624 return flags;
3625}
3626
Al Virob37ad282006-10-19 23:28:59 -07003627static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3629 struct nfsd4_open *open)
3630{
3631 struct iattr iattr = {
3632 .ia_valid = ATTR_SIZE,
3633 .ia_size = 0,
3634 };
3635 if (!open->op_truncate)
3636 return 0;
3637 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003638 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3640}
3641
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003642static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003643 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3644 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003645{
Trond Myklebustde186432014-07-10 14:07:26 -04003646 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003647 __be32 status;
3648 int oflag = nfs4_access_to_omode(open->op_share_access);
3649 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003650 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003651
Trond Myklebustde186432014-07-10 14:07:26 -04003652 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003653
3654 /*
3655 * Are we trying to set a deny mode that would conflict with
3656 * current access?
3657 */
3658 status = nfs4_file_check_deny(fp, open->op_share_deny);
3659 if (status != nfs_ok) {
3660 spin_unlock(&fp->fi_lock);
3661 goto out;
3662 }
3663
3664 /* set access to the file */
3665 status = nfs4_file_get_access(fp, open->op_share_access);
3666 if (status != nfs_ok) {
3667 spin_unlock(&fp->fi_lock);
3668 goto out;
3669 }
3670
3671 /* Set access bits in stateid */
3672 old_access_bmap = stp->st_access_bmap;
3673 set_access(open->op_share_access, stp);
3674
3675 /* Set new deny mask */
3676 old_deny_bmap = stp->st_deny_bmap;
3677 set_deny(open->op_share_deny, stp);
3678 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3679
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003680 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003681 spin_unlock(&fp->fi_lock);
3682 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003683 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003684 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003685 spin_lock(&fp->fi_lock);
3686 if (!fp->fi_fds[oflag]) {
3687 fp->fi_fds[oflag] = filp;
3688 filp = NULL;
3689 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003690 }
Trond Myklebustde186432014-07-10 14:07:26 -04003691 spin_unlock(&fp->fi_lock);
3692 if (filp)
3693 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003694
3695 status = nfsd4_truncate(rqstp, cur_fh, open);
3696 if (status)
3697 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003698out:
3699 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003700out_put_access:
3701 stp->st_access_bmap = old_access_bmap;
3702 nfs4_file_put_access(fp, open->op_share_access);
3703 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3704 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003705}
3706
Al Virob37ad282006-10-19 23:28:59 -07003707static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003708nfs4_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 -07003709{
Al Virob37ad282006-10-19 23:28:59 -07003710 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003711 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003713 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003714 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003715
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003716 /* test and set deny mode */
3717 spin_lock(&fp->fi_lock);
3718 status = nfs4_file_check_deny(fp, open->op_share_deny);
3719 if (status == nfs_ok) {
3720 old_deny_bmap = stp->st_deny_bmap;
3721 set_deny(open->op_share_deny, stp);
3722 fp->fi_share_deny |=
3723 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3724 }
3725 spin_unlock(&fp->fi_lock);
3726
3727 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003730 status = nfsd4_truncate(rqstp, cur_fh, open);
3731 if (status != nfs_ok)
3732 reset_union_bmap_deny(old_deny_bmap, stp);
3733 return status;
3734}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003737nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003739 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740}
3741
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003742/* Should we give out recallable state?: */
3743static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3744{
3745 if (clp->cl_cb_state == NFSD4_CB_UP)
3746 return true;
3747 /*
3748 * In the sessions case, since we don't have to establish a
3749 * separate connection for callbacks, we assume it's OK
3750 * until we hear otherwise:
3751 */
3752 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3753}
3754
Jeff Laytond564fbe2014-07-16 10:31:58 -04003755static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003756{
3757 struct file_lock *fl;
3758
3759 fl = locks_alloc_lock();
3760 if (!fl)
3761 return NULL;
3762 locks_init_lock(fl);
3763 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003764 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003765 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3766 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003767 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003768 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003769 return fl;
3770}
3771
J. Bruce Fields99c41512013-05-21 16:21:25 -04003772static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003773{
Trond Myklebust11b91642014-07-29 21:34:08 -04003774 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003775 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003776 struct file *filp;
3777 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003778
Jeff Laytond564fbe2014-07-16 10:31:58 -04003779 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003780 if (!fl)
3781 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003782 filp = find_readable_file(fp);
3783 if (!filp) {
3784 /* We should always have a readable file here */
3785 WARN_ON_ONCE(1);
3786 return -EBADF;
3787 }
3788 fl->fl_file = filp;
3789 status = vfs_setlease(filp, fl->fl_type, &fl);
3790 if (status) {
3791 locks_free_lock(fl);
3792 goto out_fput;
3793 }
Benny Halevycdc97502014-05-30 09:09:30 -04003794 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003795 spin_lock(&fp->fi_lock);
3796 /* Did the lease get broken before we took the lock? */
3797 status = -EAGAIN;
3798 if (fp->fi_had_conflict)
3799 goto out_unlock;
3800 /* Race breaker */
3801 if (fp->fi_lease) {
3802 status = 0;
3803 atomic_inc(&fp->fi_delegees);
3804 hash_delegation_locked(dp, fp);
3805 goto out_unlock;
3806 }
3807 fp->fi_lease = fl;
3808 fp->fi_deleg_file = filp;
3809 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003810 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003811 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003812 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003813 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003814out_unlock:
3815 spin_unlock(&fp->fi_lock);
3816 spin_unlock(&state_lock);
3817out_fput:
3818 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003819 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003820}
3821
Jeff Layton0b266932014-07-25 07:34:25 -04003822static struct nfs4_delegation *
3823nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3824 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003825{
Jeff Layton0b266932014-07-25 07:34:25 -04003826 int status;
3827 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003828
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003829 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003830 return ERR_PTR(-EAGAIN);
3831
3832 dp = alloc_init_deleg(clp, fh);
3833 if (!dp)
3834 return ERR_PTR(-ENOMEM);
3835
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003836 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003837 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003838 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003839 dp->dl_stid.sc_file = fp;
Jeff Layton417c6622014-07-21 09:34:57 -04003840 if (!fp->fi_lease) {
3841 spin_unlock(&fp->fi_lock);
3842 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003843 status = nfs4_setlease(dp);
3844 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003845 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003846 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003847 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003848 status = -EAGAIN;
3849 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003850 }
Benny Halevy931ee562014-05-30 09:09:27 -04003851 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003852 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003853out_unlock:
3854 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003855 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003856out:
3857 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003858 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003859 return ERR_PTR(status);
3860 }
3861 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003862}
3863
Benny Halevy4aa89132012-02-21 14:16:44 -08003864static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3865{
3866 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3867 if (status == -EAGAIN)
3868 open->op_why_no_deleg = WND4_CONTENTION;
3869 else {
3870 open->op_why_no_deleg = WND4_RESOURCE;
3871 switch (open->op_deleg_want) {
3872 case NFS4_SHARE_WANT_READ_DELEG:
3873 case NFS4_SHARE_WANT_WRITE_DELEG:
3874 case NFS4_SHARE_WANT_ANY_DELEG:
3875 break;
3876 case NFS4_SHARE_WANT_CANCEL:
3877 open->op_why_no_deleg = WND4_CANCELLED;
3878 break;
3879 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003880 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003881 }
3882 }
3883}
3884
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885/*
3886 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003887 *
3888 * Note we don't support write delegations, and won't until the vfs has
3889 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 */
3891static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003892nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3893 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894{
3895 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003896 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3897 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003898 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003899 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003901 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003902 open->op_recall = 0;
3903 switch (open->op_claim_type) {
3904 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003905 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003906 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003907 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3908 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003909 break;
3910 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003911 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003912 /*
3913 * Let's not give out any delegations till everyone's
3914 * had the chance to reclaim theirs....
3915 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003916 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003917 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003918 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003919 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003920 /*
3921 * Also, if the file was opened for write or
3922 * create, there's a good chance the client's
3923 * about to write to it, resulting in an
3924 * immediate recall (since we don't support
3925 * write delegations):
3926 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003927 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003928 goto out_no_deleg;
3929 if (open->op_create == NFS4_OPEN_CREATE)
3930 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003931 break;
3932 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003933 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003934 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003935 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003936 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003937 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003939 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003941 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003942 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003943 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003944 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003945 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003946out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003947 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3948 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003949 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003950 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003951 open->op_recall = 1;
3952 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003953
3954 /* 4.1 client asking for a delegation? */
3955 if (open->op_deleg_want)
3956 nfsd4_open_deleg_none_ext(open, status);
3957 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958}
3959
Benny Halevye27f49c2012-02-21 14:16:54 -08003960static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3961 struct nfs4_delegation *dp)
3962{
3963 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3964 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3965 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3966 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3967 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3968 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3969 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3970 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3971 }
3972 /* Otherwise the client must be confused wanting a delegation
3973 * it already has, therefore we don't return
3974 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3975 */
3976}
3977
Al Virob37ad282006-10-19 23:28:59 -07003978__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3980{
Andy Adamson66689582009-04-03 08:28:45 +03003981 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003982 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003984 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003985 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003986 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 /*
3989 * Lookup file; if found, lookup stateid and check open request,
3990 * and check for delegations in the process of being recalled.
3991 * If not found, create the nfs4_file struct
3992 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003993 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04003994 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04003995 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003996 if (status)
3997 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003998 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004000 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004001 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004002 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004003 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004004 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 }
4006
4007 /*
4008 * OPEN the file, or upgrade an existing OPEN.
4009 * If truncate fails, the OPEN fails.
4010 */
4011 if (stp) {
4012 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004013 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 if (status)
4015 goto out;
4016 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004017 stp = open->op_stp;
4018 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04004019 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004020 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4021 if (status) {
4022 release_open_stateid(stp);
4023 goto out;
4024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004026 update_stateid(&stp->st_stid.sc_stateid);
4027 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028
Benny Halevyd24433c2012-02-16 20:57:17 +02004029 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004030 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4031 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4032 open->op_why_no_deleg = WND4_NOT_WANTED;
4033 goto nodeleg;
4034 }
4035 }
4036
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 /*
4038 * Attempt to hand out a delegation. No error return, because the
4039 * OPEN succeeds even if we fail.
4040 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004041 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004042nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 status = nfs_ok;
4044
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004045 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004046 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004048 /* 4.1 client trying to upgrade/downgrade delegation? */
4049 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004050 open->op_deleg_want)
4051 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004052
NeilBrown13cd2182005-06-23 22:03:10 -07004053 if (fp)
4054 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004055 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05004056 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 /*
4058 * To finish the open response, we just need to set the rflags.
4059 */
4060 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004061 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004062 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004064 if (dp)
4065 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004066 if (stp)
4067 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068
4069 return status;
4070}
4071
Jeff Layton58fb12e2014-07-29 21:34:27 -04004072void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4073 struct nfsd4_open *open, __be32 status)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004074{
4075 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004076 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004077
Jeff Laytond3134b12014-07-29 21:34:32 -04004078 nfsd4_cstate_assign_replay(cstate, so);
4079 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004080 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004081 if (open->op_file)
4082 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004083 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004084 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004085}
4086
Al Virob37ad282006-10-19 23:28:59 -07004087__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004088nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4089 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090{
4091 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004092 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004093 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 dprintk("process_renew(%08x/%08x): starting\n",
4096 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004097 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004098 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004100 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004102 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004103 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 goto out;
4105 status = nfs_ok;
4106out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 return status;
4108}
4109
NeilBrowna76b4312005-06-23 22:04:01 -07004110static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004111nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004112{
Jeff Layton33dcc482012-04-10 11:08:48 -04004113 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004114 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004115 return;
4116
NeilBrowna76b4312005-06-23 22:04:01 -07004117 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004118 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004119 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004120 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004121 /*
4122 * Now that every NFSv4 client has had the chance to recover and
4123 * to see the (possibly new, possibly shorter) lease time, we
4124 * can safely set the next grace time to the current lease time:
4125 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03004126 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07004127}
4128
NeilBrownfd39ca92005-06-23 22:04:03 -07004129static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004130nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131{
4132 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004133 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004135 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004137 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004138 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004141 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004142 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004143 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004144 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 clp = list_entry(pos, struct nfs4_client, cl_lru);
4146 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4147 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004148 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 break;
4150 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004151 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004152 dprintk("NFSD: client in use (clientid %08x)\n",
4153 clp->cl_clientid.cl_id);
4154 continue;
4155 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004156 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004157 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004158 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004159 list_for_each_safe(pos, next, &reaplist) {
4160 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 dprintk("NFSD: purging unused client (clientid %08x)\n",
4162 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004163 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 expire_client(clp);
4165 }
Benny Halevycdc97502014-05-30 09:09:30 -04004166 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004167 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004169 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4170 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004172 t = dp->dl_time - cutoff;
4173 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 break;
4175 }
Jeff Layton42690672014-07-25 07:34:20 -04004176 unhash_delegation_locked(dp);
4177 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 }
Benny Halevycdc97502014-05-30 09:09:30 -04004179 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004180 while (!list_empty(&reaplist)) {
4181 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4182 dl_recall_lru);
4183 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004184 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 }
Jeff Layton217526e2014-07-30 08:27:11 -04004186
4187 spin_lock(&nn->client_lock);
4188 while (!list_empty(&nn->close_lru)) {
4189 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4190 oo_close_lru);
4191 if (time_after((unsigned long)oo->oo_time,
4192 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004193 t = oo->oo_time - cutoff;
4194 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 break;
4196 }
Jeff Layton217526e2014-07-30 08:27:11 -04004197 list_del_init(&oo->oo_close_lru);
4198 stp = oo->oo_last_closed_stid;
4199 oo->oo_last_closed_stid = NULL;
4200 spin_unlock(&nn->client_lock);
4201 nfs4_put_stid(&stp->st_stid);
4202 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 }
Jeff Layton217526e2014-07-30 08:27:11 -04004204 spin_unlock(&nn->client_lock);
4205
Jeff Laytona832e7a2014-05-30 09:09:26 -04004206 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004207 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208}
4209
Harvey Harrisona254b242008-02-20 12:49:00 -08004210static struct workqueue_struct *laundry_wq;
4211static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004212
4213static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004214laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215{
4216 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004217 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4218 work);
4219 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4220 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004222 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004224 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225}
4226
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004227static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004228{
Trond Myklebust11b91642014-07-29 21:34:08 -04004229 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004230 return nfserr_bad_stateid;
4231 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232}
4233
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004235access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004237 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4238 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4239 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240}
4241
4242static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004243access_permit_write(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_WRITE, stp) ||
4246 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247}
4248
4249static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004250__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251{
Al Virob37ad282006-10-19 23:28:59 -07004252 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253
J. Bruce Fields02921912010-07-29 15:16:59 -04004254 /* For lock stateid's, we test the parent open, not the lock: */
4255 if (stp->st_openstp)
4256 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004257 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004259 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 goto out;
4261 status = nfs_ok;
4262out:
4263 return status;
4264}
4265
Al Virob37ad282006-10-19 23:28:59 -07004266static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004267check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004269 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004271 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004272 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 * conflicting state; so we must wait out the grace period. */
4274 return nfserr_grace;
4275 } else if (flags & WR_STATE)
4276 return nfs4_share_conflict(current_fh,
4277 NFS4_SHARE_DENY_WRITE);
4278 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4279 return nfs4_share_conflict(current_fh,
4280 NFS4_SHARE_DENY_READ);
4281}
4282
4283/*
4284 * Allow READ/WRITE during grace period on recovered state only for files
4285 * that are not able to provide mandatory locking.
4286 */
4287static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004288grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004290 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291}
4292
J. Bruce Fields81b82962011-08-23 11:03:29 -04004293/* Returns true iff a is later than b: */
4294static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4295{
Jim Rees1a9357f2013-05-17 17:33:00 -04004296 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004297}
4298
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004299static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004300{
Andy Adamson66689582009-04-03 08:28:45 +03004301 /*
4302 * When sessions are used the stateid generation number is ignored
4303 * when it is zero.
4304 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004305 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004306 return nfs_ok;
4307
4308 if (in->si_generation == ref->si_generation)
4309 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004310
J. Bruce Fields0836f582008-01-26 19:08:12 -05004311 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004312 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004313 return nfserr_bad_stateid;
4314 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004315 * However, we could see a stateid from the past, even from a
4316 * non-buggy client. For example, if the client sends a lock
4317 * while some IO is outstanding, the lock may bump si_generation
4318 * while the IO is still in flight. The client could avoid that
4319 * situation by waiting for responses on all the IO requests,
4320 * but better performance may result in retrying IO that
4321 * receives an old_stateid error if requests are rarely
4322 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004323 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004324 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004325}
4326
Chuck Lever7df302f2012-05-29 13:56:37 -04004327static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004328{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004329 struct nfs4_stid *s;
4330 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004331 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004332
Chuck Lever7df302f2012-05-29 13:56:37 -04004333 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004334 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004335 /* Client debugging aid. */
4336 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4337 char addr_str[INET6_ADDRSTRLEN];
4338 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4339 sizeof(addr_str));
4340 pr_warn_ratelimited("NFSD: client %s testing state ID "
4341 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004342 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004343 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004344 spin_lock(&cl->cl_lock);
4345 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004346 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004347 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004348 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004349 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004350 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004351 switch (s->sc_type) {
4352 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004353 status = nfs_ok;
4354 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004355 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004356 status = nfserr_deleg_revoked;
4357 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004358 case NFS4_OPEN_STID:
4359 case NFS4_LOCK_STID:
4360 ols = openlockstateid(s);
4361 if (ols->st_stateowner->so_is_open_owner
4362 && !(openowner(ols->st_stateowner)->oo_flags
4363 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004364 status = nfserr_bad_stateid;
4365 else
4366 status = nfs_ok;
4367 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004368 default:
4369 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004370 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004371 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004372 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004373 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004374 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004375out_unlock:
4376 spin_unlock(&cl->cl_lock);
4377 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004378}
4379
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004380static __be32
4381nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4382 stateid_t *stateid, unsigned char typemask,
4383 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004384{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004385 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004386
4387 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4388 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004389 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004390 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004391 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004392 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004393 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004394 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004395 if (status)
4396 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004397 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004398 if (!*s)
4399 return nfserr_bad_stateid;
4400 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004401}
4402
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403/*
4404* Checks for stateid operations
4405*/
Al Virob37ad282006-10-19 23:28:59 -07004406__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004407nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004408 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004410 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004411 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004413 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004415 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004416 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004417 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 if (filpp)
4420 *filpp = NULL;
4421
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004422 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 return nfserr_grace;
4424
4425 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004426 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004428 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004429 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004430 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004431 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04004432 return status;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004433 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4434 if (status)
4435 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004436 switch (s->sc_type) {
4437 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004438 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004439 status = nfs4_check_delegmode(dp, flags);
4440 if (status)
4441 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004442 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004443 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004444 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004445 WARN_ON_ONCE(1);
4446 status = nfserr_serverfault;
4447 goto out;
4448 }
Trond Myklebustde186432014-07-10 14:07:26 -04004449 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004450 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004451 break;
4452 case NFS4_OPEN_STID:
4453 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004454 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004455 status = nfs4_check_fh(current_fh, stp);
4456 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004458 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004459 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004461 status = nfs4_check_openmode(stp, flags);
4462 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004464 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004465 struct nfs4_file *fp = stp->st_stid.sc_file;
4466
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004467 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004468 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004469 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004470 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004471 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004472 break;
4473 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004474 status = nfserr_bad_stateid;
4475 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 }
4477 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004478 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004479 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004481 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 return status;
4483}
4484
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004485/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004486 * Test if the stateid is valid
4487 */
4488__be32
4489nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4490 struct nfsd4_test_stateid *test_stateid)
4491{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004492 struct nfsd4_test_stateid_id *stateid;
4493 struct nfs4_client *cl = cstate->session->se_client;
4494
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004495 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004496 stateid->ts_id_status =
4497 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004498
Bryan Schumaker17456802011-07-13 10:50:48 -04004499 return nfs_ok;
4500}
4501
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004502__be32
4503nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4504 struct nfsd4_free_stateid *free_stateid)
4505{
4506 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004507 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004508 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004509 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004510 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004511 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004512
Jeff Layton1af71cc2014-07-29 21:34:14 -04004513 spin_lock(&cl->cl_lock);
4514 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004515 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004516 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004517 switch (s->sc_type) {
4518 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004519 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004520 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004521 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004522 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4523 if (ret)
4524 break;
4525 ret = nfserr_locks_held;
4526 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004527 case NFS4_LOCK_STID:
4528 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4529 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004530 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004531 stp = openlockstateid(s);
4532 ret = nfserr_locks_held;
4533 if (check_for_locks(stp->st_stid.sc_file,
4534 lockowner(stp->st_stateowner)))
4535 break;
4536 unhash_lock_stateid(stp);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004537 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004538 nfs4_put_stid(s);
4539 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004540 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004541 case NFS4_REVOKED_DELEG_STID:
4542 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004543 list_del_init(&dp->dl_recall_lru);
4544 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004545 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004546 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004547 goto out;
4548 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004549 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004550out_unlock:
4551 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004552out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004553 return ret;
4554}
4555
NeilBrown4c4cd222005-07-07 17:59:27 -07004556static inline int
4557setlkflg (int type)
4558{
4559 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4560 RD_STATE : WR_STATE;
4561}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004563static __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 -04004564{
4565 struct svc_fh *current_fh = &cstate->current_fh;
4566 struct nfs4_stateowner *sop = stp->st_stateowner;
4567 __be32 status;
4568
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004569 status = nfsd4_check_seqid(cstate, sop, seqid);
4570 if (status)
4571 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004572 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4573 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004574 /*
4575 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004576 * nfserr_replay_me from the previous step, and
4577 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004578 */
4579 return nfserr_bad_stateid;
4580 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4581 if (status)
4582 return status;
4583 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004584}
4585
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586/*
4587 * Checks for sequence id mutating operations.
4588 */
Al Virob37ad282006-10-19 23:28:59 -07004589static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004590nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004591 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004592 struct nfs4_ol_stateid **stpp,
4593 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004595 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004596 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004597 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004599 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4600 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004601
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004603 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004604 if (status)
4605 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004606 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004607 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004609 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004610 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004611 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004612 else
4613 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004614 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004615}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004616
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004617static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4618 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004619{
4620 __be32 status;
4621 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004622 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004624 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004625 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004626 if (status)
4627 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004628 oo = openowner(stp->st_stateowner);
4629 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4630 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004631 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004632 }
4633 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004634 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635}
4636
Al Virob37ad282006-10-19 23:28:59 -07004637__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004638nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004639 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640{
Al Virob37ad282006-10-19 23:28:59 -07004641 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004642 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004643 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004644 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645
Al Viroa6a9f182013-09-16 10:57:01 -04004646 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4647 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004649 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004650 if (status)
4651 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004653 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004654 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004655 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004656 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004657 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004658 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004659 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004660 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004661 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004662 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004663 update_stateid(&stp->st_stid.sc_stateid);
4664 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004665 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004666 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004667
Jeff Layton2a4317c2012-03-21 16:42:43 -04004668 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004669 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004670put_stateid:
4671 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004673 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 return status;
4675}
4676
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004677static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004679 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004680 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004681 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004682 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004683}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004684
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004685static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4686{
4687 switch (to_access) {
4688 case NFS4_SHARE_ACCESS_READ:
4689 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4690 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4691 break;
4692 case NFS4_SHARE_ACCESS_WRITE:
4693 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4694 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4695 break;
4696 case NFS4_SHARE_ACCESS_BOTH:
4697 break;
4698 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004699 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 }
4701}
4702
Al Virob37ad282006-10-19 23:28:59 -07004703__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004704nfsd4_open_downgrade(struct svc_rqst *rqstp,
4705 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004706 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707{
Al Virob37ad282006-10-19 23:28:59 -07004708 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004709 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004710 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711
Al Viroa6a9f182013-09-16 10:57:01 -04004712 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4713 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004715 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004716 if (od->od_deleg_want)
4717 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4718 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004720 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004721 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004722 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004725 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004726 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004728 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004730 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004731 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004733 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004735 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
Jeff Laytonce0fc432012-05-11 09:45:14 -04004737 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004739 update_stateid(&stp->st_stid.sc_stateid);
4740 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004742put_stateid:
4743 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004745 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 return status;
4747}
4748
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004749static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4750{
Trond Myklebustacf92952014-06-30 11:48:37 -04004751 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytond83017f2014-07-29 21:34:42 -04004752 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004753
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004754 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04004755 spin_lock(&clp->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04004756 unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004757
Jeff Laytond83017f2014-07-29 21:34:42 -04004758 if (clp->cl_minorversion) {
4759 put_ol_stateid_locked(s, &reaplist);
4760 spin_unlock(&clp->cl_lock);
4761 free_ol_stateid_reaplist(&reaplist);
4762 } else {
4763 spin_unlock(&clp->cl_lock);
4764 free_ol_stateid_reaplist(&reaplist);
Jeff Laytond3134b12014-07-29 21:34:32 -04004765 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04004766 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004767}
4768
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769/*
4770 * nfs4_unlock_state() called after encode
4771 */
Al Virob37ad282006-10-19 23:28:59 -07004772__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004773nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004774 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775{
Al Virob37ad282006-10-19 23:28:59 -07004776 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004777 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004778 struct net *net = SVC_NET(rqstp);
4779 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780
Al Viroa6a9f182013-09-16 10:57:01 -04004781 dprintk("NFSD: nfsd4_close on file %pd\n",
4782 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004784 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4785 &close->cl_stateid,
4786 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004787 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004788 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004789 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004791 update_stateid(&stp->st_stid.sc_stateid);
4792 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004794 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004795
4796 /* put reference from nfs4_preprocess_seqid_op */
4797 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 return status;
4800}
4801
Al Virob37ad282006-10-19 23:28:59 -07004802__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004803nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4804 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004806 struct nfs4_delegation *dp;
4807 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004808 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004809 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004810 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004812 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004813 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004815 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004816 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004817 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004818 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004819 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004820 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004821 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004822
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004823 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004824put_stateid:
4825 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826out:
4827 return status;
4828}
4829
4830
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832
Benny Halevy87df4de2008-12-15 19:42:03 +02004833static inline u64
4834end_offset(u64 start, u64 len)
4835{
4836 u64 end;
4837
4838 end = start + len;
4839 return end >= start ? end: NFS4_MAX_UINT64;
4840}
4841
4842/* last octet in a range */
4843static inline u64
4844last_byte_offset(u64 start, u64 len)
4845{
4846 u64 end;
4847
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004848 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004849 end = start + len;
4850 return end > start ? end - 1: NFS4_MAX_UINT64;
4851}
4852
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853/*
4854 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4855 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4856 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4857 * locking, this prevents us from being completely protocol-compliant. The
4858 * real solution to this problem is to start using unsigned file offsets in
4859 * the VFS, but this is a very deep change!
4860 */
4861static inline void
4862nfs4_transform_lock_offset(struct file_lock *lock)
4863{
4864 if (lock->fl_start < 0)
4865 lock->fl_start = OFFSET_MAX;
4866 if (lock->fl_end < 0)
4867 lock->fl_end = OFFSET_MAX;
4868}
4869
NeilBrownd5b90262006-04-10 22:55:22 -07004870/* Hack!: For now, we're defining this just so we can use a pointer to it
4871 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004872static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004873};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874
4875static inline void
4876nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4877{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004878 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
NeilBrownd5b90262006-04-10 22:55:22 -07004880 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004881 lo = (struct nfs4_lockowner *) fl->fl_owner;
4882 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4883 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004884 if (!deny->ld_owner.data)
4885 /* We just don't care that much */
4886 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004887 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4888 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004889 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004890nevermind:
4891 deny->ld_owner.len = 0;
4892 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004893 deny->ld_clientid.cl_boot = 0;
4894 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 }
4896 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004897 deny->ld_length = NFS4_MAX_UINT64;
4898 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4900 deny->ld_type = NFS4_READ_LT;
4901 if (fl->fl_type != F_RDLCK)
4902 deny->ld_type = NFS4_WRITE_LT;
4903}
4904
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004905static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004906find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004907 struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004909 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004910 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911
Trond Myklebust0a880a22014-07-30 08:27:10 -04004912 lockdep_assert_held(&clp->cl_lock);
4913
Trond Myklebustd4f04892014-07-29 21:34:36 -04004914 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4915 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004916 if (so->so_is_open_owner)
4917 continue;
Trond Myklebustd4f04892014-07-29 21:34:36 -04004918 if (!same_owner_str(so, owner))
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004919 continue;
Jeff Layton5db1c032014-07-29 21:34:28 -04004920 atomic_inc(&so->so_count);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004921 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 }
4923 return NULL;
4924}
4925
Trond Myklebustc58c6612014-07-29 21:34:35 -04004926static struct nfs4_lockowner *
4927find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004928 struct nfs4_client *clp)
Trond Myklebustc58c6612014-07-29 21:34:35 -04004929{
4930 struct nfs4_lockowner *lo;
4931
Trond Myklebustd4f04892014-07-29 21:34:36 -04004932 spin_lock(&clp->cl_lock);
4933 lo = find_lockowner_str_locked(clid, owner, clp);
4934 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004935 return lo;
4936}
4937
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004938static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4939{
Trond Myklebustc58c6612014-07-29 21:34:35 -04004940 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004941}
4942
Jeff Layton6b180f02014-07-29 21:34:26 -04004943static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4944{
4945 struct nfs4_lockowner *lo = lockowner(sop);
4946
4947 kmem_cache_free(lockowner_slab, lo);
4948}
4949
4950static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004951 .so_unhash = nfs4_unhash_lockowner,
4952 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004953};
4954
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955/*
4956 * Alloc a lock owner structure.
4957 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004958 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004960 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004962static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004963alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
4964 struct nfs4_ol_stateid *open_stp,
4965 struct nfsd4_lock *lock)
4966{
Trond Myklebustc58c6612014-07-29 21:34:35 -04004967 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004969 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4970 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004972 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4973 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04004974 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04004975 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04004976 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004977 ret = find_lockowner_str_locked(&clp->cl_clientid,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004978 &lock->lk_new_owner, clp);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004979 if (ret == NULL) {
4980 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004981 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004982 ret = lo;
4983 } else
4984 nfs4_free_lockowner(&lo->lo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04004985 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004986 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987}
4988
Jeff Layton356a95e2014-07-29 21:34:13 -04004989static void
4990init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
4991 struct nfs4_file *fp, struct inode *inode,
4992 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004994 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995
Jeff Layton356a95e2014-07-29 21:34:13 -04004996 lockdep_assert_held(&clp->cl_lock);
4997
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004998 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004999 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005000 stp->st_stateowner = &lo->lo_owner;
Trond Myklebuste4f1dd72014-07-29 21:34:30 -04005001 atomic_inc(&lo->lo_owner.so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07005002 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005003 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04005004 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005005 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005007 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005008 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005009 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005010 spin_lock(&fp->fi_lock);
5011 list_add(&stp->st_perfile, &fp->fi_stateids);
5012 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013}
5014
Jeff Laytonc53530d2014-06-30 11:48:39 -04005015static struct nfs4_ol_stateid *
5016find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5017{
5018 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005019 struct nfs4_client *clp = lo->lo_owner.so_client;
5020
5021 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005022
5023 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005024 if (lst->st_stid.sc_file == fp) {
5025 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005026 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005027 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005028 }
5029 return NULL;
5030}
5031
Jeff Layton356a95e2014-07-29 21:34:13 -04005032static struct nfs4_ol_stateid *
5033find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5034 struct inode *inode, struct nfs4_ol_stateid *ost,
5035 bool *new)
5036{
5037 struct nfs4_stid *ns = NULL;
5038 struct nfs4_ol_stateid *lst;
5039 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5040 struct nfs4_client *clp = oo->oo_owner.so_client;
5041
5042 spin_lock(&clp->cl_lock);
5043 lst = find_lock_stateid(lo, fi);
5044 if (lst == NULL) {
5045 spin_unlock(&clp->cl_lock);
5046 ns = nfs4_alloc_stid(clp, stateid_slab);
5047 if (ns == NULL)
5048 return NULL;
5049
5050 spin_lock(&clp->cl_lock);
5051 lst = find_lock_stateid(lo, fi);
5052 if (likely(!lst)) {
5053 lst = openlockstateid(ns);
5054 init_lock_stateid(lst, lo, fi, inode, ost);
5055 ns = NULL;
5056 *new = true;
5057 }
5058 }
5059 spin_unlock(&clp->cl_lock);
5060 if (ns)
5061 nfs4_put_stid(ns);
5062 return lst;
5063}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005064
NeilBrownfd39ca92005-06-23 22:04:03 -07005065static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066check_lock_length(u64 offset, u64 length)
5067{
Benny Halevy87df4de2008-12-15 19:42:03 +02005068 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 LOFF_OVERFLOW(offset, length)));
5070}
5071
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005072static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005073{
Trond Myklebust11b91642014-07-29 21:34:08 -04005074 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005075
Jeff Layton7214e862014-07-10 14:07:33 -04005076 lockdep_assert_held(&fp->fi_lock);
5077
Jeff Layton82c5ff12012-05-11 09:45:13 -04005078 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005079 return;
Jeff Layton12659652014-07-10 14:07:28 -04005080 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005081 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005082}
5083
Jeff Layton356a95e2014-07-29 21:34:13 -04005084static __be32
5085lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5086 struct nfs4_ol_stateid *ost,
5087 struct nfsd4_lock *lock,
5088 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005089{
Jeff Layton5db1c032014-07-29 21:34:28 -04005090 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005091 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005092 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5093 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005094 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005095 struct nfs4_lockowner *lo;
5096 unsigned int strhashval;
5097
Trond Myklebustd4f04892014-07-29 21:34:36 -04005098 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005099 if (!lo) {
Trond Myklebustd4f04892014-07-29 21:34:36 -04005100 strhashval = ownerstr_hashval(&lock->v.new.owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005101 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5102 if (lo == NULL)
5103 return nfserr_jukebox;
5104 } else {
5105 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005106 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005107 if (!cstate->minorversion &&
5108 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005109 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005110 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005111
Jeff Layton356a95e2014-07-29 21:34:13 -04005112 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005113 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005114 status = nfserr_jukebox;
5115 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005116 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005117 status = nfs_ok;
5118out:
5119 nfs4_put_stateowner(&lo->lo_owner);
5120 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005121}
5122
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123/*
5124 * LOCK operation
5125 */
Al Virob37ad282006-10-19 23:28:59 -07005126__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005127nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005128 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005130 struct nfs4_openowner *open_sop = NULL;
5131 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005132 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005133 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005134 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005135 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005136 struct file_lock *file_lock = NULL;
5137 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005138 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005139 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005140 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005141 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005142 struct net *net = SVC_NET(rqstp);
5143 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144
5145 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5146 (long long) lock->lk_offset,
5147 (long long) lock->lk_length);
5148
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 if (check_lock_length(lock->lk_offset, lock->lk_length))
5150 return nfserr_inval;
5151
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005152 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005153 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005154 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5155 return status;
5156 }
5157
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005159 if (nfsd4_has_session(cstate))
5160 /* See rfc 5661 18.10.3: given clientid is ignored: */
5161 memcpy(&lock->v.new.clientid,
5162 &cstate->session->se_client->cl_clientid,
5163 sizeof(clientid_t));
5164
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005166 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005170 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 lock->lk_new_open_seqid,
5172 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005173 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005174 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005176 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005177 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005178 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005179 &lock->v.new.clientid))
5180 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005181 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005182 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005183 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005184 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005185 lock->lk_old_lock_seqid,
5186 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005187 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005188 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005189 if (status)
5190 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005191 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005193 lkflg = setlkflg(lock->lk_type);
5194 status = nfs4_check_openmode(lock_stp, lkflg);
5195 if (status)
5196 goto out;
5197
NeilBrown0dd395d2005-07-07 17:59:15 -07005198 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005199 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005200 goto out;
5201 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005202 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005203 goto out;
5204
Jeff Layton21179d82012-08-21 08:03:32 -04005205 file_lock = locks_alloc_lock();
5206 if (!file_lock) {
5207 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5208 status = nfserr_jukebox;
5209 goto out;
5210 }
5211
Trond Myklebust11b91642014-07-29 21:34:08 -04005212 fp = lock_stp->st_stid.sc_file;
Jeff Layton21179d82012-08-21 08:03:32 -04005213 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 switch (lock->lk_type) {
5215 case NFS4_READ_LT:
5216 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005217 spin_lock(&fp->fi_lock);
5218 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005219 if (filp)
5220 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005221 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005222 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005223 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 case NFS4_WRITE_LT:
5225 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005226 spin_lock(&fp->fi_lock);
5227 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005228 if (filp)
5229 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005230 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005231 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005232 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 default:
5234 status = nfserr_inval;
5235 goto out;
5236 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005237 if (!filp) {
5238 status = nfserr_openmode;
5239 goto out;
5240 }
Jeff Layton21179d82012-08-21 08:03:32 -04005241 file_lock->fl_owner = (fl_owner_t)lock_sop;
5242 file_lock->fl_pid = current->tgid;
5243 file_lock->fl_file = filp;
5244 file_lock->fl_flags = FL_POSIX;
5245 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5246 file_lock->fl_start = lock->lk_offset;
5247 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5248 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249
Jeff Layton21179d82012-08-21 08:03:32 -04005250 conflock = locks_alloc_lock();
5251 if (!conflock) {
5252 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5253 status = nfserr_jukebox;
5254 goto out;
5255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256
Jeff Layton21179d82012-08-21 08:03:32 -04005257 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005258 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005260 update_stateid(&lock_stp->st_stid.sc_stateid);
5261 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005263 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005264 break;
5265 case (EAGAIN): /* conflock holds conflicting lock */
5266 status = nfserr_denied;
5267 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005268 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005269 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 case (EDEADLK):
5271 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005272 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005273 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005274 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005275 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005276 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278out:
Trond Myklebustde186432014-07-10 14:07:26 -04005279 if (filp)
5280 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005281 if (lock_stp) {
5282 /* Bump seqid manually if the 4.0 replay owner is openowner */
5283 if (cstate->replay_owner &&
5284 cstate->replay_owner != &lock_sop->lo_owner &&
5285 seqid_mutating_err(ntohl(status)))
5286 lock_sop->lo_owner.so_seqid++;
5287
5288 /*
5289 * If this is a new, never-before-used stateid, and we are
5290 * returning an error, then just go ahead and release it.
5291 */
5292 if (status && new)
5293 release_lock_stateid(lock_stp);
5294
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005295 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005296 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005297 if (open_stp)
5298 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005299 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005300 if (file_lock)
5301 locks_free_lock(file_lock);
5302 if (conflock)
5303 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 return status;
5305}
5306
5307/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005308 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5309 * so we do a temporary open here just to get an open file to pass to
5310 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5311 * inode operation.)
5312 */
Al Viro04da6e92012-04-13 00:00:04 -04005313static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005314{
5315 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005316 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5317 if (!err) {
5318 err = nfserrno(vfs_test_lock(file, lock));
5319 nfsd_close(file);
5320 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005321 return err;
5322}
5323
5324/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 * LOCKT operation
5326 */
Al Virob37ad282006-10-19 23:28:59 -07005327__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005328nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5329 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330{
Jeff Layton21179d82012-08-21 08:03:32 -04005331 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005332 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005333 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005334 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005336 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 return nfserr_grace;
5338
5339 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5340 return nfserr_inval;
5341
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005342 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005343 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005344 if (status)
5345 goto out;
5346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005348 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350
Jeff Layton21179d82012-08-21 08:03:32 -04005351 file_lock = locks_alloc_lock();
5352 if (!file_lock) {
5353 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5354 status = nfserr_jukebox;
5355 goto out;
5356 }
5357 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 switch (lockt->lt_type) {
5359 case NFS4_READ_LT:
5360 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005361 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 break;
5363 case NFS4_WRITE_LT:
5364 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005365 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 break;
5367 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005368 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 status = nfserr_inval;
5370 goto out;
5371 }
5372
Trond Myklebustd4f04892014-07-29 21:34:36 -04005373 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5374 cstate->clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005375 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005376 file_lock->fl_owner = (fl_owner_t)lo;
5377 file_lock->fl_pid = current->tgid;
5378 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379
Jeff Layton21179d82012-08-21 08:03:32 -04005380 file_lock->fl_start = lockt->lt_offset;
5381 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382
Jeff Layton21179d82012-08-21 08:03:32 -04005383 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384
Jeff Layton21179d82012-08-21 08:03:32 -04005385 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005386 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005387 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005388
Jeff Layton21179d82012-08-21 08:03:32 -04005389 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005391 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 }
5393out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005394 if (lo)
5395 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04005396 if (file_lock)
5397 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 return status;
5399}
5400
Al Virob37ad282006-10-19 23:28:59 -07005401__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005402nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005403 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005405 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005407 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005408 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005409 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005410 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5411
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5413 (long long) locku->lu_offset,
5414 (long long) locku->lu_length);
5415
5416 if (check_lock_length(locku->lu_offset, locku->lu_length))
5417 return nfserr_inval;
5418
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005419 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005420 &locku->lu_stateid, NFS4_LOCK_STID,
5421 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005422 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005424 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005425 if (!filp) {
5426 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005427 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005428 }
Jeff Layton21179d82012-08-21 08:03:32 -04005429 file_lock = locks_alloc_lock();
5430 if (!file_lock) {
5431 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5432 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005433 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005434 }
5435 locks_init_lock(file_lock);
5436 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005437 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005438 file_lock->fl_pid = current->tgid;
5439 file_lock->fl_file = filp;
5440 file_lock->fl_flags = FL_POSIX;
5441 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5442 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443
Jeff Layton21179d82012-08-21 08:03:32 -04005444 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5445 locku->lu_length);
5446 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447
Jeff Layton21179d82012-08-21 08:03:32 -04005448 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005449 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005450 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 goto out_nfserr;
5452 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005453 update_stateid(&stp->st_stid.sc_stateid);
5454 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005455fput:
5456 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005457put_stateid:
5458 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005460 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005461 if (file_lock)
5462 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 return status;
5464
5465out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005466 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005467 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468}
5469
5470/*
5471 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005472 * true: locks held by lockowner
5473 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005475static bool
5476check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477{
5478 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005479 int status = false;
5480 struct file *filp = find_any_file(fp);
5481 struct inode *inode;
5482
5483 if (!filp) {
5484 /* Any valid lock stateid should have some sort of access */
5485 WARN_ON_ONCE(1);
5486 return status;
5487 }
5488
5489 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490
Jeff Layton1c8c6012013-06-21 08:58:15 -04005491 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005493 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005494 status = true;
5495 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005498 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005499 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 return status;
5501}
5502
Al Virob37ad282006-10-19 23:28:59 -07005503__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005504nfsd4_release_lockowner(struct svc_rqst *rqstp,
5505 struct nfsd4_compound_state *cstate,
5506 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507{
5508 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005509 struct nfs4_stateowner *sop;
5510 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005511 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005513 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005514 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005515 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005516 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517
5518 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5519 clid->cl_boot, clid->cl_id);
5520
Jeff Layton4b24ca72014-06-30 11:48:44 -04005521 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005522 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04005523 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005524
Trond Myklebustd4f04892014-07-29 21:34:36 -04005525 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005526 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005527 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005528 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005529 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005530
5531 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005532 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005533
Jeff Layton882e9d22014-07-29 21:34:37 -04005534 /* see if there are still any locks associated with it */
5535 lo = lockowner(sop);
5536 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5537 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5538 status = nfserr_locks_held;
5539 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04005540 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04005541 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005542 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005543
5544 atomic_inc(&sop->so_count);
5545 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005546 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005547 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005548 if (lo)
5549 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 return status;
5551}
5552
5553static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005554alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555{
NeilBrowna55370a2005-06-23 22:03:52 -07005556 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557}
5558
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005559bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005560nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005561{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005562 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005563
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005564 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005565 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005566}
5567
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568/*
5569 * failure => all reset bets are off, nfserr_no_grace...
5570 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005571struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005572nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573{
5574 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005575 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576
NeilBrowna55370a2005-06-23 22:03:52 -07005577 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5578 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005579 if (crp) {
5580 strhashval = clientstr_hashval(name);
5581 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005582 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005583 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005584 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005585 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005586 }
5587 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588}
5589
Jeff Layton2a4317c2012-03-21 16:42:43 -04005590void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005591nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005592{
5593 list_del(&crp->cr_strhash);
5594 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005595 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005596}
5597
5598void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005599nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600{
5601 struct nfs4_client_reclaim *crp = NULL;
5602 int i;
5603
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005605 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5606 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005608 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 }
5610 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005611 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612}
5613
5614/*
5615 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005616struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005617nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618{
5619 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 struct nfs4_client_reclaim *crp = NULL;
5621
Jeff Layton278c9312012-11-12 15:00:52 -05005622 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623
Jeff Layton278c9312012-11-12 15:00:52 -05005624 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005625 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005626 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 return crp;
5628 }
5629 }
5630 return NULL;
5631}
5632
5633/*
5634* Called from OPEN. Look for clientid in reclaim list.
5635*/
Al Virob37ad282006-10-19 23:28:59 -07005636__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005637nfs4_check_open_reclaim(clientid_t *clid,
5638 struct nfsd4_compound_state *cstate,
5639 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005641 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005642
5643 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005644 status = lookup_clientid(clid, cstate, nn);
5645 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005646 return nfserr_reclaim_bad;
5647
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005648 if (nfsd4_client_record_check(cstate->clp))
5649 return nfserr_reclaim_bad;
5650
5651 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652}
5653
Bryan Schumaker65178db2011-11-01 13:35:21 -04005654#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04005655static inline void
5656put_client(struct nfs4_client *clp)
5657{
5658 atomic_dec(&clp->cl_refcount);
5659}
5660
Jeff Layton285abde2014-07-30 08:27:24 -04005661static struct nfs4_client *
5662nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5663{
5664 struct nfs4_client *clp;
5665 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5666 nfsd_net_id);
5667
5668 if (!nfsd_netns_ready(nn))
5669 return NULL;
5670
5671 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5672 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5673 return clp;
5674 }
5675 return NULL;
5676}
5677
Jeff Layton7ec0e362014-07-30 08:27:17 -04005678u64
Jeff Layton285abde2014-07-30 08:27:24 -04005679nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04005680{
5681 struct nfs4_client *clp;
5682 u64 count = 0;
5683 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5684 nfsd_net_id);
5685 char buf[INET6_ADDRSTRLEN];
5686
5687 if (!nfsd_netns_ready(nn))
5688 return 0;
5689
5690 spin_lock(&nn->client_lock);
5691 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5692 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5693 pr_info("NFS Client: %s\n", buf);
5694 ++count;
5695 }
5696 spin_unlock(&nn->client_lock);
5697
5698 return count;
5699}
Bryan Schumaker65178db2011-11-01 13:35:21 -04005700
Jeff Laytona0926d12014-07-30 08:27:18 -04005701u64
Jeff Layton285abde2014-07-30 08:27:24 -04005702nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04005703{
5704 u64 count = 0;
5705 struct nfs4_client *clp;
5706 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5707 nfsd_net_id);
5708
5709 if (!nfsd_netns_ready(nn))
5710 return count;
5711
5712 spin_lock(&nn->client_lock);
5713 clp = nfsd_find_client(addr, addr_size);
5714 if (clp) {
5715 if (mark_client_expired_locked(clp) == nfs_ok)
5716 ++count;
5717 else
5718 clp = NULL;
5719 }
5720 spin_unlock(&nn->client_lock);
5721
5722 if (clp)
5723 expire_client(clp);
5724
5725 return count;
5726}
5727
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005728u64
Jeff Layton285abde2014-07-30 08:27:24 -04005729nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005730{
5731 u64 count = 0;
5732 struct nfs4_client *clp, *next;
5733 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5734 nfsd_net_id);
5735 LIST_HEAD(reaplist);
5736
5737 if (!nfsd_netns_ready(nn))
5738 return count;
5739
5740 spin_lock(&nn->client_lock);
5741 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5742 if (mark_client_expired_locked(clp) == nfs_ok) {
5743 list_add(&clp->cl_lru, &reaplist);
5744 if (max != 0 && ++count >= max)
5745 break;
5746 }
5747 }
5748 spin_unlock(&nn->client_lock);
5749
5750 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
5751 expire_client(clp);
5752
5753 return count;
5754}
5755
Bryan Schumaker184c1842012-11-29 11:40:44 -05005756static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5757 const char *type)
5758{
5759 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005760 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005761 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5762}
5763
Jeff Layton016200c2014-07-30 08:27:21 -04005764static void
5765nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
5766 struct list_head *collect)
5767{
5768 struct nfs4_client *clp = lst->st_stid.sc_client;
5769 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5770 nfsd_net_id);
5771
5772 if (!collect)
5773 return;
5774
5775 lockdep_assert_held(&nn->client_lock);
5776 atomic_inc(&clp->cl_refcount);
5777 list_add(&lst->st_locks, collect);
5778}
5779
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005780static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04005781 struct list_head *collect,
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005782 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005783{
5784 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005785 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005786 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005787 u64 count = 0;
5788
Jeff Layton016200c2014-07-30 08:27:21 -04005789 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005790 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005791 list_for_each_entry_safe(stp, st_next,
5792 &oop->oo_owner.so_stateids, st_perstateowner) {
5793 list_for_each_entry_safe(lst, lst_next,
5794 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04005795 if (func) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005796 func(lst);
Jeff Layton016200c2014-07-30 08:27:21 -04005797 nfsd_inject_add_lock_to_list(lst,
5798 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04005799 }
Jeff Layton016200c2014-07-30 08:27:21 -04005800 ++count;
5801 /*
5802 * Despite the fact that these functions deal
5803 * with 64-bit integers for "count", we must
5804 * ensure that it doesn't blow up the
5805 * clp->cl_refcount. Throw a warning if we
5806 * start to approach INT_MAX here.
5807 */
5808 WARN_ON_ONCE(count == (INT_MAX / 2));
5809 if (count == max)
5810 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005811 }
5812 }
5813 }
Jeff Layton016200c2014-07-30 08:27:21 -04005814out:
5815 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005816
5817 return count;
5818}
5819
Jeff Layton016200c2014-07-30 08:27:21 -04005820static u64
5821nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
5822 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05005823{
Jeff Layton016200c2014-07-30 08:27:21 -04005824 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005825}
5826
Jeff Layton016200c2014-07-30 08:27:21 -04005827static u64
5828nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05005829{
Jeff Layton016200c2014-07-30 08:27:21 -04005830 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005831 nfsd_print_count(clp, count, "locked files");
5832 return count;
5833}
5834
Jeff Layton016200c2014-07-30 08:27:21 -04005835u64
Jeff Layton285abde2014-07-30 08:27:24 -04005836nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04005837{
5838 struct nfs4_client *clp;
5839 u64 count = 0;
5840 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5841 nfsd_net_id);
5842
5843 if (!nfsd_netns_ready(nn))
5844 return 0;
5845
5846 spin_lock(&nn->client_lock);
5847 list_for_each_entry(clp, &nn->client_lru, cl_lru)
5848 count += nfsd_print_client_locks(clp);
5849 spin_unlock(&nn->client_lock);
5850
5851 return count;
5852}
5853
5854static void
5855nfsd_reap_locks(struct list_head *reaplist)
5856{
5857 struct nfs4_client *clp;
5858 struct nfs4_ol_stateid *stp, *next;
5859
5860 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
5861 list_del_init(&stp->st_locks);
5862 clp = stp->st_stid.sc_client;
5863 nfs4_put_stid(&stp->st_stid);
5864 put_client(clp);
5865 }
5866}
5867
5868u64
Jeff Layton285abde2014-07-30 08:27:24 -04005869nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04005870{
5871 unsigned int count = 0;
5872 struct nfs4_client *clp;
5873 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5874 nfsd_net_id);
5875 LIST_HEAD(reaplist);
5876
5877 if (!nfsd_netns_ready(nn))
5878 return count;
5879
5880 spin_lock(&nn->client_lock);
5881 clp = nfsd_find_client(addr, addr_size);
5882 if (clp)
5883 count = nfsd_collect_client_locks(clp, &reaplist, 0);
5884 spin_unlock(&nn->client_lock);
5885 nfsd_reap_locks(&reaplist);
5886 return count;
5887}
5888
5889u64
Jeff Layton285abde2014-07-30 08:27:24 -04005890nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04005891{
5892 u64 count = 0;
5893 struct nfs4_client *clp;
5894 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5895 nfsd_net_id);
5896 LIST_HEAD(reaplist);
5897
5898 if (!nfsd_netns_ready(nn))
5899 return count;
5900
5901 spin_lock(&nn->client_lock);
5902 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5903 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
5904 if (max != 0 && count >= max)
5905 break;
5906 }
5907 spin_unlock(&nn->client_lock);
5908 nfsd_reap_locks(&reaplist);
5909 return count;
5910}
5911
Jeff Layton82e05ef2014-07-30 08:27:22 -04005912static u64
5913nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
5914 struct list_head *collect,
5915 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005916{
5917 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04005918 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5919 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005920 u64 count = 0;
5921
Jeff Layton82e05ef2014-07-30 08:27:22 -04005922 lockdep_assert_held(&nn->client_lock);
5923
5924 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005925 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04005926 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005927 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04005928 if (collect) {
5929 atomic_inc(&clp->cl_refcount);
5930 list_add(&oop->oo_perclient, collect);
5931 }
5932 }
5933 ++count;
5934 /*
5935 * Despite the fact that these functions deal with
5936 * 64-bit integers for "count", we must ensure that
5937 * it doesn't blow up the clp->cl_refcount. Throw a
5938 * warning if we start to approach INT_MAX here.
5939 */
5940 WARN_ON_ONCE(count == (INT_MAX / 2));
5941 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005942 break;
5943 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04005944 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005945
5946 return count;
5947}
5948
Jeff Layton82e05ef2014-07-30 08:27:22 -04005949static u64
5950nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005951{
Jeff Layton82e05ef2014-07-30 08:27:22 -04005952 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
5953
5954 nfsd_print_count(clp, count, "openowners");
5955 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005956}
5957
Jeff Layton82e05ef2014-07-30 08:27:22 -04005958static u64
5959nfsd_collect_client_openowners(struct nfs4_client *clp,
5960 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05005961{
Jeff Layton82e05ef2014-07-30 08:27:22 -04005962 return nfsd_foreach_client_openowner(clp, max, collect,
5963 unhash_openowner_locked);
5964}
5965
5966u64
Jeff Layton285abde2014-07-30 08:27:24 -04005967nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04005968{
5969 struct nfs4_client *clp;
5970 u64 count = 0;
5971 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5972 nfsd_net_id);
5973
5974 if (!nfsd_netns_ready(nn))
5975 return 0;
5976
5977 spin_lock(&nn->client_lock);
5978 list_for_each_entry(clp, &nn->client_lru, cl_lru)
5979 count += nfsd_print_client_openowners(clp);
5980 spin_unlock(&nn->client_lock);
5981
5982 return count;
5983}
5984
5985static void
5986nfsd_reap_openowners(struct list_head *reaplist)
5987{
5988 struct nfs4_client *clp;
5989 struct nfs4_openowner *oop, *next;
5990
5991 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
5992 list_del_init(&oop->oo_perclient);
5993 clp = oop->oo_owner.so_client;
5994 release_openowner(oop);
5995 put_client(clp);
5996 }
5997}
5998
5999u64
Jeff Layton285abde2014-07-30 08:27:24 -04006000nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6001 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006002{
6003 unsigned int count = 0;
6004 struct nfs4_client *clp;
6005 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6006 nfsd_net_id);
6007 LIST_HEAD(reaplist);
6008
6009 if (!nfsd_netns_ready(nn))
6010 return count;
6011
6012 spin_lock(&nn->client_lock);
6013 clp = nfsd_find_client(addr, addr_size);
6014 if (clp)
6015 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6016 spin_unlock(&nn->client_lock);
6017 nfsd_reap_openowners(&reaplist);
6018 return count;
6019}
6020
6021u64
Jeff Layton285abde2014-07-30 08:27:24 -04006022nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006023{
6024 u64 count = 0;
6025 struct nfs4_client *clp;
6026 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6027 nfsd_net_id);
6028 LIST_HEAD(reaplist);
6029
6030 if (!nfsd_netns_ready(nn))
6031 return count;
6032
6033 spin_lock(&nn->client_lock);
6034 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6035 count += nfsd_collect_client_openowners(clp, &reaplist,
6036 max - count);
6037 if (max != 0 && count >= max)
6038 break;
6039 }
6040 spin_unlock(&nn->client_lock);
6041 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006042 return count;
6043}
6044
Bryan Schumaker269de302012-11-29 11:40:42 -05006045static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6046 struct list_head *victims)
6047{
6048 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006049 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6050 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006051 u64 count = 0;
6052
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006053 lockdep_assert_held(&nn->client_lock);
6054
6055 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006056 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006057 if (victims) {
6058 /*
6059 * It's not safe to mess with delegations that have a
6060 * non-zero dl_time. They might have already been broken
6061 * and could be processed by the laundromat outside of
6062 * the state_lock. Just leave them be.
6063 */
6064 if (dp->dl_time != 0)
6065 continue;
6066
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006067 atomic_inc(&clp->cl_refcount);
Jeff Layton42690672014-07-25 07:34:20 -04006068 unhash_delegation_locked(dp);
6069 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006070 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006071 ++count;
6072 /*
6073 * Despite the fact that these functions deal with
6074 * 64-bit integers for "count", we must ensure that
6075 * it doesn't blow up the clp->cl_refcount. Throw a
6076 * warning if we start to approach INT_MAX here.
6077 */
6078 WARN_ON_ONCE(count == (INT_MAX / 2));
6079 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006080 break;
6081 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006082 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006083 return count;
6084}
6085
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006086static u64
6087nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006088{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006089 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006090
6091 nfsd_print_count(clp, count, "delegations");
6092 return count;
6093}
6094
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006095u64
Jeff Layton285abde2014-07-30 08:27:24 -04006096nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006097{
6098 struct nfs4_client *clp;
6099 u64 count = 0;
6100 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6101 nfsd_net_id);
6102
6103 if (!nfsd_netns_ready(nn))
6104 return 0;
6105
6106 spin_lock(&nn->client_lock);
6107 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6108 count += nfsd_print_client_delegations(clp);
6109 spin_unlock(&nn->client_lock);
6110
6111 return count;
6112}
6113
6114static void
6115nfsd_forget_delegations(struct list_head *reaplist)
6116{
6117 struct nfs4_client *clp;
6118 struct nfs4_delegation *dp, *next;
6119
6120 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6121 list_del_init(&dp->dl_recall_lru);
6122 clp = dp->dl_stid.sc_client;
6123 revoke_delegation(dp);
6124 put_client(clp);
6125 }
6126}
6127
6128u64
Jeff Layton285abde2014-07-30 08:27:24 -04006129nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6130 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006131{
6132 u64 count = 0;
6133 struct nfs4_client *clp;
6134 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6135 nfsd_net_id);
6136 LIST_HEAD(reaplist);
6137
6138 if (!nfsd_netns_ready(nn))
6139 return count;
6140
6141 spin_lock(&nn->client_lock);
6142 clp = nfsd_find_client(addr, addr_size);
6143 if (clp)
6144 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6145 spin_unlock(&nn->client_lock);
6146
6147 nfsd_forget_delegations(&reaplist);
6148 return count;
6149}
6150
6151u64
Jeff Layton285abde2014-07-30 08:27:24 -04006152nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006153{
6154 u64 count = 0;
6155 struct nfs4_client *clp;
6156 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6157 nfsd_net_id);
6158 LIST_HEAD(reaplist);
6159
6160 if (!nfsd_netns_ready(nn))
6161 return count;
6162
6163 spin_lock(&nn->client_lock);
6164 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6165 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6166 if (max != 0 && count >= max)
6167 break;
6168 }
6169 spin_unlock(&nn->client_lock);
6170 nfsd_forget_delegations(&reaplist);
6171 return count;
6172}
6173
6174static void
6175nfsd_recall_delegations(struct list_head *reaplist)
6176{
6177 struct nfs4_client *clp;
6178 struct nfs4_delegation *dp, *next;
6179
6180 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6181 list_del_init(&dp->dl_recall_lru);
6182 clp = dp->dl_stid.sc_client;
6183 /*
6184 * We skipped all entries that had a zero dl_time before,
6185 * so we can now reset the dl_time back to 0. If a delegation
6186 * break comes in now, then it won't make any difference since
6187 * we're recalling it either way.
6188 */
6189 spin_lock(&state_lock);
6190 dp->dl_time = 0;
6191 spin_unlock(&state_lock);
6192 nfsd_break_one_deleg(dp);
6193 put_client(clp);
6194 }
6195}
6196
6197u64
Jeff Layton285abde2014-07-30 08:27:24 -04006198nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006199 size_t addr_size)
6200{
6201 u64 count = 0;
6202 struct nfs4_client *clp;
6203 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6204 nfsd_net_id);
6205 LIST_HEAD(reaplist);
6206
6207 if (!nfsd_netns_ready(nn))
6208 return count;
6209
6210 spin_lock(&nn->client_lock);
6211 clp = nfsd_find_client(addr, addr_size);
6212 if (clp)
6213 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6214 spin_unlock(&nn->client_lock);
6215
6216 nfsd_recall_delegations(&reaplist);
6217 return count;
6218}
6219
6220u64
Jeff Layton285abde2014-07-30 08:27:24 -04006221nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006222{
6223 u64 count = 0;
6224 struct nfs4_client *clp, *next;
6225 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6226 nfsd_net_id);
6227 LIST_HEAD(reaplist);
6228
6229 if (!nfsd_netns_ready(nn))
6230 return count;
6231
6232 spin_lock(&nn->client_lock);
6233 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6234 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6235 if (max != 0 && ++count >= max)
6236 break;
6237 }
6238 spin_unlock(&nn->client_lock);
6239 nfsd_recall_delegations(&reaplist);
6240 return count;
6241}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006242#endif /* CONFIG_NFSD_FAULT_INJECTION */
6243
Meelap Shahc2f1a552007-07-17 04:04:39 -07006244/*
6245 * Since the lifetime of a delegation isn't limited to that of an open, a
6246 * client may quite reasonably hang on to a delegation as long as it has
6247 * the inode cached. This becomes an obvious problem the first time a
6248 * client's inode cache approaches the size of the server's total memory.
6249 *
6250 * For now we avoid this problem by imposing a hard limit on the number
6251 * of delegations, which varies according to the server's memory size.
6252 */
6253static void
6254set_max_delegations(void)
6255{
6256 /*
6257 * Allow at most 4 delegations per megabyte of RAM. Quick
6258 * estimates suggest that in the worst case (where every delegation
6259 * is for a different inode), a delegation could take about 1.5K,
6260 * giving a worst case usage of about 6% of memory.
6261 */
6262 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6263}
6264
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006265static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006266{
6267 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6268 int i;
6269
6270 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6271 CLIENT_HASH_SIZE, GFP_KERNEL);
6272 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006273 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006274 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6275 CLIENT_HASH_SIZE, GFP_KERNEL);
6276 if (!nn->unconf_id_hashtbl)
6277 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006278 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6279 SESSION_HASH_SIZE, GFP_KERNEL);
6280 if (!nn->sessionid_hashtbl)
6281 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006282
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006283 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006284 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006285 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006286 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006287 for (i = 0; i < SESSION_HASH_SIZE; i++)
6288 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006289 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006290 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03006291 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03006292 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006293 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03006294 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006295
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006296 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006297 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006298
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006299 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006300
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006301err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03006302 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006303err_unconf_id:
6304 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006305err:
6306 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006307}
6308
6309static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006310nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006311{
6312 int i;
6313 struct nfs4_client *clp = NULL;
6314 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6315
6316 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6317 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6318 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6319 destroy_client(clp);
6320 }
6321 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006322
Kinglong Mee2b905632014-03-26 22:09:30 +08006323 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6324 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6325 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6326 destroy_client(clp);
6327 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006328 }
6329
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006330 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006331 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006332 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006333 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006334}
6335
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006336int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006337nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07006338{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006339 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006340 int ret;
6341
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006342 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006343 if (ret)
6344 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006345 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006346 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006347 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04006348 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006349 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03006350 nn->nfsd4_grace, net);
6351 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006352 return 0;
6353}
6354
6355/* initialization to perform when the nfsd service is started: */
6356
6357int
6358nfs4_state_start(void)
6359{
6360 int ret;
6361
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006362 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006363 if (ret)
6364 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07006365 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05006366 if (laundry_wq == NULL) {
6367 ret = -ENOMEM;
6368 goto out_recovery;
6369 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006370 ret = nfsd4_create_callback_queue();
6371 if (ret)
6372 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006373
Meelap Shahc2f1a552007-07-17 04:04:39 -07006374 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006375
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006376 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006377
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006378out_free_laundry:
6379 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05006380out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006381 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382}
6383
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006384void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006385nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006389 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006391 cancel_delayed_work_sync(&nn->laundromat_work);
6392 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05006393
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04006395 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006396 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006398 unhash_delegation_locked(dp);
6399 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400 }
Benny Halevycdc97502014-05-30 09:09:30 -04006401 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402 list_for_each_safe(pos, next, &reaplist) {
6403 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006404 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04006405 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406 }
6407
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006408 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006409 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410}
6411
6412void
6413nfs4_state_shutdown(void)
6414{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006415 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006416 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006418
6419static void
6420get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6421{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006422 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6423 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006424}
6425
6426static void
6427put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6428{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006429 if (cstate->minorversion) {
6430 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6431 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6432 }
6433}
6434
6435void
6436clear_current_stateid(struct nfsd4_compound_state *cstate)
6437{
6438 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006439}
6440
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006441/*
6442 * functions to set current state id
6443 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006444void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006445nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6446{
6447 put_stateid(cstate, &odp->od_stateid);
6448}
6449
6450void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006451nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6452{
6453 put_stateid(cstate, &open->op_stateid);
6454}
6455
6456void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006457nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6458{
6459 put_stateid(cstate, &close->cl_stateid);
6460}
6461
6462void
6463nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6464{
6465 put_stateid(cstate, &lock->lk_resp_stateid);
6466}
6467
6468/*
6469 * functions to consume current state id
6470 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006471
6472void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006473nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6474{
6475 get_stateid(cstate, &odp->od_stateid);
6476}
6477
6478void
6479nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6480{
6481 get_stateid(cstate, &drp->dr_stateid);
6482}
6483
6484void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006485nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6486{
6487 get_stateid(cstate, &fsp->fr_stateid);
6488}
6489
6490void
6491nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6492{
6493 get_stateid(cstate, &setattr->sa_stateid);
6494}
6495
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006496void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006497nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6498{
6499 get_stateid(cstate, &close->cl_stateid);
6500}
6501
6502void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006503nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006504{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006505 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006506}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006507
6508void
6509nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6510{
6511 get_stateid(cstate, &read->rd_stateid);
6512}
6513
6514void
6515nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6516{
6517 get_stateid(cstate, &write->wr_stateid);
6518}