blob: ec24272529a0a2b5750326e81f0b54211236f647 [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 Fieldsabcdff092013-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 Fieldsabcdff092013-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 Fieldsabcdff092013-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 Fieldsabcdff092013-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 Fieldsabcdff092013-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 Adamsonf9bb94c42009-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 Adamsonf9bb94c42009-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;
J. Bruce Fields70b28232014-09-16 17:37:32 -04004125 /*
4126 * If the server goes down again right now, an NFSv4
4127 * client will still be allowed to reclaim after it comes back up,
4128 * even if it hasn't yet had a chance to reclaim state this time.
4129 *
4130 */
Jeff Layton919b8042014-09-12 16:40:20 -04004131 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004132 /*
4133 * At this point, NFSv4 clients can still reclaim. But if the
4134 * server crashes, any that have not yet reclaimed will be out
4135 * of luck on the next boot.
4136 *
4137 * (NFSv4.1+ clients are considered to have reclaimed once they
4138 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4139 * have reclaimed after their first OPEN.)
4140 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004141 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004142 /*
4143 * At this point, and once lockd and/or any other containers
4144 * exit their grace period, further reclaims will fail and
4145 * regular locking can resume.
4146 */
NeilBrowna76b4312005-06-23 22:04:01 -07004147}
4148
NeilBrownfd39ca92005-06-23 22:04:03 -07004149static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004150nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151{
4152 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004153 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004155 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004157 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004158 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004161 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004162 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004163 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004164 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 clp = list_entry(pos, struct nfs4_client, cl_lru);
4166 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4167 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004168 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 break;
4170 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004171 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004172 dprintk("NFSD: client in use (clientid %08x)\n",
4173 clp->cl_clientid.cl_id);
4174 continue;
4175 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004176 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004177 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004178 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004179 list_for_each_safe(pos, next, &reaplist) {
4180 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 dprintk("NFSD: purging unused client (clientid %08x)\n",
4182 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004183 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 expire_client(clp);
4185 }
Benny Halevycdc97502014-05-30 09:09:30 -04004186 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004187 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004189 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4190 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004192 t = dp->dl_time - cutoff;
4193 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 break;
4195 }
Jeff Layton42690672014-07-25 07:34:20 -04004196 unhash_delegation_locked(dp);
4197 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 }
Benny Halevycdc97502014-05-30 09:09:30 -04004199 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004200 while (!list_empty(&reaplist)) {
4201 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4202 dl_recall_lru);
4203 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004204 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 }
Jeff Layton217526e2014-07-30 08:27:11 -04004206
4207 spin_lock(&nn->client_lock);
4208 while (!list_empty(&nn->close_lru)) {
4209 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4210 oo_close_lru);
4211 if (time_after((unsigned long)oo->oo_time,
4212 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004213 t = oo->oo_time - cutoff;
4214 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 break;
4216 }
Jeff Layton217526e2014-07-30 08:27:11 -04004217 list_del_init(&oo->oo_close_lru);
4218 stp = oo->oo_last_closed_stid;
4219 oo->oo_last_closed_stid = NULL;
4220 spin_unlock(&nn->client_lock);
4221 nfs4_put_stid(&stp->st_stid);
4222 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 }
Jeff Layton217526e2014-07-30 08:27:11 -04004224 spin_unlock(&nn->client_lock);
4225
Jeff Laytona832e7a2014-05-30 09:09:26 -04004226 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004227 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228}
4229
Harvey Harrisona254b242008-02-20 12:49:00 -08004230static struct workqueue_struct *laundry_wq;
4231static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004232
4233static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004234laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235{
4236 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004237 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4238 work);
4239 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4240 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004242 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004244 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245}
4246
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004247static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004248{
Trond Myklebust11b91642014-07-29 21:34:08 -04004249 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004250 return nfserr_bad_stateid;
4251 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252}
4253
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004255access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004257 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4258 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4259 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260}
4261
4262static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004263access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004265 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4266 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267}
4268
4269static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004270__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271{
Al Virob37ad282006-10-19 23:28:59 -07004272 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
J. Bruce Fields02921912010-07-29 15:16:59 -04004274 /* For lock stateid's, we test the parent open, not the lock: */
4275 if (stp->st_openstp)
4276 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004277 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004279 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 goto out;
4281 status = nfs_ok;
4282out:
4283 return status;
4284}
4285
Al Virob37ad282006-10-19 23:28:59 -07004286static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004287check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004289 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004291 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004292 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 * conflicting state; so we must wait out the grace period. */
4294 return nfserr_grace;
4295 } else if (flags & WR_STATE)
4296 return nfs4_share_conflict(current_fh,
4297 NFS4_SHARE_DENY_WRITE);
4298 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4299 return nfs4_share_conflict(current_fh,
4300 NFS4_SHARE_DENY_READ);
4301}
4302
4303/*
4304 * Allow READ/WRITE during grace period on recovered state only for files
4305 * that are not able to provide mandatory locking.
4306 */
4307static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004308grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004310 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311}
4312
J. Bruce Fields81b82962011-08-23 11:03:29 -04004313/* Returns true iff a is later than b: */
4314static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4315{
Jim Rees1a9357f2013-05-17 17:33:00 -04004316 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004317}
4318
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004319static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004320{
Andy Adamson66689582009-04-03 08:28:45 +03004321 /*
4322 * When sessions are used the stateid generation number is ignored
4323 * when it is zero.
4324 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004325 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004326 return nfs_ok;
4327
4328 if (in->si_generation == ref->si_generation)
4329 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004330
J. Bruce Fields0836f582008-01-26 19:08:12 -05004331 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004332 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004333 return nfserr_bad_stateid;
4334 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004335 * However, we could see a stateid from the past, even from a
4336 * non-buggy client. For example, if the client sends a lock
4337 * while some IO is outstanding, the lock may bump si_generation
4338 * while the IO is still in flight. The client could avoid that
4339 * situation by waiting for responses on all the IO requests,
4340 * but better performance may result in retrying IO that
4341 * receives an old_stateid error if requests are rarely
4342 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004343 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004344 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004345}
4346
Chuck Lever7df302f2012-05-29 13:56:37 -04004347static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004348{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004349 struct nfs4_stid *s;
4350 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004351 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004352
Chuck Lever7df302f2012-05-29 13:56:37 -04004353 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004354 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004355 /* Client debugging aid. */
4356 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4357 char addr_str[INET6_ADDRSTRLEN];
4358 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4359 sizeof(addr_str));
4360 pr_warn_ratelimited("NFSD: client %s testing state ID "
4361 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004362 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004363 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004364 spin_lock(&cl->cl_lock);
4365 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004366 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004367 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004368 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004369 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004370 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004371 switch (s->sc_type) {
4372 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004373 status = nfs_ok;
4374 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004375 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004376 status = nfserr_deleg_revoked;
4377 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004378 case NFS4_OPEN_STID:
4379 case NFS4_LOCK_STID:
4380 ols = openlockstateid(s);
4381 if (ols->st_stateowner->so_is_open_owner
4382 && !(openowner(ols->st_stateowner)->oo_flags
4383 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004384 status = nfserr_bad_stateid;
4385 else
4386 status = nfs_ok;
4387 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004388 default:
4389 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004390 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004391 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004392 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004393 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004394 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004395out_unlock:
4396 spin_unlock(&cl->cl_lock);
4397 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004398}
4399
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004400static __be32
4401nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4402 stateid_t *stateid, unsigned char typemask,
4403 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004404{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004405 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004406
4407 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4408 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004409 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004410 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004411 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004412 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004413 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004414 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004415 if (status)
4416 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004417 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004418 if (!*s)
4419 return nfserr_bad_stateid;
4420 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004421}
4422
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423/*
4424* Checks for stateid operations
4425*/
Al Virob37ad282006-10-19 23:28:59 -07004426__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004427nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004428 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004430 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004431 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004433 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004435 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004436 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004437 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 if (filpp)
4440 *filpp = NULL;
4441
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004442 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 return nfserr_grace;
4444
4445 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004446 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004448 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004449 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004450 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004451 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04004452 return status;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004453 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4454 if (status)
4455 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004456 switch (s->sc_type) {
4457 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004458 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004459 status = nfs4_check_delegmode(dp, flags);
4460 if (status)
4461 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004462 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004463 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004464 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004465 WARN_ON_ONCE(1);
4466 status = nfserr_serverfault;
4467 goto out;
4468 }
Trond Myklebustde186432014-07-10 14:07:26 -04004469 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004470 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004471 break;
4472 case NFS4_OPEN_STID:
4473 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004474 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004475 status = nfs4_check_fh(current_fh, stp);
4476 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004478 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004479 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004481 status = nfs4_check_openmode(stp, flags);
4482 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004484 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004485 struct nfs4_file *fp = stp->st_stid.sc_file;
4486
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004487 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004488 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004489 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004490 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004491 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004492 break;
4493 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004494 status = nfserr_bad_stateid;
4495 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 }
4497 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004498 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004499 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004501 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 return status;
4503}
4504
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004505/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004506 * Test if the stateid is valid
4507 */
4508__be32
4509nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4510 struct nfsd4_test_stateid *test_stateid)
4511{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004512 struct nfsd4_test_stateid_id *stateid;
4513 struct nfs4_client *cl = cstate->session->se_client;
4514
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004515 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004516 stateid->ts_id_status =
4517 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004518
Bryan Schumaker17456802011-07-13 10:50:48 -04004519 return nfs_ok;
4520}
4521
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004522__be32
4523nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4524 struct nfsd4_free_stateid *free_stateid)
4525{
4526 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004527 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004528 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004529 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004530 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004531 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004532
Jeff Layton1af71cc2014-07-29 21:34:14 -04004533 spin_lock(&cl->cl_lock);
4534 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004535 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004536 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004537 switch (s->sc_type) {
4538 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004539 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004540 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004541 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004542 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4543 if (ret)
4544 break;
4545 ret = nfserr_locks_held;
4546 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004547 case NFS4_LOCK_STID:
4548 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4549 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004550 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004551 stp = openlockstateid(s);
4552 ret = nfserr_locks_held;
4553 if (check_for_locks(stp->st_stid.sc_file,
4554 lockowner(stp->st_stateowner)))
4555 break;
4556 unhash_lock_stateid(stp);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004557 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004558 nfs4_put_stid(s);
4559 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004560 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004561 case NFS4_REVOKED_DELEG_STID:
4562 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004563 list_del_init(&dp->dl_recall_lru);
4564 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004565 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004566 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004567 goto out;
4568 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004569 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004570out_unlock:
4571 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004572out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004573 return ret;
4574}
4575
NeilBrown4c4cd222005-07-07 17:59:27 -07004576static inline int
4577setlkflg (int type)
4578{
4579 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4580 RD_STATE : WR_STATE;
4581}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004583static __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 -04004584{
4585 struct svc_fh *current_fh = &cstate->current_fh;
4586 struct nfs4_stateowner *sop = stp->st_stateowner;
4587 __be32 status;
4588
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004589 status = nfsd4_check_seqid(cstate, sop, seqid);
4590 if (status)
4591 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004592 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4593 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004594 /*
4595 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004596 * nfserr_replay_me from the previous step, and
4597 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004598 */
4599 return nfserr_bad_stateid;
4600 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4601 if (status)
4602 return status;
4603 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004604}
4605
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606/*
4607 * Checks for sequence id mutating operations.
4608 */
Al Virob37ad282006-10-19 23:28:59 -07004609static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004610nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004611 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004612 struct nfs4_ol_stateid **stpp,
4613 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004615 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004616 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004617 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004619 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4620 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004621
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004623 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004624 if (status)
4625 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004626 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004627 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004629 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004630 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004631 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004632 else
4633 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004634 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004635}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004636
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004637static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4638 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004639{
4640 __be32 status;
4641 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004642 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004644 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004645 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004646 if (status)
4647 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004648 oo = openowner(stp->st_stateowner);
4649 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4650 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004651 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004652 }
4653 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004654 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655}
4656
Al Virob37ad282006-10-19 23:28:59 -07004657__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004658nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004659 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660{
Al Virob37ad282006-10-19 23:28:59 -07004661 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004662 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004663 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004664 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
Al Viroa6a9f182013-09-16 10:57:01 -04004666 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4667 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004669 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004670 if (status)
4671 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004673 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004674 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004675 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004676 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004677 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004678 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004679 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004680 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004681 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004682 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004683 update_stateid(&stp->st_stid.sc_stateid);
4684 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004685 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004686 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004687
Jeff Layton2a4317c2012-03-21 16:42:43 -04004688 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004689 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004690put_stateid:
4691 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004693 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 return status;
4695}
4696
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004697static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004699 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004700 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004701 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004702 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004703}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004704
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004705static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4706{
4707 switch (to_access) {
4708 case NFS4_SHARE_ACCESS_READ:
4709 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4710 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4711 break;
4712 case NFS4_SHARE_ACCESS_WRITE:
4713 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4714 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4715 break;
4716 case NFS4_SHARE_ACCESS_BOTH:
4717 break;
4718 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004719 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 }
4721}
4722
Al Virob37ad282006-10-19 23:28:59 -07004723__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004724nfsd4_open_downgrade(struct svc_rqst *rqstp,
4725 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004726 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727{
Al Virob37ad282006-10-19 23:28:59 -07004728 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004729 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004730 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731
Al Viroa6a9f182013-09-16 10:57:01 -04004732 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4733 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004735 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004736 if (od->od_deleg_want)
4737 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4738 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004740 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004741 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004742 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004745 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004746 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004748 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004750 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004751 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004753 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004755 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756
Jeff Laytonce0fc432012-05-11 09:45:14 -04004757 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004759 update_stateid(&stp->st_stid.sc_stateid);
4760 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004762put_stateid:
4763 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004765 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 return status;
4767}
4768
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004769static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4770{
Trond Myklebustacf92952014-06-30 11:48:37 -04004771 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytond83017f2014-07-29 21:34:42 -04004772 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004773
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004774 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04004775 spin_lock(&clp->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04004776 unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004777
Jeff Laytond83017f2014-07-29 21:34:42 -04004778 if (clp->cl_minorversion) {
4779 put_ol_stateid_locked(s, &reaplist);
4780 spin_unlock(&clp->cl_lock);
4781 free_ol_stateid_reaplist(&reaplist);
4782 } else {
4783 spin_unlock(&clp->cl_lock);
4784 free_ol_stateid_reaplist(&reaplist);
Jeff Laytond3134b12014-07-29 21:34:32 -04004785 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04004786 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004787}
4788
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789/*
4790 * nfs4_unlock_state() called after encode
4791 */
Al Virob37ad282006-10-19 23:28:59 -07004792__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004793nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004794 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795{
Al Virob37ad282006-10-19 23:28:59 -07004796 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004797 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004798 struct net *net = SVC_NET(rqstp);
4799 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800
Al Viroa6a9f182013-09-16 10:57:01 -04004801 dprintk("NFSD: nfsd4_close on file %pd\n",
4802 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004804 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4805 &close->cl_stateid,
4806 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004807 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004808 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004809 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004811 update_stateid(&stp->st_stid.sc_stateid);
4812 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004814 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004815
4816 /* put reference from nfs4_preprocess_seqid_op */
4817 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 return status;
4820}
4821
Al Virob37ad282006-10-19 23:28:59 -07004822__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004823nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4824 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004826 struct nfs4_delegation *dp;
4827 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004828 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004829 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004830 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004832 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004833 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004835 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004836 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004837 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004838 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004839 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004840 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004841 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004842
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004843 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004844put_stateid:
4845 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846out:
4847 return status;
4848}
4849
4850
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852
Benny Halevy87df4de2008-12-15 19:42:03 +02004853static inline u64
4854end_offset(u64 start, u64 len)
4855{
4856 u64 end;
4857
4858 end = start + len;
4859 return end >= start ? end: NFS4_MAX_UINT64;
4860}
4861
4862/* last octet in a range */
4863static inline u64
4864last_byte_offset(u64 start, u64 len)
4865{
4866 u64 end;
4867
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004868 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004869 end = start + len;
4870 return end > start ? end - 1: NFS4_MAX_UINT64;
4871}
4872
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873/*
4874 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4875 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4876 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4877 * locking, this prevents us from being completely protocol-compliant. The
4878 * real solution to this problem is to start using unsigned file offsets in
4879 * the VFS, but this is a very deep change!
4880 */
4881static inline void
4882nfs4_transform_lock_offset(struct file_lock *lock)
4883{
4884 if (lock->fl_start < 0)
4885 lock->fl_start = OFFSET_MAX;
4886 if (lock->fl_end < 0)
4887 lock->fl_end = OFFSET_MAX;
4888}
4889
NeilBrownd5b90262006-04-10 22:55:22 -07004890/* Hack!: For now, we're defining this just so we can use a pointer to it
4891 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004892static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004893};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894
4895static inline void
4896nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4897{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004898 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899
NeilBrownd5b90262006-04-10 22:55:22 -07004900 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004901 lo = (struct nfs4_lockowner *) fl->fl_owner;
4902 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4903 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004904 if (!deny->ld_owner.data)
4905 /* We just don't care that much */
4906 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004907 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4908 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004909 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004910nevermind:
4911 deny->ld_owner.len = 0;
4912 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004913 deny->ld_clientid.cl_boot = 0;
4914 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 }
4916 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004917 deny->ld_length = NFS4_MAX_UINT64;
4918 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4920 deny->ld_type = NFS4_READ_LT;
4921 if (fl->fl_type != F_RDLCK)
4922 deny->ld_type = NFS4_WRITE_LT;
4923}
4924
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004925static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004926find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004927 struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004929 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004930 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931
Trond Myklebust0a880a22014-07-30 08:27:10 -04004932 lockdep_assert_held(&clp->cl_lock);
4933
Trond Myklebustd4f04892014-07-29 21:34:36 -04004934 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4935 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004936 if (so->so_is_open_owner)
4937 continue;
Trond Myklebustd4f04892014-07-29 21:34:36 -04004938 if (!same_owner_str(so, owner))
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004939 continue;
Jeff Layton5db1c032014-07-29 21:34:28 -04004940 atomic_inc(&so->so_count);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004941 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 }
4943 return NULL;
4944}
4945
Trond Myklebustc58c6612014-07-29 21:34:35 -04004946static struct nfs4_lockowner *
4947find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004948 struct nfs4_client *clp)
Trond Myklebustc58c6612014-07-29 21:34:35 -04004949{
4950 struct nfs4_lockowner *lo;
4951
Trond Myklebustd4f04892014-07-29 21:34:36 -04004952 spin_lock(&clp->cl_lock);
4953 lo = find_lockowner_str_locked(clid, owner, clp);
4954 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004955 return lo;
4956}
4957
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004958static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4959{
Trond Myklebustc58c6612014-07-29 21:34:35 -04004960 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004961}
4962
Jeff Layton6b180f02014-07-29 21:34:26 -04004963static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4964{
4965 struct nfs4_lockowner *lo = lockowner(sop);
4966
4967 kmem_cache_free(lockowner_slab, lo);
4968}
4969
4970static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004971 .so_unhash = nfs4_unhash_lockowner,
4972 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004973};
4974
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975/*
4976 * Alloc a lock owner structure.
4977 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004978 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004980 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004982static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004983alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
4984 struct nfs4_ol_stateid *open_stp,
4985 struct nfsd4_lock *lock)
4986{
Trond Myklebustc58c6612014-07-29 21:34:35 -04004987 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004989 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4990 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004992 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4993 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04004994 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04004995 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04004996 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004997 ret = find_lockowner_str_locked(&clp->cl_clientid,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004998 &lock->lk_new_owner, clp);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004999 if (ret == NULL) {
5000 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005001 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005002 ret = lo;
5003 } else
5004 nfs4_free_lockowner(&lo->lo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005005 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005006 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007}
5008
Jeff Layton356a95e2014-07-29 21:34:13 -04005009static void
5010init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5011 struct nfs4_file *fp, struct inode *inode,
5012 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005014 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015
Jeff Layton356a95e2014-07-29 21:34:13 -04005016 lockdep_assert_held(&clp->cl_lock);
5017
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005018 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005019 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005020 stp->st_stateowner = &lo->lo_owner;
Trond Myklebuste4f1dd72014-07-29 21:34:30 -04005021 atomic_inc(&lo->lo_owner.so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07005022 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005023 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04005024 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005025 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005027 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005028 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005029 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005030 spin_lock(&fp->fi_lock);
5031 list_add(&stp->st_perfile, &fp->fi_stateids);
5032 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033}
5034
Jeff Laytonc53530d2014-06-30 11:48:39 -04005035static struct nfs4_ol_stateid *
5036find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5037{
5038 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005039 struct nfs4_client *clp = lo->lo_owner.so_client;
5040
5041 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005042
5043 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005044 if (lst->st_stid.sc_file == fp) {
5045 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005046 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005047 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005048 }
5049 return NULL;
5050}
5051
Jeff Layton356a95e2014-07-29 21:34:13 -04005052static struct nfs4_ol_stateid *
5053find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5054 struct inode *inode, struct nfs4_ol_stateid *ost,
5055 bool *new)
5056{
5057 struct nfs4_stid *ns = NULL;
5058 struct nfs4_ol_stateid *lst;
5059 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5060 struct nfs4_client *clp = oo->oo_owner.so_client;
5061
5062 spin_lock(&clp->cl_lock);
5063 lst = find_lock_stateid(lo, fi);
5064 if (lst == NULL) {
5065 spin_unlock(&clp->cl_lock);
5066 ns = nfs4_alloc_stid(clp, stateid_slab);
5067 if (ns == NULL)
5068 return NULL;
5069
5070 spin_lock(&clp->cl_lock);
5071 lst = find_lock_stateid(lo, fi);
5072 if (likely(!lst)) {
5073 lst = openlockstateid(ns);
5074 init_lock_stateid(lst, lo, fi, inode, ost);
5075 ns = NULL;
5076 *new = true;
5077 }
5078 }
5079 spin_unlock(&clp->cl_lock);
5080 if (ns)
5081 nfs4_put_stid(ns);
5082 return lst;
5083}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005084
NeilBrownfd39ca92005-06-23 22:04:03 -07005085static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086check_lock_length(u64 offset, u64 length)
5087{
Benny Halevy87df4de2008-12-15 19:42:03 +02005088 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 LOFF_OVERFLOW(offset, length)));
5090}
5091
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005092static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005093{
Trond Myklebust11b91642014-07-29 21:34:08 -04005094 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005095
Jeff Layton7214e862014-07-10 14:07:33 -04005096 lockdep_assert_held(&fp->fi_lock);
5097
Jeff Layton82c5ff12012-05-11 09:45:13 -04005098 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005099 return;
Jeff Layton12659652014-07-10 14:07:28 -04005100 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005101 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005102}
5103
Jeff Layton356a95e2014-07-29 21:34:13 -04005104static __be32
5105lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5106 struct nfs4_ol_stateid *ost,
5107 struct nfsd4_lock *lock,
5108 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005109{
Jeff Layton5db1c032014-07-29 21:34:28 -04005110 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005111 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005112 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5113 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005114 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005115 struct nfs4_lockowner *lo;
5116 unsigned int strhashval;
5117
Trond Myklebustd4f04892014-07-29 21:34:36 -04005118 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005119 if (!lo) {
Trond Myklebustd4f04892014-07-29 21:34:36 -04005120 strhashval = ownerstr_hashval(&lock->v.new.owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005121 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5122 if (lo == NULL)
5123 return nfserr_jukebox;
5124 } else {
5125 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005126 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005127 if (!cstate->minorversion &&
5128 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005129 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005130 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005131
Jeff Layton356a95e2014-07-29 21:34:13 -04005132 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005133 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005134 status = nfserr_jukebox;
5135 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005136 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005137 status = nfs_ok;
5138out:
5139 nfs4_put_stateowner(&lo->lo_owner);
5140 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005141}
5142
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143/*
5144 * LOCK operation
5145 */
Al Virob37ad282006-10-19 23:28:59 -07005146__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005147nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005148 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005150 struct nfs4_openowner *open_sop = NULL;
5151 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005152 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005153 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005154 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005155 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005156 struct file_lock *file_lock = NULL;
5157 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005158 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005159 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005160 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005161 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005162 struct net *net = SVC_NET(rqstp);
5163 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164
5165 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5166 (long long) lock->lk_offset,
5167 (long long) lock->lk_length);
5168
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 if (check_lock_length(lock->lk_offset, lock->lk_length))
5170 return nfserr_inval;
5171
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005172 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005173 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005174 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5175 return status;
5176 }
5177
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005179 if (nfsd4_has_session(cstate))
5180 /* See rfc 5661 18.10.3: given clientid is ignored: */
5181 memcpy(&lock->v.new.clientid,
5182 &cstate->session->se_client->cl_clientid,
5183 sizeof(clientid_t));
5184
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005186 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005190 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 lock->lk_new_open_seqid,
5192 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005193 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005194 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005196 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005197 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005198 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005199 &lock->v.new.clientid))
5200 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005201 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005202 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005203 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005204 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005205 lock->lk_old_lock_seqid,
5206 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005207 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005208 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005209 if (status)
5210 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005211 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005213 lkflg = setlkflg(lock->lk_type);
5214 status = nfs4_check_openmode(lock_stp, lkflg);
5215 if (status)
5216 goto out;
5217
NeilBrown0dd395d2005-07-07 17:59:15 -07005218 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005219 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005220 goto out;
5221 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005222 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005223 goto out;
5224
Jeff Layton21179d82012-08-21 08:03:32 -04005225 file_lock = locks_alloc_lock();
5226 if (!file_lock) {
5227 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5228 status = nfserr_jukebox;
5229 goto out;
5230 }
5231
Trond Myklebust11b91642014-07-29 21:34:08 -04005232 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 switch (lock->lk_type) {
5234 case NFS4_READ_LT:
5235 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005236 spin_lock(&fp->fi_lock);
5237 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005238 if (filp)
5239 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005240 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005241 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005242 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 case NFS4_WRITE_LT:
5244 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005245 spin_lock(&fp->fi_lock);
5246 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005247 if (filp)
5248 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005249 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005250 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005251 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 default:
5253 status = nfserr_inval;
5254 goto out;
5255 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005256 if (!filp) {
5257 status = nfserr_openmode;
5258 goto out;
5259 }
Jeff Layton21179d82012-08-21 08:03:32 -04005260 file_lock->fl_owner = (fl_owner_t)lock_sop;
5261 file_lock->fl_pid = current->tgid;
5262 file_lock->fl_file = filp;
5263 file_lock->fl_flags = FL_POSIX;
5264 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5265 file_lock->fl_start = lock->lk_offset;
5266 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5267 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268
Jeff Layton21179d82012-08-21 08:03:32 -04005269 conflock = locks_alloc_lock();
5270 if (!conflock) {
5271 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5272 status = nfserr_jukebox;
5273 goto out;
5274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275
Jeff Layton21179d82012-08-21 08:03:32 -04005276 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005277 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005279 update_stateid(&lock_stp->st_stid.sc_stateid);
5280 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005282 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005283 break;
5284 case (EAGAIN): /* conflock holds conflicting lock */
5285 status = nfserr_denied;
5286 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005287 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005288 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 case (EDEADLK):
5290 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005291 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005292 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005293 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005294 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005295 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297out:
Trond Myklebustde186432014-07-10 14:07:26 -04005298 if (filp)
5299 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005300 if (lock_stp) {
5301 /* Bump seqid manually if the 4.0 replay owner is openowner */
5302 if (cstate->replay_owner &&
5303 cstate->replay_owner != &lock_sop->lo_owner &&
5304 seqid_mutating_err(ntohl(status)))
5305 lock_sop->lo_owner.so_seqid++;
5306
5307 /*
5308 * If this is a new, never-before-used stateid, and we are
5309 * returning an error, then just go ahead and release it.
5310 */
5311 if (status && new)
5312 release_lock_stateid(lock_stp);
5313
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005314 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005315 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005316 if (open_stp)
5317 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005318 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005319 if (file_lock)
5320 locks_free_lock(file_lock);
5321 if (conflock)
5322 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 return status;
5324}
5325
5326/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005327 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5328 * so we do a temporary open here just to get an open file to pass to
5329 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5330 * inode operation.)
5331 */
Al Viro04da6e92012-04-13 00:00:04 -04005332static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005333{
5334 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005335 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5336 if (!err) {
5337 err = nfserrno(vfs_test_lock(file, lock));
5338 nfsd_close(file);
5339 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005340 return err;
5341}
5342
5343/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 * LOCKT operation
5345 */
Al Virob37ad282006-10-19 23:28:59 -07005346__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005347nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5348 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349{
Jeff Layton21179d82012-08-21 08:03:32 -04005350 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005351 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005352 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005353 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005355 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 return nfserr_grace;
5357
5358 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5359 return nfserr_inval;
5360
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005361 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005362 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005363 if (status)
5364 goto out;
5365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005367 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369
Jeff Layton21179d82012-08-21 08:03:32 -04005370 file_lock = locks_alloc_lock();
5371 if (!file_lock) {
5372 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5373 status = nfserr_jukebox;
5374 goto out;
5375 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005376
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 switch (lockt->lt_type) {
5378 case NFS4_READ_LT:
5379 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005380 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 break;
5382 case NFS4_WRITE_LT:
5383 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005384 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 break;
5386 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005387 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 status = nfserr_inval;
5389 goto out;
5390 }
5391
Trond Myklebustd4f04892014-07-29 21:34:36 -04005392 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5393 cstate->clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005394 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005395 file_lock->fl_owner = (fl_owner_t)lo;
5396 file_lock->fl_pid = current->tgid;
5397 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398
Jeff Layton21179d82012-08-21 08:03:32 -04005399 file_lock->fl_start = lockt->lt_offset;
5400 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401
Jeff Layton21179d82012-08-21 08:03:32 -04005402 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403
Jeff Layton21179d82012-08-21 08:03:32 -04005404 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005405 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005406 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005407
Jeff Layton21179d82012-08-21 08:03:32 -04005408 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005410 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 }
5412out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005413 if (lo)
5414 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04005415 if (file_lock)
5416 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 return status;
5418}
5419
Al Virob37ad282006-10-19 23:28:59 -07005420__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005421nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005422 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005424 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005426 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005427 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005428 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005429 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5430
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5432 (long long) locku->lu_offset,
5433 (long long) locku->lu_length);
5434
5435 if (check_lock_length(locku->lu_offset, locku->lu_length))
5436 return nfserr_inval;
5437
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005438 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005439 &locku->lu_stateid, NFS4_LOCK_STID,
5440 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005441 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005443 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005444 if (!filp) {
5445 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005446 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005447 }
Jeff Layton21179d82012-08-21 08:03:32 -04005448 file_lock = locks_alloc_lock();
5449 if (!file_lock) {
5450 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5451 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005452 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005453 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005454
Jeff Layton21179d82012-08-21 08:03:32 -04005455 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005456 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005457 file_lock->fl_pid = current->tgid;
5458 file_lock->fl_file = filp;
5459 file_lock->fl_flags = FL_POSIX;
5460 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5461 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462
Jeff Layton21179d82012-08-21 08:03:32 -04005463 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5464 locku->lu_length);
5465 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466
Jeff Layton21179d82012-08-21 08:03:32 -04005467 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005468 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005469 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 goto out_nfserr;
5471 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005472 update_stateid(&stp->st_stid.sc_stateid);
5473 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005474fput:
5475 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005476put_stateid:
5477 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005479 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005480 if (file_lock)
5481 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 return status;
5483
5484out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005485 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005486 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487}
5488
5489/*
5490 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005491 * true: locks held by lockowner
5492 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005494static bool
5495check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496{
5497 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005498 int status = false;
5499 struct file *filp = find_any_file(fp);
5500 struct inode *inode;
5501
5502 if (!filp) {
5503 /* Any valid lock stateid should have some sort of access */
5504 WARN_ON_ONCE(1);
5505 return status;
5506 }
5507
5508 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509
Jeff Layton1c8c6012013-06-21 08:58:15 -04005510 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005512 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005513 status = true;
5514 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005517 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005518 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 return status;
5520}
5521
Al Virob37ad282006-10-19 23:28:59 -07005522__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005523nfsd4_release_lockowner(struct svc_rqst *rqstp,
5524 struct nfsd4_compound_state *cstate,
5525 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526{
5527 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005528 struct nfs4_stateowner *sop;
5529 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005530 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005532 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005533 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005534 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005535 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536
5537 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5538 clid->cl_boot, clid->cl_id);
5539
Jeff Layton4b24ca72014-06-30 11:48:44 -04005540 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005541 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04005542 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005543
Trond Myklebustd4f04892014-07-29 21:34:36 -04005544 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005545 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005546 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005547 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005548 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005549
5550 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005551 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005552
Jeff Layton882e9d22014-07-29 21:34:37 -04005553 /* see if there are still any locks associated with it */
5554 lo = lockowner(sop);
5555 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5556 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5557 status = nfserr_locks_held;
5558 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04005559 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04005560 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005561 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005562
5563 atomic_inc(&sop->so_count);
5564 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005565 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005566 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005567 if (lo)
5568 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 return status;
5570}
5571
5572static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005573alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574{
NeilBrowna55370a2005-06-23 22:03:52 -07005575 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576}
5577
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005578bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005579nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005580{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005581 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005582
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005583 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005584 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005585}
5586
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587/*
5588 * failure => all reset bets are off, nfserr_no_grace...
5589 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005590struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005591nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592{
5593 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005594 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595
NeilBrowna55370a2005-06-23 22:03:52 -07005596 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5597 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005598 if (crp) {
5599 strhashval = clientstr_hashval(name);
5600 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005601 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005602 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005603 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005604 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005605 }
5606 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607}
5608
Jeff Layton2a4317c2012-03-21 16:42:43 -04005609void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005610nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005611{
5612 list_del(&crp->cr_strhash);
5613 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005614 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005615}
5616
5617void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005618nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619{
5620 struct nfs4_client_reclaim *crp = NULL;
5621 int i;
5622
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005624 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5625 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005627 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 }
5629 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005630 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631}
5632
5633/*
5634 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005635struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005636nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637{
5638 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 struct nfs4_client_reclaim *crp = NULL;
5640
Jeff Layton278c9312012-11-12 15:00:52 -05005641 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642
Jeff Layton278c9312012-11-12 15:00:52 -05005643 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005644 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005645 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 return crp;
5647 }
5648 }
5649 return NULL;
5650}
5651
5652/*
5653* Called from OPEN. Look for clientid in reclaim list.
5654*/
Al Virob37ad282006-10-19 23:28:59 -07005655__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005656nfs4_check_open_reclaim(clientid_t *clid,
5657 struct nfsd4_compound_state *cstate,
5658 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005660 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005661
5662 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005663 status = lookup_clientid(clid, cstate, nn);
5664 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005665 return nfserr_reclaim_bad;
5666
Jeff Layton3b3e7b72014-09-12 16:40:22 -04005667 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
5668 return nfserr_no_grace;
5669
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005670 if (nfsd4_client_record_check(cstate->clp))
5671 return nfserr_reclaim_bad;
5672
5673 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674}
5675
Bryan Schumaker65178db2011-11-01 13:35:21 -04005676#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04005677static inline void
5678put_client(struct nfs4_client *clp)
5679{
5680 atomic_dec(&clp->cl_refcount);
5681}
5682
Jeff Layton285abde2014-07-30 08:27:24 -04005683static struct nfs4_client *
5684nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5685{
5686 struct nfs4_client *clp;
5687 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5688 nfsd_net_id);
5689
5690 if (!nfsd_netns_ready(nn))
5691 return NULL;
5692
5693 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5694 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5695 return clp;
5696 }
5697 return NULL;
5698}
5699
Jeff Layton7ec0e362014-07-30 08:27:17 -04005700u64
Jeff Layton285abde2014-07-30 08:27:24 -04005701nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04005702{
5703 struct nfs4_client *clp;
5704 u64 count = 0;
5705 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5706 nfsd_net_id);
5707 char buf[INET6_ADDRSTRLEN];
5708
5709 if (!nfsd_netns_ready(nn))
5710 return 0;
5711
5712 spin_lock(&nn->client_lock);
5713 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5714 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5715 pr_info("NFS Client: %s\n", buf);
5716 ++count;
5717 }
5718 spin_unlock(&nn->client_lock);
5719
5720 return count;
5721}
Bryan Schumaker65178db2011-11-01 13:35:21 -04005722
Jeff Laytona0926d12014-07-30 08:27:18 -04005723u64
Jeff Layton285abde2014-07-30 08:27:24 -04005724nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04005725{
5726 u64 count = 0;
5727 struct nfs4_client *clp;
5728 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5729 nfsd_net_id);
5730
5731 if (!nfsd_netns_ready(nn))
5732 return count;
5733
5734 spin_lock(&nn->client_lock);
5735 clp = nfsd_find_client(addr, addr_size);
5736 if (clp) {
5737 if (mark_client_expired_locked(clp) == nfs_ok)
5738 ++count;
5739 else
5740 clp = NULL;
5741 }
5742 spin_unlock(&nn->client_lock);
5743
5744 if (clp)
5745 expire_client(clp);
5746
5747 return count;
5748}
5749
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005750u64
Jeff Layton285abde2014-07-30 08:27:24 -04005751nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005752{
5753 u64 count = 0;
5754 struct nfs4_client *clp, *next;
5755 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5756 nfsd_net_id);
5757 LIST_HEAD(reaplist);
5758
5759 if (!nfsd_netns_ready(nn))
5760 return count;
5761
5762 spin_lock(&nn->client_lock);
5763 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5764 if (mark_client_expired_locked(clp) == nfs_ok) {
5765 list_add(&clp->cl_lru, &reaplist);
5766 if (max != 0 && ++count >= max)
5767 break;
5768 }
5769 }
5770 spin_unlock(&nn->client_lock);
5771
5772 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
5773 expire_client(clp);
5774
5775 return count;
5776}
5777
Bryan Schumaker184c1842012-11-29 11:40:44 -05005778static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5779 const char *type)
5780{
5781 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005782 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005783 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5784}
5785
Jeff Layton016200c2014-07-30 08:27:21 -04005786static void
5787nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
5788 struct list_head *collect)
5789{
5790 struct nfs4_client *clp = lst->st_stid.sc_client;
5791 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5792 nfsd_net_id);
5793
5794 if (!collect)
5795 return;
5796
5797 lockdep_assert_held(&nn->client_lock);
5798 atomic_inc(&clp->cl_refcount);
5799 list_add(&lst->st_locks, collect);
5800}
5801
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005802static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04005803 struct list_head *collect,
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005804 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005805{
5806 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005807 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005808 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005809 u64 count = 0;
5810
Jeff Layton016200c2014-07-30 08:27:21 -04005811 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005812 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005813 list_for_each_entry_safe(stp, st_next,
5814 &oop->oo_owner.so_stateids, st_perstateowner) {
5815 list_for_each_entry_safe(lst, lst_next,
5816 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04005817 if (func) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005818 func(lst);
Jeff Layton016200c2014-07-30 08:27:21 -04005819 nfsd_inject_add_lock_to_list(lst,
5820 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04005821 }
Jeff Layton016200c2014-07-30 08:27:21 -04005822 ++count;
5823 /*
5824 * Despite the fact that these functions deal
5825 * with 64-bit integers for "count", we must
5826 * ensure that it doesn't blow up the
5827 * clp->cl_refcount. Throw a warning if we
5828 * start to approach INT_MAX here.
5829 */
5830 WARN_ON_ONCE(count == (INT_MAX / 2));
5831 if (count == max)
5832 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005833 }
5834 }
5835 }
Jeff Layton016200c2014-07-30 08:27:21 -04005836out:
5837 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005838
5839 return count;
5840}
5841
Jeff Layton016200c2014-07-30 08:27:21 -04005842static u64
5843nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
5844 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05005845{
Jeff Layton016200c2014-07-30 08:27:21 -04005846 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005847}
5848
Jeff Layton016200c2014-07-30 08:27:21 -04005849static u64
5850nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05005851{
Jeff Layton016200c2014-07-30 08:27:21 -04005852 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005853 nfsd_print_count(clp, count, "locked files");
5854 return count;
5855}
5856
Jeff Layton016200c2014-07-30 08:27:21 -04005857u64
Jeff Layton285abde2014-07-30 08:27:24 -04005858nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04005859{
5860 struct nfs4_client *clp;
5861 u64 count = 0;
5862 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5863 nfsd_net_id);
5864
5865 if (!nfsd_netns_ready(nn))
5866 return 0;
5867
5868 spin_lock(&nn->client_lock);
5869 list_for_each_entry(clp, &nn->client_lru, cl_lru)
5870 count += nfsd_print_client_locks(clp);
5871 spin_unlock(&nn->client_lock);
5872
5873 return count;
5874}
5875
5876static void
5877nfsd_reap_locks(struct list_head *reaplist)
5878{
5879 struct nfs4_client *clp;
5880 struct nfs4_ol_stateid *stp, *next;
5881
5882 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
5883 list_del_init(&stp->st_locks);
5884 clp = stp->st_stid.sc_client;
5885 nfs4_put_stid(&stp->st_stid);
5886 put_client(clp);
5887 }
5888}
5889
5890u64
Jeff Layton285abde2014-07-30 08:27:24 -04005891nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04005892{
5893 unsigned int count = 0;
5894 struct nfs4_client *clp;
5895 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5896 nfsd_net_id);
5897 LIST_HEAD(reaplist);
5898
5899 if (!nfsd_netns_ready(nn))
5900 return count;
5901
5902 spin_lock(&nn->client_lock);
5903 clp = nfsd_find_client(addr, addr_size);
5904 if (clp)
5905 count = nfsd_collect_client_locks(clp, &reaplist, 0);
5906 spin_unlock(&nn->client_lock);
5907 nfsd_reap_locks(&reaplist);
5908 return count;
5909}
5910
5911u64
Jeff Layton285abde2014-07-30 08:27:24 -04005912nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04005913{
5914 u64 count = 0;
5915 struct nfs4_client *clp;
5916 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5917 nfsd_net_id);
5918 LIST_HEAD(reaplist);
5919
5920 if (!nfsd_netns_ready(nn))
5921 return count;
5922
5923 spin_lock(&nn->client_lock);
5924 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5925 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
5926 if (max != 0 && count >= max)
5927 break;
5928 }
5929 spin_unlock(&nn->client_lock);
5930 nfsd_reap_locks(&reaplist);
5931 return count;
5932}
5933
Jeff Layton82e05ef2014-07-30 08:27:22 -04005934static u64
5935nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
5936 struct list_head *collect,
5937 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005938{
5939 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04005940 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5941 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005942 u64 count = 0;
5943
Jeff Layton82e05ef2014-07-30 08:27:22 -04005944 lockdep_assert_held(&nn->client_lock);
5945
5946 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005947 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04005948 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005949 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04005950 if (collect) {
5951 atomic_inc(&clp->cl_refcount);
5952 list_add(&oop->oo_perclient, collect);
5953 }
5954 }
5955 ++count;
5956 /*
5957 * Despite the fact that these functions deal with
5958 * 64-bit integers for "count", we must ensure that
5959 * it doesn't blow up the clp->cl_refcount. Throw a
5960 * warning if we start to approach INT_MAX here.
5961 */
5962 WARN_ON_ONCE(count == (INT_MAX / 2));
5963 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005964 break;
5965 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04005966 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005967
5968 return count;
5969}
5970
Jeff Layton82e05ef2014-07-30 08:27:22 -04005971static u64
5972nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005973{
Jeff Layton82e05ef2014-07-30 08:27:22 -04005974 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
5975
5976 nfsd_print_count(clp, count, "openowners");
5977 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005978}
5979
Jeff Layton82e05ef2014-07-30 08:27:22 -04005980static u64
5981nfsd_collect_client_openowners(struct nfs4_client *clp,
5982 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05005983{
Jeff Layton82e05ef2014-07-30 08:27:22 -04005984 return nfsd_foreach_client_openowner(clp, max, collect,
5985 unhash_openowner_locked);
5986}
5987
5988u64
Jeff Layton285abde2014-07-30 08:27:24 -04005989nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04005990{
5991 struct nfs4_client *clp;
5992 u64 count = 0;
5993 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5994 nfsd_net_id);
5995
5996 if (!nfsd_netns_ready(nn))
5997 return 0;
5998
5999 spin_lock(&nn->client_lock);
6000 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6001 count += nfsd_print_client_openowners(clp);
6002 spin_unlock(&nn->client_lock);
6003
6004 return count;
6005}
6006
6007static void
6008nfsd_reap_openowners(struct list_head *reaplist)
6009{
6010 struct nfs4_client *clp;
6011 struct nfs4_openowner *oop, *next;
6012
6013 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6014 list_del_init(&oop->oo_perclient);
6015 clp = oop->oo_owner.so_client;
6016 release_openowner(oop);
6017 put_client(clp);
6018 }
6019}
6020
6021u64
Jeff Layton285abde2014-07-30 08:27:24 -04006022nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6023 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006024{
6025 unsigned int count = 0;
6026 struct nfs4_client *clp;
6027 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6028 nfsd_net_id);
6029 LIST_HEAD(reaplist);
6030
6031 if (!nfsd_netns_ready(nn))
6032 return count;
6033
6034 spin_lock(&nn->client_lock);
6035 clp = nfsd_find_client(addr, addr_size);
6036 if (clp)
6037 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6038 spin_unlock(&nn->client_lock);
6039 nfsd_reap_openowners(&reaplist);
6040 return count;
6041}
6042
6043u64
Jeff Layton285abde2014-07-30 08:27:24 -04006044nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006045{
6046 u64 count = 0;
6047 struct nfs4_client *clp;
6048 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6049 nfsd_net_id);
6050 LIST_HEAD(reaplist);
6051
6052 if (!nfsd_netns_ready(nn))
6053 return count;
6054
6055 spin_lock(&nn->client_lock);
6056 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6057 count += nfsd_collect_client_openowners(clp, &reaplist,
6058 max - count);
6059 if (max != 0 && count >= max)
6060 break;
6061 }
6062 spin_unlock(&nn->client_lock);
6063 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006064 return count;
6065}
6066
Bryan Schumaker269de302012-11-29 11:40:42 -05006067static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6068 struct list_head *victims)
6069{
6070 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006071 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6072 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006073 u64 count = 0;
6074
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006075 lockdep_assert_held(&nn->client_lock);
6076
6077 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006078 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006079 if (victims) {
6080 /*
6081 * It's not safe to mess with delegations that have a
6082 * non-zero dl_time. They might have already been broken
6083 * and could be processed by the laundromat outside of
6084 * the state_lock. Just leave them be.
6085 */
6086 if (dp->dl_time != 0)
6087 continue;
6088
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006089 atomic_inc(&clp->cl_refcount);
Jeff Layton42690672014-07-25 07:34:20 -04006090 unhash_delegation_locked(dp);
6091 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006092 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006093 ++count;
6094 /*
6095 * Despite the fact that these functions deal with
6096 * 64-bit integers for "count", we must ensure that
6097 * it doesn't blow up the clp->cl_refcount. Throw a
6098 * warning if we start to approach INT_MAX here.
6099 */
6100 WARN_ON_ONCE(count == (INT_MAX / 2));
6101 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006102 break;
6103 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006104 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006105 return count;
6106}
6107
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006108static u64
6109nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006110{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006111 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006112
6113 nfsd_print_count(clp, count, "delegations");
6114 return count;
6115}
6116
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006117u64
Jeff Layton285abde2014-07-30 08:27:24 -04006118nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006119{
6120 struct nfs4_client *clp;
6121 u64 count = 0;
6122 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6123 nfsd_net_id);
6124
6125 if (!nfsd_netns_ready(nn))
6126 return 0;
6127
6128 spin_lock(&nn->client_lock);
6129 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6130 count += nfsd_print_client_delegations(clp);
6131 spin_unlock(&nn->client_lock);
6132
6133 return count;
6134}
6135
6136static void
6137nfsd_forget_delegations(struct list_head *reaplist)
6138{
6139 struct nfs4_client *clp;
6140 struct nfs4_delegation *dp, *next;
6141
6142 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6143 list_del_init(&dp->dl_recall_lru);
6144 clp = dp->dl_stid.sc_client;
6145 revoke_delegation(dp);
6146 put_client(clp);
6147 }
6148}
6149
6150u64
Jeff Layton285abde2014-07-30 08:27:24 -04006151nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6152 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006153{
6154 u64 count = 0;
6155 struct nfs4_client *clp;
6156 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6157 nfsd_net_id);
6158 LIST_HEAD(reaplist);
6159
6160 if (!nfsd_netns_ready(nn))
6161 return count;
6162
6163 spin_lock(&nn->client_lock);
6164 clp = nfsd_find_client(addr, addr_size);
6165 if (clp)
6166 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6167 spin_unlock(&nn->client_lock);
6168
6169 nfsd_forget_delegations(&reaplist);
6170 return count;
6171}
6172
6173u64
Jeff Layton285abde2014-07-30 08:27:24 -04006174nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006175{
6176 u64 count = 0;
6177 struct nfs4_client *clp;
6178 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6179 nfsd_net_id);
6180 LIST_HEAD(reaplist);
6181
6182 if (!nfsd_netns_ready(nn))
6183 return count;
6184
6185 spin_lock(&nn->client_lock);
6186 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6187 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6188 if (max != 0 && count >= max)
6189 break;
6190 }
6191 spin_unlock(&nn->client_lock);
6192 nfsd_forget_delegations(&reaplist);
6193 return count;
6194}
6195
6196static void
6197nfsd_recall_delegations(struct list_head *reaplist)
6198{
6199 struct nfs4_client *clp;
6200 struct nfs4_delegation *dp, *next;
6201
6202 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6203 list_del_init(&dp->dl_recall_lru);
6204 clp = dp->dl_stid.sc_client;
6205 /*
6206 * We skipped all entries that had a zero dl_time before,
6207 * so we can now reset the dl_time back to 0. If a delegation
6208 * break comes in now, then it won't make any difference since
6209 * we're recalling it either way.
6210 */
6211 spin_lock(&state_lock);
6212 dp->dl_time = 0;
6213 spin_unlock(&state_lock);
6214 nfsd_break_one_deleg(dp);
6215 put_client(clp);
6216 }
6217}
6218
6219u64
Jeff Layton285abde2014-07-30 08:27:24 -04006220nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006221 size_t addr_size)
6222{
6223 u64 count = 0;
6224 struct nfs4_client *clp;
6225 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6226 nfsd_net_id);
6227 LIST_HEAD(reaplist);
6228
6229 if (!nfsd_netns_ready(nn))
6230 return count;
6231
6232 spin_lock(&nn->client_lock);
6233 clp = nfsd_find_client(addr, addr_size);
6234 if (clp)
6235 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6236 spin_unlock(&nn->client_lock);
6237
6238 nfsd_recall_delegations(&reaplist);
6239 return count;
6240}
6241
6242u64
Jeff Layton285abde2014-07-30 08:27:24 -04006243nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006244{
6245 u64 count = 0;
6246 struct nfs4_client *clp, *next;
6247 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6248 nfsd_net_id);
6249 LIST_HEAD(reaplist);
6250
6251 if (!nfsd_netns_ready(nn))
6252 return count;
6253
6254 spin_lock(&nn->client_lock);
6255 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6256 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6257 if (max != 0 && ++count >= max)
6258 break;
6259 }
6260 spin_unlock(&nn->client_lock);
6261 nfsd_recall_delegations(&reaplist);
6262 return count;
6263}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006264#endif /* CONFIG_NFSD_FAULT_INJECTION */
6265
Meelap Shahc2f1a552007-07-17 04:04:39 -07006266/*
6267 * Since the lifetime of a delegation isn't limited to that of an open, a
6268 * client may quite reasonably hang on to a delegation as long as it has
6269 * the inode cached. This becomes an obvious problem the first time a
6270 * client's inode cache approaches the size of the server's total memory.
6271 *
6272 * For now we avoid this problem by imposing a hard limit on the number
6273 * of delegations, which varies according to the server's memory size.
6274 */
6275static void
6276set_max_delegations(void)
6277{
6278 /*
6279 * Allow at most 4 delegations per megabyte of RAM. Quick
6280 * estimates suggest that in the worst case (where every delegation
6281 * is for a different inode), a delegation could take about 1.5K,
6282 * giving a worst case usage of about 6% of memory.
6283 */
6284 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6285}
6286
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006287static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006288{
6289 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6290 int i;
6291
6292 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6293 CLIENT_HASH_SIZE, GFP_KERNEL);
6294 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006295 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006296 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6297 CLIENT_HASH_SIZE, GFP_KERNEL);
6298 if (!nn->unconf_id_hashtbl)
6299 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006300 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6301 SESSION_HASH_SIZE, GFP_KERNEL);
6302 if (!nn->sessionid_hashtbl)
6303 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006304
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006305 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006306 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006307 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006308 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006309 for (i = 0; i < SESSION_HASH_SIZE; i++)
6310 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006311 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006312 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03006313 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03006314 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006315 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03006316 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006317
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006318 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006319 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006320
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006321 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006322
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006323err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03006324 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006325err_unconf_id:
6326 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006327err:
6328 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006329}
6330
6331static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006332nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006333{
6334 int i;
6335 struct nfs4_client *clp = NULL;
6336 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6337
6338 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6339 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6340 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6341 destroy_client(clp);
6342 }
6343 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006344
Kinglong Mee2b905632014-03-26 22:09:30 +08006345 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6346 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6347 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6348 destroy_client(clp);
6349 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006350 }
6351
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006352 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006353 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006354 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006355 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006356}
6357
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006358int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006359nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07006360{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006361 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006362 int ret;
6363
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006364 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006365 if (ret)
6366 return ret;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006367 nn->boot_time = get_seconds();
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04006368 nn->grace_ended = false;
Jeff Laytond4318ac2014-09-12 16:40:21 -04006369 locks_start_grace(net, &nn->nfsd4_manager);
6370 nfsd4_client_tracking_init(net);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006371 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03006372 nn->nfsd4_grace, net);
6373 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006374 return 0;
6375}
6376
6377/* initialization to perform when the nfsd service is started: */
6378
6379int
6380nfs4_state_start(void)
6381{
6382 int ret;
6383
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006384 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006385 if (ret)
6386 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07006387 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05006388 if (laundry_wq == NULL) {
6389 ret = -ENOMEM;
6390 goto out_recovery;
6391 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006392 ret = nfsd4_create_callback_queue();
6393 if (ret)
6394 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006395
Meelap Shahc2f1a552007-07-17 04:04:39 -07006396 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006397
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006398 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006399
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006400out_free_laundry:
6401 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05006402out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006403 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404}
6405
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006406void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006407nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006411 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006413 cancel_delayed_work_sync(&nn->laundromat_work);
6414 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05006415
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04006417 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006418 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006420 unhash_delegation_locked(dp);
6421 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422 }
Benny Halevycdc97502014-05-30 09:09:30 -04006423 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424 list_for_each_safe(pos, next, &reaplist) {
6425 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006426 list_del_init(&dp->dl_recall_lru);
Jeff Laytonafbda402014-08-09 10:22:41 -04006427 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04006428 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429 }
6430
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006431 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006432 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433}
6434
6435void
6436nfs4_state_shutdown(void)
6437{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006438 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006439 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006441
6442static void
6443get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6444{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006445 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6446 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006447}
6448
6449static void
6450put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6451{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006452 if (cstate->minorversion) {
6453 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6454 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6455 }
6456}
6457
6458void
6459clear_current_stateid(struct nfsd4_compound_state *cstate)
6460{
6461 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006462}
6463
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006464/*
6465 * functions to set current state id
6466 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006467void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006468nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6469{
6470 put_stateid(cstate, &odp->od_stateid);
6471}
6472
6473void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006474nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6475{
6476 put_stateid(cstate, &open->op_stateid);
6477}
6478
6479void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006480nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6481{
6482 put_stateid(cstate, &close->cl_stateid);
6483}
6484
6485void
6486nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6487{
6488 put_stateid(cstate, &lock->lk_resp_stateid);
6489}
6490
6491/*
6492 * functions to consume current state id
6493 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006494
6495void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006496nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6497{
6498 get_stateid(cstate, &odp->od_stateid);
6499}
6500
6501void
6502nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6503{
6504 get_stateid(cstate, &drp->dr_stateid);
6505}
6506
6507void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006508nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6509{
6510 get_stateid(cstate, &fsp->fr_stateid);
6511}
6512
6513void
6514nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6515{
6516 get_stateid(cstate, &setattr->sa_stateid);
6517}
6518
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006519void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006520nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6521{
6522 get_stateid(cstate, &close->cl_stateid);
6523}
6524
6525void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006526nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006527{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006528 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006529}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006530
6531void
6532nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6533{
6534 get_stateid(cstate, &read->rd_stateid);
6535}
6536
6537void
6538nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6539{
6540 get_stateid(cstate, &write->wr_stateid);
6541}