blob: fc88b57516b219501ab2c57a9c584e76caa2e599 [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 Layton6bcc0342014-08-09 10:22:40 -0400676 struct file *filp = NULL;
677 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -0400678
Jeff Layton6bcc0342014-08-09 10:22:40 -0400679 spin_lock(&fp->fi_lock);
680 if (fp->fi_lease && atomic_dec_and_test(&fp->fi_delegees)) {
681 swap(filp, fp->fi_deleg_file);
682 fl = fp->fi_lease;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500683 fp->fi_lease = NULL;
Jeff Layton6bcc0342014-08-09 10:22:40 -0400684 }
685 spin_unlock(&fp->fi_lock);
686
687 if (filp) {
688 vfs_setlease(filp, F_UNLCK, &fl);
689 fput(filp);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400693static void unhash_stid(struct nfs4_stid *s)
694{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500695 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400696}
697
Benny Halevy931ee562014-05-30 09:09:27 -0400698static void
699hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
700{
Benny Halevycdc97502014-05-30 09:09:30 -0400701 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400702 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400703
Trond Myklebust67cb1272014-07-29 21:34:17 -0400704 atomic_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400705 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400706 list_add(&dp->dl_perfile, &fp->fi_delegations);
707 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
708}
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710static void
Jeff Layton42690672014-07-25 07:34:20 -0400711unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Trond Myklebust11b91642014-07-29 21:34:08 -0400713 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400714
Jeff Layton42690672014-07-25 07:34:20 -0400715 lockdep_assert_held(&state_lock);
716
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400717 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400718 /* Ensure that deleg break won't try to requeue it */
719 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400720 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400721 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400723 list_del_init(&dp->dl_perfile);
724 spin_unlock(&fp->fi_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400725}
726
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400727static void destroy_delegation(struct nfs4_delegation *dp)
728{
Jeff Layton42690672014-07-25 07:34:20 -0400729 spin_lock(&state_lock);
730 unhash_delegation_locked(dp);
731 spin_unlock(&state_lock);
Jeff Laytonafbda402014-08-09 10:22:41 -0400732 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -0400733 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400734}
735
736static void revoke_delegation(struct nfs4_delegation *dp)
737{
738 struct nfs4_client *clp = dp->dl_stid.sc_client;
739
Jeff Layton2d4a5322014-07-25 07:34:21 -0400740 WARN_ON(!list_empty(&dp->dl_recall_lru));
741
Jeff Laytonafbda402014-08-09 10:22:41 -0400742 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
743
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400744 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400745 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400746 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400747 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400748 spin_lock(&clp->cl_lock);
749 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
750 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400751 }
752}
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754/*
755 * SETCLIENTID state
756 */
757
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400758static unsigned int clientid_hashval(u32 id)
759{
760 return id & CLIENT_HASH_MASK;
761}
762
763static unsigned int clientstr_hashval(const char *name)
764{
765 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
766}
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400769 * We store the NONE, READ, WRITE, and BOTH bits separately in the
770 * st_{access,deny}_bmap field of the stateid, in order to track not
771 * only what share bits are currently in force, but also what
772 * combinations of share bits previous opens have used. This allows us
773 * to enforce the recommendation of rfc 3530 14.2.19 that the server
774 * return an error if the client attempt to downgrade to a combination
775 * of share bits not explicable by closing some of its previous opens.
776 *
777 * XXX: This enforcement is actually incomplete, since we don't keep
778 * track of access/deny bit combinations; so, e.g., we allow:
779 *
780 * OPEN allow read, deny write
781 * OPEN allow both, deny none
782 * DOWNGRADE allow read, deny none
783 *
784 * which we should reject.
785 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400786static unsigned int
787bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400788 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400789 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400790
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400791 for (i = 1; i < 4; i++) {
792 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400793 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400794 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400795 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400796}
797
Jeff Layton82c5ff12012-05-11 09:45:13 -0400798/* set share access for a given stateid */
799static inline void
800set_access(u32 access, struct nfs4_ol_stateid *stp)
801{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400802 unsigned char mask = 1 << access;
803
804 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
805 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400806}
807
808/* clear share access for a given stateid */
809static inline void
810clear_access(u32 access, struct nfs4_ol_stateid *stp)
811{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400812 unsigned char mask = 1 << access;
813
814 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
815 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400816}
817
818/* test whether a given stateid has access */
819static inline bool
820test_access(u32 access, struct nfs4_ol_stateid *stp)
821{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400822 unsigned char mask = 1 << access;
823
824 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400825}
826
Jeff Laytonce0fc432012-05-11 09:45:14 -0400827/* set share deny for a given stateid */
828static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400829set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400830{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400831 unsigned char mask = 1 << deny;
832
833 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
834 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400835}
836
837/* clear share deny for a given stateid */
838static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400839clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400840{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400841 unsigned char mask = 1 << deny;
842
843 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
844 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400845}
846
847/* test whether a given stateid is denying specific access */
848static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400849test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400850{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400851 unsigned char mask = 1 << deny;
852
853 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400854}
855
856static int nfs4_access_to_omode(u32 access)
857{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400858 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400859 case NFS4_SHARE_ACCESS_READ:
860 return O_RDONLY;
861 case NFS4_SHARE_ACCESS_WRITE:
862 return O_WRONLY;
863 case NFS4_SHARE_ACCESS_BOTH:
864 return O_RDWR;
865 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500866 WARN_ON_ONCE(1);
867 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400868}
869
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400870/*
871 * A stateid that had a deny mode associated with it is being released
872 * or downgraded. Recalculate the deny mode on the file.
873 */
874static void
875recalculate_deny_mode(struct nfs4_file *fp)
876{
877 struct nfs4_ol_stateid *stp;
878
879 spin_lock(&fp->fi_lock);
880 fp->fi_share_deny = 0;
881 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
882 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
883 spin_unlock(&fp->fi_lock);
884}
885
886static void
887reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
888{
889 int i;
890 bool change = false;
891
892 for (i = 1; i < 4; i++) {
893 if ((i & deny) != i) {
894 change = true;
895 clear_deny(i, stp);
896 }
897 }
898
899 /* Recalculate per-file deny mode if there was a change */
900 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -0400901 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400902}
903
Jeff Layton82c5ff12012-05-11 09:45:13 -0400904/* release all access and file references for a given stateid */
905static void
906release_all_access(struct nfs4_ol_stateid *stp)
907{
908 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -0400909 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400910
911 if (fp && stp->st_deny_bmap != 0)
912 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400913
914 for (i = 1; i < 4; i++) {
915 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -0400916 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400917 clear_access(i, stp);
918 }
919}
920
Jeff Layton6b180f02014-07-29 21:34:26 -0400921static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
922{
Jeff Laytona819ecc2014-07-29 21:34:38 -0400923 struct nfs4_client *clp = sop->so_client;
924
925 might_lock(&clp->cl_lock);
926
927 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -0400928 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -0400929 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -0400930 spin_unlock(&clp->cl_lock);
Jeff Layton6b180f02014-07-29 21:34:26 -0400931 kfree(sop->so_owner.data);
932 sop->so_ops->so_free(sop);
933}
934
Jeff Layton4ae098d2014-07-29 21:34:43 -0400935static void unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500936{
Trond Myklebust11b91642014-07-29 21:34:08 -0400937 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -0400938
Jeff Layton1c755dc2014-07-29 21:34:12 -0400939 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
940
Trond Myklebust1d31a252014-07-10 14:07:25 -0400941 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500942 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400943 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500944 list_del(&stp->st_perstateowner);
945}
946
Trond Myklebust60116952014-07-29 21:34:06 -0400947static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500948{
Trond Myklebust60116952014-07-29 21:34:06 -0400949 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400950
Trond Myklebust60116952014-07-29 21:34:06 -0400951 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -0400952 if (stp->st_stateowner)
953 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -0400954 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500955}
956
Jeff Laytonb49e0842014-07-29 21:34:11 -0400957static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500958{
Jeff Laytonb49e0842014-07-29 21:34:11 -0400959 struct nfs4_ol_stateid *stp = openlockstateid(stid);
960 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500961 struct file *file;
962
Jeff Laytonb49e0842014-07-29 21:34:11 -0400963 file = find_any_file(stp->st_stid.sc_file);
964 if (file)
965 filp_close(file, (fl_owner_t)lo);
966 nfs4_free_ol_stateid(stid);
967}
968
Jeff Layton2c41beb2014-07-29 21:34:41 -0400969/*
970 * Put the persistent reference to an already unhashed generic stateid, while
971 * holding the cl_lock. If it's the last reference, then put it onto the
972 * reaplist for later destruction.
973 */
974static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
975 struct list_head *reaplist)
976{
977 struct nfs4_stid *s = &stp->st_stid;
978 struct nfs4_client *clp = s->sc_client;
979
980 lockdep_assert_held(&clp->cl_lock);
981
982 WARN_ON_ONCE(!list_empty(&stp->st_locks));
983
984 if (!atomic_dec_and_test(&s->sc_count)) {
985 wake_up_all(&close_wq);
986 return;
987 }
988
989 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
990 list_add(&stp->st_locks, reaplist);
991}
992
Jeff Layton3c1c9952014-07-29 21:34:39 -0400993static void unhash_lock_stateid(struct nfs4_ol_stateid *stp)
994{
995 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
996
997 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
998
999 list_del_init(&stp->st_locks);
Jeff Layton4ae098d2014-07-29 21:34:43 -04001000 unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001001 unhash_stid(&stp->st_stid);
1002}
1003
Jeff Layton5adfd882014-07-29 21:34:31 -04001004static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001005{
Jeff Layton1c755dc2014-07-29 21:34:12 -04001006 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1007
1008 spin_lock(&oo->oo_owner.so_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001009 unhash_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001010 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04001011 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001012}
1013
Trond Myklebustc58c6612014-07-29 21:34:35 -04001014static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001015{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001016 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001017
Trond Myklebustd4f04892014-07-29 21:34:36 -04001018 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001019
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001020 list_del_init(&lo->lo_owner.so_strhash);
1021}
1022
Jeff Layton2c41beb2014-07-29 21:34:41 -04001023/*
1024 * Free a list of generic stateids that were collected earlier after being
1025 * fully unhashed.
1026 */
1027static void
1028free_ol_stateid_reaplist(struct list_head *reaplist)
1029{
1030 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001031 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001032
1033 might_sleep();
1034
1035 while (!list_empty(reaplist)) {
1036 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1037 st_locks);
1038 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001039 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001040 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001041 if (fp)
1042 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001043 }
1044}
1045
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001046static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001047{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001048 struct nfs4_client *clp = lo->lo_owner.so_client;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001049 struct nfs4_ol_stateid *stp;
1050 struct list_head reaplist;
1051
1052 INIT_LIST_HEAD(&reaplist);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001053
Trond Myklebustd4f04892014-07-29 21:34:36 -04001054 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001055 unhash_lockowner_locked(lo);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001056 while (!list_empty(&lo->lo_owner.so_stateids)) {
1057 stp = list_first_entry(&lo->lo_owner.so_stateids,
1058 struct nfs4_ol_stateid, st_perstateowner);
1059 unhash_lock_stateid(stp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001060 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001061 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001062 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001063 free_ol_stateid_reaplist(&reaplist);
Jeff Layton6b180f02014-07-29 21:34:26 -04001064 nfs4_put_stateowner(&lo->lo_owner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001065}
1066
Jeff Laytond83017f2014-07-29 21:34:42 -04001067static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1068 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001069{
1070 struct nfs4_ol_stateid *stp;
1071
1072 while (!list_empty(&open_stp->st_locks)) {
1073 stp = list_entry(open_stp->st_locks.next,
1074 struct nfs4_ol_stateid, st_locks);
Jeff Laytond83017f2014-07-29 21:34:42 -04001075 unhash_lock_stateid(stp);
1076 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001077 }
1078}
1079
Jeff Laytond83017f2014-07-29 21:34:42 -04001080static void unhash_open_stateid(struct nfs4_ol_stateid *stp,
1081 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001082{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001083 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1084
Jeff Layton4ae098d2014-07-29 21:34:43 -04001085 unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001086 release_open_stateid_locks(stp, reaplist);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001087}
1088
1089static void release_open_stateid(struct nfs4_ol_stateid *stp)
1090{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001091 LIST_HEAD(reaplist);
1092
1093 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04001094 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001095 put_ol_stateid_locked(stp, &reaplist);
1096 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1097 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001098}
1099
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001100static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001101{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001102 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001103
Trond Myklebustd4f04892014-07-29 21:34:36 -04001104 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001105
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001106 list_del_init(&oo->oo_owner.so_strhash);
1107 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001108}
1109
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001110static void release_last_closed_stateid(struct nfs4_openowner *oo)
1111{
Jeff Layton217526e2014-07-30 08:27:11 -04001112 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1113 nfsd_net_id);
1114 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001115
Jeff Layton217526e2014-07-30 08:27:11 -04001116 spin_lock(&nn->client_lock);
1117 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001118 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001119 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001120 oo->oo_last_closed_stid = NULL;
1121 }
Jeff Layton217526e2014-07-30 08:27:11 -04001122 spin_unlock(&nn->client_lock);
1123 if (s)
1124 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001125}
1126
Jeff Layton2c41beb2014-07-29 21:34:41 -04001127static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001128{
1129 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001130 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001131 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001132
Jeff Layton2c41beb2014-07-29 21:34:41 -04001133 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001134
Trond Myklebustd4f04892014-07-29 21:34:36 -04001135 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001136 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001137 while (!list_empty(&oo->oo_owner.so_stateids)) {
1138 stp = list_first_entry(&oo->oo_owner.so_stateids,
1139 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytond83017f2014-07-29 21:34:42 -04001140 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001141 put_ol_stateid_locked(stp, &reaplist);
1142 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001143 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001144 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001145 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001146 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001147}
1148
Marc Eshel5282fd72009-04-03 08:27:52 +03001149static inline int
1150hash_sessionid(struct nfs4_sessionid *sessionid)
1151{
1152 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1153
1154 return sid->sequence % SESSION_HASH_SIZE;
1155}
1156
Trond Myklebust8f199b82012-03-20 15:11:17 -04001157#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001158static inline void
1159dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1160{
1161 u32 *ptr = (u32 *)(&sessionid->data[0]);
1162 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1163}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001164#else
1165static inline void
1166dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1167{
1168}
1169#endif
1170
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001171/*
1172 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1173 * won't be used for replay.
1174 */
1175void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1176{
1177 struct nfs4_stateowner *so = cstate->replay_owner;
1178
1179 if (nfserr == nfserr_replay_me)
1180 return;
1181
1182 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001183 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001184 return;
1185 }
1186 if (!so)
1187 return;
1188 if (so->so_is_open_owner)
1189 release_last_closed_stateid(openowner(so));
1190 so->so_seqid++;
1191 return;
1192}
Marc Eshel5282fd72009-04-03 08:27:52 +03001193
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001194static void
1195gen_sessionid(struct nfsd4_session *ses)
1196{
1197 struct nfs4_client *clp = ses->se_client;
1198 struct nfsd4_sessionid *sid;
1199
1200 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1201 sid->clientid = clp->cl_clientid;
1202 sid->sequence = current_sessionid++;
1203 sid->reserved = 0;
1204}
1205
1206/*
Andy Adamsona6496372009-08-28 08:45:01 -04001207 * The protocol defines ca_maxresponssize_cached to include the size of
1208 * the rpc header, but all we need to cache is the data starting after
1209 * the end of the initial SEQUENCE operation--the rest we regenerate
1210 * each time. Therefore we can advertise a ca_maxresponssize_cached
1211 * value that is the number of bytes in our cache plus a few additional
1212 * bytes. In order to stay on the safe side, and not promise more than
1213 * we can cache, those additional bytes must be the minimum possible: 24
1214 * bytes of rpc header (xid through accept state, with AUTH_NULL
1215 * verifier), 12 for the compound header (with zero-length tag), and 44
1216 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001217 */
Andy Adamsona6496372009-08-28 08:45:01 -04001218#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1219
Andy Adamson557ce262009-08-28 08:45:04 -04001220static void
1221free_session_slots(struct nfsd4_session *ses)
1222{
1223 int i;
1224
1225 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1226 kfree(ses->se_slots[i]);
1227}
1228
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001229/*
1230 * We don't actually need to cache the rpc and session headers, so we
1231 * can allocate a little less for each slot:
1232 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001233static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001234{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001235 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001236
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001237 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1238 size = 0;
1239 else
1240 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1241 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001242}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001243
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001244/*
1245 * XXX: If we run out of reserved DRC memory we could (up to a point)
1246 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001247 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001248 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001249static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001250{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001251 u32 slotsize = slot_bytes(ca);
1252 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001253 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001254
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001255 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001256 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1257 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001258 num = min_t(int, num, avail / slotsize);
1259 nfsd_drc_mem_used += num * slotsize;
1260 spin_unlock(&nfsd_drc_lock);
1261
1262 return num;
1263}
1264
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001265static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001266{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001267 int slotsize = slot_bytes(ca);
1268
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001269 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001270 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001271 spin_unlock(&nfsd_drc_lock);
1272}
1273
Kinglong Mee60810e52014-01-01 00:35:47 +08001274static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1275 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001276{
Kinglong Mee60810e52014-01-01 00:35:47 +08001277 int numslots = fattrs->maxreqs;
1278 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001279 struct nfsd4_session *new;
1280 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001281
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001282 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001283 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1284 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001285
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001286 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001287 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001288 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001289 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001290 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001291 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001292 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001293 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001294 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001295
1296 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1297 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1298
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001299 return new;
1300out_free:
1301 while (i--)
1302 kfree(new->se_slots[i]);
1303 kfree(new);
1304 return NULL;
1305}
1306
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001307static void free_conn(struct nfsd4_conn *c)
1308{
1309 svc_xprt_put(c->cn_xprt);
1310 kfree(c);
1311}
1312
1313static void nfsd4_conn_lost(struct svc_xpt_user *u)
1314{
1315 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1316 struct nfs4_client *clp = c->cn_session->se_client;
1317
1318 spin_lock(&clp->cl_lock);
1319 if (!list_empty(&c->cn_persession)) {
1320 list_del(&c->cn_persession);
1321 free_conn(c);
1322 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001323 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001324 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001325}
1326
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001327static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001328{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001329 struct nfsd4_conn *conn;
1330
1331 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1332 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001333 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001334 svc_xprt_get(rqstp->rq_xprt);
1335 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001336 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001337 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1338 return conn;
1339}
1340
J. Bruce Fields328ead22010-09-29 16:11:06 -04001341static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1342{
1343 conn->cn_session = ses;
1344 list_add(&conn->cn_persession, &ses->se_conns);
1345}
1346
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001347static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1348{
1349 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001350
1351 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001352 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001353 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001354}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001355
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001356static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001357{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001358 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001359 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001360}
1361
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001362static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001363{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001364 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001365
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001366 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001367 ret = nfsd4_register_conn(conn);
1368 if (ret)
1369 /* oops; xprt is already down: */
1370 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001371 /* We may have gained or lost a callback channel: */
1372 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001373}
1374
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001375static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001376{
1377 u32 dir = NFS4_CDFC4_FORE;
1378
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001379 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001380 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001381 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001382}
1383
1384/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001385static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001386{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001387 struct nfs4_client *clp = s->se_client;
1388 struct nfsd4_conn *c;
1389
1390 spin_lock(&clp->cl_lock);
1391 while (!list_empty(&s->se_conns)) {
1392 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1393 list_del_init(&c->cn_persession);
1394 spin_unlock(&clp->cl_lock);
1395
1396 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1397 free_conn(c);
1398
1399 spin_lock(&clp->cl_lock);
1400 }
1401 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001402}
1403
J. Bruce Fields1377b692012-09-11 21:42:40 -04001404static void __free_session(struct nfsd4_session *ses)
1405{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001406 free_session_slots(ses);
1407 kfree(ses);
1408}
1409
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001410static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001411{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001412 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001413 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001414 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001415}
Andy Adamson557ce262009-08-28 08:45:04 -04001416
Fengguang Wu135ae822012-11-10 07:20:25 -05001417static 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 -04001418{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001419 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001420 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001421
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001422 new->se_client = clp;
1423 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001424
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001425 INIT_LIST_HEAD(&new->se_conns);
1426
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001427 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001428 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001429 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001430 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001431 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001432 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001433 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001434 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001435 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001436 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001437
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001438 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001439 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001440 /*
1441 * This is a little silly; with sessions there's no real
1442 * use for the callback address. Use the peer address
1443 * as a reasonable default for now, but consider fixing
1444 * the rpc client not to require an address in the
1445 * future:
1446 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001447 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1448 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001449 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001450}
1451
Benny Halevy9089f1b2010-05-12 00:12:26 +03001452/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001453static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001454__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001455{
1456 struct nfsd4_session *elem;
1457 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001458 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001459
Trond Myklebust0a880a22014-07-30 08:27:10 -04001460 lockdep_assert_held(&nn->client_lock);
1461
Marc Eshel5282fd72009-04-03 08:27:52 +03001462 dump_sessionid(__func__, sessionid);
1463 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001464 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001465 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001466 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1467 NFS4_MAX_SESSIONID_LEN)) {
1468 return elem;
1469 }
1470 }
1471
1472 dprintk("%s: session not found\n", __func__);
1473 return NULL;
1474}
1475
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001476static struct nfsd4_session *
1477find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1478 __be32 *ret)
1479{
1480 struct nfsd4_session *session;
1481 __be32 status = nfserr_badsession;
1482
1483 session = __find_in_sessionid_hashtbl(sessionid, net);
1484 if (!session)
1485 goto out;
1486 status = nfsd4_get_session_locked(session);
1487 if (status)
1488 session = NULL;
1489out:
1490 *ret = status;
1491 return session;
1492}
1493
Benny Halevy9089f1b2010-05-12 00:12:26 +03001494/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001495static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001496unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001497{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001498 struct nfs4_client *clp = ses->se_client;
1499 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1500
1501 lockdep_assert_held(&nn->client_lock);
1502
Andy Adamson7116ed62009-04-03 08:27:43 +03001503 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001504 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001505 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001506 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001507}
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1510static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001511STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001513 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001515 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001516 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return 1;
1518}
1519
1520/*
1521 * XXX Should we use a slab cache ?
1522 * This type of memory management is somewhat inefficient, but we use it
1523 * anyway since SETCLIENTID is not a common operation.
1524 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001525static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
1527 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001528 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001530 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1531 if (clp == NULL)
1532 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001533 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001534 if (clp->cl_name.data == NULL)
1535 goto err_no_name;
1536 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1537 OWNER_HASH_SIZE, GFP_KERNEL);
1538 if (!clp->cl_ownerstr_hashtbl)
1539 goto err_no_hashtbl;
1540 for (i = 0; i < OWNER_HASH_SIZE; i++)
1541 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001542 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001543 INIT_LIST_HEAD(&clp->cl_sessions);
1544 idr_init(&clp->cl_stateids);
1545 atomic_set(&clp->cl_refcount, 0);
1546 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1547 INIT_LIST_HEAD(&clp->cl_idhash);
1548 INIT_LIST_HEAD(&clp->cl_openowners);
1549 INIT_LIST_HEAD(&clp->cl_delegations);
1550 INIT_LIST_HEAD(&clp->cl_lru);
1551 INIT_LIST_HEAD(&clp->cl_callbacks);
1552 INIT_LIST_HEAD(&clp->cl_revoked);
1553 spin_lock_init(&clp->cl_lock);
1554 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001556err_no_hashtbl:
1557 kfree(clp->cl_name.data);
1558err_no_name:
1559 kfree(clp);
1560 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
1562
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001563static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564free_client(struct nfs4_client *clp)
1565{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001566 while (!list_empty(&clp->cl_sessions)) {
1567 struct nfsd4_session *ses;
1568 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1569 se_perclnt);
1570 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001571 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1572 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001573 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001574 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001575 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001576 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001578 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 kfree(clp);
1580}
1581
Benny Halevy84d38ac2010-05-12 00:13:16 +03001582/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001583static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001584unhash_client_locked(struct nfs4_client *clp)
1585{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001586 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001587 struct nfsd4_session *ses;
1588
Trond Myklebust0a880a22014-07-30 08:27:10 -04001589 lockdep_assert_held(&nn->client_lock);
1590
Trond Myklebust4beb3452014-07-30 08:27:02 -04001591 /* Mark the client as expired! */
1592 clp->cl_time = 0;
1593 /* Make it invisible */
1594 if (!list_empty(&clp->cl_idhash)) {
1595 list_del_init(&clp->cl_idhash);
1596 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1597 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1598 else
1599 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1600 }
1601 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001602 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001603 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1604 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001605 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001606}
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001609unhash_client(struct nfs4_client *clp)
1610{
1611 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1612
1613 spin_lock(&nn->client_lock);
1614 unhash_client_locked(clp);
1615 spin_unlock(&nn->client_lock);
1616}
1617
Jeff Layton97403d92014-07-30 08:27:12 -04001618static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1619{
1620 if (atomic_read(&clp->cl_refcount))
1621 return nfserr_jukebox;
1622 unhash_client_locked(clp);
1623 return nfs_ok;
1624}
1625
Trond Myklebust4beb3452014-07-30 08:27:02 -04001626static void
1627__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001629 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 struct list_head reaplist;
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001634 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001635 while (!list_empty(&clp->cl_delegations)) {
1636 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001637 unhash_delegation_locked(dp);
1638 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
Benny Halevycdc97502014-05-30 09:09:30 -04001640 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 while (!list_empty(&reaplist)) {
1642 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001643 list_del_init(&dp->dl_recall_lru);
Jeff Laytonafbda402014-08-09 10:22:41 -04001644 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04001645 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001647 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001648 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001649 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001650 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001651 }
NeilBrownea1da632005-06-23 22:04:17 -07001652 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001653 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Jeff Laytond3134b12014-07-29 21:34:32 -04001654 atomic_inc(&oo->oo_owner.so_count);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001655 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001657 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001658 if (clp->cl_cb_conn.cb_xprt)
1659 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001660 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661}
1662
Trond Myklebust4beb3452014-07-30 08:27:02 -04001663static void
1664destroy_client(struct nfs4_client *clp)
1665{
1666 unhash_client(clp);
1667 __destroy_client(clp);
1668}
1669
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001670static void expire_client(struct nfs4_client *clp)
1671{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001672 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001673 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001674 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001675}
1676
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001677static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1678{
1679 memcpy(target->cl_verifier.data, source->data,
1680 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001683static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1684{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1686 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1687}
1688
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001689static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001690{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001691 if (source->cr_principal) {
1692 target->cr_principal =
1693 kstrdup(source->cr_principal, GFP_KERNEL);
1694 if (target->cr_principal == NULL)
1695 return -ENOMEM;
1696 } else
1697 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001698 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 target->cr_uid = source->cr_uid;
1700 target->cr_gid = source->cr_gid;
1701 target->cr_group_info = source->cr_group_info;
1702 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001703 target->cr_gss_mech = source->cr_gss_mech;
1704 if (source->cr_gss_mech)
1705 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001706 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
1708
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001709static long long
1710compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1711{
1712 long long res;
1713
1714 res = o1->len - o2->len;
1715 if (res)
1716 return res;
1717 return (long long)memcmp(o1->data, o2->data, o1->len);
1718}
1719
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001720static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001721{
NeilBrowna55370a2005-06-23 22:03:52 -07001722 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723}
1724
1725static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001726same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1727{
1728 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729}
1730
1731static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001732same_clid(clientid_t *cl1, clientid_t *cl2)
1733{
1734 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735}
1736
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001737static bool groups_equal(struct group_info *g1, struct group_info *g2)
1738{
1739 int i;
1740
1741 if (g1->ngroups != g2->ngroups)
1742 return false;
1743 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001744 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001745 return false;
1746 return true;
1747}
1748
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001749/*
1750 * RFC 3530 language requires clid_inuse be returned when the
1751 * "principal" associated with a requests differs from that previously
1752 * used. We use uid, gid's, and gss principal string as our best
1753 * approximation. We also don't want to allow non-gss use of a client
1754 * established using gss: in theory cr_principal should catch that
1755 * change, but in practice cr_principal can be null even in the gss case
1756 * since gssd doesn't always pass down a principal string.
1757 */
1758static bool is_gss_cred(struct svc_cred *cr)
1759{
1760 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1761 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1762}
1763
1764
Vivek Trivedi5559b502012-07-24 21:18:20 +05301765static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001766same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1767{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001768 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001769 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1770 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001771 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1772 return false;
1773 if (cr1->cr_principal == cr2->cr_principal)
1774 return true;
1775 if (!cr1->cr_principal || !cr2->cr_principal)
1776 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301777 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
1779
J. Bruce Fields57266a62013-04-13 14:27:29 -04001780static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1781{
1782 struct svc_cred *cr = &rqstp->rq_cred;
1783 u32 service;
1784
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001785 if (!cr->cr_gss_mech)
1786 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001787 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1788 return service == RPC_GSS_SVC_INTEGRITY ||
1789 service == RPC_GSS_SVC_PRIVACY;
1790}
1791
1792static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1793{
1794 struct svc_cred *cr = &rqstp->rq_cred;
1795
1796 if (!cl->cl_mach_cred)
1797 return true;
1798 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1799 return false;
1800 if (!svc_rqst_integrity_protected(rqstp))
1801 return false;
1802 if (!cr->cr_principal)
1803 return false;
1804 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1805}
1806
Jeff Layton294ac322014-07-30 08:27:15 -04001807static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001808{
Chuck Leverab4684d2012-03-02 17:13:50 -05001809 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Jeff Laytonf4199922014-06-17 07:44:11 -04001811 /*
1812 * This is opaque to client, so no need to byte-swap. Use
1813 * __force to keep sparse happy
1814 */
1815 verf[0] = (__force __be32)get_seconds();
Jeff Layton294ac322014-07-30 08:27:15 -04001816 verf[1] = (__force __be32)nn->clientid_counter;
Chuck Leverab4684d2012-03-02 17:13:50 -05001817 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
1819
Jeff Layton294ac322014-07-30 08:27:15 -04001820static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1821{
1822 clp->cl_clientid.cl_boot = nn->boot_time;
1823 clp->cl_clientid.cl_id = nn->clientid_counter++;
1824 gen_confirm(clp, nn);
1825}
1826
Jeff Layton4770d722014-07-29 21:34:10 -04001827static struct nfs4_stid *
1828find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001829{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001830 struct nfs4_stid *ret;
1831
1832 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1833 if (!ret || !ret->sc_type)
1834 return NULL;
1835 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001836}
1837
Jeff Layton4770d722014-07-29 21:34:10 -04001838static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001839find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001840{
1841 struct nfs4_stid *s;
1842
Jeff Layton4770d722014-07-29 21:34:10 -04001843 spin_lock(&cl->cl_lock);
1844 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001845 if (s != NULL) {
1846 if (typemask & s->sc_type)
1847 atomic_inc(&s->sc_count);
1848 else
1849 s = NULL;
1850 }
Jeff Layton4770d722014-07-29 21:34:10 -04001851 spin_unlock(&cl->cl_lock);
1852 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001853}
1854
Jeff Layton2216d442012-11-12 15:00:57 -05001855static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001856 struct svc_rqst *rqstp, nfs4_verifier *verf)
1857{
1858 struct nfs4_client *clp;
1859 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001860 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001861 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001862
1863 clp = alloc_client(name);
1864 if (clp == NULL)
1865 return NULL;
1866
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001867 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1868 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001869 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001870 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001871 }
Jeff Layton02e12152014-07-16 10:31:57 -04001872 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001873 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001874 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001875 copy_verf(clp, verf);
1876 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001877 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001878 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001879 return clp;
1880}
1881
NeilBrownfd39ca92005-06-23 22:04:03 -07001882static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001883add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1884{
1885 struct rb_node **new = &(root->rb_node), *parent = NULL;
1886 struct nfs4_client *clp;
1887
1888 while (*new) {
1889 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1890 parent = *new;
1891
1892 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1893 new = &((*new)->rb_left);
1894 else
1895 new = &((*new)->rb_right);
1896 }
1897
1898 rb_link_node(&new_clp->cl_namenode, parent, new);
1899 rb_insert_color(&new_clp->cl_namenode, root);
1900}
1901
1902static struct nfs4_client *
1903find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1904{
1905 long long cmp;
1906 struct rb_node *node = root->rb_node;
1907 struct nfs4_client *clp;
1908
1909 while (node) {
1910 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1911 cmp = compare_blob(&clp->cl_name, name);
1912 if (cmp > 0)
1913 node = node->rb_left;
1914 else if (cmp < 0)
1915 node = node->rb_right;
1916 else
1917 return clp;
1918 }
1919 return NULL;
1920}
1921
1922static void
1923add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001926 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Trond Myklebust0a880a22014-07-30 08:27:10 -04001928 lockdep_assert_held(&nn->client_lock);
1929
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001930 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001931 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001933 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001934 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935}
1936
NeilBrownfd39ca92005-06-23 22:04:03 -07001937static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938move_to_confirmed(struct nfs4_client *clp)
1939{
1940 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001941 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Trond Myklebust0a880a22014-07-30 08:27:10 -04001943 lockdep_assert_held(&nn->client_lock);
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001946 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001947 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001948 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001949 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001950 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951}
1952
1953static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001954find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
1956 struct nfs4_client *clp;
1957 unsigned int idhashval = clientid_hashval(clid->cl_id);
1958
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001959 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001960 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001961 if ((bool)clp->cl_minorversion != sessions)
1962 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001963 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 }
1967 return NULL;
1968}
1969
1970static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001971find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1972{
1973 struct list_head *tbl = nn->conf_id_hashtbl;
1974
Trond Myklebust0a880a22014-07-30 08:27:10 -04001975 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001976 return find_client_in_id_table(tbl, clid, sessions);
1977}
1978
1979static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001980find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001982 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Trond Myklebust0a880a22014-07-30 08:27:10 -04001984 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001985 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986}
1987
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001988static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001989{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001990 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001991}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001992
NeilBrown28ce6052005-06-23 22:03:56 -07001993static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001994find_confirmed_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 Kinsbursky382a62e2012-11-14 18:21:26 +03001997 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001998}
1999
2000static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002001find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002002{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002003 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002004 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002005}
2006
NeilBrownfd39ca92005-06-23 22:04:03 -07002007static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002008gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002010 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002011 struct sockaddr *sa = svc_addr(rqstp);
2012 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002013 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Jeff Layton7077ecb2009-08-14 12:57:58 -04002015 /* Currently, we only support tcp and tcp6 for the callback channel */
2016 if (se->se_callback_netid_len == 3 &&
2017 !memcmp(se->se_callback_netid_val, "tcp", 3))
2018 expected_family = AF_INET;
2019 else if (se->se_callback_netid_len == 4 &&
2020 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2021 expected_family = AF_INET6;
2022 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 goto out_err;
2024
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002025 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002026 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002027 (struct sockaddr *)&conn->cb_addr,
2028 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002029
J. Bruce Fields07263f12010-05-31 19:09:40 -04002030 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002032
J. Bruce Fields07263f12010-05-31 19:09:40 -04002033 if (conn->cb_addr.ss_family == AF_INET6)
2034 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002035
J. Bruce Fields07263f12010-05-31 19:09:40 -04002036 conn->cb_prog = se->se_callback_prog;
2037 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002038 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 return;
2040out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002041 conn->cb_addr.ss_family = AF_UNSPEC;
2042 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002043 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 "will not receive delegations\n",
2045 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 return;
2048}
2049
Andy Adamson074fe892009-04-03 08:28:15 +03002050/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002051 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002052 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002053static void
Andy Adamson074fe892009-04-03 08:28:15 +03002054nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2055{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002056 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002057 struct nfsd4_slot *slot = resp->cstate.slot;
2058 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002059
Andy Adamson557ce262009-08-28 08:45:04 -04002060 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002061
Andy Adamson557ce262009-08-28 08:45:04 -04002062 slot->sl_opcnt = resp->opcnt;
2063 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002064
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002065 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002066 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002067 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002068 return;
2069 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002070 base = resp->cstate.data_offset;
2071 slot->sl_datalen = buf->len - base;
2072 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04002073 WARN("%s: sessions DRC could not cache compound\n", __func__);
2074 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002075}
2076
2077/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002078 * Encode the replay sequence operation from the slot values.
2079 * If cachethis is FALSE encode the uncached rep error on the next
2080 * operation which sets resp->p and increments resp->opcnt for
2081 * nfs4svc_encode_compoundres.
2082 *
Andy Adamson074fe892009-04-03 08:28:15 +03002083 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002084static __be32
2085nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2086 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002087{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002088 struct nfsd4_op *op;
2089 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002090
Andy Adamsonabfabf82009-07-23 19:02:18 -04002091 /* Encode the replayed sequence operation */
2092 op = &args->ops[resp->opcnt - 1];
2093 nfsd4_encode_operation(resp, op);
2094
2095 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002096 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002097 op = &args->ops[resp->opcnt++];
2098 op->status = nfserr_retry_uncached_rep;
2099 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002100 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002101 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002102}
2103
2104/*
Andy Adamson557ce262009-08-28 08:45:04 -04002105 * The sequence operation is not cached because we can use the slot and
2106 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002107 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002108static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002109nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2110 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002111{
Andy Adamson557ce262009-08-28 08:45:04 -04002112 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002113 struct xdr_stream *xdr = &resp->xdr;
2114 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002115 __be32 status;
2116
Andy Adamson557ce262009-08-28 08:45:04 -04002117 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002118
Andy Adamsonabfabf82009-07-23 19:02:18 -04002119 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002120 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002121 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002122
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002123 p = xdr_reserve_space(xdr, slot->sl_datalen);
2124 if (!p) {
2125 WARN_ON_ONCE(1);
2126 return nfserr_serverfault;
2127 }
2128 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2129 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002130
Andy Adamson557ce262009-08-28 08:45:04 -04002131 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002132 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002133}
2134
Andy Adamson0733d212009-04-03 08:28:01 +03002135/*
2136 * Set the exchange_id flags returned by the server.
2137 */
2138static void
2139nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2140{
2141 /* pNFS is not supported */
2142 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2143
2144 /* Referrals are supported, Migration is not. */
2145 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2146
2147 /* set the wire flags to return to client. */
2148 clid->flags = new->cl_exchange_flags;
2149}
2150
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002151static bool client_has_state(struct nfs4_client *clp)
2152{
2153 /*
2154 * Note clp->cl_openowners check isn't quite right: there's no
2155 * need to count owners without stateid's.
2156 *
2157 * Also note we should probably be using this in 4.0 case too.
2158 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002159 return !list_empty(&clp->cl_openowners)
2160 || !list_empty(&clp->cl_delegations)
2161 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002162}
2163
Al Virob37ad282006-10-19 23:28:59 -07002164__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002165nfsd4_exchange_id(struct svc_rqst *rqstp,
2166 struct nfsd4_compound_state *cstate,
2167 struct nfsd4_exchange_id *exid)
2168{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002169 struct nfs4_client *conf, *new;
2170 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002171 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002172 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002173 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002174 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002175 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002176 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002177
Jeff Layton363168b2009-08-14 12:57:56 -04002178 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002179 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002180 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002181 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002182 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002183
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002184 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002185 return nfserr_inval;
2186
Andy Adamson0733d212009-04-03 08:28:01 +03002187 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002188 case SP4_MACH_CRED:
2189 if (!svc_rqst_integrity_protected(rqstp))
2190 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002191 case SP4_NONE:
2192 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002193 default: /* checked by xdr code */
2194 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002195 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002196 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002197 }
2198
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002199 new = create_client(exid->clname, rqstp, &verf);
2200 if (new == NULL)
2201 return nfserr_jukebox;
2202
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002203 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002204 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002205 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002206 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002207 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2208 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2209
J. Bruce Fields136e6582012-05-12 20:37:23 -04002210 if (update) {
2211 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002212 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002213 goto out;
2214 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002215 if (!mach_creds_match(conf, rqstp)) {
2216 status = nfserr_wrong_cred;
2217 goto out;
2218 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002219 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002220 status = nfserr_perm;
2221 goto out;
2222 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002223 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002224 status = nfserr_not_same;
2225 goto out;
2226 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002227 /* case 6 */
2228 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002229 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002230 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002231 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002232 if (client_has_state(conf)) {
2233 status = nfserr_clid_inuse;
2234 goto out;
2235 }
Andy Adamson0733d212009-04-03 08:28:01 +03002236 goto out_new;
2237 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002238 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002239 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002240 goto out_copy;
2241 }
2242 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002243 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002244 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002245 }
2246
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002247 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002248 status = nfserr_noent;
2249 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002250 }
2251
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002252 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002253 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002254 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002255
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002256 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002257out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002258 if (conf) {
2259 status = mark_client_expired_locked(conf);
2260 if (status)
2261 goto out;
2262 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002263 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002264 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002265
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002266 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002267 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002268 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002269out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002270 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2271 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002272
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002273 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2274 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002275
2276 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002277 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002278 status = nfs_ok;
2279
2280out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002281 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002282 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002283 expire_client(new);
2284 if (unconf)
2285 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002286 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002287}
2288
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002289static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002290check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002291{
Andy Adamson88e588d2009-07-23 19:02:15 -04002292 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2293 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002294
2295 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002296 if (slot_inuse) {
2297 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002298 return nfserr_jukebox;
2299 else
2300 return nfserr_seq_misordered;
2301 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002302 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002303 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002304 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002305 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002306 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002307 return nfserr_seq_misordered;
2308}
2309
Andy Adamson49557cc2009-07-23 19:02:16 -04002310/*
2311 * Cache the create session result into the create session single DRC
2312 * slot cache by saving the xdr structure. sl_seqid has been set.
2313 * Do this for solo or embedded create session operations.
2314 */
2315static void
2316nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002317 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002318{
2319 slot->sl_status = nfserr;
2320 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2321}
2322
2323static __be32
2324nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2325 struct nfsd4_clid_slot *slot)
2326{
2327 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2328 return slot->sl_status;
2329}
2330
Mi Jinlong1b74c252011-07-14 14:50:17 +08002331#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2332 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2333 1 + /* MIN tag is length with zero, only length */ \
2334 3 + /* version, opcount, opcode */ \
2335 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2336 /* seqid, slotID, slotID, cache */ \
2337 4 ) * sizeof(__be32))
2338
2339#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2340 2 + /* verifier: AUTH_NULL, length 0 */\
2341 1 + /* status */ \
2342 1 + /* MIN tag is length with zero, only length */ \
2343 3 + /* opcount, opcode, opstatus*/ \
2344 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2345 /* seqid, slotID, slotID, slotID, status */ \
2346 5 ) * sizeof(__be32))
2347
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002348static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002349{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002350 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2351
J. Bruce Fields373cd402013-04-08 15:42:12 -04002352 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2353 return nfserr_toosmall;
2354 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2355 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002356 ca->headerpadsz = 0;
2357 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2358 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2359 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2360 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2361 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2362 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2363 /*
2364 * Note decreasing slot size below client's request may make it
2365 * difficult for client to function correctly, whereas
2366 * decreasing the number of slots will (just?) affect
2367 * performance. When short on memory we therefore prefer to
2368 * decrease number of slots instead of their size. Clients that
2369 * request larger slots than they need will get poor results:
2370 */
2371 ca->maxreqs = nfsd4_get_drc_mem(ca);
2372 if (!ca->maxreqs)
2373 return nfserr_jukebox;
2374
J. Bruce Fields373cd402013-04-08 15:42:12 -04002375 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002376}
2377
Kinglong Mee8a891632013-12-23 18:11:02 +08002378#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2379 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2380#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2381 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2382
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002383static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2384{
2385 ca->headerpadsz = 0;
2386
2387 /*
2388 * These RPC_MAX_HEADER macros are overkill, especially since we
2389 * don't even do gss on the backchannel yet. But this is still
2390 * less than 1k. Tighten up this estimate in the unlikely event
2391 * it turns out to be a problem for some client:
2392 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002393 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002394 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002395 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002396 return nfserr_toosmall;
2397 ca->maxresp_cached = 0;
2398 if (ca->maxops < 2)
2399 return nfserr_toosmall;
2400
2401 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002402}
2403
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002404static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2405{
2406 switch (cbs->flavor) {
2407 case RPC_AUTH_NULL:
2408 case RPC_AUTH_UNIX:
2409 return nfs_ok;
2410 default:
2411 /*
2412 * GSS case: the spec doesn't allow us to return this
2413 * error. But it also doesn't allow us not to support
2414 * GSS.
2415 * I'd rather this fail hard than return some error the
2416 * client might think it can already handle:
2417 */
2418 return nfserr_encr_alg_unsupp;
2419 }
2420}
2421
Andy Adamson069b6ad2009-04-03 08:27:58 +03002422__be32
2423nfsd4_create_session(struct svc_rqst *rqstp,
2424 struct nfsd4_compound_state *cstate,
2425 struct nfsd4_create_session *cr_ses)
2426{
Jeff Layton363168b2009-08-14 12:57:56 -04002427 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002428 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002429 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002430 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002431 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002432 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002433 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002434 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002435
Mi Jinlonga62573d2011-03-23 17:57:07 +08002436 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2437 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002438 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2439 if (status)
2440 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002441 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002442 if (status)
2443 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002444 status = check_backchannel_attrs(&cr_ses->back_channel);
2445 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002446 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002447 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002448 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002449 if (!new)
2450 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002451 conn = alloc_conn_from_crses(rqstp, cr_ses);
2452 if (!conn)
2453 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002454
Jeff Laytond20c11d2014-07-30 08:27:07 -04002455 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002456 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002457 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002458 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002459
2460 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002461 status = nfserr_wrong_cred;
2462 if (!mach_creds_match(conf, rqstp))
2463 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002464 cs_slot = &conf->cl_cs_slot;
2465 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002466 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002467 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002468 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002469 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002470 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002471 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002472 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002473 } else if (unconf) {
2474 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002475 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002476 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002477 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002478 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002479 status = nfserr_wrong_cred;
2480 if (!mach_creds_match(unconf, rqstp))
2481 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002482 cs_slot = &unconf->cl_cs_slot;
2483 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002484 if (status) {
2485 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002486 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002487 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002488 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002489 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002490 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002491 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002492 if (status) {
2493 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002494 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002495 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002496 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002497 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002498 conf = unconf;
2499 } else {
2500 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002501 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002502 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002503 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002504 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002505 * We do not support RDMA or persistent sessions
2506 */
2507 cr_ses->flags &= ~SESSION4_PERSIST;
2508 cr_ses->flags &= ~SESSION4_RDMA;
2509
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002510 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002511 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002512
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002513 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002514 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002515 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002516 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002517
Jeff Laytond20c11d2014-07-30 08:27:07 -04002518 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002519 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002520 spin_unlock(&nn->client_lock);
2521 /* init connection and backchannel */
2522 nfsd4_init_conn(rqstp, conn, new);
2523 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002524 if (old)
2525 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002526 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002527out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002528 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002529 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002530 if (old)
2531 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002532out_free_session:
2533 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002534out_release_drc_mem:
2535 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002536 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002537}
2538
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002539static __be32 nfsd4_map_bcts_dir(u32 *dir)
2540{
2541 switch (*dir) {
2542 case NFS4_CDFC4_FORE:
2543 case NFS4_CDFC4_BACK:
2544 return nfs_ok;
2545 case NFS4_CDFC4_FORE_OR_BOTH:
2546 case NFS4_CDFC4_BACK_OR_BOTH:
2547 *dir = NFS4_CDFC4_BOTH;
2548 return nfs_ok;
2549 };
2550 return nfserr_inval;
2551}
2552
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002553__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2554{
2555 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002556 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002557 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002558
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002559 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2560 if (status)
2561 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002562 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002563 session->se_cb_prog = bc->bc_cb_program;
2564 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002565 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002566
2567 nfsd4_probe_callback(session->se_client);
2568
2569 return nfs_ok;
2570}
2571
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002572__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2573 struct nfsd4_compound_state *cstate,
2574 struct nfsd4_bind_conn_to_session *bcts)
2575{
2576 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002577 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002578 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002579 struct net *net = SVC_NET(rqstp);
2580 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002581
2582 if (!nfsd4_last_compound_op(rqstp))
2583 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002584 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002585 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002586 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002587 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002588 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002589 status = nfserr_wrong_cred;
2590 if (!mach_creds_match(session->se_client, rqstp))
2591 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002592 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002593 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002594 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002595 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002596 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002597 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002598 goto out;
2599 nfsd4_init_conn(rqstp, conn, session);
2600 status = nfs_ok;
2601out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002602 nfsd4_put_session(session);
2603out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002604 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002605}
2606
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002607static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2608{
2609 if (!session)
2610 return 0;
2611 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2612}
2613
Andy Adamson069b6ad2009-04-03 08:27:58 +03002614__be32
2615nfsd4_destroy_session(struct svc_rqst *r,
2616 struct nfsd4_compound_state *cstate,
2617 struct nfsd4_destroy_session *sessionid)
2618{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002619 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002620 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002621 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002622 struct net *net = SVC_NET(r);
2623 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002624
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002625 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002626 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002627 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002628 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002629 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002630 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002631 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002632 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002633 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002634 if (!ses)
2635 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002636 status = nfserr_wrong_cred;
2637 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002638 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002639 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002640 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002641 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002642 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002643 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002644
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002645 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002646
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002647 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002648 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002649out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002650 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002651out_client_lock:
2652 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002653out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03002654 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002655}
2656
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002657static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002658{
2659 struct nfsd4_conn *c;
2660
2661 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002662 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002663 return c;
2664 }
2665 }
2666 return NULL;
2667}
2668
J. Bruce Fields57266a62013-04-13 14:27:29 -04002669static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002670{
2671 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002672 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002673 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002674 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002675
2676 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002677 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002678 if (c)
2679 goto out_free;
2680 status = nfserr_conn_not_bound_to_session;
2681 if (clp->cl_mach_cred)
2682 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002683 __nfsd4_hash_conn(new, ses);
2684 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002685 ret = nfsd4_register_conn(new);
2686 if (ret)
2687 /* oops; xprt is already down: */
2688 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002689 return nfs_ok;
2690out_free:
2691 spin_unlock(&clp->cl_lock);
2692 free_conn(new);
2693 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002694}
2695
Mi Jinlong868b89c2011-04-27 09:09:58 +08002696static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2697{
2698 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2699
2700 return args->opcnt > session->se_fchannel.maxops;
2701}
2702
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002703static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2704 struct nfsd4_session *session)
2705{
2706 struct xdr_buf *xb = &rqstp->rq_arg;
2707
2708 return xb->len > session->se_fchannel.maxreq_sz;
2709}
2710
Andy Adamson069b6ad2009-04-03 08:27:58 +03002711__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002712nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002713 struct nfsd4_compound_state *cstate,
2714 struct nfsd4_sequence *seq)
2715{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002716 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002717 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002718 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002719 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002720 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002721 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002722 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002723 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002724 struct net *net = SVC_NET(rqstp);
2725 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002726
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002727 if (resp->opcnt != 1)
2728 return nfserr_sequence_pos;
2729
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002730 /*
2731 * Will be either used or freed by nfsd4_sequence_check_conn
2732 * below.
2733 */
2734 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2735 if (!conn)
2736 return nfserr_jukebox;
2737
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002738 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002739 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002740 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002741 goto out_no_session;
2742 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002743
Mi Jinlong868b89c2011-04-27 09:09:58 +08002744 status = nfserr_too_many_ops;
2745 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002746 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002747
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002748 status = nfserr_req_too_big;
2749 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002750 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002751
Benny Halevyb85d4c02009-04-03 08:28:08 +03002752 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002753 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002754 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002755
Andy Adamson557ce262009-08-28 08:45:04 -04002756 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002757 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2758
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002759 /* We do not negotiate the number of slots yet, so set the
2760 * maxslots to the session maxreqs which is used to encode
2761 * sr_highest_slotid and the sr_target_slot id to maxslots */
2762 seq->maxslots = session->se_fchannel.maxreqs;
2763
J. Bruce Fields73e79482012-02-13 16:39:00 -05002764 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2765 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002766 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002767 status = nfserr_seq_misordered;
2768 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002769 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002770 cstate->slot = slot;
2771 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002772 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002773 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002774 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002775 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002776 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002777 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002778 }
2779 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002780 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002781
J. Bruce Fields57266a62013-04-13 14:27:29 -04002782 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002783 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002784 if (status)
2785 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002786
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002787 buflen = (seq->cachethis) ?
2788 session->se_fchannel.maxresp_cached :
2789 session->se_fchannel.maxresp_sz;
2790 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2791 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002792 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002793 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002794 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002795
2796 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002797 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002798 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002799 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002800 if (seq->cachethis)
2801 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002802 else
2803 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002804
2805 cstate->slot = slot;
2806 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002807 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002808
Benny Halevyb85d4c02009-04-03 08:28:08 +03002809out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002810 switch (clp->cl_cb_state) {
2811 case NFSD4_CB_DOWN:
2812 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2813 break;
2814 case NFSD4_CB_FAULT:
2815 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2816 break;
2817 default:
2818 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002819 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002820 if (!list_empty(&clp->cl_revoked))
2821 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002822out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002823 if (conn)
2824 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002825 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002826 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002827out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002828 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002829 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002830}
2831
Trond Myklebustb6076642014-06-30 11:48:35 -04002832void
2833nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2834{
2835 struct nfsd4_compound_state *cs = &resp->cstate;
2836
2837 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002838 if (cs->status != nfserr_replay_cache) {
2839 nfsd4_store_cache_entry(resp);
2840 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2841 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002842 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002843 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002844 } else if (cs->clp)
2845 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002846}
2847
Mi Jinlong345c2842011-10-20 17:51:39 +08002848__be32
2849nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2850{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002851 struct nfs4_client *conf, *unconf;
2852 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002853 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002854 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002855
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002856 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002857 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002858 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002859 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002860
2861 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002862 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002863 status = nfserr_clientid_busy;
2864 goto out;
2865 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04002866 status = mark_client_expired_locked(conf);
2867 if (status)
2868 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002869 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08002870 } else if (unconf)
2871 clp = unconf;
2872 else {
2873 status = nfserr_stale_clientid;
2874 goto out;
2875 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002876 if (!mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002877 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002878 status = nfserr_wrong_cred;
2879 goto out;
2880 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002881 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002882out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002883 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002884 if (clp)
2885 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002886 return status;
2887}
2888
Andy Adamson069b6ad2009-04-03 08:27:58 +03002889__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002890nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2891{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002892 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002893
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002894 if (rc->rca_one_fs) {
2895 if (!cstate->current_fh.fh_dentry)
2896 return nfserr_nofilehandle;
2897 /*
2898 * We don't take advantage of the rca_one_fs case.
2899 * That's OK, it's optional, we can safely ignore it.
2900 */
2901 return nfs_ok;
2902 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002903
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002904 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002905 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2906 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002907 goto out;
2908
2909 status = nfserr_stale_clientid;
2910 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002911 /*
2912 * The following error isn't really legal.
2913 * But we only get here if the client just explicitly
2914 * destroyed the client. Surely it no longer cares what
2915 * error it gets back on an operation for the dead
2916 * client.
2917 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002918 goto out;
2919
2920 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002921 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002922out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002923 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002924}
2925
2926__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002927nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2928 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002930 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002932 struct nfs4_client *conf, *new;
2933 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002934 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002935 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2936
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002937 new = create_client(clname, rqstp, &clverifier);
2938 if (new == NULL)
2939 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002940 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002941 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002942 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002943 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002944 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002946 if (clp_used_exchangeid(conf))
2947 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002948 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002949 char addr_str[INET6_ADDRSTRLEN];
2950 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2951 sizeof(addr_str));
2952 dprintk("NFSD: setclientid: string in use by client "
2953 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 goto out;
2955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002957 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002958 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002959 unhash_client_locked(unconf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002960 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002961 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002963 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002964 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002965 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002966 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002967 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2969 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2970 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002971 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 status = nfs_ok;
2973out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002974 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002975 if (new)
2976 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002977 if (unconf)
2978 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 return status;
2980}
2981
2982
Al Virob37ad282006-10-19 23:28:59 -07002983__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002984nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2985 struct nfsd4_compound_state *cstate,
2986 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987{
NeilBrown21ab45a2005-06-23 22:04:14 -07002988 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002989 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2991 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002992 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002993 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002995 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07002997
Jeff Laytond20c11d2014-07-30 08:27:07 -04002998 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002999 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003000 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003001 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003002 * We try hard to give out unique clientid's, so if we get an
3003 * attempt to confirm the same clientid with a different cred,
3004 * there's a bug somewhere. Let's charitably assume it's our
3005 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003006 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04003007 status = nfserr_serverfault;
3008 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3009 goto out;
3010 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3011 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003012 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003013 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3014 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003016 else /* case 4: client hasn't noticed we rebooted yet? */
3017 status = nfserr_stale_clientid;
3018 goto out;
3019 }
3020 status = nfs_ok;
3021 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003022 old = unconf;
3023 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003024 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003025 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003026 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3027 if (old) {
3028 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003029 if (status) {
3030 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003031 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003032 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003033 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003034 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003035 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003036 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003037 get_client_locked(conf);
3038 spin_unlock(&nn->client_lock);
3039 nfsd4_probe_callback(conf);
3040 spin_lock(&nn->client_lock);
3041 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003043 spin_unlock(&nn->client_lock);
3044 if (old)
3045 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 return status;
3047}
3048
J. Bruce Fields32513b42011-10-13 16:00:16 -04003049static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003051 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3052}
3053
3054/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003055static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003056{
Trond Myklebustca943212014-07-23 16:17:39 -04003057 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
Trond Myklebust950e0112014-06-30 11:48:31 -04003059 lockdep_assert_held(&state_lock);
3060
J. Bruce Fields32513b42011-10-13 16:00:16 -04003061 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003062 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003063 INIT_LIST_HEAD(&fp->fi_stateids);
3064 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003065 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003066 fp->fi_had_conflict = false;
3067 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003068 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003069 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3070 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04003071 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072}
3073
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003074void
NeilBrowne60d4392005-06-23 22:03:01 -07003075nfsd4_free_slabs(void)
3076{
Christoph Hellwigabf11352014-05-21 07:43:03 -07003077 kmem_cache_destroy(openowner_slab);
3078 kmem_cache_destroy(lockowner_slab);
3079 kmem_cache_destroy(file_slab);
3080 kmem_cache_destroy(stateid_slab);
3081 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003082}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
Bryan Schumaker72083392011-11-01 15:24:59 -04003084int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085nfsd4_init_slabs(void)
3086{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003087 openowner_slab = kmem_cache_create("nfsd4_openowners",
3088 sizeof(struct nfs4_openowner), 0, 0, NULL);
3089 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003090 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003091 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003092 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003093 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003094 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003095 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003096 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003097 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003098 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003099 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003100 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003101 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003102 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003103 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003104 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003105 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003106 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003108
3109out_free_stateid_slab:
3110 kmem_cache_destroy(stateid_slab);
3111out_free_file_slab:
3112 kmem_cache_destroy(file_slab);
3113out_free_lockowner_slab:
3114 kmem_cache_destroy(lockowner_slab);
3115out_free_openowner_slab:
3116 kmem_cache_destroy(openowner_slab);
3117out:
NeilBrowne60d4392005-06-23 22:03:01 -07003118 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3119 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120}
3121
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003122static void init_nfs4_replay(struct nfs4_replay *rp)
3123{
3124 rp->rp_status = nfserr_serverfault;
3125 rp->rp_buflen = 0;
3126 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003127 mutex_init(&rp->rp_mutex);
3128}
3129
3130static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3131 struct nfs4_stateowner *so)
3132{
3133 if (!nfsd4_has_session(cstate)) {
3134 mutex_lock(&so->so_replay.rp_mutex);
3135 cstate->replay_owner = so;
3136 atomic_inc(&so->so_count);
3137 }
3138}
3139
3140void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3141{
3142 struct nfs4_stateowner *so = cstate->replay_owner;
3143
3144 if (so != NULL) {
3145 cstate->replay_owner = NULL;
3146 mutex_unlock(&so->so_replay.rp_mutex);
3147 nfs4_put_stateowner(so);
3148 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003149}
3150
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003151static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152{
3153 struct nfs4_stateowner *sop;
3154
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003155 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003156 if (!sop)
3157 return NULL;
3158
3159 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3160 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003161 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003162 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003164 sop->so_owner.len = owner->len;
3165
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003166 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003167 sop->so_client = clp;
3168 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003169 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003170 return sop;
3171}
3172
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003173static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003174{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003175 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003176
Trond Myklebustd4f04892014-07-29 21:34:36 -04003177 list_add(&oo->oo_owner.so_strhash,
3178 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003179 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180}
3181
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003182static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3183{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003184 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003185}
3186
Jeff Layton6b180f02014-07-29 21:34:26 -04003187static void nfs4_free_openowner(struct nfs4_stateowner *so)
3188{
3189 struct nfs4_openowner *oo = openowner(so);
3190
3191 kmem_cache_free(openowner_slab, oo);
3192}
3193
3194static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003195 .so_unhash = nfs4_unhash_openowner,
3196 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003197};
3198
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003199static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003200alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003201 struct nfsd4_compound_state *cstate)
3202{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003203 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003204 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003206 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3207 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003209 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003210 oo->oo_owner.so_is_open_owner = 1;
3211 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003212 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003213 if (nfsd4_has_session(cstate))
3214 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003215 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003216 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003217 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003218 spin_lock(&clp->cl_lock);
3219 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003220 if (ret == NULL) {
3221 hash_openowner(oo, clp, strhashval);
3222 ret = oo;
3223 } else
3224 nfs4_free_openowner(&oo->oo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003225 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003226 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227}
3228
J. Bruce Fields996e0932011-10-17 11:14:48 -04003229static 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 -04003230 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003232 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003233 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003234 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003235 stp->st_stateowner = &oo->oo_owner;
Jeff Laytond3134b12014-07-29 21:34:32 -04003236 atomic_inc(&stp->st_stateowner->so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07003237 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003238 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 stp->st_access_bmap = 0;
3240 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003241 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003242 spin_lock(&oo->oo_owner.so_client->cl_lock);
3243 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003244 spin_lock(&fp->fi_lock);
3245 list_add(&stp->st_perfile, &fp->fi_stateids);
3246 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003247 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248}
3249
Jeff Laytond3134b12014-07-29 21:34:32 -04003250/*
3251 * In the 4.0 case we need to keep the owners around a little while to handle
3252 * CLOSE replay. We still do need to release any file access that is held by
3253 * them before returning however.
3254 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003256move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257{
Jeff Layton217526e2014-07-30 08:27:11 -04003258 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003259 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3260 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3261 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003262
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003263 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
Jeff Laytonb401be222014-07-29 21:34:33 -04003265 /*
3266 * We know that we hold one reference via nfsd4_close, and another
3267 * "persistent" reference for the client. If the refcount is higher
3268 * than 2, then there are still calls in progress that are using this
3269 * stateid. We can't put the sc_file reference until they are finished.
3270 * Wait for the refcount to drop to 2. Since it has been unhashed,
3271 * there should be no danger of the refcount going back up again at
3272 * this point.
3273 */
3274 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3275
Jeff Laytond3134b12014-07-29 21:34:32 -04003276 release_all_access(s);
3277 if (s->st_stid.sc_file) {
3278 put_nfs4_file(s->st_stid.sc_file);
3279 s->st_stid.sc_file = NULL;
3280 }
Jeff Layton217526e2014-07-30 08:27:11 -04003281
3282 spin_lock(&nn->client_lock);
3283 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003284 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003285 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003286 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003287 spin_unlock(&nn->client_lock);
3288 if (last)
3289 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290}
3291
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292/* search file_hashtbl[] for file */
3293static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003294find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295{
Trond Myklebustca943212014-07-23 16:17:39 -04003296 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 struct nfs4_file *fp;
3298
Trond Myklebust950e0112014-06-30 11:48:31 -04003299 lockdep_assert_held(&state_lock);
3300
Jeff Layton89876f82013-04-02 09:01:59 -04003301 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003302 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003303 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 }
3307 return NULL;
3308}
3309
Trond Myklebust950e0112014-06-30 11:48:31 -04003310static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003311find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003312{
3313 struct nfs4_file *fp;
3314
3315 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003316 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003317 spin_unlock(&state_lock);
3318 return fp;
3319}
3320
3321static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003322find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003323{
3324 struct nfs4_file *fp;
3325
3326 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003327 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003328 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003329 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003330 fp = new;
3331 }
3332 spin_unlock(&state_lock);
3333
3334 return fp;
3335}
3336
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003337/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 * Called to check deny when READ with all zero stateid or
3339 * WRITE with all zero or all one stateid
3340 */
Al Virob37ad282006-10-19 23:28:59 -07003341static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3343{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003345 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
Trond Myklebustca943212014-07-23 16:17:39 -04003347 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003348 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003349 return ret;
3350 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003351 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003352 if (fp->fi_share_deny & deny_type)
3353 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003354 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003355 put_nfs4_file(fp);
3356 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357}
3358
Jeff Layton02e12152014-07-16 10:31:57 -04003359void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360{
Trond Myklebust11b91642014-07-29 21:34:08 -04003361 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3362 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003363
Trond Myklebust11b91642014-07-29 21:34:08 -04003364 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003365
Jeff Layton02e12152014-07-16 10:31:57 -04003366 /*
3367 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003368 * already holding inode->i_lock.
3369 *
Jeff Laytondff13992014-07-08 14:02:49 -04003370 * If the dl_time != 0, then we know that it has already been
3371 * queued for a lease break. Don't queue it again.
3372 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003373 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003374 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003375 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003376 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003377 }
Jeff Layton02e12152014-07-16 10:31:57 -04003378 spin_unlock(&state_lock);
3379}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380
Jeff Layton02e12152014-07-16 10:31:57 -04003381static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3382{
3383 /*
3384 * We're assuming the state code never drops its reference
3385 * without first removing the lease. Since we're in this lease
3386 * callback (and since the lease code is serialized by the kernel
3387 * lock) we know the server hasn't removed the lease yet, we know
3388 * it's safe to take a reference.
3389 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003390 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003391 nfsd4_cb_recall(dp);
3392}
3393
Jeff Layton1c8c6012013-06-21 08:58:15 -04003394/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003395static void nfsd_break_deleg_cb(struct file_lock *fl)
3396{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003397 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3398 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003399
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003400 if (!fp) {
3401 WARN(1, "(%p)->fl_owner NULL\n", fl);
3402 return;
3403 }
3404 if (fp->fi_had_conflict) {
3405 WARN(1, "duplicate break on %p\n", fp);
3406 return;
3407 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003408 /*
3409 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003410 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003411 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003412 */
3413 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
Jeff Layton02e12152014-07-16 10:31:57 -04003415 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003416 fp->fi_had_conflict = true;
3417 /*
3418 * If there are no delegations on the list, then we can't count on this
3419 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3420 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3421 * true should keep any new delegations from being hashed.
3422 */
3423 if (list_empty(&fp->fi_delegations))
3424 fl->fl_break_time = jiffies;
3425 else
3426 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3427 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003428 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429}
3430
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431static
3432int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3433{
3434 if (arg & F_UNLCK)
3435 return lease_modify(onlist, arg);
3436 else
3437 return -EAGAIN;
3438}
3439
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003440static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003441 .lm_break = nfsd_break_deleg_cb,
3442 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443};
3444
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003445static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3446{
3447 if (nfsd4_has_session(cstate))
3448 return nfs_ok;
3449 if (seqid == so->so_seqid - 1)
3450 return nfserr_replay_me;
3451 if (seqid == so->so_seqid)
3452 return nfs_ok;
3453 return nfserr_bad_seqid;
3454}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
Jeff Layton4b24ca72014-06-30 11:48:44 -04003456static __be32 lookup_clientid(clientid_t *clid,
3457 struct nfsd4_compound_state *cstate,
3458 struct nfsd_net *nn)
3459{
3460 struct nfs4_client *found;
3461
3462 if (cstate->clp) {
3463 found = cstate->clp;
3464 if (!same_clid(&found->cl_clientid, clid))
3465 return nfserr_stale_clientid;
3466 return nfs_ok;
3467 }
3468
3469 if (STALE_CLIENTID(clid, nn))
3470 return nfserr_stale_clientid;
3471
3472 /*
3473 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3474 * cached already then we know this is for is for v4.0 and "sessions"
3475 * will be false.
3476 */
3477 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003478 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003479 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003480 if (!found) {
3481 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003482 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003483 }
3484 atomic_inc(&found->cl_refcount);
3485 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003486
3487 /* Cache the nfs4_client in cstate! */
3488 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003489 return nfs_ok;
3490}
3491
Al Virob37ad282006-10-19 23:28:59 -07003492__be32
Andy Adamson66689582009-04-03 08:28:45 +03003493nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003494 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 clientid_t *clientid = &open->op_clientid;
3497 struct nfs4_client *clp = NULL;
3498 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003499 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003500 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003502 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003504 /*
3505 * In case we need it later, after we've already created the
3506 * file and don't want to risk a further failure:
3507 */
3508 open->op_file = nfsd4_alloc_file();
3509 if (open->op_file == NULL)
3510 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511
Trond Myklebust2d91e892014-06-30 11:48:46 -04003512 status = lookup_clientid(clientid, cstate, nn);
3513 if (status)
3514 return status;
3515 clp = cstate->clp;
3516
Trond Myklebustd4f04892014-07-29 21:34:36 -04003517 strhashval = ownerstr_hashval(&open->op_owner);
3518 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003519 open->op_openowner = oo;
3520 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003521 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003523 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003524 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003525 release_openowner(oo);
3526 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003527 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003528 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003529 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3530 if (status)
3531 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003532 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003533new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003534 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003535 if (oo == NULL)
3536 return nfserr_jukebox;
3537 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003538alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003539 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003540 if (!open->op_stp)
3541 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003542 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543}
3544
Al Virob37ad282006-10-19 23:28:59 -07003545static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003546nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3547{
3548 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3549 return nfserr_openmode;
3550 else
3551 return nfs_ok;
3552}
3553
Daniel Mackc47d8322011-05-16 16:38:14 +02003554static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003555{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003556 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3557}
3558
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003559static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003560{
3561 struct nfs4_stid *ret;
3562
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003563 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003564 if (!ret)
3565 return NULL;
3566 return delegstateid(ret);
3567}
3568
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003569static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3570{
3571 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3572 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3573}
3574
Al Virob37ad282006-10-19 23:28:59 -07003575static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003576nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003577 struct nfs4_delegation **dp)
3578{
3579 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003580 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003581 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003582
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003583 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3584 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003585 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003586 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003587 status = nfs4_check_delegmode(deleg, flags);
3588 if (status) {
3589 nfs4_put_stid(&deleg->dl_stid);
3590 goto out;
3591 }
3592 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003593out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003594 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003595 return nfs_ok;
3596 if (status)
3597 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003598 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003599 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003600}
3601
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003602static struct nfs4_ol_stateid *
3603nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003605 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003606 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607
Trond Myklebust1d31a252014-07-10 14:07:25 -04003608 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003609 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 /* ignore lock owners */
3611 if (local->st_stateowner->so_is_open_owner == 0)
3612 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003613 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003614 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003615 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003616 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003619 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003620 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621}
3622
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003623static inline int nfs4_access_to_access(u32 nfs4_access)
3624{
3625 int flags = 0;
3626
3627 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3628 flags |= NFSD_MAY_READ;
3629 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3630 flags |= NFSD_MAY_WRITE;
3631 return flags;
3632}
3633
Al Virob37ad282006-10-19 23:28:59 -07003634static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3636 struct nfsd4_open *open)
3637{
3638 struct iattr iattr = {
3639 .ia_valid = ATTR_SIZE,
3640 .ia_size = 0,
3641 };
3642 if (!open->op_truncate)
3643 return 0;
3644 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003645 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3647}
3648
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003649static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003650 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3651 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003652{
Trond Myklebustde186432014-07-10 14:07:26 -04003653 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003654 __be32 status;
3655 int oflag = nfs4_access_to_omode(open->op_share_access);
3656 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003657 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003658
Trond Myklebustde186432014-07-10 14:07:26 -04003659 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003660
3661 /*
3662 * Are we trying to set a deny mode that would conflict with
3663 * current access?
3664 */
3665 status = nfs4_file_check_deny(fp, open->op_share_deny);
3666 if (status != nfs_ok) {
3667 spin_unlock(&fp->fi_lock);
3668 goto out;
3669 }
3670
3671 /* set access to the file */
3672 status = nfs4_file_get_access(fp, open->op_share_access);
3673 if (status != nfs_ok) {
3674 spin_unlock(&fp->fi_lock);
3675 goto out;
3676 }
3677
3678 /* Set access bits in stateid */
3679 old_access_bmap = stp->st_access_bmap;
3680 set_access(open->op_share_access, stp);
3681
3682 /* Set new deny mask */
3683 old_deny_bmap = stp->st_deny_bmap;
3684 set_deny(open->op_share_deny, stp);
3685 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3686
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003687 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003688 spin_unlock(&fp->fi_lock);
3689 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003690 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003691 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003692 spin_lock(&fp->fi_lock);
3693 if (!fp->fi_fds[oflag]) {
3694 fp->fi_fds[oflag] = filp;
3695 filp = NULL;
3696 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003697 }
Trond Myklebustde186432014-07-10 14:07:26 -04003698 spin_unlock(&fp->fi_lock);
3699 if (filp)
3700 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003701
3702 status = nfsd4_truncate(rqstp, cur_fh, open);
3703 if (status)
3704 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003705out:
3706 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003707out_put_access:
3708 stp->st_access_bmap = old_access_bmap;
3709 nfs4_file_put_access(fp, open->op_share_access);
3710 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3711 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003712}
3713
Al Virob37ad282006-10-19 23:28:59 -07003714static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003715nfs4_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 -07003716{
Al Virob37ad282006-10-19 23:28:59 -07003717 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003718 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003720 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003721 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003722
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003723 /* test and set deny mode */
3724 spin_lock(&fp->fi_lock);
3725 status = nfs4_file_check_deny(fp, open->op_share_deny);
3726 if (status == nfs_ok) {
3727 old_deny_bmap = stp->st_deny_bmap;
3728 set_deny(open->op_share_deny, stp);
3729 fp->fi_share_deny |=
3730 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3731 }
3732 spin_unlock(&fp->fi_lock);
3733
3734 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003737 status = nfsd4_truncate(rqstp, cur_fh, open);
3738 if (status != nfs_ok)
3739 reset_union_bmap_deny(old_deny_bmap, stp);
3740 return status;
3741}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003744nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003746 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747}
3748
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003749/* Should we give out recallable state?: */
3750static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3751{
3752 if (clp->cl_cb_state == NFSD4_CB_UP)
3753 return true;
3754 /*
3755 * In the sessions case, since we don't have to establish a
3756 * separate connection for callbacks, we assume it's OK
3757 * until we hear otherwise:
3758 */
3759 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3760}
3761
Jeff Laytond564fbe2014-07-16 10:31:58 -04003762static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003763{
3764 struct file_lock *fl;
3765
3766 fl = locks_alloc_lock();
3767 if (!fl)
3768 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003769 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003770 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003771 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3772 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003773 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003774 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003775 return fl;
3776}
3777
J. Bruce Fields99c41512013-05-21 16:21:25 -04003778static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003779{
Trond Myklebust11b91642014-07-29 21:34:08 -04003780 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003781 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003782 struct file *filp;
3783 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003784
Jeff Laytond564fbe2014-07-16 10:31:58 -04003785 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003786 if (!fl)
3787 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003788 filp = find_readable_file(fp);
3789 if (!filp) {
3790 /* We should always have a readable file here */
3791 WARN_ON_ONCE(1);
3792 return -EBADF;
3793 }
3794 fl->fl_file = filp;
3795 status = vfs_setlease(filp, fl->fl_type, &fl);
3796 if (status) {
3797 locks_free_lock(fl);
3798 goto out_fput;
3799 }
Benny Halevycdc97502014-05-30 09:09:30 -04003800 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003801 spin_lock(&fp->fi_lock);
3802 /* Did the lease get broken before we took the lock? */
3803 status = -EAGAIN;
3804 if (fp->fi_had_conflict)
3805 goto out_unlock;
3806 /* Race breaker */
3807 if (fp->fi_lease) {
3808 status = 0;
3809 atomic_inc(&fp->fi_delegees);
3810 hash_delegation_locked(dp, fp);
3811 goto out_unlock;
3812 }
3813 fp->fi_lease = fl;
3814 fp->fi_deleg_file = filp;
3815 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003816 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003817 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003818 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003819 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003820out_unlock:
3821 spin_unlock(&fp->fi_lock);
3822 spin_unlock(&state_lock);
3823out_fput:
3824 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003825 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003826}
3827
Jeff Layton0b266932014-07-25 07:34:25 -04003828static struct nfs4_delegation *
3829nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3830 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003831{
Jeff Layton0b266932014-07-25 07:34:25 -04003832 int status;
3833 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003834
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003835 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003836 return ERR_PTR(-EAGAIN);
3837
3838 dp = alloc_init_deleg(clp, fh);
3839 if (!dp)
3840 return ERR_PTR(-ENOMEM);
3841
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003842 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003843 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003844 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003845 dp->dl_stid.sc_file = fp;
Jeff Layton417c6622014-07-21 09:34:57 -04003846 if (!fp->fi_lease) {
3847 spin_unlock(&fp->fi_lock);
3848 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003849 status = nfs4_setlease(dp);
3850 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003851 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003852 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003853 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003854 status = -EAGAIN;
3855 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003856 }
Benny Halevy931ee562014-05-30 09:09:27 -04003857 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003858 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003859out_unlock:
3860 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003861 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003862out:
3863 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003864 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003865 return ERR_PTR(status);
3866 }
3867 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003868}
3869
Benny Halevy4aa89132012-02-21 14:16:44 -08003870static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3871{
3872 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3873 if (status == -EAGAIN)
3874 open->op_why_no_deleg = WND4_CONTENTION;
3875 else {
3876 open->op_why_no_deleg = WND4_RESOURCE;
3877 switch (open->op_deleg_want) {
3878 case NFS4_SHARE_WANT_READ_DELEG:
3879 case NFS4_SHARE_WANT_WRITE_DELEG:
3880 case NFS4_SHARE_WANT_ANY_DELEG:
3881 break;
3882 case NFS4_SHARE_WANT_CANCEL:
3883 open->op_why_no_deleg = WND4_CANCELLED;
3884 break;
3885 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003886 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003887 }
3888 }
3889}
3890
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891/*
3892 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003893 *
3894 * Note we don't support write delegations, and won't until the vfs has
3895 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 */
3897static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003898nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3899 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900{
3901 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003902 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3903 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003904 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003905 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003907 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003908 open->op_recall = 0;
3909 switch (open->op_claim_type) {
3910 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003911 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003912 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003913 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3914 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003915 break;
3916 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003917 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003918 /*
3919 * Let's not give out any delegations till everyone's
3920 * had the chance to reclaim theirs....
3921 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003922 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003923 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003924 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003925 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003926 /*
3927 * Also, if the file was opened for write or
3928 * create, there's a good chance the client's
3929 * about to write to it, resulting in an
3930 * immediate recall (since we don't support
3931 * write delegations):
3932 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003933 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003934 goto out_no_deleg;
3935 if (open->op_create == NFS4_OPEN_CREATE)
3936 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003937 break;
3938 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003939 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003940 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003941 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003942 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003943 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003945 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003947 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003948 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003949 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003950 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003951 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003952out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003953 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3954 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003955 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003956 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003957 open->op_recall = 1;
3958 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003959
3960 /* 4.1 client asking for a delegation? */
3961 if (open->op_deleg_want)
3962 nfsd4_open_deleg_none_ext(open, status);
3963 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964}
3965
Benny Halevye27f49c2012-02-21 14:16:54 -08003966static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3967 struct nfs4_delegation *dp)
3968{
3969 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3970 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3971 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3972 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3973 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3974 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3975 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3976 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3977 }
3978 /* Otherwise the client must be confused wanting a delegation
3979 * it already has, therefore we don't return
3980 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3981 */
3982}
3983
Al Virob37ad282006-10-19 23:28:59 -07003984__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3986{
Andy Adamson66689582009-04-03 08:28:45 +03003987 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003988 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003990 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003991 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003992 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 /*
3995 * Lookup file; if found, lookup stateid and check open request,
3996 * and check for delegations in the process of being recalled.
3997 * If not found, create the nfs4_file struct
3998 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003999 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004000 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004001 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004002 if (status)
4003 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04004004 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004006 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004007 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004008 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004009 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004010 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 }
4012
4013 /*
4014 * OPEN the file, or upgrade an existing OPEN.
4015 * If truncate fails, the OPEN fails.
4016 */
4017 if (stp) {
4018 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004019 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 if (status)
4021 goto out;
4022 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004023 stp = open->op_stp;
4024 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04004025 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004026 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4027 if (status) {
4028 release_open_stateid(stp);
4029 goto out;
4030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004032 update_stateid(&stp->st_stid.sc_stateid);
4033 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034
Benny Halevyd24433c2012-02-16 20:57:17 +02004035 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004036 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4037 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4038 open->op_why_no_deleg = WND4_NOT_WANTED;
4039 goto nodeleg;
4040 }
4041 }
4042
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 /*
4044 * Attempt to hand out a delegation. No error return, because the
4045 * OPEN succeeds even if we fail.
4046 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004047 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004048nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 status = nfs_ok;
4050
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004051 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004052 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004054 /* 4.1 client trying to upgrade/downgrade delegation? */
4055 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004056 open->op_deleg_want)
4057 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004058
NeilBrown13cd2182005-06-23 22:03:10 -07004059 if (fp)
4060 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004061 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05004062 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 /*
4064 * To finish the open response, we just need to set the rflags.
4065 */
4066 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004067 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004068 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004070 if (dp)
4071 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004072 if (stp)
4073 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
4075 return status;
4076}
4077
Jeff Layton58fb12e2014-07-29 21:34:27 -04004078void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4079 struct nfsd4_open *open, __be32 status)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004080{
4081 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004082 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004083
Jeff Laytond3134b12014-07-29 21:34:32 -04004084 nfsd4_cstate_assign_replay(cstate, so);
4085 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004086 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004087 if (open->op_file)
4088 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004089 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004090 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004091}
4092
Al Virob37ad282006-10-19 23:28:59 -07004093__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004094nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4095 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096{
4097 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004098 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004099 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 dprintk("process_renew(%08x/%08x): starting\n",
4102 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004103 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004104 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004106 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004108 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004109 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 goto out;
4111 status = nfs_ok;
4112out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 return status;
4114}
4115
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04004116void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004117nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004118{
Jeff Layton33dcc482012-04-10 11:08:48 -04004119 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004120 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004121 return;
4122
NeilBrowna76b4312005-06-23 22:04:01 -07004123 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004124 nn->grace_ended = true;
Jeff Layton919b8042014-09-12 16:40:20 -04004125 nfsd4_record_grace_done(nn);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004126 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004127 /*
4128 * Now that every NFSv4 client has had the chance to recover and
4129 * to see the (possibly new, possibly shorter) lease time, we
4130 * can safely set the next grace time to the current lease time:
4131 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03004132 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07004133}
4134
NeilBrownfd39ca92005-06-23 22:04:03 -07004135static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004136nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137{
4138 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004139 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004141 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004143 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004144 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004147 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004148 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004149 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004150 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 clp = list_entry(pos, struct nfs4_client, cl_lru);
4152 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4153 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004154 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 break;
4156 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004157 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004158 dprintk("NFSD: client in use (clientid %08x)\n",
4159 clp->cl_clientid.cl_id);
4160 continue;
4161 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004162 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004163 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004164 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004165 list_for_each_safe(pos, next, &reaplist) {
4166 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 dprintk("NFSD: purging unused client (clientid %08x)\n",
4168 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004169 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 expire_client(clp);
4171 }
Benny Halevycdc97502014-05-30 09:09:30 -04004172 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004173 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004175 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4176 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004178 t = dp->dl_time - cutoff;
4179 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 break;
4181 }
Jeff Layton42690672014-07-25 07:34:20 -04004182 unhash_delegation_locked(dp);
4183 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 }
Benny Halevycdc97502014-05-30 09:09:30 -04004185 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004186 while (!list_empty(&reaplist)) {
4187 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4188 dl_recall_lru);
4189 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004190 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 }
Jeff Layton217526e2014-07-30 08:27:11 -04004192
4193 spin_lock(&nn->client_lock);
4194 while (!list_empty(&nn->close_lru)) {
4195 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4196 oo_close_lru);
4197 if (time_after((unsigned long)oo->oo_time,
4198 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004199 t = oo->oo_time - cutoff;
4200 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 break;
4202 }
Jeff Layton217526e2014-07-30 08:27:11 -04004203 list_del_init(&oo->oo_close_lru);
4204 stp = oo->oo_last_closed_stid;
4205 oo->oo_last_closed_stid = NULL;
4206 spin_unlock(&nn->client_lock);
4207 nfs4_put_stid(&stp->st_stid);
4208 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 }
Jeff Layton217526e2014-07-30 08:27:11 -04004210 spin_unlock(&nn->client_lock);
4211
Jeff Laytona832e7a2014-05-30 09:09:26 -04004212 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004213 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214}
4215
Harvey Harrisona254b242008-02-20 12:49:00 -08004216static struct workqueue_struct *laundry_wq;
4217static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004218
4219static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004220laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221{
4222 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004223 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4224 work);
4225 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4226 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004228 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004230 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231}
4232
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004233static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004234{
Trond Myklebust11b91642014-07-29 21:34:08 -04004235 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004236 return nfserr_bad_stateid;
4237 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238}
4239
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004241access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004243 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4244 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4245 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246}
4247
4248static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004249access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004251 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4252 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253}
4254
4255static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004256__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257{
Al Virob37ad282006-10-19 23:28:59 -07004258 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
J. Bruce Fields02921912010-07-29 15:16:59 -04004260 /* For lock stateid's, we test the parent open, not the lock: */
4261 if (stp->st_openstp)
4262 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004263 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004265 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 goto out;
4267 status = nfs_ok;
4268out:
4269 return status;
4270}
4271
Al Virob37ad282006-10-19 23:28:59 -07004272static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004273check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004275 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004277 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004278 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 * conflicting state; so we must wait out the grace period. */
4280 return nfserr_grace;
4281 } else if (flags & WR_STATE)
4282 return nfs4_share_conflict(current_fh,
4283 NFS4_SHARE_DENY_WRITE);
4284 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4285 return nfs4_share_conflict(current_fh,
4286 NFS4_SHARE_DENY_READ);
4287}
4288
4289/*
4290 * Allow READ/WRITE during grace period on recovered state only for files
4291 * that are not able to provide mandatory locking.
4292 */
4293static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004294grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004296 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297}
4298
J. Bruce Fields81b82962011-08-23 11:03:29 -04004299/* Returns true iff a is later than b: */
4300static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4301{
Jim Rees1a9357f2013-05-17 17:33:00 -04004302 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004303}
4304
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004305static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004306{
Andy Adamson66689582009-04-03 08:28:45 +03004307 /*
4308 * When sessions are used the stateid generation number is ignored
4309 * when it is zero.
4310 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004311 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004312 return nfs_ok;
4313
4314 if (in->si_generation == ref->si_generation)
4315 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004316
J. Bruce Fields0836f582008-01-26 19:08:12 -05004317 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004318 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004319 return nfserr_bad_stateid;
4320 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004321 * However, we could see a stateid from the past, even from a
4322 * non-buggy client. For example, if the client sends a lock
4323 * while some IO is outstanding, the lock may bump si_generation
4324 * while the IO is still in flight. The client could avoid that
4325 * situation by waiting for responses on all the IO requests,
4326 * but better performance may result in retrying IO that
4327 * receives an old_stateid error if requests are rarely
4328 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004329 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004330 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004331}
4332
Chuck Lever7df302f2012-05-29 13:56:37 -04004333static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004334{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004335 struct nfs4_stid *s;
4336 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004337 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004338
Chuck Lever7df302f2012-05-29 13:56:37 -04004339 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004340 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004341 /* Client debugging aid. */
4342 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4343 char addr_str[INET6_ADDRSTRLEN];
4344 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4345 sizeof(addr_str));
4346 pr_warn_ratelimited("NFSD: client %s testing state ID "
4347 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004348 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004349 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004350 spin_lock(&cl->cl_lock);
4351 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004352 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004353 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004354 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004355 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004356 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004357 switch (s->sc_type) {
4358 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004359 status = nfs_ok;
4360 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004361 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004362 status = nfserr_deleg_revoked;
4363 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004364 case NFS4_OPEN_STID:
4365 case NFS4_LOCK_STID:
4366 ols = openlockstateid(s);
4367 if (ols->st_stateowner->so_is_open_owner
4368 && !(openowner(ols->st_stateowner)->oo_flags
4369 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004370 status = nfserr_bad_stateid;
4371 else
4372 status = nfs_ok;
4373 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004374 default:
4375 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004376 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004377 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004378 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004379 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004380 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004381out_unlock:
4382 spin_unlock(&cl->cl_lock);
4383 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004384}
4385
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004386static __be32
4387nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4388 stateid_t *stateid, unsigned char typemask,
4389 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004390{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004391 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004392
4393 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4394 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004395 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004396 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004397 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004398 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004399 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004400 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004401 if (status)
4402 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004403 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004404 if (!*s)
4405 return nfserr_bad_stateid;
4406 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004407}
4408
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409/*
4410* Checks for stateid operations
4411*/
Al Virob37ad282006-10-19 23:28:59 -07004412__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004413nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004414 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004416 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004417 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004419 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004421 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004422 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004423 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 if (filpp)
4426 *filpp = NULL;
4427
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004428 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 return nfserr_grace;
4430
4431 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004432 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004434 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004435 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004436 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004437 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04004438 return status;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004439 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4440 if (status)
4441 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004442 switch (s->sc_type) {
4443 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004444 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004445 status = nfs4_check_delegmode(dp, flags);
4446 if (status)
4447 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004448 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004449 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004450 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004451 WARN_ON_ONCE(1);
4452 status = nfserr_serverfault;
4453 goto out;
4454 }
Trond Myklebustde186432014-07-10 14:07:26 -04004455 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004456 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004457 break;
4458 case NFS4_OPEN_STID:
4459 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004460 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004461 status = nfs4_check_fh(current_fh, stp);
4462 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004464 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004465 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004467 status = nfs4_check_openmode(stp, flags);
4468 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004470 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004471 struct nfs4_file *fp = stp->st_stid.sc_file;
4472
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004473 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004474 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004475 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004476 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004477 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004478 break;
4479 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004480 status = nfserr_bad_stateid;
4481 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 }
4483 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004484 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004485 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004487 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 return status;
4489}
4490
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004491/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004492 * Test if the stateid is valid
4493 */
4494__be32
4495nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4496 struct nfsd4_test_stateid *test_stateid)
4497{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004498 struct nfsd4_test_stateid_id *stateid;
4499 struct nfs4_client *cl = cstate->session->se_client;
4500
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004501 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004502 stateid->ts_id_status =
4503 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004504
Bryan Schumaker17456802011-07-13 10:50:48 -04004505 return nfs_ok;
4506}
4507
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004508__be32
4509nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4510 struct nfsd4_free_stateid *free_stateid)
4511{
4512 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004513 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004514 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004515 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004516 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004517 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004518
Jeff Layton1af71cc2014-07-29 21:34:14 -04004519 spin_lock(&cl->cl_lock);
4520 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004521 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004522 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004523 switch (s->sc_type) {
4524 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004525 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004526 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004527 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004528 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4529 if (ret)
4530 break;
4531 ret = nfserr_locks_held;
4532 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004533 case NFS4_LOCK_STID:
4534 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4535 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004536 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004537 stp = openlockstateid(s);
4538 ret = nfserr_locks_held;
4539 if (check_for_locks(stp->st_stid.sc_file,
4540 lockowner(stp->st_stateowner)))
4541 break;
4542 unhash_lock_stateid(stp);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004543 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004544 nfs4_put_stid(s);
4545 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004546 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004547 case NFS4_REVOKED_DELEG_STID:
4548 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004549 list_del_init(&dp->dl_recall_lru);
4550 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004551 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004552 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004553 goto out;
4554 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004555 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004556out_unlock:
4557 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004558out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004559 return ret;
4560}
4561
NeilBrown4c4cd222005-07-07 17:59:27 -07004562static inline int
4563setlkflg (int type)
4564{
4565 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4566 RD_STATE : WR_STATE;
4567}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004569static __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 -04004570{
4571 struct svc_fh *current_fh = &cstate->current_fh;
4572 struct nfs4_stateowner *sop = stp->st_stateowner;
4573 __be32 status;
4574
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004575 status = nfsd4_check_seqid(cstate, sop, seqid);
4576 if (status)
4577 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004578 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4579 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004580 /*
4581 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004582 * nfserr_replay_me from the previous step, and
4583 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004584 */
4585 return nfserr_bad_stateid;
4586 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4587 if (status)
4588 return status;
4589 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004590}
4591
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592/*
4593 * Checks for sequence id mutating operations.
4594 */
Al Virob37ad282006-10-19 23:28:59 -07004595static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004596nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004597 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004598 struct nfs4_ol_stateid **stpp,
4599 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004601 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004602 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004603 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004605 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4606 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004607
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004609 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004610 if (status)
4611 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004612 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004613 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004615 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004616 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004617 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004618 else
4619 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004620 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004621}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004622
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004623static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4624 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004625{
4626 __be32 status;
4627 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004628 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004630 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004631 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004632 if (status)
4633 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004634 oo = openowner(stp->st_stateowner);
4635 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4636 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004637 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004638 }
4639 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004640 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641}
4642
Al Virob37ad282006-10-19 23:28:59 -07004643__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004644nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004645 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646{
Al Virob37ad282006-10-19 23:28:59 -07004647 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004648 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004649 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004650 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651
Al Viroa6a9f182013-09-16 10:57:01 -04004652 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4653 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004655 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004656 if (status)
4657 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004659 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004660 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004661 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004662 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004663 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004664 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004665 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004666 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004667 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004668 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004669 update_stateid(&stp->st_stid.sc_stateid);
4670 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004671 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004672 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004673
Jeff Layton2a4317c2012-03-21 16:42:43 -04004674 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004675 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004676put_stateid:
4677 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004679 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 return status;
4681}
4682
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004683static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004685 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004686 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004687 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004688 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004689}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004690
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004691static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4692{
4693 switch (to_access) {
4694 case NFS4_SHARE_ACCESS_READ:
4695 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4696 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4697 break;
4698 case NFS4_SHARE_ACCESS_WRITE:
4699 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4700 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4701 break;
4702 case NFS4_SHARE_ACCESS_BOTH:
4703 break;
4704 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004705 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 }
4707}
4708
Al Virob37ad282006-10-19 23:28:59 -07004709__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004710nfsd4_open_downgrade(struct svc_rqst *rqstp,
4711 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004712 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713{
Al Virob37ad282006-10-19 23:28:59 -07004714 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004715 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004716 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717
Al Viroa6a9f182013-09-16 10:57:01 -04004718 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4719 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004721 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004722 if (od->od_deleg_want)
4723 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4724 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004726 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004727 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004728 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004731 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004732 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004734 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004736 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004737 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004739 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004741 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742
Jeff Laytonce0fc432012-05-11 09:45:14 -04004743 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004745 update_stateid(&stp->st_stid.sc_stateid);
4746 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004748put_stateid:
4749 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004751 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 return status;
4753}
4754
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004755static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4756{
Trond Myklebustacf92952014-06-30 11:48:37 -04004757 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytond83017f2014-07-29 21:34:42 -04004758 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004759
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004760 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04004761 spin_lock(&clp->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04004762 unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004763
Jeff Laytond83017f2014-07-29 21:34:42 -04004764 if (clp->cl_minorversion) {
4765 put_ol_stateid_locked(s, &reaplist);
4766 spin_unlock(&clp->cl_lock);
4767 free_ol_stateid_reaplist(&reaplist);
4768 } else {
4769 spin_unlock(&clp->cl_lock);
4770 free_ol_stateid_reaplist(&reaplist);
Jeff Laytond3134b12014-07-29 21:34:32 -04004771 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04004772 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004773}
4774
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775/*
4776 * nfs4_unlock_state() called after encode
4777 */
Al Virob37ad282006-10-19 23:28:59 -07004778__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004779nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004780 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781{
Al Virob37ad282006-10-19 23:28:59 -07004782 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004783 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004784 struct net *net = SVC_NET(rqstp);
4785 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786
Al Viroa6a9f182013-09-16 10:57:01 -04004787 dprintk("NFSD: nfsd4_close on file %pd\n",
4788 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004790 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4791 &close->cl_stateid,
4792 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004793 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004794 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004795 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004797 update_stateid(&stp->st_stid.sc_stateid);
4798 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004800 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004801
4802 /* put reference from nfs4_preprocess_seqid_op */
4803 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 return status;
4806}
4807
Al Virob37ad282006-10-19 23:28:59 -07004808__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004809nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4810 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004812 struct nfs4_delegation *dp;
4813 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004814 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004815 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004816 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004818 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004819 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004821 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004822 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004823 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004824 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004825 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004826 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004827 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004828
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004829 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004830put_stateid:
4831 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832out:
4833 return status;
4834}
4835
4836
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838
Benny Halevy87df4de2008-12-15 19:42:03 +02004839static inline u64
4840end_offset(u64 start, u64 len)
4841{
4842 u64 end;
4843
4844 end = start + len;
4845 return end >= start ? end: NFS4_MAX_UINT64;
4846}
4847
4848/* last octet in a range */
4849static inline u64
4850last_byte_offset(u64 start, u64 len)
4851{
4852 u64 end;
4853
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004854 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004855 end = start + len;
4856 return end > start ? end - 1: NFS4_MAX_UINT64;
4857}
4858
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859/*
4860 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4861 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4862 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4863 * locking, this prevents us from being completely protocol-compliant. The
4864 * real solution to this problem is to start using unsigned file offsets in
4865 * the VFS, but this is a very deep change!
4866 */
4867static inline void
4868nfs4_transform_lock_offset(struct file_lock *lock)
4869{
4870 if (lock->fl_start < 0)
4871 lock->fl_start = OFFSET_MAX;
4872 if (lock->fl_end < 0)
4873 lock->fl_end = OFFSET_MAX;
4874}
4875
NeilBrownd5b90262006-04-10 22:55:22 -07004876/* Hack!: For now, we're defining this just so we can use a pointer to it
4877 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004878static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004879};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880
4881static inline void
4882nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4883{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004884 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885
NeilBrownd5b90262006-04-10 22:55:22 -07004886 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004887 lo = (struct nfs4_lockowner *) fl->fl_owner;
4888 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4889 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004890 if (!deny->ld_owner.data)
4891 /* We just don't care that much */
4892 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004893 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4894 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004895 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004896nevermind:
4897 deny->ld_owner.len = 0;
4898 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004899 deny->ld_clientid.cl_boot = 0;
4900 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 }
4902 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004903 deny->ld_length = NFS4_MAX_UINT64;
4904 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4906 deny->ld_type = NFS4_READ_LT;
4907 if (fl->fl_type != F_RDLCK)
4908 deny->ld_type = NFS4_WRITE_LT;
4909}
4910
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004911static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004912find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004913 struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004915 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004916 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917
Trond Myklebust0a880a22014-07-30 08:27:10 -04004918 lockdep_assert_held(&clp->cl_lock);
4919
Trond Myklebustd4f04892014-07-29 21:34:36 -04004920 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4921 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004922 if (so->so_is_open_owner)
4923 continue;
Trond Myklebustd4f04892014-07-29 21:34:36 -04004924 if (!same_owner_str(so, owner))
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004925 continue;
Jeff Layton5db1c032014-07-29 21:34:28 -04004926 atomic_inc(&so->so_count);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004927 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 }
4929 return NULL;
4930}
4931
Trond Myklebustc58c6612014-07-29 21:34:35 -04004932static struct nfs4_lockowner *
4933find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004934 struct nfs4_client *clp)
Trond Myklebustc58c6612014-07-29 21:34:35 -04004935{
4936 struct nfs4_lockowner *lo;
4937
Trond Myklebustd4f04892014-07-29 21:34:36 -04004938 spin_lock(&clp->cl_lock);
4939 lo = find_lockowner_str_locked(clid, owner, clp);
4940 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004941 return lo;
4942}
4943
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004944static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4945{
Trond Myklebustc58c6612014-07-29 21:34:35 -04004946 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004947}
4948
Jeff Layton6b180f02014-07-29 21:34:26 -04004949static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4950{
4951 struct nfs4_lockowner *lo = lockowner(sop);
4952
4953 kmem_cache_free(lockowner_slab, lo);
4954}
4955
4956static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004957 .so_unhash = nfs4_unhash_lockowner,
4958 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004959};
4960
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961/*
4962 * Alloc a lock owner structure.
4963 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004964 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004966 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004968static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004969alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
4970 struct nfs4_ol_stateid *open_stp,
4971 struct nfsd4_lock *lock)
4972{
Trond Myklebustc58c6612014-07-29 21:34:35 -04004973 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004975 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4976 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004978 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4979 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04004980 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04004981 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04004982 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004983 ret = find_lockowner_str_locked(&clp->cl_clientid,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004984 &lock->lk_new_owner, clp);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004985 if (ret == NULL) {
4986 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004987 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004988 ret = lo;
4989 } else
4990 nfs4_free_lockowner(&lo->lo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04004991 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004992 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993}
4994
Jeff Layton356a95e2014-07-29 21:34:13 -04004995static void
4996init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
4997 struct nfs4_file *fp, struct inode *inode,
4998 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005000 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001
Jeff Layton356a95e2014-07-29 21:34:13 -04005002 lockdep_assert_held(&clp->cl_lock);
5003
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005004 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005005 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005006 stp->st_stateowner = &lo->lo_owner;
Trond Myklebuste4f1dd72014-07-29 21:34:30 -04005007 atomic_inc(&lo->lo_owner.so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07005008 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005009 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04005010 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005011 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005013 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005014 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005015 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005016 spin_lock(&fp->fi_lock);
5017 list_add(&stp->st_perfile, &fp->fi_stateids);
5018 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019}
5020
Jeff Laytonc53530d2014-06-30 11:48:39 -04005021static struct nfs4_ol_stateid *
5022find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5023{
5024 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005025 struct nfs4_client *clp = lo->lo_owner.so_client;
5026
5027 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005028
5029 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005030 if (lst->st_stid.sc_file == fp) {
5031 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005032 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005033 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005034 }
5035 return NULL;
5036}
5037
Jeff Layton356a95e2014-07-29 21:34:13 -04005038static struct nfs4_ol_stateid *
5039find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5040 struct inode *inode, struct nfs4_ol_stateid *ost,
5041 bool *new)
5042{
5043 struct nfs4_stid *ns = NULL;
5044 struct nfs4_ol_stateid *lst;
5045 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5046 struct nfs4_client *clp = oo->oo_owner.so_client;
5047
5048 spin_lock(&clp->cl_lock);
5049 lst = find_lock_stateid(lo, fi);
5050 if (lst == NULL) {
5051 spin_unlock(&clp->cl_lock);
5052 ns = nfs4_alloc_stid(clp, stateid_slab);
5053 if (ns == NULL)
5054 return NULL;
5055
5056 spin_lock(&clp->cl_lock);
5057 lst = find_lock_stateid(lo, fi);
5058 if (likely(!lst)) {
5059 lst = openlockstateid(ns);
5060 init_lock_stateid(lst, lo, fi, inode, ost);
5061 ns = NULL;
5062 *new = true;
5063 }
5064 }
5065 spin_unlock(&clp->cl_lock);
5066 if (ns)
5067 nfs4_put_stid(ns);
5068 return lst;
5069}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005070
NeilBrownfd39ca92005-06-23 22:04:03 -07005071static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072check_lock_length(u64 offset, u64 length)
5073{
Benny Halevy87df4de2008-12-15 19:42:03 +02005074 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 LOFF_OVERFLOW(offset, length)));
5076}
5077
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005078static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005079{
Trond Myklebust11b91642014-07-29 21:34:08 -04005080 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005081
Jeff Layton7214e862014-07-10 14:07:33 -04005082 lockdep_assert_held(&fp->fi_lock);
5083
Jeff Layton82c5ff12012-05-11 09:45:13 -04005084 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005085 return;
Jeff Layton12659652014-07-10 14:07:28 -04005086 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005087 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005088}
5089
Jeff Layton356a95e2014-07-29 21:34:13 -04005090static __be32
5091lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5092 struct nfs4_ol_stateid *ost,
5093 struct nfsd4_lock *lock,
5094 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005095{
Jeff Layton5db1c032014-07-29 21:34:28 -04005096 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005097 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005098 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5099 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005100 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005101 struct nfs4_lockowner *lo;
5102 unsigned int strhashval;
5103
Trond Myklebustd4f04892014-07-29 21:34:36 -04005104 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005105 if (!lo) {
Trond Myklebustd4f04892014-07-29 21:34:36 -04005106 strhashval = ownerstr_hashval(&lock->v.new.owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005107 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5108 if (lo == NULL)
5109 return nfserr_jukebox;
5110 } else {
5111 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005112 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005113 if (!cstate->minorversion &&
5114 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005115 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005116 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005117
Jeff Layton356a95e2014-07-29 21:34:13 -04005118 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005119 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005120 status = nfserr_jukebox;
5121 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005122 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005123 status = nfs_ok;
5124out:
5125 nfs4_put_stateowner(&lo->lo_owner);
5126 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005127}
5128
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129/*
5130 * LOCK operation
5131 */
Al Virob37ad282006-10-19 23:28:59 -07005132__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005133nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005134 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005136 struct nfs4_openowner *open_sop = NULL;
5137 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005138 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005139 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005140 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005141 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005142 struct file_lock *file_lock = NULL;
5143 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005144 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005145 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005146 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005147 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005148 struct net *net = SVC_NET(rqstp);
5149 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150
5151 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5152 (long long) lock->lk_offset,
5153 (long long) lock->lk_length);
5154
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 if (check_lock_length(lock->lk_offset, lock->lk_length))
5156 return nfserr_inval;
5157
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005158 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005159 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005160 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5161 return status;
5162 }
5163
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005165 if (nfsd4_has_session(cstate))
5166 /* See rfc 5661 18.10.3: given clientid is ignored: */
5167 memcpy(&lock->v.new.clientid,
5168 &cstate->session->se_client->cl_clientid,
5169 sizeof(clientid_t));
5170
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005172 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005176 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 lock->lk_new_open_seqid,
5178 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005179 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005180 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005182 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005183 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005184 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005185 &lock->v.new.clientid))
5186 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005187 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005188 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005189 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005190 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005191 lock->lk_old_lock_seqid,
5192 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005193 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005194 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005195 if (status)
5196 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005197 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005199 lkflg = setlkflg(lock->lk_type);
5200 status = nfs4_check_openmode(lock_stp, lkflg);
5201 if (status)
5202 goto out;
5203
NeilBrown0dd395d2005-07-07 17:59:15 -07005204 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005205 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005206 goto out;
5207 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005208 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005209 goto out;
5210
Jeff Layton21179d82012-08-21 08:03:32 -04005211 file_lock = locks_alloc_lock();
5212 if (!file_lock) {
5213 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5214 status = nfserr_jukebox;
5215 goto out;
5216 }
5217
Trond Myklebust11b91642014-07-29 21:34:08 -04005218 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 switch (lock->lk_type) {
5220 case NFS4_READ_LT:
5221 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005222 spin_lock(&fp->fi_lock);
5223 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005224 if (filp)
5225 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005226 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005227 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005228 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 case NFS4_WRITE_LT:
5230 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005231 spin_lock(&fp->fi_lock);
5232 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005233 if (filp)
5234 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005235 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005236 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005237 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 default:
5239 status = nfserr_inval;
5240 goto out;
5241 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005242 if (!filp) {
5243 status = nfserr_openmode;
5244 goto out;
5245 }
Jeff Layton21179d82012-08-21 08:03:32 -04005246 file_lock->fl_owner = (fl_owner_t)lock_sop;
5247 file_lock->fl_pid = current->tgid;
5248 file_lock->fl_file = filp;
5249 file_lock->fl_flags = FL_POSIX;
5250 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5251 file_lock->fl_start = lock->lk_offset;
5252 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5253 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254
Jeff Layton21179d82012-08-21 08:03:32 -04005255 conflock = locks_alloc_lock();
5256 if (!conflock) {
5257 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5258 status = nfserr_jukebox;
5259 goto out;
5260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261
Jeff Layton21179d82012-08-21 08:03:32 -04005262 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005263 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005265 update_stateid(&lock_stp->st_stid.sc_stateid);
5266 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005268 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005269 break;
5270 case (EAGAIN): /* conflock holds conflicting lock */
5271 status = nfserr_denied;
5272 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005273 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005274 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 case (EDEADLK):
5276 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005277 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005278 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005279 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005280 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005281 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283out:
Trond Myklebustde186432014-07-10 14:07:26 -04005284 if (filp)
5285 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005286 if (lock_stp) {
5287 /* Bump seqid manually if the 4.0 replay owner is openowner */
5288 if (cstate->replay_owner &&
5289 cstate->replay_owner != &lock_sop->lo_owner &&
5290 seqid_mutating_err(ntohl(status)))
5291 lock_sop->lo_owner.so_seqid++;
5292
5293 /*
5294 * If this is a new, never-before-used stateid, and we are
5295 * returning an error, then just go ahead and release it.
5296 */
5297 if (status && new)
5298 release_lock_stateid(lock_stp);
5299
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005300 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005301 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005302 if (open_stp)
5303 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005304 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005305 if (file_lock)
5306 locks_free_lock(file_lock);
5307 if (conflock)
5308 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 return status;
5310}
5311
5312/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005313 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5314 * so we do a temporary open here just to get an open file to pass to
5315 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5316 * inode operation.)
5317 */
Al Viro04da6e92012-04-13 00:00:04 -04005318static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005319{
5320 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005321 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5322 if (!err) {
5323 err = nfserrno(vfs_test_lock(file, lock));
5324 nfsd_close(file);
5325 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005326 return err;
5327}
5328
5329/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 * LOCKT operation
5331 */
Al Virob37ad282006-10-19 23:28:59 -07005332__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005333nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5334 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335{
Jeff Layton21179d82012-08-21 08:03:32 -04005336 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005337 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005338 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005339 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005341 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 return nfserr_grace;
5343
5344 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5345 return nfserr_inval;
5346
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005347 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005348 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005349 if (status)
5350 goto out;
5351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005353 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355
Jeff Layton21179d82012-08-21 08:03:32 -04005356 file_lock = locks_alloc_lock();
5357 if (!file_lock) {
5358 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5359 status = nfserr_jukebox;
5360 goto out;
5361 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005362
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 switch (lockt->lt_type) {
5364 case NFS4_READ_LT:
5365 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005366 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 break;
5368 case NFS4_WRITE_LT:
5369 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005370 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 break;
5372 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005373 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 status = nfserr_inval;
5375 goto out;
5376 }
5377
Trond Myklebustd4f04892014-07-29 21:34:36 -04005378 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5379 cstate->clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005380 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005381 file_lock->fl_owner = (fl_owner_t)lo;
5382 file_lock->fl_pid = current->tgid;
5383 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384
Jeff Layton21179d82012-08-21 08:03:32 -04005385 file_lock->fl_start = lockt->lt_offset;
5386 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387
Jeff Layton21179d82012-08-21 08:03:32 -04005388 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389
Jeff Layton21179d82012-08-21 08:03:32 -04005390 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005391 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005392 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005393
Jeff Layton21179d82012-08-21 08:03:32 -04005394 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005396 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 }
5398out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005399 if (lo)
5400 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04005401 if (file_lock)
5402 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 return status;
5404}
5405
Al Virob37ad282006-10-19 23:28:59 -07005406__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005407nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005408 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005410 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005412 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005413 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005414 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005415 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5416
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5418 (long long) locku->lu_offset,
5419 (long long) locku->lu_length);
5420
5421 if (check_lock_length(locku->lu_offset, locku->lu_length))
5422 return nfserr_inval;
5423
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005424 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005425 &locku->lu_stateid, NFS4_LOCK_STID,
5426 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005427 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005429 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005430 if (!filp) {
5431 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005432 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005433 }
Jeff Layton21179d82012-08-21 08:03:32 -04005434 file_lock = locks_alloc_lock();
5435 if (!file_lock) {
5436 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5437 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005438 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005439 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005440
Jeff Layton21179d82012-08-21 08:03:32 -04005441 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005442 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005443 file_lock->fl_pid = current->tgid;
5444 file_lock->fl_file = filp;
5445 file_lock->fl_flags = FL_POSIX;
5446 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5447 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448
Jeff Layton21179d82012-08-21 08:03:32 -04005449 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5450 locku->lu_length);
5451 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452
Jeff Layton21179d82012-08-21 08:03:32 -04005453 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005454 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005455 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 goto out_nfserr;
5457 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005458 update_stateid(&stp->st_stid.sc_stateid);
5459 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005460fput:
5461 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005462put_stateid:
5463 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005465 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005466 if (file_lock)
5467 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 return status;
5469
5470out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005471 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005472 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473}
5474
5475/*
5476 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005477 * true: locks held by lockowner
5478 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005480static bool
5481check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482{
5483 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005484 int status = false;
5485 struct file *filp = find_any_file(fp);
5486 struct inode *inode;
5487
5488 if (!filp) {
5489 /* Any valid lock stateid should have some sort of access */
5490 WARN_ON_ONCE(1);
5491 return status;
5492 }
5493
5494 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495
Jeff Layton1c8c6012013-06-21 08:58:15 -04005496 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005498 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005499 status = true;
5500 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005503 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005504 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 return status;
5506}
5507
Al Virob37ad282006-10-19 23:28:59 -07005508__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005509nfsd4_release_lockowner(struct svc_rqst *rqstp,
5510 struct nfsd4_compound_state *cstate,
5511 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512{
5513 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005514 struct nfs4_stateowner *sop;
5515 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005516 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005518 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005519 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005520 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005521 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522
5523 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5524 clid->cl_boot, clid->cl_id);
5525
Jeff Layton4b24ca72014-06-30 11:48:44 -04005526 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005527 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04005528 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005529
Trond Myklebustd4f04892014-07-29 21:34:36 -04005530 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005531 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005532 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005533 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005534 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005535
5536 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005537 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005538
Jeff Layton882e9d22014-07-29 21:34:37 -04005539 /* see if there are still any locks associated with it */
5540 lo = lockowner(sop);
5541 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5542 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5543 status = nfserr_locks_held;
5544 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04005545 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04005546 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005547 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005548
5549 atomic_inc(&sop->so_count);
5550 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005551 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005552 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005553 if (lo)
5554 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 return status;
5556}
5557
5558static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005559alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560{
NeilBrowna55370a2005-06-23 22:03:52 -07005561 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562}
5563
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005564bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005565nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005566{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005567 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005568
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005569 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005570 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005571}
5572
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573/*
5574 * failure => all reset bets are off, nfserr_no_grace...
5575 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005576struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005577nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578{
5579 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005580 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581
NeilBrowna55370a2005-06-23 22:03:52 -07005582 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5583 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005584 if (crp) {
5585 strhashval = clientstr_hashval(name);
5586 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005587 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005588 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005589 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005590 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005591 }
5592 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593}
5594
Jeff Layton2a4317c2012-03-21 16:42:43 -04005595void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005596nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005597{
5598 list_del(&crp->cr_strhash);
5599 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005600 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005601}
5602
5603void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005604nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605{
5606 struct nfs4_client_reclaim *crp = NULL;
5607 int i;
5608
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005610 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5611 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005613 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 }
5615 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005616 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617}
5618
5619/*
5620 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005621struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005622nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623{
5624 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 struct nfs4_client_reclaim *crp = NULL;
5626
Jeff Layton278c9312012-11-12 15:00:52 -05005627 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628
Jeff Layton278c9312012-11-12 15:00:52 -05005629 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005630 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005631 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 return crp;
5633 }
5634 }
5635 return NULL;
5636}
5637
5638/*
5639* Called from OPEN. Look for clientid in reclaim list.
5640*/
Al Virob37ad282006-10-19 23:28:59 -07005641__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005642nfs4_check_open_reclaim(clientid_t *clid,
5643 struct nfsd4_compound_state *cstate,
5644 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005646 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005647
5648 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005649 status = lookup_clientid(clid, cstate, nn);
5650 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005651 return nfserr_reclaim_bad;
5652
Jeff Layton3b3e7b72014-09-12 16:40:22 -04005653 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
5654 return nfserr_no_grace;
5655
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005656 if (nfsd4_client_record_check(cstate->clp))
5657 return nfserr_reclaim_bad;
5658
5659 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660}
5661
Bryan Schumaker65178db2011-11-01 13:35:21 -04005662#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04005663static inline void
5664put_client(struct nfs4_client *clp)
5665{
5666 atomic_dec(&clp->cl_refcount);
5667}
5668
Jeff Layton285abde2014-07-30 08:27:24 -04005669static struct nfs4_client *
5670nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5671{
5672 struct nfs4_client *clp;
5673 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5674 nfsd_net_id);
5675
5676 if (!nfsd_netns_ready(nn))
5677 return NULL;
5678
5679 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5680 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5681 return clp;
5682 }
5683 return NULL;
5684}
5685
Jeff Layton7ec0e362014-07-30 08:27:17 -04005686u64
Jeff Layton285abde2014-07-30 08:27:24 -04005687nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04005688{
5689 struct nfs4_client *clp;
5690 u64 count = 0;
5691 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5692 nfsd_net_id);
5693 char buf[INET6_ADDRSTRLEN];
5694
5695 if (!nfsd_netns_ready(nn))
5696 return 0;
5697
5698 spin_lock(&nn->client_lock);
5699 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5700 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5701 pr_info("NFS Client: %s\n", buf);
5702 ++count;
5703 }
5704 spin_unlock(&nn->client_lock);
5705
5706 return count;
5707}
Bryan Schumaker65178db2011-11-01 13:35:21 -04005708
Jeff Laytona0926d12014-07-30 08:27:18 -04005709u64
Jeff Layton285abde2014-07-30 08:27:24 -04005710nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04005711{
5712 u64 count = 0;
5713 struct nfs4_client *clp;
5714 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5715 nfsd_net_id);
5716
5717 if (!nfsd_netns_ready(nn))
5718 return count;
5719
5720 spin_lock(&nn->client_lock);
5721 clp = nfsd_find_client(addr, addr_size);
5722 if (clp) {
5723 if (mark_client_expired_locked(clp) == nfs_ok)
5724 ++count;
5725 else
5726 clp = NULL;
5727 }
5728 spin_unlock(&nn->client_lock);
5729
5730 if (clp)
5731 expire_client(clp);
5732
5733 return count;
5734}
5735
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005736u64
Jeff Layton285abde2014-07-30 08:27:24 -04005737nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005738{
5739 u64 count = 0;
5740 struct nfs4_client *clp, *next;
5741 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5742 nfsd_net_id);
5743 LIST_HEAD(reaplist);
5744
5745 if (!nfsd_netns_ready(nn))
5746 return count;
5747
5748 spin_lock(&nn->client_lock);
5749 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5750 if (mark_client_expired_locked(clp) == nfs_ok) {
5751 list_add(&clp->cl_lru, &reaplist);
5752 if (max != 0 && ++count >= max)
5753 break;
5754 }
5755 }
5756 spin_unlock(&nn->client_lock);
5757
5758 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
5759 expire_client(clp);
5760
5761 return count;
5762}
5763
Bryan Schumaker184c1842012-11-29 11:40:44 -05005764static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5765 const char *type)
5766{
5767 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005768 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005769 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5770}
5771
Jeff Layton016200c2014-07-30 08:27:21 -04005772static void
5773nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
5774 struct list_head *collect)
5775{
5776 struct nfs4_client *clp = lst->st_stid.sc_client;
5777 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5778 nfsd_net_id);
5779
5780 if (!collect)
5781 return;
5782
5783 lockdep_assert_held(&nn->client_lock);
5784 atomic_inc(&clp->cl_refcount);
5785 list_add(&lst->st_locks, collect);
5786}
5787
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005788static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04005789 struct list_head *collect,
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005790 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005791{
5792 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005793 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005794 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005795 u64 count = 0;
5796
Jeff Layton016200c2014-07-30 08:27:21 -04005797 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005798 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005799 list_for_each_entry_safe(stp, st_next,
5800 &oop->oo_owner.so_stateids, st_perstateowner) {
5801 list_for_each_entry_safe(lst, lst_next,
5802 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04005803 if (func) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005804 func(lst);
Jeff Layton016200c2014-07-30 08:27:21 -04005805 nfsd_inject_add_lock_to_list(lst,
5806 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04005807 }
Jeff Layton016200c2014-07-30 08:27:21 -04005808 ++count;
5809 /*
5810 * Despite the fact that these functions deal
5811 * with 64-bit integers for "count", we must
5812 * ensure that it doesn't blow up the
5813 * clp->cl_refcount. Throw a warning if we
5814 * start to approach INT_MAX here.
5815 */
5816 WARN_ON_ONCE(count == (INT_MAX / 2));
5817 if (count == max)
5818 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005819 }
5820 }
5821 }
Jeff Layton016200c2014-07-30 08:27:21 -04005822out:
5823 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005824
5825 return count;
5826}
5827
Jeff Layton016200c2014-07-30 08:27:21 -04005828static u64
5829nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
5830 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05005831{
Jeff Layton016200c2014-07-30 08:27:21 -04005832 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005833}
5834
Jeff Layton016200c2014-07-30 08:27:21 -04005835static u64
5836nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05005837{
Jeff Layton016200c2014-07-30 08:27:21 -04005838 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005839 nfsd_print_count(clp, count, "locked files");
5840 return count;
5841}
5842
Jeff Layton016200c2014-07-30 08:27:21 -04005843u64
Jeff Layton285abde2014-07-30 08:27:24 -04005844nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04005845{
5846 struct nfs4_client *clp;
5847 u64 count = 0;
5848 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5849 nfsd_net_id);
5850
5851 if (!nfsd_netns_ready(nn))
5852 return 0;
5853
5854 spin_lock(&nn->client_lock);
5855 list_for_each_entry(clp, &nn->client_lru, cl_lru)
5856 count += nfsd_print_client_locks(clp);
5857 spin_unlock(&nn->client_lock);
5858
5859 return count;
5860}
5861
5862static void
5863nfsd_reap_locks(struct list_head *reaplist)
5864{
5865 struct nfs4_client *clp;
5866 struct nfs4_ol_stateid *stp, *next;
5867
5868 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
5869 list_del_init(&stp->st_locks);
5870 clp = stp->st_stid.sc_client;
5871 nfs4_put_stid(&stp->st_stid);
5872 put_client(clp);
5873 }
5874}
5875
5876u64
Jeff Layton285abde2014-07-30 08:27:24 -04005877nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04005878{
5879 unsigned int count = 0;
5880 struct nfs4_client *clp;
5881 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5882 nfsd_net_id);
5883 LIST_HEAD(reaplist);
5884
5885 if (!nfsd_netns_ready(nn))
5886 return count;
5887
5888 spin_lock(&nn->client_lock);
5889 clp = nfsd_find_client(addr, addr_size);
5890 if (clp)
5891 count = nfsd_collect_client_locks(clp, &reaplist, 0);
5892 spin_unlock(&nn->client_lock);
5893 nfsd_reap_locks(&reaplist);
5894 return count;
5895}
5896
5897u64
Jeff Layton285abde2014-07-30 08:27:24 -04005898nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04005899{
5900 u64 count = 0;
5901 struct nfs4_client *clp;
5902 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5903 nfsd_net_id);
5904 LIST_HEAD(reaplist);
5905
5906 if (!nfsd_netns_ready(nn))
5907 return count;
5908
5909 spin_lock(&nn->client_lock);
5910 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5911 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
5912 if (max != 0 && count >= max)
5913 break;
5914 }
5915 spin_unlock(&nn->client_lock);
5916 nfsd_reap_locks(&reaplist);
5917 return count;
5918}
5919
Jeff Layton82e05ef2014-07-30 08:27:22 -04005920static u64
5921nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
5922 struct list_head *collect,
5923 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005924{
5925 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04005926 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5927 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005928 u64 count = 0;
5929
Jeff Layton82e05ef2014-07-30 08:27:22 -04005930 lockdep_assert_held(&nn->client_lock);
5931
5932 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005933 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04005934 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005935 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04005936 if (collect) {
5937 atomic_inc(&clp->cl_refcount);
5938 list_add(&oop->oo_perclient, collect);
5939 }
5940 }
5941 ++count;
5942 /*
5943 * Despite the fact that these functions deal with
5944 * 64-bit integers for "count", we must ensure that
5945 * it doesn't blow up the clp->cl_refcount. Throw a
5946 * warning if we start to approach INT_MAX here.
5947 */
5948 WARN_ON_ONCE(count == (INT_MAX / 2));
5949 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005950 break;
5951 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04005952 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005953
5954 return count;
5955}
5956
Jeff Layton82e05ef2014-07-30 08:27:22 -04005957static u64
5958nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005959{
Jeff Layton82e05ef2014-07-30 08:27:22 -04005960 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
5961
5962 nfsd_print_count(clp, count, "openowners");
5963 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005964}
5965
Jeff Layton82e05ef2014-07-30 08:27:22 -04005966static u64
5967nfsd_collect_client_openowners(struct nfs4_client *clp,
5968 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05005969{
Jeff Layton82e05ef2014-07-30 08:27:22 -04005970 return nfsd_foreach_client_openowner(clp, max, collect,
5971 unhash_openowner_locked);
5972}
5973
5974u64
Jeff Layton285abde2014-07-30 08:27:24 -04005975nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04005976{
5977 struct nfs4_client *clp;
5978 u64 count = 0;
5979 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5980 nfsd_net_id);
5981
5982 if (!nfsd_netns_ready(nn))
5983 return 0;
5984
5985 spin_lock(&nn->client_lock);
5986 list_for_each_entry(clp, &nn->client_lru, cl_lru)
5987 count += nfsd_print_client_openowners(clp);
5988 spin_unlock(&nn->client_lock);
5989
5990 return count;
5991}
5992
5993static void
5994nfsd_reap_openowners(struct list_head *reaplist)
5995{
5996 struct nfs4_client *clp;
5997 struct nfs4_openowner *oop, *next;
5998
5999 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6000 list_del_init(&oop->oo_perclient);
6001 clp = oop->oo_owner.so_client;
6002 release_openowner(oop);
6003 put_client(clp);
6004 }
6005}
6006
6007u64
Jeff Layton285abde2014-07-30 08:27:24 -04006008nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6009 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006010{
6011 unsigned int count = 0;
6012 struct nfs4_client *clp;
6013 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6014 nfsd_net_id);
6015 LIST_HEAD(reaplist);
6016
6017 if (!nfsd_netns_ready(nn))
6018 return count;
6019
6020 spin_lock(&nn->client_lock);
6021 clp = nfsd_find_client(addr, addr_size);
6022 if (clp)
6023 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6024 spin_unlock(&nn->client_lock);
6025 nfsd_reap_openowners(&reaplist);
6026 return count;
6027}
6028
6029u64
Jeff Layton285abde2014-07-30 08:27:24 -04006030nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006031{
6032 u64 count = 0;
6033 struct nfs4_client *clp;
6034 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6035 nfsd_net_id);
6036 LIST_HEAD(reaplist);
6037
6038 if (!nfsd_netns_ready(nn))
6039 return count;
6040
6041 spin_lock(&nn->client_lock);
6042 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6043 count += nfsd_collect_client_openowners(clp, &reaplist,
6044 max - count);
6045 if (max != 0 && count >= max)
6046 break;
6047 }
6048 spin_unlock(&nn->client_lock);
6049 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006050 return count;
6051}
6052
Bryan Schumaker269de302012-11-29 11:40:42 -05006053static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6054 struct list_head *victims)
6055{
6056 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006057 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6058 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006059 u64 count = 0;
6060
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006061 lockdep_assert_held(&nn->client_lock);
6062
6063 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006064 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006065 if (victims) {
6066 /*
6067 * It's not safe to mess with delegations that have a
6068 * non-zero dl_time. They might have already been broken
6069 * and could be processed by the laundromat outside of
6070 * the state_lock. Just leave them be.
6071 */
6072 if (dp->dl_time != 0)
6073 continue;
6074
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006075 atomic_inc(&clp->cl_refcount);
Jeff Layton42690672014-07-25 07:34:20 -04006076 unhash_delegation_locked(dp);
6077 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006078 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006079 ++count;
6080 /*
6081 * Despite the fact that these functions deal with
6082 * 64-bit integers for "count", we must ensure that
6083 * it doesn't blow up the clp->cl_refcount. Throw a
6084 * warning if we start to approach INT_MAX here.
6085 */
6086 WARN_ON_ONCE(count == (INT_MAX / 2));
6087 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006088 break;
6089 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006090 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006091 return count;
6092}
6093
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006094static u64
6095nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006096{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006097 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006098
6099 nfsd_print_count(clp, count, "delegations");
6100 return count;
6101}
6102
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006103u64
Jeff Layton285abde2014-07-30 08:27:24 -04006104nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006105{
6106 struct nfs4_client *clp;
6107 u64 count = 0;
6108 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6109 nfsd_net_id);
6110
6111 if (!nfsd_netns_ready(nn))
6112 return 0;
6113
6114 spin_lock(&nn->client_lock);
6115 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6116 count += nfsd_print_client_delegations(clp);
6117 spin_unlock(&nn->client_lock);
6118
6119 return count;
6120}
6121
6122static void
6123nfsd_forget_delegations(struct list_head *reaplist)
6124{
6125 struct nfs4_client *clp;
6126 struct nfs4_delegation *dp, *next;
6127
6128 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6129 list_del_init(&dp->dl_recall_lru);
6130 clp = dp->dl_stid.sc_client;
6131 revoke_delegation(dp);
6132 put_client(clp);
6133 }
6134}
6135
6136u64
Jeff Layton285abde2014-07-30 08:27:24 -04006137nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6138 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006139{
6140 u64 count = 0;
6141 struct nfs4_client *clp;
6142 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6143 nfsd_net_id);
6144 LIST_HEAD(reaplist);
6145
6146 if (!nfsd_netns_ready(nn))
6147 return count;
6148
6149 spin_lock(&nn->client_lock);
6150 clp = nfsd_find_client(addr, addr_size);
6151 if (clp)
6152 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6153 spin_unlock(&nn->client_lock);
6154
6155 nfsd_forget_delegations(&reaplist);
6156 return count;
6157}
6158
6159u64
Jeff Layton285abde2014-07-30 08:27:24 -04006160nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006161{
6162 u64 count = 0;
6163 struct nfs4_client *clp;
6164 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6165 nfsd_net_id);
6166 LIST_HEAD(reaplist);
6167
6168 if (!nfsd_netns_ready(nn))
6169 return count;
6170
6171 spin_lock(&nn->client_lock);
6172 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6173 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6174 if (max != 0 && count >= max)
6175 break;
6176 }
6177 spin_unlock(&nn->client_lock);
6178 nfsd_forget_delegations(&reaplist);
6179 return count;
6180}
6181
6182static void
6183nfsd_recall_delegations(struct list_head *reaplist)
6184{
6185 struct nfs4_client *clp;
6186 struct nfs4_delegation *dp, *next;
6187
6188 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6189 list_del_init(&dp->dl_recall_lru);
6190 clp = dp->dl_stid.sc_client;
6191 /*
6192 * We skipped all entries that had a zero dl_time before,
6193 * so we can now reset the dl_time back to 0. If a delegation
6194 * break comes in now, then it won't make any difference since
6195 * we're recalling it either way.
6196 */
6197 spin_lock(&state_lock);
6198 dp->dl_time = 0;
6199 spin_unlock(&state_lock);
6200 nfsd_break_one_deleg(dp);
6201 put_client(clp);
6202 }
6203}
6204
6205u64
Jeff Layton285abde2014-07-30 08:27:24 -04006206nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006207 size_t addr_size)
6208{
6209 u64 count = 0;
6210 struct nfs4_client *clp;
6211 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6212 nfsd_net_id);
6213 LIST_HEAD(reaplist);
6214
6215 if (!nfsd_netns_ready(nn))
6216 return count;
6217
6218 spin_lock(&nn->client_lock);
6219 clp = nfsd_find_client(addr, addr_size);
6220 if (clp)
6221 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6222 spin_unlock(&nn->client_lock);
6223
6224 nfsd_recall_delegations(&reaplist);
6225 return count;
6226}
6227
6228u64
Jeff Layton285abde2014-07-30 08:27:24 -04006229nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006230{
6231 u64 count = 0;
6232 struct nfs4_client *clp, *next;
6233 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6234 nfsd_net_id);
6235 LIST_HEAD(reaplist);
6236
6237 if (!nfsd_netns_ready(nn))
6238 return count;
6239
6240 spin_lock(&nn->client_lock);
6241 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6242 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6243 if (max != 0 && ++count >= max)
6244 break;
6245 }
6246 spin_unlock(&nn->client_lock);
6247 nfsd_recall_delegations(&reaplist);
6248 return count;
6249}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006250#endif /* CONFIG_NFSD_FAULT_INJECTION */
6251
Meelap Shahc2f1a552007-07-17 04:04:39 -07006252/*
6253 * Since the lifetime of a delegation isn't limited to that of an open, a
6254 * client may quite reasonably hang on to a delegation as long as it has
6255 * the inode cached. This becomes an obvious problem the first time a
6256 * client's inode cache approaches the size of the server's total memory.
6257 *
6258 * For now we avoid this problem by imposing a hard limit on the number
6259 * of delegations, which varies according to the server's memory size.
6260 */
6261static void
6262set_max_delegations(void)
6263{
6264 /*
6265 * Allow at most 4 delegations per megabyte of RAM. Quick
6266 * estimates suggest that in the worst case (where every delegation
6267 * is for a different inode), a delegation could take about 1.5K,
6268 * giving a worst case usage of about 6% of memory.
6269 */
6270 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6271}
6272
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006273static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006274{
6275 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6276 int i;
6277
6278 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6279 CLIENT_HASH_SIZE, GFP_KERNEL);
6280 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006281 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006282 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6283 CLIENT_HASH_SIZE, GFP_KERNEL);
6284 if (!nn->unconf_id_hashtbl)
6285 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006286 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6287 SESSION_HASH_SIZE, GFP_KERNEL);
6288 if (!nn->sessionid_hashtbl)
6289 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006290
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006291 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006292 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006293 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006294 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006295 for (i = 0; i < SESSION_HASH_SIZE; i++)
6296 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006297 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006298 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03006299 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03006300 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006301 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03006302 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006303
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006304 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006305 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006306
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006307 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006308
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006309err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03006310 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006311err_unconf_id:
6312 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006313err:
6314 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006315}
6316
6317static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006318nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006319{
6320 int i;
6321 struct nfs4_client *clp = NULL;
6322 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6323
6324 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6325 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6326 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6327 destroy_client(clp);
6328 }
6329 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006330
Kinglong Mee2b905632014-03-26 22:09:30 +08006331 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6332 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6333 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6334 destroy_client(clp);
6335 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006336 }
6337
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006338 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006339 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006340 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006341 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006342}
6343
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006344int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006345nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07006346{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006347 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006348 int ret;
6349
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006350 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006351 if (ret)
6352 return ret;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006353 nn->boot_time = get_seconds();
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04006354 nn->grace_ended = false;
Jeff Laytond4318ac2014-09-12 16:40:21 -04006355 locks_start_grace(net, &nn->nfsd4_manager);
6356 nfsd4_client_tracking_init(net);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006357 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03006358 nn->nfsd4_grace, net);
6359 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006360 return 0;
6361}
6362
6363/* initialization to perform when the nfsd service is started: */
6364
6365int
6366nfs4_state_start(void)
6367{
6368 int ret;
6369
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006370 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006371 if (ret)
6372 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07006373 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05006374 if (laundry_wq == NULL) {
6375 ret = -ENOMEM;
6376 goto out_recovery;
6377 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006378 ret = nfsd4_create_callback_queue();
6379 if (ret)
6380 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006381
Meelap Shahc2f1a552007-07-17 04:04:39 -07006382 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006383
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006384 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006385
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006386out_free_laundry:
6387 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05006388out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006389 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390}
6391
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006392void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006393nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006397 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006399 cancel_delayed_work_sync(&nn->laundromat_work);
6400 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05006401
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04006403 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006404 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006406 unhash_delegation_locked(dp);
6407 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408 }
Benny Halevycdc97502014-05-30 09:09:30 -04006409 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 list_for_each_safe(pos, next, &reaplist) {
6411 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006412 list_del_init(&dp->dl_recall_lru);
Jeff Laytonafbda402014-08-09 10:22:41 -04006413 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04006414 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415 }
6416
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006417 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006418 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419}
6420
6421void
6422nfs4_state_shutdown(void)
6423{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006424 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006425 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006427
6428static void
6429get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6430{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006431 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6432 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006433}
6434
6435static void
6436put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6437{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006438 if (cstate->minorversion) {
6439 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6440 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6441 }
6442}
6443
6444void
6445clear_current_stateid(struct nfsd4_compound_state *cstate)
6446{
6447 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006448}
6449
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006450/*
6451 * functions to set current state id
6452 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006453void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006454nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6455{
6456 put_stateid(cstate, &odp->od_stateid);
6457}
6458
6459void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006460nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6461{
6462 put_stateid(cstate, &open->op_stateid);
6463}
6464
6465void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006466nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6467{
6468 put_stateid(cstate, &close->cl_stateid);
6469}
6470
6471void
6472nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6473{
6474 put_stateid(cstate, &lock->lk_resp_stateid);
6475}
6476
6477/*
6478 * functions to consume current state id
6479 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006480
6481void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006482nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6483{
6484 get_stateid(cstate, &odp->od_stateid);
6485}
6486
6487void
6488nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6489{
6490 get_stateid(cstate, &drp->dr_stateid);
6491}
6492
6493void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006494nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6495{
6496 get_stateid(cstate, &fsp->fr_stateid);
6497}
6498
6499void
6500nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6501{
6502 get_stateid(cstate, &setattr->sa_stateid);
6503}
6504
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006505void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006506nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6507{
6508 get_stateid(cstate, &close->cl_stateid);
6509}
6510
6511void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006512nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006513{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006514 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006515}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006516
6517void
6518nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6519{
6520 get_stateid(cstate, &read->rd_stateid);
6521}
6522
6523void
6524nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6525{
6526 get_stateid(cstate, &write->wr_stateid);
6527}